1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #ifndef TENSORFLOW_CORE_LIB_STRINGS_NUMBERS_H_
17 #define TENSORFLOW_CORE_LIB_STRINGS_NUMBERS_H_
18
19 #include <string>
20
21 #include "tensorflow/core/lib/core/stringpiece.h"
22 #include "tensorflow/core/platform/types.h"
23
24 namespace tensorflow {
25 namespace strings {
26
27 // ----------------------------------------------------------------------
28 // FastIntToBufferLeft()
29 // These are intended for speed.
30 //
31 // All functions take the output buffer as an arg. FastInt() uses
32 // at most 22 bytes, FastTime() uses exactly 30 bytes. They all
33 // return a pointer to the beginning of the output, which is the same as
34 // the beginning of the input buffer.
35 //
36 // NOTE: In 64-bit land, sizeof(time_t) is 8, so it is possible
37 // to pass to FastTimeToBuffer() a time whose year cannot be
38 // represented in 4 digits. In this case, the output buffer
39 // will contain the string "Invalid:<value>"
40 // ----------------------------------------------------------------------
41
42 // Previously documented minimums -- the buffers provided must be at least this
43 // long, though these numbers are subject to change:
44 // Int32, UInt32: 12 bytes
45 // Int64, UInt64, Int, Uint: 22 bytes
46 // Time: 30 bytes
47 // Use kFastToBufferSize rather than hardcoding constants.
48 static const int kFastToBufferSize = 32;
49
50 // ----------------------------------------------------------------------
51 // FastInt32ToBufferLeft()
52 // FastUInt32ToBufferLeft()
53 // FastInt64ToBufferLeft()
54 // FastUInt64ToBufferLeft()
55 //
56 // These functions convert their numeric argument to an ASCII
57 // representation of the numeric value in base 10, with the
58 // representation being left-aligned in the buffer. The caller is
59 // responsible for ensuring that the buffer has enough space to hold
60 // the output. The buffer should typically be at least kFastToBufferSize
61 // bytes.
62 //
63 // Returns the number of characters written.
64 // ----------------------------------------------------------------------
65
66 size_t FastInt32ToBufferLeft(int32 i, char* buffer); // at least 12 bytes
67 size_t FastUInt32ToBufferLeft(uint32 i, char* buffer); // at least 12 bytes
68 size_t FastInt64ToBufferLeft(int64 i, char* buffer); // at least 22 bytes
69 size_t FastUInt64ToBufferLeft(uint64 i, char* buffer); // at least 22 bytes
70
71 // Required buffer size for DoubleToBuffer is kFastToBufferSize.
72 // Required buffer size for FloatToBuffer is kFastToBufferSize.
73 size_t DoubleToBuffer(double value, char* buffer);
74 size_t FloatToBuffer(float value, char* buffer);
75
76 // Convert a 64-bit fingerprint value to an ASCII representation.
77 string FpToString(Fprint fp);
78
79 // Attempt to parse a fingerprint in the form encoded by FpToString. If
80 // successful, stores the fingerprint in *fp and returns true. Otherwise,
81 // returns false.
82 bool StringToFp(const string& s, Fprint* fp);
83
84 // Convert a 64-bit fingerprint value to an ASCII representation that
85 // is terminated by a '\0'.
86 // Buf must point to an array of at least kFastToBufferSize characters
87 StringPiece Uint64ToHexString(uint64 v, char* buf);
88
89 // Attempt to parse a uint64 in the form encoded by FastUint64ToHexString. If
90 // successful, stores the value in *v and returns true. Otherwise,
91 // returns false.
92 bool HexStringToUint64(const StringPiece& s, uint64* v);
93
94 // Convert strings to 32bit integer values.
95 // Leading and trailing spaces are allowed.
96 // Return false with overflow or invalid input.
97 bool safe_strto32(StringPiece str, int32* value);
98
99 // Convert strings to unsigned 32bit integer values.
100 // Leading and trailing spaces are allowed.
101 // Return false with overflow or invalid input.
102 bool safe_strtou32(StringPiece str, uint32* value);
103
104 // Convert strings to 64bit integer values.
105 // Leading and trailing spaces are allowed.
106 // Return false with overflow or invalid input.
107 bool safe_strto64(StringPiece str, int64* value);
108
109 // Convert strings to unsigned 64bit integer values.
110 // Leading and trailing spaces are allowed.
111 // Return false with overflow or invalid input.
112 bool safe_strtou64(StringPiece str, uint64* value);
113
114 // Convert strings to floating point values.
115 // Leading and trailing spaces are allowed.
116 // Values may be rounded on over- and underflow.
117 // Returns false on invalid input or if `strlen(value) >= kFastToBufferSize`.
118 bool safe_strtof(StringPiece str, float* value);
119
120 // Convert strings to double precision floating point values.
121 // Leading and trailing spaces are allowed.
122 // Values may be rounded on over- and underflow.
123 // Returns false on invalid input or if `strlen(value) >= kFastToBufferSize`.
124 bool safe_strtod(StringPiece str, double* value);
125
ProtoParseNumeric(StringPiece s,int32 * value)126 inline bool ProtoParseNumeric(StringPiece s, int32* value) {
127 return safe_strto32(s, value);
128 }
129
ProtoParseNumeric(StringPiece s,uint32 * value)130 inline bool ProtoParseNumeric(StringPiece s, uint32* value) {
131 return safe_strtou32(s, value);
132 }
133
ProtoParseNumeric(StringPiece s,int64 * value)134 inline bool ProtoParseNumeric(StringPiece s, int64* value) {
135 return safe_strto64(s, value);
136 }
137
ProtoParseNumeric(StringPiece s,uint64 * value)138 inline bool ProtoParseNumeric(StringPiece s, uint64* value) {
139 return safe_strtou64(s, value);
140 }
141
ProtoParseNumeric(StringPiece s,float * value)142 inline bool ProtoParseNumeric(StringPiece s, float* value) {
143 return safe_strtof(s, value);
144 }
145
ProtoParseNumeric(StringPiece s,double * value)146 inline bool ProtoParseNumeric(StringPiece s, double* value) {
147 return safe_strtod(s, value);
148 }
149
150 // Convert strings to number of type T.
151 // Leading and trailing spaces are allowed.
152 // Values may be rounded on over- and underflow.
153 template <typename T>
SafeStringToNumeric(StringPiece s,T * value)154 bool SafeStringToNumeric(StringPiece s, T* value) {
155 return ProtoParseNumeric(s, value);
156 }
157
158 // Converts from an int64 to a human readable string representing the
159 // same number, using decimal powers. e.g. 1200000 -> "1.20M".
160 string HumanReadableNum(int64 value);
161
162 // Converts from an int64 representing a number of bytes to a
163 // human readable string representing the same number.
164 // e.g. 12345678 -> "11.77MiB".
165 string HumanReadableNumBytes(int64 num_bytes);
166
167 // Converts a time interval as double to a human readable
168 // string. For example:
169 // 0.001 -> "1 ms"
170 // 10.0 -> "10 s"
171 // 933120.0 -> "10.8 days"
172 // 39420000.0 -> "1.25 years"
173 // -10 -> "-10 s"
174 string HumanReadableElapsedTime(double seconds);
175
176 } // namespace strings
177 } // namespace tensorflow
178
179 #endif // TENSORFLOW_CORE_LIB_STRINGS_NUMBERS_H_
180