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

Alexey Samsonov via libc-commits libc-commits at lists.llvm.org
Fri Oct 31 11:48:26 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;
----------------
vonosmas wrote:

Good point, done

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


More information about the libc-commits mailing list