diff options
Diffstat (limited to 'llvm/lib/Support/StringRef.cpp')
-rw-r--r-- | llvm/lib/Support/StringRef.cpp | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp index 6207f626..3cce83a 100644 --- a/llvm/lib/Support/StringRef.cpp +++ b/llvm/lib/Support/StringRef.cpp @@ -509,7 +509,7 @@ bool llvm::getAsSignedInteger(StringRef Str, unsigned Radix, return !Str.empty(); } -bool StringRef::getAsInteger(unsigned Radix, APInt &Result) const { +bool StringRef::consumeInteger(unsigned Radix, APInt &Result) { StringRef Str = *this; // Autosense radix if not specified. @@ -529,6 +529,7 @@ bool StringRef::getAsInteger(unsigned Radix, APInt &Result) const { // If it was nothing but zeroes.... if (Str.empty()) { Result = APInt(64, 0); + *this = Str; return false; } @@ -561,12 +562,12 @@ bool StringRef::getAsInteger(unsigned Radix, APInt &Result) const { else if (Str[0] >= 'A' && Str[0] <= 'Z') CharVal = Str[0]-'A'+10; else - return true; + break; // If the parsed value is larger than the integer radix, the string is // invalid. if (CharVal >= Radix) - return true; + break; // Add in this character. if (IsPowerOf2Radix) { @@ -581,9 +582,25 @@ bool StringRef::getAsInteger(unsigned Radix, APInt &Result) const { Str = Str.substr(1); } + // We consider the operation a failure if no characters were consumed + // successfully. + if (size() == Str.size()) + return true; + + *this = Str; return false; } +bool StringRef::getAsInteger(unsigned Radix, APInt &Result) const { + StringRef Str = *this; + if (Str.consumeInteger(Radix, Result)) + return true; + + // For getAsInteger, we require the whole string to be consumed or else we + // consider it a failure. + return !Str.empty(); +} + bool StringRef::getAsDouble(double &Result, bool AllowInexact) const { APFloat F(0.0); auto StatusOrErr = F.convertFromString(*this, APFloat::rmNearestTiesToEven); |