Skip to content

Commit 2ff44d7

Browse files
authored
[ADT] Make getAutoSenseRadix in StringRef global (#152503)
Needed in #152308
1 parent 06f06de commit 2ff44d7

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

llvm/include/llvm/ADT/StringRef.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ namespace llvm {
3838
LLVM_ABI bool getAsSignedInteger(StringRef Str, unsigned Radix,
3939
long long &Result);
4040

41+
LLVM_ABI unsigned getAutoSenseRadix(StringRef &Str);
42+
4143
LLVM_ABI bool consumeUnsignedInteger(StringRef &Str, unsigned Radix,
4244
unsigned long long &Result);
4345
LLVM_ABI bool consumeSignedInteger(StringRef &Str, unsigned Radix,

llvm/lib/Support/StringRef.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ size_t StringRef::count(StringRef Str) const {
385385
return Count;
386386
}
387387

388-
static unsigned GetAutoSenseRadix(StringRef &Str) {
388+
unsigned llvm::getAutoSenseRadix(StringRef &Str) {
389389
if (Str.empty())
390390
return 10;
391391

@@ -410,7 +410,7 @@ bool llvm::consumeUnsignedInteger(StringRef &Str, unsigned Radix,
410410
unsigned long long &Result) {
411411
// Autosense radix if not specified.
412412
if (Radix == 0)
413-
Radix = GetAutoSenseRadix(Str);
413+
Radix = getAutoSenseRadix(Str);
414414

415415
// Empty strings (after the radix autosense) are invalid.
416416
if (Str.empty()) return true;
@@ -509,7 +509,7 @@ bool StringRef::consumeInteger(unsigned Radix, APInt &Result) {
509509

510510
// Autosense radix if not specified.
511511
if (Radix == 0)
512-
Radix = GetAutoSenseRadix(Str);
512+
Radix = getAutoSenseRadix(Str);
513513

514514
assert(Radix > 1 && Radix <= 36);
515515

llvm/unittests/ADT/StringRefTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,19 @@ TEST(StringRefTest, Hashing) {
619619
hash_value(StringRef("hello world").slice(1, -1)));
620620
}
621621

622+
TEST(StringRefTest, getAutoSenseRadix) {
623+
struct RadixPair {
624+
const char *Str;
625+
unsigned Expected;
626+
} RadixNumbers[] = {{"123", 10}, {"1", 10}, {"0b1", 2}, {"01", 8}, {"0o1", 8},
627+
{"0x1", 16}, {"0", 10}, {"00", 8}, {"", 10}};
628+
for (size_t i = 0; i < std::size(RadixNumbers); ++i) {
629+
StringRef number = RadixNumbers[i].Str;
630+
unsigned radix = getAutoSenseRadix(number);
631+
EXPECT_EQ(radix, RadixNumbers[i].Expected);
632+
}
633+
}
634+
622635
struct UnsignedPair {
623636
const char *Str;
624637
uint64_t Expected;

0 commit comments

Comments
 (0)