From: Daniel Dunbar Date: Sun, 6 Nov 2011 18:04:43 +0000 (+0000) Subject: ADT/StringRef: Add ::lower() and ::upper() methods. X-Git-Tag: android-x86-6.0-r1~447 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=bc4eb46f8fff6ba1a1768b2527a26c507a132ac7;p=android-x86%2Fexternal-llvm.git ADT/StringRef: Add ::lower() and ::upper() methods. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@143880 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/ADT/StringRef.h b/include/llvm/ADT/StringRef.h index 8396921744a..6c338068962 100644 --- a/include/llvm/ADT/StringRef.h +++ b/include/llvm/ADT/StringRef.h @@ -327,6 +327,16 @@ namespace llvm { bool getAsInteger(unsigned Radix, APInt &Result) const; /// @} + /// @name String Operations + /// @{ + + // lower - Convert the given ASCII string to lowercase. + std::string lower() const; + + /// upper - Convert the given ASCII string to uppercase. + std::string upper() const; + + /// @} /// @name Substring Operations /// @{ diff --git a/lib/Support/StringRef.cpp b/lib/Support/StringRef.cpp index 576b95f6a4e..c78b6d0afc8 100644 --- a/lib/Support/StringRef.cpp +++ b/lib/Support/StringRef.cpp @@ -25,6 +25,12 @@ static char ascii_tolower(char x) { return x; } +static char ascii_toupper(char x) { + if (x >= 'a' && x <= 'z') + return x - 'a' + 'A'; + return x; +} + static bool ascii_isdigit(char x) { return x >= '0' && x <= '9'; } @@ -132,6 +138,26 @@ unsigned StringRef::edit_distance(llvm::StringRef Other, } //===----------------------------------------------------------------------===// +// String Operations +//===----------------------------------------------------------------------===// + +std::string StringRef::lower() const { + std::string Result(size(), char()); + for (size_type i = 0, e = size(); i != e; ++i) { + Result[i] = ascii_tolower(Data[i]); + } + return Result; +} + +std::string StringRef::upper() const { + std::string Result(size(), char()); + for (size_type i = 0, e = size(); i != e; ++i) { + Result[i] = ascii_tolower(Data[i]); + } + return Result; +} + +//===----------------------------------------------------------------------===// // String Searching //===----------------------------------------------------------------------===//