aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/StringRef.cpp
diff options
context:
space:
mode:
authorThomas Preud'homme <thomas.preudhomme@arm.com>2023-05-16 09:24:57 +0000
committerThomas Preud'homme <thomas.preudhomme@arm.com>2023-05-23 14:01:38 +0100
commitdd00421c60716973f5d919967fd4d6ef84025979 (patch)
tree8bb59a9a7b1e2a497a71ccc0b01bedc8043e2414 /llvm/lib/Support/StringRef.cpp
parent1ff828c6c837055b1f773ac1271f3ce249a1b0e1 (diff)
downloadllvm-dd00421c60716973f5d919967fd4d6ef84025979.zip
llvm-dd00421c60716973f5d919967fd4d6ef84025979.tar.gz
llvm-dd00421c60716973f5d919967fd4d6ef84025979.tar.bz2
Add StringRef::consumeInteger(APInt)
This will be required to allow arbitrary precision support to FileCheck's numeric variables and expressions. Note: as per getAsInteger(), this does not support negative value. If there is interest for that it can be added in a separate patch. Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D150878
Diffstat (limited to 'llvm/lib/Support/StringRef.cpp')
-rw-r--r--llvm/lib/Support/StringRef.cpp23
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);