[llvm] [ADT][NFC] Make `getAutoSenseRadix` in `StringRef` global (PR #152503)
Ilia Kuklin via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 7 08:03:11 PDT 2025
https://github.com/kuilpd updated https://github.com/llvm/llvm-project/pull/152503
>From 59523f34f73dc1521789edc97ce36426bcfb195d Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Wed, 6 Aug 2025 21:51:56 +0500
Subject: [PATCH 1/2] [LLDB][NFC] Make `getAutoSenseRadix` in `StringRef`
global
---
llvm/include/llvm/ADT/StringRef.h | 2 ++
llvm/lib/Support/StringRef.cpp | 6 +++---
2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index 0ced1c0379a3b..16aca4d45892d 100644
--- a/llvm/include/llvm/ADT/StringRef.h
+++ b/llvm/include/llvm/ADT/StringRef.h
@@ -38,6 +38,8 @@ namespace llvm {
LLVM_ABI bool getAsSignedInteger(StringRef Str, unsigned Radix,
long long &Result);
+ LLVM_ABI unsigned getAutoSenseRadix(StringRef &Str);
+
LLVM_ABI bool consumeUnsignedInteger(StringRef &Str, unsigned Radix,
unsigned long long &Result);
LLVM_ABI bool consumeSignedInteger(StringRef &Str, unsigned Radix,
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp
index dc758785e40d5..b6a2f8aeadccf 100644
--- a/llvm/lib/Support/StringRef.cpp
+++ b/llvm/lib/Support/StringRef.cpp
@@ -385,7 +385,7 @@ size_t StringRef::count(StringRef Str) const {
return Count;
}
-static unsigned GetAutoSenseRadix(StringRef &Str) {
+unsigned llvm::getAutoSenseRadix(StringRef &Str) {
if (Str.empty())
return 10;
@@ -410,7 +410,7 @@ bool llvm::consumeUnsignedInteger(StringRef &Str, unsigned Radix,
unsigned long long &Result) {
// Autosense radix if not specified.
if (Radix == 0)
- Radix = GetAutoSenseRadix(Str);
+ Radix = getAutoSenseRadix(Str);
// Empty strings (after the radix autosense) are invalid.
if (Str.empty()) return true;
@@ -509,7 +509,7 @@ bool StringRef::consumeInteger(unsigned Radix, APInt &Result) {
// Autosense radix if not specified.
if (Radix == 0)
- Radix = GetAutoSenseRadix(Str);
+ Radix = getAutoSenseRadix(Str);
assert(Radix > 1 && Radix <= 36);
>From 469ec9dde24c679ba7ffc2e3b86da93abf765450 Mon Sep 17 00:00:00 2001
From: Ilia Kuklin <ikuklin at accesssoftek.com>
Date: Thu, 7 Aug 2025 20:01:59 +0500
Subject: [PATCH 2/2] Add a unit test
---
llvm/unittests/ADT/StringRefTest.cpp | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/llvm/unittests/ADT/StringRefTest.cpp b/llvm/unittests/ADT/StringRefTest.cpp
index ec9cdc197597d..f85bb0a4e52a7 100644
--- a/llvm/unittests/ADT/StringRefTest.cpp
+++ b/llvm/unittests/ADT/StringRefTest.cpp
@@ -619,6 +619,29 @@ TEST(StringRefTest, Hashing) {
hash_value(StringRef("hello world").slice(1, -1)));
}
+struct RadixPair {
+ const char *Str;
+ unsigned Expected;
+} RadixNumbers[] =
+ { {"123", 10}
+ , {"1", 10}
+ , {"0b1", 2}
+ , {"01", 8}
+ , {"0o1", 8}
+ , {"0x1", 16}
+ , {"0", 10}
+ , {"00", 8}
+ , {"", 10}
+ };
+
+TEST(StringRefTest, getAutoSenseRadix) {
+ for (size_t i = 0; i < std::size(RadixNumbers); ++i) {
+ StringRef number = RadixNumbers[i].Str;
+ unsigned radix = getAutoSenseRadix(number);
+ EXPECT_EQ(radix, RadixNumbers[i].Expected);
+ }
+}
+
struct UnsignedPair {
const char *Str;
uint64_t Expected;
More information about the llvm-commits
mailing list