/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/util/include/RAT/GLG4StringUtil.hh Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/util/include/RAT/GLG4StringUtil.hh Source File
Ratpac-two
GLG4StringUtil.hh
1 #ifndef __GLG4StringUtil_hh__
2 #define __GLG4StringUtil_hh__
3 
4 // Miscellaneous string utilities derived from BSD-licensed stlplus library
5 // Copyright 2004, Andy Rushton
6 // Renamed to prevent linker collision - (SS)
7 
8 #include <stdarg.h>
9 
10 #include <cstdlib>
11 #include <cstring>
12 #include <stdexcept>
13 #include <string>
14 #include <vector>
15 
16 #include "format_types.hpp"
17 
19 // Conversions of Integer types to string
21 
22 // The radix (i.e. base) for these conversions can be any value from base 2 to
23 // base 36 specifying any other radix causes std::invalid_argument to be thrown
24 
25 // The way in which the radix is displayed is defined in radix_types.hpp
26 // If any other value is used, std::invalid_argument is thrown
27 
28 // The width argument specifies the number of numerical digits to use in the
29 // result This is a minimum - if the value requires more digits then it will be
30 // wider than the width argument However, if it is smaller, then it will be
31 // extended to the specified width Then, the radix display prefix is added to
32 // this width For example, using the hash representation of 0 in hex with
33 // width=4 gives: 16#0000 - so there's 4 digits in the number part
34 
35 std::string util_to_string(bool i, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
36  unsigned width = 0);
37 
38 std::string util_to_string(short i, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
39  unsigned width = 0);
40 
41 std::string util_to_string(unsigned short i, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
42  unsigned width = 0);
43 
44 std::string util_to_string(int i, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
45  unsigned width = 0);
46 
47 std::string util_to_string(unsigned int i, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
48  unsigned width = 0);
49 
50 std::string util_to_string(long i, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
51  unsigned width = 0);
52 
53 std::string util_to_string(unsigned long i, unsigned radix = 10, radix_display_t display = radix_c_style_or_hash,
54  unsigned width = 0);
55 
56 std::string util_to_string(const void *, unsigned radix = 16, radix_display_t display = radix_c_style_or_hash,
57  unsigned width = 0);
58 
60 // convert a real type to string
62 
63 // Only decimal radix is supported
64 
65 // The way in which the number is displayed is defined in radix_types.hpp
66 // Using any other value for the display type causes std::invalid_argument to be
67 // thrown
68 
69 std::string util_to_string(float f, real_display_t display = display_mixed, unsigned width = 0, unsigned precision = 6);
70 std::string util_to_string(double f, real_display_t display = display_mixed, unsigned width = 0,
71  unsigned precision = 6);
72 
74 // convert a string to a simple type
76 
77 // Convert a string to an integer type
78 // supports all the formats described above for the reverse conversion
79 // If the radix is set to zero, the conversions deduce the radix from the string
80 // representation So, 0b prefix is binary, 0 prefix is octal, 0x is hex and
81 // <base># prefix is my hash format A non-zero radix should be used when the
82 // string value has no radix information and is non-decimal e.g. the hex value
83 // FEDCBA has no indication that it is hex, so specify radix 16 The radix must
84 // be either zero as explained above, or in the range 2 to 16 Any other value
85 // will cause std::invalid_argument to be thrown
86 
87 bool util_to_bool(const std::string &value, unsigned radix = 0);
88 
89 short util_to_short(const std::string &value, unsigned radix = 0);
90 
91 unsigned short util_to_ushort(const std::string &value, unsigned radix = 0);
92 
93 int util_to_int(const std::string &value, unsigned radix = 0);
94 
95 unsigned int util_to_uint(const std::string &value, unsigned radix = 0);
96 
97 long util_to_long(const std::string &value, unsigned radix = 0);
98 
99 unsigned long util_to_ulong(const std::string &value, unsigned radix = 0);
100 
101 void *util_to_void_star(const std::string &value, unsigned radix = 0);
102 
103 // Convert a floating-point type
104 
105 float util_to_float(const std::string &value);
106 
107 double util_to_double(const std::string &value);
108 
110 // Perl-inspired split/join functions
112 
113 // splits the string at every occurance of splitter and adds it as a separate
114 // string to the return value the splitter is removed a string with no splitter
115 // in it will give a single-vector string an empty string gives an empty vector
116 std::vector<std::string> util_split(const std::string &str, const std::string &splitter = "\n");
117 
118 // the reverse of the above
119 // joins the string vector to create a single string with the joiner inserted
120 // between the joins Note: the joiner will not be added at the beginning or the
121 // end However, there are optional fields to add such prefix and suffix strings
122 std::string util_join(const std::vector<std::string> &, const std::string &joiner = "\n",
123  const std::string &prefix = "", const std::string &suffix = "");
124 
125 // strips leading and trailing characters from s. This function by
126 // Glenn Horton-Smith.
127 std::string util_strip(const std::string &s, const std::string &stripchars);
128 // strip spaces, tabs, and quotation marks
129 std::string util_strip_default(const std::string &s);
130 
131 // Really handy functions which work like sprintf, but operate on
132 // C++ strings instead
133 
134 int util_dprintf(std::string &formatted, const char *format, ...);
135 int util_vdprintf(std::string &formatted, const char *format, va_list args);
136 
137 std::string util_dformat(const char *format, ...);
138 std::string util_vdformat(const char *format, va_list);
139 
140 #endif