[libc][NFC] rename str_conv_utils to str_to_integer
authorMichael Jones <michaelrj@google.com>
Tue, 2 Nov 2021 21:49:38 +0000 (14:49 -0700)
committerMichael Jones <michaelrj@google.com>
Wed, 3 Nov 2021 22:56:28 +0000 (15:56 -0700)
rename str_conv_utils to str_to_integer to be more
in line with str_to_float.

Reviewed By: sivachandra, lntue

Differential Revision: https://reviews.llvm.org/D113061

18 files changed:
libc/src/__support/CMakeLists.txt
libc/src/__support/detailed_powers_of_ten.h
libc/src/__support/high_precision_decimal.h
libc/src/__support/str_conv_utils.h [deleted file]
libc/src/__support/str_to_float.h
libc/src/__support/str_to_integer.h [new file with mode: 0644]
libc/src/inttypes/CMakeLists.txt
libc/src/inttypes/strtoimax.cpp
libc/src/inttypes/strtoumax.cpp
libc/src/stdlib/CMakeLists.txt
libc/src/stdlib/atoi.cpp
libc/src/stdlib/atol.cpp
libc/src/stdlib/atoll.cpp
libc/src/stdlib/strtol.cpp
libc/src/stdlib/strtoll.cpp
libc/src/stdlib/strtoul.cpp
libc/src/stdlib/strtoull.cpp
libc/test/src/__support/CMakeLists.txt

index 5d2667dcb7fa451cbb002b4ee932bd8a49d9210f..567ed069b82c5e5ef3e8a12abe2bcd3a58d09af8 100644 (file)
@@ -23,11 +23,22 @@ add_header_library(
 )
 
 add_header_library(
-  str_conv_utils
+  str_to_integer
+  HDRS
+    str_to_integer.h
+  DEPENDS
+    .ctype_utils
+    libc.include.errno
+    libc.src.errno.__errno_location
+    libc.src.__support.CPP.standalone_cpp
+)
+
+add_header_library(
+  str_to_float
   HDRS
-    str_conv_utils.h
     str_to_float.h
   DEPENDS
+    .str_to_integer
     .ctype_utils
     .high_precision_decimal
     libc.include.errno
index e168b95b8998eb36fba8dc02715971684fcb47f2..9a58ba5dff782eabc1bbc6b8c8d9abd5ca97c97c 100644 (file)
@@ -21,7 +21,7 @@ namespace internal {
 // and contains the 128 bit mantissa approximations of the powers of 10 from
 // -348 to 347. The exponents are implied by a linear expression with slope
 // 217706.0/65536.0 ≈ log(10)/log(2). This is used by the Eisel-Lemire algorithm
-// in str_conv_utils.h.
+// in str_to_float.h.
 
 constexpr int32_t DETAILED_POWERS_OF_TEN_MIN_EXP_10 = -348;
 constexpr int32_t DETAILED_POWERS_OF_TEN_MAX_EXP_10 = 347;
index ae8aac37605ad5d12b8cc4fa14046a5c7f1f67e5..352de581a8f72d54d848e7f45d11f3581009aaae 100644 (file)
@@ -10,7 +10,7 @@
 #define LIBC_SRC_SUPPORT_HIGH_PRECISION_DECIMAL_H
 
 #include "src/__support/ctype_utils.h"
-#include "src/__support/str_conv_utils.h"
+#include "src/__support/str_to_integer.h"
 #include <stdint.h>
 
 namespace __llvm_libc {
diff --git a/libc/src/__support/str_conv_utils.h b/libc/src/__support/str_conv_utils.h
deleted file mode 100644 (file)
index 13c800a..0000000
+++ /dev/null
@@ -1,151 +0,0 @@
-//===-- Stdlib utils --------------------------------------------*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LIBC_SRC_STDLIB_STDLIB_UTILS_H
-#define LIBC_SRC_STDLIB_STDLIB_UTILS_H
-
-#include "src/__support/CPP/Limits.h"
-#include "src/__support/ctype_utils.h"
-#include <errno.h>
-#include <limits.h>
-
-namespace __llvm_libc {
-namespace internal {
-
-// Returns a pointer to the first character in src that is not a whitespace
-// character (as determined by isspace())
-static inline const char *first_non_whitespace(const char *__restrict src) {
-  while (internal::isspace(*src)) {
-    ++src;
-  }
-  return src;
-}
-
-static inline int b36_char_to_int(char input) {
-  if (isdigit(input))
-    return input - '0';
-  if (isalpha(input))
-    return (input | 32) + 10 - 'a';
-  return 0;
-}
-
-// checks if the next 3 characters of the string pointer are the start of a
-// hexadecimal number. Does not advance the string pointer.
-static inline bool is_hex_start(const char *__restrict src) {
-  return *src == '0' && (*(src + 1) | 32) == 'x' && isalnum(*(src + 2)) &&
-         b36_char_to_int(*(src + 2)) < 16;
-}
-
-// Takes the address of the string pointer and parses the base from the start of
-// it. This function will advance |src| to the first valid digit in the inferred
-// base.
-static inline int infer_base(const char *__restrict *__restrict src) {
-  // A hexadecimal number is defined as "the prefix 0x or 0X followed by a
-  // sequence of the deimal digits and the letters a (or A) through f (or F)
-  // with values 10 through 15 respectively." (C standard 6.4.4.1)
-  if (is_hex_start(*src)) {
-    (*src) += 2;
-    return 16;
-  } // An octal number is defined as "the prefix 0 optionally followed by a
-    // sequence of the digits 0 through 7 only" (C standard 6.4.4.1) and so any
-    // number that starts with 0, including just 0, is an octal number.
-  else if (**src == '0') {
-    return 8;
-  } // A decimal number is defined as beginning "with a nonzero digit and
-    // consist[ing] of a sequence of decimal digits." (C standard 6.4.4.1)
-  else {
-    return 10;
-  }
-}
-
-// Takes a pointer to a string, a pointer to a string pointer, and the base to
-// convert to. This function is used as the backend for all of the string to int
-// functions.
-template <class T>
-static inline T strtointeger(const char *__restrict src,
-                             char **__restrict str_end, int base) {
-  unsigned long long result = 0;
-  bool is_number = false;
-  const char *original_src = src;
-
-  if (base < 0 || base == 1 || base > 36) {
-    errno = EINVAL; // NOLINT
-    return 0;
-  }
-
-  src = first_non_whitespace(src);
-
-  char result_sign = '+';
-  if (*src == '+' || *src == '-') {
-    result_sign = *src;
-    ++src;
-  }
-
-  if (base == 0) {
-    base = infer_base(&src);
-  } else if (base == 16 && is_hex_start(src)) {
-    src = src + 2;
-  }
-
-  constexpr bool is_unsigned = (__llvm_libc::cpp::NumericLimits<T>::min() == 0);
-  const bool is_positive = (result_sign == '+');
-  unsigned long long constexpr NEGATIVE_MAX =
-      !is_unsigned ? static_cast<unsigned long long>(
-                         __llvm_libc::cpp::NumericLimits<T>::max()) +
-                         1
-                   : __llvm_libc::cpp::NumericLimits<T>::max();
-  unsigned long long const ABS_MAX =
-      (is_positive ? __llvm_libc::cpp::NumericLimits<T>::max() : NEGATIVE_MAX);
-  unsigned long long const ABS_MAX_DIV_BY_BASE = ABS_MAX / base;
-  while (isalnum(*src)) {
-    int cur_digit = b36_char_to_int(*src);
-    if (cur_digit >= base)
-      break;
-
-    is_number = true;
-    ++src;
-
-    // If the number has already hit the maximum value for the current type then
-    // the result cannot change, but we still need to advance src to the end of
-    // the number.
-    if (result == ABS_MAX) {
-      errno = ERANGE; // NOLINT
-      continue;
-    }
-
-    if (result > ABS_MAX_DIV_BY_BASE) {
-      result = ABS_MAX;
-      errno = ERANGE; // NOLINT
-    } else {
-      result = result * base;
-    }
-    if (result > ABS_MAX - cur_digit) {
-      result = ABS_MAX;
-      errno = ERANGE; // NOLINT
-    } else {
-      result = result + cur_digit;
-    }
-  }
-
-  if (str_end != nullptr)
-    *str_end = const_cast<char *>(is_number ? src : original_src);
-
-  if (result == ABS_MAX) {
-    if (is_positive || is_unsigned)
-      return __llvm_libc::cpp::NumericLimits<T>::max();
-    else // T is signed and there is a negative overflow
-      return __llvm_libc::cpp::NumericLimits<T>::min();
-  }
-
-  return is_positive ? static_cast<T>(result) : -static_cast<T>(result);
-}
-
-} // namespace internal
-} // namespace __llvm_libc
-
-#endif // LIBC_SRC_STDLIB_STDLIB_UTILS_H
index b62e8ff4c4dc31eb5eb7682fd5a5f372f9231442..59bd1ec8e5da4616538da532e3331de19278cb5f 100644 (file)
@@ -14,7 +14,7 @@
 #include "src/__support/ctype_utils.h"
 #include "src/__support/detailed_powers_of_ten.h"
 #include "src/__support/high_precision_decimal.h"
-#include "src/__support/str_conv_utils.h"
+#include "src/__support/str_to_integer.h"
 #include <errno.h>
 
 namespace __llvm_libc {
diff --git a/libc/src/__support/str_to_integer.h b/libc/src/__support/str_to_integer.h
new file mode 100644 (file)
index 0000000..ec7f6f5
--- /dev/null
@@ -0,0 +1,151 @@
+//===-- String to integer conversion utils ----------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LIBC_SRC_SUPPORT_STR_TO_INTEGER_H
+#define LIBC_SRC_SUPPORT_STR_TO_INTEGER_H
+
+#include "src/__support/CPP/Limits.h"
+#include "src/__support/ctype_utils.h"
+#include <errno.h>
+#include <limits.h>
+
+namespace __llvm_libc {
+namespace internal {
+
+// Returns a pointer to the first character in src that is not a whitespace
+// character (as determined by isspace())
+static inline const char *first_non_whitespace(const char *__restrict src) {
+  while (internal::isspace(*src)) {
+    ++src;
+  }
+  return src;
+}
+
+static inline int b36_char_to_int(char input) {
+  if (isdigit(input))
+    return input - '0';
+  if (isalpha(input))
+    return (input | 32) + 10 - 'a';
+  return 0;
+}
+
+// checks if the next 3 characters of the string pointer are the start of a
+// hexadecimal number. Does not advance the string pointer.
+static inline bool is_hex_start(const char *__restrict src) {
+  return *src == '0' && (*(src + 1) | 32) == 'x' && isalnum(*(src + 2)) &&
+         b36_char_to_int(*(src + 2)) < 16;
+}
+
+// Takes the address of the string pointer and parses the base from the start of
+// it. This function will advance |src| to the first valid digit in the inferred
+// base.
+static inline int infer_base(const char *__restrict *__restrict src) {
+  // A hexadecimal number is defined as "the prefix 0x or 0X followed by a
+  // sequence of the deimal digits and the letters a (or A) through f (or F)
+  // with values 10 through 15 respectively." (C standard 6.4.4.1)
+  if (is_hex_start(*src)) {
+    (*src) += 2;
+    return 16;
+  } // An octal number is defined as "the prefix 0 optionally followed by a
+    // sequence of the digits 0 through 7 only" (C standard 6.4.4.1) and so any
+    // number that starts with 0, including just 0, is an octal number.
+  else if (**src == '0') {
+    return 8;
+  } // A decimal number is defined as beginning "with a nonzero digit and
+    // consist[ing] of a sequence of decimal digits." (C standard 6.4.4.1)
+  else {
+    return 10;
+  }
+}
+
+// Takes a pointer to a string, a pointer to a string pointer, and the base to
+// convert to. This function is used as the backend for all of the string to int
+// functions.
+template <class T>
+static inline T strtointeger(const char *__restrict src,
+                             char **__restrict str_end, int base) {
+  unsigned long long result = 0;
+  bool is_number = false;
+  const char *original_src = src;
+
+  if (base < 0 || base == 1 || base > 36) {
+    errno = EINVAL; // NOLINT
+    return 0;
+  }
+
+  src = first_non_whitespace(src);
+
+  char result_sign = '+';
+  if (*src == '+' || *src == '-') {
+    result_sign = *src;
+    ++src;
+  }
+
+  if (base == 0) {
+    base = infer_base(&src);
+  } else if (base == 16 && is_hex_start(src)) {
+    src = src + 2;
+  }
+
+  constexpr bool is_unsigned = (__llvm_libc::cpp::NumericLimits<T>::min() == 0);
+  const bool is_positive = (result_sign == '+');
+  unsigned long long constexpr NEGATIVE_MAX =
+      !is_unsigned ? static_cast<unsigned long long>(
+                         __llvm_libc::cpp::NumericLimits<T>::max()) +
+                         1
+                   : __llvm_libc::cpp::NumericLimits<T>::max();
+  unsigned long long const ABS_MAX =
+      (is_positive ? __llvm_libc::cpp::NumericLimits<T>::max() : NEGATIVE_MAX);
+  unsigned long long const ABS_MAX_DIV_BY_BASE = ABS_MAX / base;
+  while (isalnum(*src)) {
+    int cur_digit = b36_char_to_int(*src);
+    if (cur_digit >= base)
+      break;
+
+    is_number = true;
+    ++src;
+
+    // If the number has already hit the maximum value for the current type then
+    // the result cannot change, but we still need to advance src to the end of
+    // the number.
+    if (result == ABS_MAX) {
+      errno = ERANGE; // NOLINT
+      continue;
+    }
+
+    if (result > ABS_MAX_DIV_BY_BASE) {
+      result = ABS_MAX;
+      errno = ERANGE; // NOLINT
+    } else {
+      result = result * base;
+    }
+    if (result > ABS_MAX - cur_digit) {
+      result = ABS_MAX;
+      errno = ERANGE; // NOLINT
+    } else {
+      result = result + cur_digit;
+    }
+  }
+
+  if (str_end != nullptr)
+    *str_end = const_cast<char *>(is_number ? src : original_src);
+
+  if (result == ABS_MAX) {
+    if (is_positive || is_unsigned)
+      return __llvm_libc::cpp::NumericLimits<T>::max();
+    else // T is signed and there is a negative overflow
+      return __llvm_libc::cpp::NumericLimits<T>::min();
+  }
+
+  return is_positive ? static_cast<T>(result) : -static_cast<T>(result);
+}
+
+} // namespace internal
+} // namespace __llvm_libc
+
+#endif // LIBC_SRC_SUPPORT_STR_TO_INTEGER_H
index e0e0f4d57f2b9f1e030e1be0429eb4aa6d462f09..00fd791320fb289a5586c54234cb6dfa7f64f71a 100644 (file)
@@ -5,7 +5,7 @@ add_entrypoint_object(
   HDRS
     strtoimax.h
   DEPENDS
-    libc.src.__support.str_conv_utils
+    libc.src.__support.str_to_integer
 )
 
 add_entrypoint_object(
@@ -15,7 +15,7 @@ add_entrypoint_object(
   HDRS
     strtoumax.h
   DEPENDS
-    libc.src.__support.str_conv_utils
+    libc.src.__support.str_to_integer
 )
 
 add_entrypoint_object(
index ecd59ff71738394fa0bd4f00b194c3b7e7477384..ef5e84e2034a7c7ea7229b4be2840e66784b4b16 100644 (file)
@@ -8,7 +8,7 @@
 
 #include "src/inttypes/strtoimax.h"
 #include "src/__support/common.h"
-#include "src/__support/str_conv_utils.h"
+#include "src/__support/str_to_integer.h"
 
 namespace __llvm_libc {
 
index cc3f1451fdae3b621a929891d5b7c888cd64433f..edf8b65f161a2d0a9912a119a393fee0bb21e371 100644 (file)
@@ -8,7 +8,7 @@
 
 #include "src/inttypes/strtoumax.h"
 #include "src/__support/common.h"
-#include "src/__support/str_conv_utils.h"
+#include "src/__support/str_to_integer.h"
 
 namespace __llvm_libc {
 
index 3700d9a2409528ef500bfcabe662952cc146966b..f5f339080e82fed57afe6213a67ff8fb25328dc5 100644 (file)
@@ -5,7 +5,7 @@ add_entrypoint_object(
   HDRS
     atoi.h
   DEPENDS
-    libc.src.__support.str_conv_utils
+    libc.src.__support.str_to_integer
 )
 
 add_entrypoint_object(
@@ -15,7 +15,7 @@ add_entrypoint_object(
   HDRS
     atof.h
   DEPENDS
-    libc.src.__support.str_conv_utils
+    libc.src.__support.str_to_float
 )
 
 add_entrypoint_object(
@@ -25,7 +25,7 @@ add_entrypoint_object(
   HDRS
     atol.h
   DEPENDS
-    libc.src.__support.str_conv_utils
+    libc.src.__support.str_to_integer
 )
 
 add_entrypoint_object(
@@ -35,7 +35,7 @@ add_entrypoint_object(
   HDRS
     atoll.h
   DEPENDS
-    libc.src.__support.str_conv_utils
+    libc.src.__support.str_to_integer
 )
 
 add_entrypoint_object(
@@ -45,7 +45,7 @@ add_entrypoint_object(
   HDRS
     strtof.h
   DEPENDS
-    libc.src.__support.str_conv_utils
+    libc.src.__support.str_to_float
 )
 
 add_entrypoint_object(
@@ -55,7 +55,7 @@ add_entrypoint_object(
   HDRS
     strtod.h
   DEPENDS
-    libc.src.__support.str_conv_utils
+    libc.src.__support.str_to_float
 )
 
 add_entrypoint_object(
@@ -65,7 +65,7 @@ add_entrypoint_object(
   HDRS
     strtol.h
   DEPENDS
-    libc.src.__support.str_conv_utils
+    libc.src.__support.str_to_integer
 )
 
 add_entrypoint_object(
@@ -75,7 +75,7 @@ add_entrypoint_object(
   HDRS
     strtoll.h
   DEPENDS
-    libc.src.__support.str_conv_utils
+    libc.src.__support.str_to_integer
 )
 
 add_entrypoint_object(
@@ -85,7 +85,7 @@ add_entrypoint_object(
   HDRS
     strtoul.h
   DEPENDS
-    libc.src.__support.str_conv_utils
+    libc.src.__support.str_to_integer
 )
 
 add_entrypoint_object(
@@ -95,7 +95,7 @@ add_entrypoint_object(
   HDRS
     strtoull.h
   DEPENDS
-    libc.src.__support.str_conv_utils
+    libc.src.__support.str_to_integer
 )
 
 add_entrypoint_object(
index f0e57caf743d580163d16290c53b479c728b1f13..37cfab14443c04c2b123134a2d596b9811af04e6 100644 (file)
@@ -8,7 +8,7 @@
 
 #include "src/stdlib/atoi.h"
 #include "src/__support/common.h"
-#include "src/__support/str_conv_utils.h"
+#include "src/__support/str_to_integer.h"
 
 namespace __llvm_libc {
 
index 8f0ed885a9c97f43d70f1b54b799b6c0ae576cda..6a1da4c7007d95fb5ae7ace04bfef53f47425b0a 100644 (file)
@@ -8,7 +8,7 @@
 
 #include "src/stdlib/atol.h"
 #include "src/__support/common.h"
-#include "src/__support/str_conv_utils.h"
+#include "src/__support/str_to_integer.h"
 
 namespace __llvm_libc {
 
index c75e5213821903047c971c6d643a4965ea04786d..ffa8105d4cc6f0f7a1a0200ca49b103a20921359 100644 (file)
@@ -8,7 +8,7 @@
 
 #include "src/stdlib/atoll.h"
 #include "src/__support/common.h"
-#include "src/__support/str_conv_utils.h"
+#include "src/__support/str_to_integer.h"
 
 namespace __llvm_libc {
 
index 1c744c929e2836fdce9f4cf9963921e23873650e..33038b51c41ee4d6b09f21e7288ace5338b2dbf4 100644 (file)
@@ -8,7 +8,7 @@
 
 #include "src/stdlib/strtol.h"
 #include "src/__support/common.h"
-#include "src/__support/str_conv_utils.h"
+#include "src/__support/str_to_integer.h"
 
 namespace __llvm_libc {
 
index e2fc37f8bf822eb846d1463f2c346de52438aee3..e2f0aac5466884a6bc5605cfa23f57803815fb7b 100644 (file)
@@ -8,7 +8,7 @@
 
 #include "src/stdlib/strtoll.h"
 #include "src/__support/common.h"
-#include "src/__support/str_conv_utils.h"
+#include "src/__support/str_to_integer.h"
 
 namespace __llvm_libc {
 
index eab264f33347f66bf0232c6c22a25323f016a561..00696799872c4879f647e70683ad450b8078b90c 100644 (file)
@@ -8,7 +8,7 @@
 
 #include "src/stdlib/strtoul.h"
 #include "src/__support/common.h"
-#include "src/__support/str_conv_utils.h"
+#include "src/__support/str_to_integer.h"
 
 namespace __llvm_libc {
 
index bece2787ba7ea0eaa45ae0dcf4bd2e98825a4c6b..db6c83872b55e133e850d99712795ea29c96666e 100644 (file)
@@ -8,7 +8,7 @@
 
 #include "src/stdlib/strtoull.h"
 #include "src/__support/common.h"
-#include "src/__support/str_conv_utils.h"
+#include "src/__support/str_to_integer.h"
 
 namespace __llvm_libc {
 
index 85b819c23ab60d38fa29f147dcbfb12e755c6d74..d2367ef52f692539af7adfb515352da1f0206921 100644 (file)
@@ -27,7 +27,7 @@ add_libc_unittest(
   SRCS
     str_to_float_test.cpp
   DEPENDS
-    libc.src.__support.str_conv_utils
+    libc.src.__support.str_to_float
 )
 
 add_executable(