vul_string.h
Go to the documentation of this file.
1 // This is core/vul/vul_string.h
2 #ifndef vul_string_h
3 #define vul_string_h
4 //:
5 // \file
6 // \brief Utility functions for C strings and std::strings
7 
8 #include <string>
9 #include <vector>
10 #ifdef _MSC_VER
11 # include <vcl_msvc_warnings.h>
12 #endif
13 
14 // C string functions:
15 
16 //: Converts all alphabetical characters to uppercase.
17 extern char* vul_string_c_upcase(char*);
18 //: Converts all alphabetical characters to lowercase.
19 extern char* vul_string_c_downcase(char*);
20 //: Capitalizes all words in a string.
21 // A word is defined as a sequence of characters separated by
22 // non-alphanumerics.
23 extern char* vul_string_c_capitalize(char*);
24 //: Removes any occurrences of rem from str, and returns the modified string.
25 extern char* vul_string_c_trim(char* str, const char* rem);
26 //: Removes any prefix occurrence of rem from str and returns modified string.
27 extern char* vul_string_c_left_trim(char* str, const char* rem);
28 //: Removes any suffix occurrence of rem from str and returns modified string.
29 extern char* vul_string_c_right_trim(char* str, const char* rem);
30 //: Reverses the order of the characters in string.
31 extern char* vul_string_c_reverse(char*);
32 
33 // std::string functions:
34 
35 //: Converts all alphabetical characters to uppercase.
36 extern std::string& vul_string_upcase(std::string&);
37 //: Converts all alphabetical characters to lowercase.
38 extern std::string& vul_string_downcase(std::string&);
39 //: Capitalizes all words in string.
40 extern std::string& vul_string_capitalize(std::string&);
41 //: Removes any occurrences of rem from str and returns modified string
42 extern std::string& vul_string_trim(std::string&, const char*);
43 //: Removes any prefix occurrence of rem from str and returns modified string
44 extern std::string& vul_string_left_trim(std::string&, const char*);
45 //: Removes any suffix occurrence of rem from str and returns modified string
46 extern std::string& vul_string_right_trim(std::string&, const char*);
47 //: Reverses the order of the characters in string
48 extern std::string& vul_string_reverse(std::string&);
49 
50 //: Reads an integer from a string
51 extern int vul_string_atoi(std::string const&);
52 
53 //: Reads a double from a string
54 extern double vul_string_atof(std::string const& s);
55 
56 //: Reads a double from a string, with k, kb, M, etc suffix.
57 // No space is allowed between the number and the suffix.
58 // k=10^3, ki=2^10, M=10^6, Mi=2^20, G=10^9, Gi=2^30, T=10^12, Ti=2^40
59 // The i suffix is from the IEC 60027 standard.
60 extern double vul_string_atof_withsuffix(std::string const& s);
61 
62 //: Convert a string to a boolean.
63 // Looks for On, true, yes, 1 to mean true. everything else is false.
64 // It ignores leading and trailing whitespace and capitalisation.
65 extern bool vul_string_to_bool(const std::string &str);
66 
67 //: Convert a string to a list of ints.
68 extern std::vector<int> vul_string_to_int_list(std::string str);
69 
70 //: Expand any environment variables in the string.
71 // \verbatim
72 // Expands "foo$VARfoo" to "foobarfoo" when $VAR=bar. If
73 // both $VAR and $VARfoo exists, an arbitrary choice will
74 // be made of which variable to use. This problem can
75 // be avoided by using the syntax "foo${VAR}foo." "$(VAR)" and
76 // "$[VAR]" can also be used. There are no inbuilt variables
77 // like in shell scripting. "$$" can be used to insert a
78 // literal "$" in to the output.
79 // \endverbatim
80 // \returns false if a matching variable could not be found.
81 extern bool vul_string_expand_var(std::string &str);
82 
83 //: replaces instances "find_str" in "full_str" with "replace_str" a given "num_times" (default 1000).
84 // \returns true iff at least one replacement took place.
85 extern bool vul_string_replace( std::string& full_str,
86  const std::string& find_str,
87  const std::string& replace_str,
88  int num_times=1000);
89 
90 //: Replace control chars with escaped representations.
91 // Space and \n are preserved, but tabs, CR, etc are escaped.
92 // This is not aimed and is not suitable for any particular input-validation
93 // security problem, such as sql-injection.
94 std::string vul_string_escape_ctrl_chars(const std::string &in);
95 
96 
97 #endif // vul_string_h
double vul_string_atof(std::string const &s)
Reads a double from a string.
Definition: vul_string.cxx:222
char * vul_string_c_upcase(char *)
Converts all alphabetical characters to uppercase.
Definition: vul_string.cxx:24
char * vul_string_c_reverse(char *)
Reverses the order of the characters in string.
Definition: vul_string.cxx:116
double vul_string_atof_withsuffix(std::string const &s)
Reads a double from a string, with k, kb, M, etc suffix.
Definition: vul_string.cxx:232
std::string & vul_string_reverse(std::string &)
Reverses the order of the characters in string.
Definition: vul_string.cxx:132
char * vul_string_c_downcase(char *)
Converts all alphabetical characters to lowercase.
Definition: vul_string.cxx:36
std::vector< int > vul_string_to_int_list(std::string str)
Convert a string to a list of ints.
Definition: vul_string.cxx:301
std::string & vul_string_upcase(std::string &)
Converts all alphabetical characters to uppercase.
Definition: vul_string.cxx:147
std::string vul_string_escape_ctrl_chars(const std::string &in)
Replace control chars with escaped representations.
Definition: vul_string.cxx:511
std::string & vul_string_left_trim(std::string &, const char *)
Removes any prefix occurrence of rem from str and returns modified string.
Definition: vul_string.cxx:198
char * vul_string_c_capitalize(char *)
Capitalizes all words in a string.
Definition: vul_string.cxx:49
bool vul_string_replace(std::string &full_str, const std::string &find_str, const std::string &replace_str, int num_times)
replaces instances "find_str" in "full_str" with "replace_str" a given "num_times".
Definition: vul_string.cxx:484
std::string & vul_string_right_trim(std::string &, const char *)
Removes any suffix occurrence of rem from str and returns modified string.
Definition: vul_string.cxx:208
bool vul_string_to_bool(const std::string &str)
Convert a string to a boolean.
Definition: vul_string.cxx:281
std::string & vul_string_capitalize(std::string &)
Capitalizes all words in string.
Definition: vul_string.cxx:163
char * vul_string_c_left_trim(char *str, const char *rem)
Removes any prefix occurrence of rem from str and returns modified string.
Definition: vul_string.cxx:81
int vul_string_atoi(std::string const &)
Reads an integer from a string.
Definition: vul_string.cxx:217
bool vul_string_expand_var(std::string &str)
Expand any environment variables in the string.
Definition: vul_string.cxx:387
std::string & vul_string_trim(std::string &, const char *)
Removes any occurrences of rem from str and returns modified string.
Definition: vul_string.cxx:184
char * vul_string_c_trim(char *str, const char *rem)
Removes any occurrences of rem from str, and returns the modified string.
Definition: vul_string.cxx:63
char * vul_string_c_right_trim(char *str, const char *rem)
Removes any suffix occurrence of rem from str and returns modified string.
Definition: vul_string.cxx:100
std::string & vul_string_downcase(std::string &)
Converts all alphabetical characters to lowercase.
Definition: vul_string.cxx:155