Skip to content

[ADT] Make getAutoSenseRadix in StringRef global #152503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions llvm/include/llvm/ADT/StringRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Support/StringRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
23 changes: 23 additions & 0 deletions llvm/unittests/ADT/StringRefTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading