about summary refs log tree commit diff
path: root/absl/strings/numbers.h
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2018-05-22T14·57-0700
committerkatzdm <katzdm@google.com>2018-05-22T14·59-0400
commit3e671c7821e6f4d80db8089e7e580e5f7448a495 (patch)
tree673192a300c0dae5c19d94a11b2ac6bd011a5a38 /absl/strings/numbers.h
parent59ae4d5a0e833bedd9d7cc059ac15a9dc130e3f7 (diff)
- f619bd3c1ab57586b7ed6010db09cdaf8a6882f9 Fix comment typos in strings/numbers.h . by Daniel Katz <katzdm@google.com>
  - 1f7096d96dcf2a75ba13c210b709432165a05963 Internal namespace change by Gennadiy Rozental <rogeeff@google.com>
  - ac2911b3047a79a1fc8bbcdd3f3cb24b02ab0054 Fixing -Wunused-function warning in failure_signal_handler. by Abseil Team <absl-team@google.com>

GitOrigin-RevId: f619bd3c1ab57586b7ed6010db09cdaf8a6882f9
Change-Id: Ib09117eacfcb40ad25b216dbe8dcc9b8f1a0df6c
Diffstat (limited to 'absl/strings/numbers.h')
-rw-r--r--absl/strings/numbers.h36
1 files changed, 22 insertions, 14 deletions
diff --git a/absl/strings/numbers.h b/absl/strings/numbers.h
index adf706a4e26e..75925e61f2c0 100644
--- a/absl/strings/numbers.h
+++ b/absl/strings/numbers.h
@@ -124,17 +124,11 @@ char* FastIntToBuffer(int_type i, char* buffer) {
   }
 }
 
-}  // namespace numbers_internal
-
-// SimpleAtoi()
-//
-// Converts a std::string to an integer, using `safe_strto?()` functions for actual
-// parsing, returning `true` if successful. The `safe_strto?()` functions apply
-// strict checking; the std::string must be a base-10 integer, optionally followed or
-// preceded by ASCII whitespace, with a value in the range of the corresponding
-// integer type.
+// Implementation of SimpleAtoi, generalized to support arbitrary base (used
+// with base different from 10 elsewhere in Abseil implementation).
 template <typename int_type>
-ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view s, int_type* out) {
+ABSL_MUST_USE_RESULT bool safe_strtoi_base(absl::string_view s, int_type* out,
+                                           int base) {
   static_assert(sizeof(*out) == 4 || sizeof(*out) == 8,
                 "SimpleAtoi works only with 32-bit or 64-bit integers.");
   static_assert(!std::is_floating_point<int_type>::value,
@@ -146,27 +140,41 @@ ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view s, int_type* out) {
   if (static_cast<int_type>(1) - 2 < 0) {  // Signed
     if (sizeof(*out) == 64 / 8) {       // 64-bit
       int64_t val;
-      parsed = numbers_internal::safe_strto64_base(s, &val, 10);
+      parsed = numbers_internal::safe_strto64_base(s, &val, base);
       *out = static_cast<int_type>(val);
     } else {  // 32-bit
       int32_t val;
-      parsed = numbers_internal::safe_strto32_base(s, &val, 10);
+      parsed = numbers_internal::safe_strto32_base(s, &val, base);
       *out = static_cast<int_type>(val);
     }
   } else {                         // Unsigned
     if (sizeof(*out) == 64 / 8) {  // 64-bit
       uint64_t val;
-      parsed = numbers_internal::safe_strtou64_base(s, &val, 10);
+      parsed = numbers_internal::safe_strtou64_base(s, &val, base);
       *out = static_cast<int_type>(val);
     } else {  // 32-bit
       uint32_t val;
-      parsed = numbers_internal::safe_strtou32_base(s, &val, 10);
+      parsed = numbers_internal::safe_strtou32_base(s, &val, base);
       *out = static_cast<int_type>(val);
     }
   }
   return parsed;
 }
 
+}  // namespace numbers_internal
+
+// SimpleAtoi()
+//
+// Converts a std::string to an integer, using `safe_strto?()` functions for actual
+// parsing, returning `true` if successful. The `safe_strto?()` functions apply
+// strict checking; the std::string must be a base-10 integer, optionally followed or
+// preceded by ASCII whitespace, with a value in the range of the corresponding
+// integer type.
+template <typename int_type>
+ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view s, int_type* out) {
+  return numbers_internal::safe_strtoi_base(s, out, 10);
+}
+
 }  // namespace absl
 
 #endif  // ABSL_STRINGS_NUMBERS_H_