1 #ifndef DYNAMIC_DEPTH_INTERNAL_STRINGS_NUMBERS_H_ // NOLINT
2 #define DYNAMIC_DEPTH_INTERNAL_STRINGS_NUMBERS_H_ // NOLINT
3
4 #include <stddef.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <time.h>
8 #include <functional>
9 #include <limits>
10 #include <string>
11
12 #include "base/integral_types.h"
13 #include "base/port.h"
14 #include "strings/ascii_ctype.h"
15
16 namespace dynamic_depth {
17 namespace strings {
18
19 // Convert strings to numeric values, with strict error checking.
20 // Leading and trailing spaces are allowed.
21 // Negative inputs are not allowed for unsigned ints (unlike strtoul).
22 //
23 // Base must be [0, 2-36].
24 // Base 0:
25 // auto-select base from first two chars:
26 // "0x" -> hex
27 // "0" -> octal
28 // else -> decimal
29 // Base 16:
30 // Number can start with "0x"
31 //
32 // On error, returns false, and sets *value to:
33 // std::numeric_limits<T>::max() on overflow
34 // std::numeric_limits<T>::min() on underflow
35 // conversion of leading substring if available ("123@@@" -> 123)
36 // 0 if no leading substring available
37 // The effect on errno is unspecified.
38 // Do not depend on testing errno.
39 bool safe_strto32_base(const string& text, int32* value, int base);
40 bool safe_strto64_base(const string& text, int64* value, int base);
41 bool safe_strtou32_base(const string& text, uint32* value, int base);
42 bool safe_strtou64_base(const string& text, uint64* value, int base);
43 bool safe_strtosize_t_base(const string& text, size_t* value, int base);
44
45 // Convenience functions with base == 10.
safe_strto32(const string & text,int32 * value)46 inline bool safe_strto32(const string& text, int32* value) {
47 return safe_strto32_base(text, value, 10);
48 }
49
safe_strto64(const string & text,int64 * value)50 inline bool safe_strto64(const string& text, int64* value) {
51 return safe_strto64_base(text, value, 10);
52 }
53
safe_strtou32(const string & text,uint32 * value)54 inline bool safe_strtou32(const string& text, uint32* value) {
55 return safe_strtou32_base(text, value, 10);
56 }
57
safe_strtou64(const string & text,uint64 * value)58 inline bool safe_strtou64(const string& text, uint64* value) {
59 return safe_strtou64_base(text, value, 10);
60 }
61
62 // Convert strings to floating point values.
63 // Leading and trailing spaces are allowed.
64 // Values may be rounded on over- and underflow.
65 bool safe_strtof(const string& str, float* value);
66
67 bool safe_strtod(const string& str, double* value);
68
69 // Previously documented minimums -- the buffers provided must be at least this
70 // long, though these numbers are subject to change:
71 // Int32, UInt32: 12 bytes
72 // Int64, UInt64, Int, Uint: 22 bytes
73 // Time: 30 bytes
74 // Use kFastToBufferSize rather than hardcoding constants.
75 static const int kFastToBufferSize = 32;
76
77 // ----------------------------------------------------------------------
78 // FastInt32ToBufferLeft()
79 // FastUInt32ToBufferLeft()
80 // FastInt64ToBufferLeft()
81 // FastUInt64ToBufferLeft()
82 //
83 // Like the Fast*ToBuffer() functions above, these are intended for speed.
84 // Unlike the Fast*ToBuffer() functions, however, these functions write
85 // their output to the beginning of the buffer (hence the name, as the
86 // output is left-aligned). The caller is responsible for ensuring that
87 // the buffer has enough space to hold the output.
88 //
89 // Returns a pointer to the end of the string (i.e. the null character
90 // terminating the string).
91 // ----------------------------------------------------------------------
92
93 char* FastInt32ToBufferLeft(int32 i, char* buffer); // at least 12 bytes
94 char* FastUInt32ToBufferLeft(uint32 i, char* buffer); // at least 12 bytes
95 char* FastInt64ToBufferLeft(int64 i, char* buffer); // at least 22 bytes
96 char* FastUInt64ToBufferLeft(uint64 i, char* buffer); // at least 22 bytes
97
98 // ----------------------------------------------------------------------
99 // SimpleFtoa()
100 // Description: converts a double or float to a string which, if passed to
101 // strtod() or strtof() respectively, will produce the exact same original
102 // double or float. Exception: for NaN values, strtod(SimpleDtoa(NaN)) or
103 // strtof(SimpleFtoa(NaN)) may produce any NaN value, not necessarily the
104 // exact same original NaN value.
105 //
106 // The output string is not guaranteed to be as short as possible.
107 //
108 // The output string, including terminating NUL, will have length
109 // less than or equal to kFastToBufferSize defined above. Of course,
110 // we would prefer that your code not depend on this property of
111 // the output string. This guarantee derives from a similar guarantee
112 // from the previous generation of char-buffer-based functions.
113 // We had to carry it forward to preserve compatibility.
114 // ----------------------------------------------------------------------
115 string SimpleFtoa(float value);
116
117 // ----------------------------------------------------------------------
118 // SimpleItoa()
119 // Description: converts an integer to a string.
120 // Faster than printf("%d").
121 //
122 // Return value: string
123 // ----------------------------------------------------------------------
SimpleItoa(int32 i)124 inline string SimpleItoa(int32 i) {
125 char buf[16]; // Longest is -2147483648
126 return string(buf, FastInt32ToBufferLeft(i, buf));
127 }
128
129 // We need this overload because otherwise SimpleItoa(5U) wouldn't compile.
SimpleItoa(uint32 i)130 inline string SimpleItoa(uint32 i) {
131 char buf[16]; // Longest is 4294967295
132 return string(buf, FastUInt32ToBufferLeft(i, buf));
133 }
134
SimpleItoa(int64 i)135 inline string SimpleItoa(int64 i) {
136 char buf[32]; // Longest is -9223372036854775808
137 return string(buf, FastInt64ToBufferLeft(i, buf));
138 }
139
140 // We need this overload because otherwise SimpleItoa(5ULL) wouldn't compile.
SimpleItoa(uint64 i)141 inline string SimpleItoa(uint64 i) {
142 char buf[32]; // Longest is 18446744073709551615
143 return string(buf, FastUInt64ToBufferLeft(i, buf));
144 }
145
SimpleItoa(long i)146 inline string SimpleItoa(long i) { // NOLINT long is OK here
147 if (sizeof(i) == 64 / 8) {
148 return SimpleItoa(static_cast<int64>(i));
149 } else if (sizeof(i) == 32 / 8) {
150 return SimpleItoa(static_cast<int32>(i));
151 }
152 }
153
SimpleItoa(unsigned long i)154 inline string SimpleItoa(unsigned long i) { // NOLINT long is OK here
155 if (sizeof(i) == 64 / 8) {
156 return SimpleItoa(static_cast<uint64>(i));
157 } else if (sizeof(i) == 32 / 8) {
158 return SimpleItoa(static_cast<uint32>(i));
159 }
160 }
161
162 // Required buffer size for FloatToBuffer is kFastToBufferSize.
163 char* FloatToBuffer(float i, char* buffer);
164
165 } // namespace strings
166 } // namespace dynamic_depth
167
168 #endif // DYNAMIC_DEPTH_INTERNAL_STRINGS_NUMBERS_H_ // NOLINT
169