[llvm-commits] [llvm] r82338 - in /llvm/trunk: include/llvm/ADT/StringRef.h lib/Support/StringRef.cpp
Chris Lattner
sabre at nondot.org
Sat Sep 19 16:58:48 PDT 2009
Author: lattner
Date: Sat Sep 19 18:58:48 2009
New Revision: 82338
URL: http://llvm.org/viewvc/llvm-project?rev=82338&view=rev
Log:
add some more overloads of StringRef::getAsInteger for
common and useful integer types.
Modified:
llvm/trunk/include/llvm/ADT/StringRef.h
llvm/trunk/lib/Support/StringRef.cpp
Modified: llvm/trunk/include/llvm/ADT/StringRef.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringRef.h?rev=82338&r1=82337&r2=82338&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringRef.h (original)
+++ llvm/trunk/include/llvm/ADT/StringRef.h Sat Sep 19 18:58:48 2009
@@ -235,8 +235,10 @@
/// 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.
Modified: llvm/trunk/lib/Support/StringRef.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/StringRef.cpp?rev=82338&r1=82337&r2=82338&view=diff
==============================================================================
--- llvm/trunk/lib/Support/StringRef.cpp (original)
+++ llvm/trunk/lib/Support/StringRef.cpp Sat Sep 19 18:58:48 2009
@@ -12,6 +12,8 @@
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 @@
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;
+}
More information about the llvm-commits
mailing list