/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/external/stlplus/include/stlplus/string_utilities.hpp Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/external/stlplus/include/stlplus/string_utilities.hpp Source File
Ratpac-two
string_utilities.hpp
1 #ifndef STRING_UTILITIES_HPP
2 #define STRING_UTILITIES_HPP
3 /*------------------------------------------------------------------------------
4 
5  Author: Andy Rushton
6  Copyright: (c) Andy Rushton, 2004
7  License: BSD License, see ../docs/license.html
8 
9  Utilities for manipulating std::strings, missing from the STL or C++ libraries
10 
11  ------------------------------------------------------------------------------*/
12 #include <bitset>
13 #include <list>
14 #include <map>
15 #include <set>
16 #include <stdexcept>
17 #include <string>
18 #include <vector>
19 
20 #include "format_types.hpp"
21 #include "os_fixes.hpp"
22 #include "textio.hpp"
23 
25 // Conversions of Integer types to string
27 
28 // The radix (i.e. base) for these conversions can be any value from base 2 to base 36
29 // specifying any other radix causes std::invalid_argument to be thrown
30 
31 // The way in which the radix is displayed is defined in radix_types.hpp
32 // If any other value is used, std::invalid_argument is thrown
33 
34 // The width argument specifies the number of numerical digits to use in the result
35 // This is a minimum - if the value requires more digits then it will be wider than the width argument
36 // However, if it is smaller, then it will be extended to the specified width
37 // Then, the radix display prefix is added to this width
38 // For example, using the hash representation of 0 in hex with width=4 gives: 16#0000 - so there's 4 digits in the
39 // number part
40 
41 std::string to_string(bool i, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
42  unsigned width = 0) throw();
43 
44 std::string to_string(short i, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
45  unsigned width = 0) throw();
46 
47 std::string to_string(unsigned short i, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
48  unsigned width = 0) throw();
49 
50 std::string to_string(int i, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
51  unsigned width = 0) throw();
52 
53 std::string to_string(unsigned int i, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
54  unsigned width = 0) throw();
55 
56 std::string to_string(long i, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
57  unsigned width = 0) throw();
58 
59 std::string to_string(unsigned long i, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
60  unsigned width = 0) throw();
61 
62 std::string to_string(const void*, unsigned radix = 16, radix_display_t display = radix_c_style_or_hash,
63  unsigned width = 0) throw();
64 
66 // convert a real type to string
68 
69 // Only decimal radix is supported
70 
71 // The way in which the number is displayed is defined in radix_types.hpp
72 // Using any other value for the display type causes std::invalid_argument to be thrown
73 
74 std::string to_string(float f, real_display_t display = display_mixed, unsigned width = 0,
75  unsigned precision = 6) throw();
76 std::string to_string(double f, real_display_t display = display_mixed, unsigned width = 0,
77  unsigned precision = 6) throw();
78 
80 // Convert a string to string
82 
83 // this is necessary for completeness, e.g. for use in vector_to_string for vector<string>
84 std::string to_string(const std::string& value);
85 
86 // ditto for char*
87 std::string to_string(const char* value);
88 
90 // convert a string to a simple type
92 
93 // Convert a string to an integer type
94 // supports all the formats described above for the reverse conversion
95 // If the radix is set to zero, the conversions deduce the radix from the string representation
96 // So, 0b prefix is binary, 0 prefix is octal, 0x is hex and <base># prefix is my hash format
97 // A non-zero radix should be used when the string value has no radix information and is non-decimal
98 // e.g. the hex value FEDCBA has no indication that it is hex, so specify radix 16
99 // The radix must be either zero as explained above, or in the range 2 to 16
100 // Any other value will cause std::invalid_argument to be thrown
101 
102 bool to_bool(const std::string& value, unsigned radix = 0) throw();
103 
104 short to_short(const std::string& value, unsigned radix = 0) throw();
105 
106 unsigned short to_ushort(const std::string& value, unsigned radix = 0) throw();
107 
108 int to_int(const std::string& value, unsigned radix = 0) throw();
109 
110 unsigned int to_uint(const std::string& value, unsigned radix = 0) throw();
111 
112 long to_long(const std::string& value, unsigned radix = 0) throw();
113 
114 unsigned long to_ulong(const std::string& value, unsigned radix = 0) throw();
115 
116 void* to_void_star(const std::string& value, unsigned radix = 0) throw();
117 
118 // Convert a floating-point type
119 
120 float to_float(const std::string& value) throw();
121 
122 double to_double(const std::string& value) throw();
123 
125 // template string conversions for pointers and STL containers
127 // Note: STLplus containers tend to have built-in string conversion functions consistent with these
128 
129 template <typename T>
130 std::string pointer_to_string(const T* value, const std::string& null_string, const std::string& prefix,
131  const std::string& suffix);
132 
133 template <size_t N>
134 std::string bitset_to_string(const std::bitset<N>& data);
135 
136 template <typename T>
137 std::string list_to_string(const std::list<T>& values, const std::string& separator);
138 
139 template <typename L, typename R>
140 std::string pair_to_string(const std::pair<L, R>& values, const std::string& separator);
141 
142 template <typename K, typename T, typename P>
143 std::string map_to_string(const std::map<K, T, P>& values, const std::string& pair_separator,
144  const std::string& separator);
145 
146 template <typename K, typename T, typename P>
147 std::string multimap_to_string(const std::multimap<K, T, P>& values, const std::string& pair_separator,
148  const std::string& separator);
149 
150 template <typename K, typename P>
151 std::string set_to_string(const std::set<K, P>& values, const std::string& separator);
152 
153 template <typename K, typename P>
154 std::string multiset_to_string(const std::multiset<K, P>& values, const std::string& separator);
155 
156 template <typename T>
157 std::string vector_to_string(const std::vector<T>& values, const std::string& separator);
158 
160 // Print routines for basic types
162 
163 // The convention is to have a print(str,val) for printing in-line (i.e. the
164 // value is on one line) and to have a print(str,val,indent) to print on a whole
165 // line, with indent before and newline after.
166 
167 // set the number of spaces to indent per indent step (i.e. the number of spaces = indent*indent_step)
168 // default is built-in and set to 2
169 void set_indent_step(unsigned step);
170 unsigned indent_step(void);
171 // utility for printing the indent, called from within the second form of print
172 otext& print_indent(otext& str, unsigned indent);
173 
174 // print routines for integer types
175 // the arguments are as for the to_string
176 
177 otext& print(otext& str, const bool& value, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
178  unsigned width = 0) throw();
179 
180 otext& print(otext& str, const short& value, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
181  unsigned width = 0) throw();
182 
183 otext& print(otext& str, const unsigned short& value, unsigned radix = 10,
184  radix_display_t display = radix_c_style_or_hash, unsigned width = 0) throw();
185 
186 otext& print(otext& str, const int& value, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
187  unsigned width = 0) throw();
188 
189 otext& print(otext& str, const unsigned int& value, unsigned radix = 10,
190  radix_display_t display = radix_c_style_or_hash, unsigned width = 0) throw();
191 
192 otext& print(otext& str, const long& value, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
193  unsigned width = 0) throw();
194 
195 otext& print(otext& str, const unsigned long& value, unsigned radix = 10,
196  radix_display_t display = radix_c_style_or_hash, unsigned width = 0) throw();
197 
198 otext& print(otext& str, const void*& value, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
199  unsigned width = 0) throw();
200 
201 // print routines for floating-point types
202 
203 otext& print(otext& str, float f, real_display_t display = display_mixed, unsigned width = 0,
204  unsigned precision = 6) throw();
205 
206 otext& print(otext& str, double f, real_display_t display = display_mixed, unsigned width = 0,
207  unsigned precision = 6) throw();
208 
209 // print routines for string
210 // this is needed for completeness, e.g. when calling print_vector on a vector of strings
211 
212 otext& print(otext& str, const std::string& value);
213 otext& print(otext& str, const std::string& value, unsigned indent);
214 
216 // template print routines for pointers and STL containers
218 // STLplus containers have these built-in
219 
220 template <typename T>
221 otext& print_pointer(otext& str, const T* value, const std::string& null_string, const std::string& prefix,
222  const std::string& suffix);
223 template <typename T>
224 otext& print_pointer(otext& str, const T* value, unsigned indent, const std::string& null_string,
225  const std::string& prefix, const std::string& suffix);
226 
227 template <size_t N>
228 otext& print_bitset(otext& str, const std::bitset<N>& value);
229 template <size_t N>
230 otext& print_bitset(otext& str, const std::bitset<N>& value, unsigned indent);
231 
232 template <typename T>
233 otext& print_list(otext& str, const std::list<T>& values, const std::string& separator);
234 template <typename T>
235 otext& print_list(otext& str, const std::list<T>& values, unsigned indent);
236 
237 template <typename L, typename R>
238 otext& print_pair(otext& str, const std::pair<L, R>& values, const std::string& separator);
239 template <typename L, typename R>
240 otext& print_pair(otext& str, const std::pair<L, R>& values, const std::string& separator, unsigned indent);
241 
242 template <typename K, typename T, typename P>
243 otext& print_map(otext& str, const std::map<K, T, P>& values, const std::string& pair_separator,
244  const std::string& separator);
245 template <typename K, typename T, typename P>
246 otext& print_map(otext& str, const std::map<K, T, P>& values, const std::string& pair_separator, unsigned indent);
247 
248 template <typename K, typename T, typename P>
249 otext& print_multimap(otext& str, const std::multimap<K, T, P>& values, const std::string& pair_separator,
250  const std::string& separator);
251 template <typename K, typename T, typename P>
252 otext& print_multimap(otext& str, const std::multimap<K, T, P>& values, const std::string& pair_separator,
253  unsigned indent);
254 
255 template <typename K, typename P>
256 otext& print_set(otext& str, const std::set<K, P>& values, const std::string& separator);
257 template <typename K, typename P>
258 otext& print_set(otext& str, const std::set<K, P>& values, unsigned indent);
259 
260 template <typename K, typename P>
261 otext& print_multiset(otext& str, const std::multiset<K, P>& values, const std::string& separator);
262 template <typename K, typename P>
263 otext& print_multiset(otext& str, const std::multiset<K, P>& values, unsigned indent);
264 
265 template <typename T>
266 otext& print_vector(otext& str, const std::vector<T>& values, const std::string& separator);
267 template <typename T>
268 otext& print_vector(otext& str, const std::vector<T>& values, unsigned indent);
269 
271 // other string manipulations
273 
274 // Padding function allows a string to be printed in a fixed-width field
275 
276 // The definitions for the alignment are declared in format_types.hpp
277 // Any other value will cause std::invalid_argument to be thrown
278 
279 std::string pad(const std::string& str, alignment_t alignment, unsigned width, char padch = ' ') throw();
280 
281 // whitespace trimming
282 std::string trim_left(const std::string& val);
283 std::string trim_right(const std::string& val);
284 std::string trim(const std::string& val);
285 
286 // case conversion for std::strings
287 std::string lowercase(const std::string& val);
288 std::string uppercase(const std::string& val);
289 
290 // convert characters represented in from_set to the characters in the same position in to_set
291 // for example:
292 // filename = translate(filename, "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
293 // converts the filename to uppercase and returns the result (Note that the uppercase function does this more easily).
294 // if the from_set is longer than the to_set, then the overlap represents characters to delete (i.e. they map to
295 // nothing)
296 std::string translate(const std::string& input, const std::string& from_set, const std::string& to_set);
297 
299 // wildcard matching
301 
302 // this function does wildcard matching of the wildcard expression against the candidate std::string
303 // wildcards are NOT regular expressions
304 // the wildcard characters are * and ? where * matches 1 or more characters and ? matches only one
305 // there are also character sets [a-z] [qwertyuiop] etc. which match 1 character
306 // TODO: character sets like [:alpha:]
307 // TODO eventually: regular expression matching and substitution (3rd party library?)
308 
309 bool match_wildcard(const std::string& wild, const std::string& match);
310 
312 // Perl-inspired split/join functions
314 
315 // splits the string at every occurance of splitter and adds it as a separate string to the return value
316 // the splitter is removed
317 // a string with no splitter in it will give a single-vector string
318 // an empty string gives an empty vector
319 std::vector<std::string> split(const std::string& str, const std::string& splitter = "\n");
320 
321 // the reverse of the above
322 // joins the string vector to create a single string with the joiner inserted between the joins
323 // Note: the joiner will not be added at the beginning or the end
324 // However, there are optional fields to add such prefix and suffix strings
325 std::string join(const std::vector<std::string>&, const std::string& joiner = "\n", const std::string& prefix = "",
326  const std::string& suffix = "");
327 
329 // special displays
330 
331 // display the parameter as a number in bytes, kbytes, Mbytes, Gbytes depending on range
332 std::string display_bytes(long bytes);
333 
334 // display the parameter in seconds as a string representation in weeks, days, hours, minutes, seconds
335 // e.g. "1d 1:01:01" means 1 day, 1 hour, 1 minute and 1 second
336 std::string display_time(unsigned seconds);
337 
339 #include "string_utilities.tpp"
340 #endif
Definition: textio.hpp:37
std::string to_string(datatype dt)
Definition: datatype.h:48