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>