1 //===-- StringRef.cpp - Lightweight String References ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/StringRef.h"
13 const size_t StringRef::npos;
15 static bool GetAsUnsignedInteger(StringRef Str, unsigned Radix,
16 unsigned long long &Result) {
17 // Autosense radix if not specified.
28 } else if (Str[1] == 'b') {
38 // Empty strings (after the radix autosense) are invalid.
39 if (Str.empty()) return true;
41 // Parse all the bytes of the string given this radix. Watch for overflow.
43 while (!Str.empty()) {
45 if (Str[0] >= '0' && Str[0] <= '9')
47 else if (Str[0] >= 'a' && Str[0] <= 'z')
48 CharVal = Str[0]-'a'+10;
49 else if (Str[0] >= 'A' && Str[0] <= 'Z')
50 CharVal = Str[0]-'A'+10;
54 // If the parsed value is larger than the integer radix, the string is
59 // Add in this character.
60 unsigned long long PrevResult = Result;
61 Result = Result*Radix+CharVal;
63 // Check for overflow.
64 if (Result < PrevResult)
73 bool StringRef::getAsInteger(unsigned Radix, unsigned long long &Result) const {
74 return GetAsUnsignedInteger(*this, Radix, Result);