Hi, Chris --<br><br>I think this new code is missing a test or two. ;-)<br><br>Misha<br><br><div class="gmail_quote">On Sat, Sep 19, 2009 at 3:47 PM, Chris Lattner <span dir="ltr"><<a href="mailto:sabre@nondot.org">sabre@nondot.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Author: lattner<br>
Date: Sat Sep 19 14:47:14 2009<br>
New Revision: 82322<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=82322&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=82322&view=rev</a><br>
Log:<br>
provide a "strtoull" operation that works on StringRef's.<br>
<br>
Modified:<br>
    llvm/trunk/include/llvm/ADT/StringRef.h<br>
    llvm/trunk/lib/Support/StringRef.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/ADT/StringRef.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringRef.h?rev=82322&r1=82321&r2=82322&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringRef.h?rev=82322&r1=82321&r2=82322&view=diff</a><br>

<br>
==============================================================================<br>
--- llvm/trunk/include/llvm/ADT/StringRef.h (original)<br>
+++ llvm/trunk/include/llvm/ADT/StringRef.h Sat Sep 19 14:47:14 2009<br>
@@ -224,6 +224,23 @@<br>
     }<br>
<br>
     /// @}<br>
+    /// @name Helpful Algorithms<br>
+    /// @{<br>
+<br>
+    /// getAsInteger - Parse the current string as an integer of the specified<br>
+    /// radix.  If Radix is specified as zero, this does radix autosensing using<br>
+    /// extended C rules: 0 is octal, 0x is hex, 0b is binary.<br>
+    ///<br>
+    /// If the string is invalid or if only a subset of the string is valid,<br>
+    /// this returns true to signify the error.  The string is considered<br>
+    /// erroneous if empty.<br>
+    ///<br>
+    //bool getAsInteger(unsigned Radix, long long &Result) const;<br>
+    bool getAsInteger(unsigned Radix, unsigned long long &Result) const;<br>
+<br>
+    // TODO: Provide overloads for int/unsigned that check for overflow.<br>
+<br>
+    /// @}<br>
     /// @name Substring Operations<br>
     /// @{<br>
<br>
<br>
Modified: llvm/trunk/lib/Support/StringRef.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/StringRef.cpp?rev=82322&r1=82321&r2=82322&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/StringRef.cpp?rev=82322&r1=82321&r2=82322&view=diff</a><br>

<br>
==============================================================================<br>
--- llvm/trunk/lib/Support/StringRef.cpp (original)<br>
+++ llvm/trunk/lib/Support/StringRef.cpp Sat Sep 19 14:47:14 2009<br>
@@ -11,3 +11,66 @@<br>
 using namespace llvm;<br>
<br>
 const size_t StringRef::npos;<br>
+<br>
+static bool GetAsUnsignedInteger(StringRef Str, unsigned Radix,<br>
+                                 unsigned long long &Result) {<br>
+  // Autosense radix if not specified.<br>
+  if (Radix == 0) {<br>
+    if (Str[0] != '0') {<br>
+      Radix = 10;<br>
+    } else {<br>
+      if (Str.size() < 2) {<br>
+        Radix = 8;<br>
+      } else {<br>
+        if (Str[1] == 'x') {<br>
+          Str = Str.substr(2);<br>
+          Radix = 16;<br>
+        } else if (Str[1] == 'b') {<br>
+          Str = Str.substr(2);<br>
+          Radix = 2;<br>
+        } else {<br>
+          Radix = 8;<br>
+        }<br>
+      }<br>
+    }<br>
+  }<br>
+<br>
+  // Empty strings (after the radix autosense) are invalid.<br>
+  if (Str.empty()) return true;<br>
+<br>
+  // Parse all the bytes of the string given this radix.  Watch for overflow.<br>
+  Result = 0;<br>
+  while (!Str.empty()) {<br>
+    unsigned CharVal;<br>
+    if (Str[0] >= '0' && Str[0] <= '9')<br>
+      CharVal = Str[0]-'0';<br>
+    else if (Str[0] >= 'a' && Str[0] <= 'z')<br>
+      CharVal = Str[0]-'a'+10;<br>
+    else if (Str[0] >= 'A' && Str[0] <= 'Z')<br>
+      CharVal = Str[0]-'A'+10;<br>
+    else<br>
+      return true;<br>
+<br>
+    // If the parsed value is larger than the integer radix, the string is<br>
+    // invalid.<br>
+    if (CharVal >= Radix)<br>
+      return true;<br>
+<br>
+    // Add in this character.<br>
+    unsigned long long PrevResult = Result;<br>
+    Result = Result*Radix+CharVal;<br>
+<br>
+    // Check for overflow.<br>
+    if (Result < PrevResult)<br>
+      return true;<br>
+<br>
+    Str = Str.substr(1);<br>
+  }<br>
+<br>
+  return false;<br>
+}<br>
+<br>
+bool StringRef::getAsInteger(unsigned Radix, unsigned long long &Result) const {<br>
+  return GetAsUnsignedInteger(*this, Radix, Result);<br>
+}<br>
+<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br>