aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/lib/strings/numbers.h
diff options
context:
space:
mode:
authorGravatar Patrick Nguyen <drpng@google.com>2017-12-28 16:04:42 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2017-12-28 16:08:58 -0800
commit20765b3e1ae3b718699592c98aa9805cb874b6d1 (patch)
treeb429a74cd0046404644f34cc8fe6ff2cab78bb85 /tensorflow/core/lib/strings/numbers.h
parent2e2715baa84720f786b38d1f9cb6887399020d6f (diff)
Merge changes from github.
PiperOrigin-RevId: 180301735
Diffstat (limited to 'tensorflow/core/lib/strings/numbers.h')
-rw-r--r--tensorflow/core/lib/strings/numbers.h32
1 files changed, 32 insertions, 0 deletions
diff --git a/tensorflow/core/lib/strings/numbers.h b/tensorflow/core/lib/strings/numbers.h
index 31b6abbac6..3c45b90274 100644
--- a/tensorflow/core/lib/strings/numbers.h
+++ b/tensorflow/core/lib/strings/numbers.h
@@ -122,6 +122,38 @@ bool safe_strtof(const char* str, float* value);
// Values may be rounded on over- and underflow.
bool safe_strtod(const char* str, double* value);
+inline bool ProtoParseNumeric(StringPiece s, int32* value) {
+ return safe_strto32(s, value);
+}
+
+inline bool ProtoParseNumeric(StringPiece s, uint32* value) {
+ return safe_strtou32(s, value);
+}
+
+inline bool ProtoParseNumeric(StringPiece s, int64* value) {
+ return safe_strto64(s, value);
+}
+
+inline bool ProtoParseNumeric(StringPiece s, uint64* value) {
+ return safe_strtou64(s, value);
+}
+
+inline bool ProtoParseNumeric(StringPiece s, float* value) {
+ return safe_strtof(s.ToString().c_str(), value);
+}
+
+inline bool ProtoParseNumeric(StringPiece s, double* value) {
+ return safe_strtod(s.ToString().c_str(), value);
+}
+
+// Convert strings to number of type T.
+// Leading and trailing spaces are allowed.
+// Values may be rounded on over- and underflow.
+template <typename T>
+bool SafeStringToNumeric(StringPiece s, T* value) {
+ return ProtoParseNumeric(s, value);
+}
+
// Converts from an int64 to a human readable string representing the
// same number, using decimal powers. e.g. 1200000 -> "1.20M".
string HumanReadableNum(int64 value);