llvm.org GIT mirror llvm / 45ff225
[Support] Merge toLower / toUpper implementations Merge the ones from StringRef and StringExtras. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@319171 91177308-0d34-0410-b5e6-96231b3b80d8 Francis Visoiu Mistrih 2 years ago
3 changed file(s) with 35 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
7676 /// Checks whether character \p C is either a decimal digit or an uppercase or
7777 /// lowercase letter as classified by "C" locale.
7878 inline bool isAlnum(char C) { return isAlpha(C) || isDigit(C); }
79
80 /// Returns the corresponding lowercase character if \p x is uppercase.
81 inline char toLower(char x) {
82 if (x >= 'A' && x <= 'Z')
83 return x - 'A' + 'a';
84 return x;
85 }
86
87 /// Returns the corresponding uppercase character if \p x is lowercase.
88 inline char toUpper(char x) {
89 if (x >= 'a' && x <= 'z')
90 return x - 'a' + 'A';
91 return x;
92 }
7993
8094 inline std::string utohexstr(uint64_t X, bool LowerCase = false) {
8195 char Buffer[17];
253267 /// it if it is not printable or if it is an escape char.
254268 void PrintEscapedString(StringRef Name, raw_ostream &Out);
255269
270 /// printLowerCase - Print each character as lowercase if it is uppercase.
271 void printLowerCase(StringRef String, raw_ostream &Out);
272
256273 namespace detail {
257274
258275 template
1212
1313 #include "llvm/ADT/StringExtras.h"
1414 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/Support/raw_ostream.h"
1516 using namespace llvm;
1617
1718 /// StrInStrNoCase - Portable version of strcasestr. Locates the first
5556 S = getToken(S.second, Delimiters);
5657 }
5758 }
59
60 void llvm::printLowerCase(StringRef String, raw_ostream &Out) {
61 for (const char C : String)
62 Out << toLower(C);
63 }
1010 #include "llvm/ADT/APFloat.h"
1111 #include "llvm/ADT/APInt.h"
1212 #include "llvm/ADT/Hashing.h"
13 #include "llvm/ADT/StringExtras.h"
1314 #include "llvm/ADT/edit_distance.h"
1415 #include
1516
2021 const size_t StringRef::npos;
2122 #endif
2223
23 static char ascii_tolower(char x) {
24 if (x >= 'A' && x <= 'Z')
25 return x - 'A' + 'a';
26 return x;
27 }
28
29 static char ascii_toupper(char x) {
30 if (x >= 'a' && x <= 'z')
31 return x - 'a' + 'A';
32 return x;
33 }
34
35 static bool ascii_isdigit(char x) {
36 return x >= '0' && x <= '9';
37 }
38
3924 // strncasecmp() is not available on non-POSIX systems, so define an
4025 // alternative function here.
4126 static int ascii_strncasecmp(const char *LHS, const char *RHS, size_t Length) {
4227 for (size_t I = 0; I < Length; ++I) {
43 unsigned char LHC = ascii_tolower(LHS[I]);
44 unsigned char RHC = ascii_tolower(RHS[I]);
28 unsigned char LHC = toLower(LHS[I]);
29 unsigned char RHC = toLower(RHS[I]);
4530 if (LHC != RHC)
4631 return LHC < RHC ? -1 : 1;
4732 }
7055 }
7156
7257 size_t StringRef::find_lower(char C, size_t From) const {
73 char L = ascii_tolower(C);
74 return find_if([L](char D) { return ascii_tolower(D) == L; }, From);
58 char L = toLower(C);
59 return find_if([L](char D) { return toLower(D) == L; }, From);
7560 }
7661
7762 /// compare_numeric - Compare strings, handle embedded numbers.
7863 int StringRef::compare_numeric(StringRef RHS) const {
7964 for (size_t I = 0, E = std::min(Length, RHS.Length); I != E; ++I) {
8065 // Check for sequences of digits.
81 if (ascii_isdigit(Data[I]) && ascii_isdigit(RHS.Data[I])) {
66 if (isDigit(Data[I]) && isDigit(RHS.Data[I])) {
8267 // The longer sequence of numbers is considered larger.
8368 // This doesn't really handle prefixed zeros well.
8469 size_t J;
8570 for (J = I + 1; J != E + 1; ++J) {
86 bool ld = J < Length && ascii_isdigit(Data[J]);
87 bool rd = J < RHS.Length && ascii_isdigit(RHS.Data[J]);
71 bool ld = J < Length && isDigit(Data[J]);
72 bool rd = J < RHS.Length && isDigit(RHS.Data[J]);
8873 if (ld != rd)
8974 return rd ? -1 : 1;
9075 if (!rd)
122107 std::string StringRef::lower() const {
123108 std::string Result(size(), char());
124109 for (size_type i = 0, e = size(); i != e; ++i) {
125 Result[i] = ascii_tolower(Data[i]);
110 Result[i] = toLower(Data[i]);
126111 }
127112 return Result;
128113 }
130115 std::string StringRef::upper() const {
131116 std::string Result(size(), char());
132117 for (size_type i = 0, e = size(); i != e; ++i) {
133 Result[i] = ascii_toupper(Data[i]);
118 Result[i] = toUpper(Data[i]);
134119 }
135120 return Result;
136121 }
209194 size_t i = From;
210195 while (i != 0) {
211196 --i;
212 if (ascii_tolower(Data[i]) == ascii_tolower(C))
197 if (toLower(Data[i]) == toLower(C))
213198 return i;
214199 }
215200 return npos;
414399 return 8;
415400 }
416401
417 if (Str[0] == '0' && Str.size() > 1 && ascii_isdigit(Str[1])) {
402 if (Str[0] == '0' && Str.size() > 1 && isDigit(Str[1])) {
418403 Str = Str.substr(1);
419404 return 8;
420405 }