[ADT] Make getAutoSenseRadix
in StringRef
global (#152503)
Needed in #152308
This commit is contained in:
parent
06f06deb77
commit
2ff44d7d65
@ -38,6 +38,8 @@ namespace llvm {
|
|||||||
LLVM_ABI bool getAsSignedInteger(StringRef Str, unsigned Radix,
|
LLVM_ABI bool getAsSignedInteger(StringRef Str, unsigned Radix,
|
||||||
long long &Result);
|
long long &Result);
|
||||||
|
|
||||||
|
LLVM_ABI unsigned getAutoSenseRadix(StringRef &Str);
|
||||||
|
|
||||||
LLVM_ABI bool consumeUnsignedInteger(StringRef &Str, unsigned Radix,
|
LLVM_ABI bool consumeUnsignedInteger(StringRef &Str, unsigned Radix,
|
||||||
unsigned long long &Result);
|
unsigned long long &Result);
|
||||||
LLVM_ABI bool consumeSignedInteger(StringRef &Str, unsigned Radix,
|
LLVM_ABI bool consumeSignedInteger(StringRef &Str, unsigned Radix,
|
||||||
|
@ -385,7 +385,7 @@ size_t StringRef::count(StringRef Str) const {
|
|||||||
return Count;
|
return Count;
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned GetAutoSenseRadix(StringRef &Str) {
|
unsigned llvm::getAutoSenseRadix(StringRef &Str) {
|
||||||
if (Str.empty())
|
if (Str.empty())
|
||||||
return 10;
|
return 10;
|
||||||
|
|
||||||
@ -410,7 +410,7 @@ bool llvm::consumeUnsignedInteger(StringRef &Str, unsigned Radix,
|
|||||||
unsigned long long &Result) {
|
unsigned long long &Result) {
|
||||||
// Autosense radix if not specified.
|
// Autosense radix if not specified.
|
||||||
if (Radix == 0)
|
if (Radix == 0)
|
||||||
Radix = GetAutoSenseRadix(Str);
|
Radix = getAutoSenseRadix(Str);
|
||||||
|
|
||||||
// Empty strings (after the radix autosense) are invalid.
|
// Empty strings (after the radix autosense) are invalid.
|
||||||
if (Str.empty()) return true;
|
if (Str.empty()) return true;
|
||||||
@ -509,7 +509,7 @@ bool StringRef::consumeInteger(unsigned Radix, APInt &Result) {
|
|||||||
|
|
||||||
// Autosense radix if not specified.
|
// Autosense radix if not specified.
|
||||||
if (Radix == 0)
|
if (Radix == 0)
|
||||||
Radix = GetAutoSenseRadix(Str);
|
Radix = getAutoSenseRadix(Str);
|
||||||
|
|
||||||
assert(Radix > 1 && Radix <= 36);
|
assert(Radix > 1 && Radix <= 36);
|
||||||
|
|
||||||
|
@ -619,6 +619,19 @@ TEST(StringRefTest, Hashing) {
|
|||||||
hash_value(StringRef("hello world").slice(1, -1)));
|
hash_value(StringRef("hello world").slice(1, -1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(StringRefTest, getAutoSenseRadix) {
|
||||||
|
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}};
|
||||||
|
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 {
|
struct UnsignedPair {
|
||||||
const char *Str;
|
const char *Str;
|
||||||
uint64_t Expected;
|
uint64_t Expected;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user