[libc] [llvm] [libc] Templatize strtointeger implementation. (PR #165884)

Michael Jones via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 31 10:48:52 PDT 2025


================
@@ -25,36 +25,72 @@
 #include "src/__support/macros/config.h"
 #include "src/__support/str_to_num_result.h"
 #include "src/__support/uint128.h"
+#include "src/__support/wctype_utils.h"
 
 namespace LIBC_NAMESPACE_DECL {
 namespace internal {
 
 // Returns the idx to the first character in src that is not a whitespace
-// character (as determined by isspace())
+// character (as determined by isspace() / iswspace())
+template <typename CharType>
 LIBC_INLINE size_t
-first_non_whitespace(const char *__restrict src,
+first_non_whitespace(const CharType *__restrict src,
                      size_t src_len = cpp::numeric_limits<size_t>::max()) {
   size_t src_cur = 0;
-  while (src_cur < src_len && internal::isspace(src[src_cur])) {
+  while (src_cur < src_len) {
+    if constexpr (cpp::is_same_v<CharType, char>) {
+      if (!internal::isspace(src[src_cur]))
+        break;
+    } else {
+      if (!internal::iswspace(src[src_cur]))
+        break;
+    }
     ++src_cur;
   }
   return src_cur;
 }
 
+// Returns 1 if 'src' starts with + or - sign, and returns 0 otherwise.
+// Writes the sign value to |is_positive|.
+template <typename CharType>
+LIBC_INLINE static size_t consume_sign(const CharType *__restrict src,
+                                       bool *is_positive) {
+  *is_positive = true;
----------------
michaelrj-google wrote:

I think this function would be cleaner if it returned the positive/negative sign without the pointer argument. One option would be changing the return type to be signed, returning 1 on a `+`, -1 on a `-`, otherwise 0.

https://github.com/llvm/llvm-project/pull/165884


More information about the llvm-commits mailing list