aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2009-09-19 23:58:48 +0000
committerChris Lattner <sabre@nondot.org>2009-09-19 23:58:48 +0000
commit63c6b7dc67f0061340c0701f3d5b1de142f58cec (patch)
treec8155d0fa9d52866e8df1922ee0e0c96a35443b7
parent3670a01d0be3bab1aa5856fea02a854aee2b65d6 (diff)
downloadexternal_llvm-63c6b7dc67f0061340c0701f3d5b1de142f58cec.zip
external_llvm-63c6b7dc67f0061340c0701f3d5b1de142f58cec.tar.gz
external_llvm-63c6b7dc67f0061340c0701f3d5b1de142f58cec.tar.bz2
add some more overloads of StringRef::getAsInteger for
common and useful integer types. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82338 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/ADT/StringRef.h4
-rw-r--r--lib/Support/StringRef.cpp45
2 files changed, 48 insertions, 1 deletions
diff --git a/include/llvm/ADT/StringRef.h b/include/llvm/ADT/StringRef.h
index 2015a66..01f5d7b 100644
--- a/include/llvm/ADT/StringRef.h
+++ b/include/llvm/ADT/StringRef.h
@@ -235,8 +235,10 @@ namespace llvm {
/// this returns true to signify the error. The string is considered
/// erroneous if empty.
///
- //bool getAsInteger(unsigned Radix, long long &Result) const;
+ bool getAsInteger(unsigned Radix, long long &Result) const;
bool getAsInteger(unsigned Radix, unsigned long long &Result) const;
+ bool getAsInteger(unsigned Radix, int &Result) const;
+ bool getAsInteger(unsigned Radix, unsigned &Result) const;
// TODO: Provide overloads for int/unsigned that check for overflow.
diff --git a/lib/Support/StringRef.cpp b/lib/Support/StringRef.cpp
index 2e7d3c0..4c42701 100644
--- a/lib/Support/StringRef.cpp
+++ b/lib/Support/StringRef.cpp
@@ -12,6 +12,8 @@ using namespace llvm;
const size_t StringRef::npos;
+/// GetAsUnsignedInteger - Workhorse method that converts a integer character
+/// sequence of radix up to 36 to an unsigned long long value.
static bool GetAsUnsignedInteger(StringRef Str, unsigned Radix,
unsigned long long &Result) {
// Autosense radix if not specified.
@@ -74,3 +76,46 @@ bool StringRef::getAsInteger(unsigned Radix, unsigned long long &Result) const {
return GetAsUnsignedInteger(*this, Radix, Result);
}
+
+bool StringRef::getAsInteger(unsigned Radix, long long &Result) const {
+ unsigned long long ULLVal;
+
+ // Handle positive strings first.
+ if (empty() || front() != '-') {
+ if (GetAsUnsignedInteger(*this, Radix, ULLVal) ||
+ // Check for value so large it overflows a signed value.
+ (long long)ULLVal < 0)
+ return true;
+ Result = ULLVal;
+ return false;
+ }
+
+ // Get the positive part of the value.
+ if (GetAsUnsignedInteger(substr(1), Radix, ULLVal) ||
+ // Reject values so large they'd overflow as negative signed, but allow
+ // "-0". This negates the unsigned so that the negative isn't undefined
+ // on signed overflow.
+ (long long)-ULLVal > 0)
+ return true;
+
+ Result = -ULLVal;
+ return false;
+}
+
+bool StringRef::getAsInteger(unsigned Radix, int &Result) const {
+ long long Val;
+ if (getAsInteger(Radix, Val) ||
+ (int)Val != Val)
+ return true;
+ Result = Val;
+ return false;
+}
+
+bool StringRef::getAsInteger(unsigned Radix, unsigned &Result) const {
+ unsigned long long Val;
+ if (getAsInteger(Radix, Val) ||
+ (unsigned)Val != Val)
+ return true;
+ Result = Val;
+ return false;
+}