diff options
Diffstat (limited to 'include/llvm/ADT/StringExtras.h')
-rw-r--r-- | include/llvm/ADT/StringExtras.h | 62 |
1 files changed, 49 insertions, 13 deletions
diff --git a/include/llvm/ADT/StringExtras.h b/include/llvm/ADT/StringExtras.h index cc32bf43f29c8..60652f8c55c5a 100644 --- a/include/llvm/ADT/StringExtras.h +++ b/include/llvm/ADT/StringExtras.h @@ -17,6 +17,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Twine.h" #include <cassert> #include <cstddef> #include <cstdint> @@ -33,33 +34,65 @@ class raw_ostream; /// hexdigit - Return the hexadecimal character for the /// given number \p X (which should be less than 16). -static inline char hexdigit(unsigned X, bool LowerCase = false) { +inline char hexdigit(unsigned X, bool LowerCase = false) { const char HexChar = LowerCase ? 'a' : 'A'; return X < 10 ? '0' + X : HexChar + X - 10; } /// Construct a string ref from a boolean. -static inline StringRef toStringRef(bool B) { - return StringRef(B ? "true" : "false"); -} +inline StringRef toStringRef(bool B) { return StringRef(B ? "true" : "false"); } /// Construct a string ref from an array ref of unsigned chars. -static inline StringRef toStringRef(ArrayRef<uint8_t> Input) { +inline StringRef toStringRef(ArrayRef<uint8_t> Input) { return StringRef(reinterpret_cast<const char *>(Input.begin()), Input.size()); } +/// Construct a string ref from an array ref of unsigned chars. +inline ArrayRef<uint8_t> arrayRefFromStringRef(StringRef Input) { + return {Input.bytes_begin(), Input.bytes_end()}; +} + /// Interpret the given character \p C as a hexadecimal digit and return its /// value. /// /// If \p C is not a valid hex digit, -1U is returned. -static inline unsigned hexDigitValue(char C) { +inline unsigned hexDigitValue(char C) { if (C >= '0' && C <= '9') return C-'0'; if (C >= 'a' && C <= 'f') return C-'a'+10U; if (C >= 'A' && C <= 'F') return C-'A'+10U; return -1U; } -static inline std::string utohexstr(uint64_t X, bool LowerCase = false) { +/// Checks if character \p C is one of the 10 decimal digits. +inline bool isDigit(char C) { return C >= '0' && C <= '9'; } + +/// Checks if character \p C is a hexadecimal numeric character. +inline bool isHexDigit(char C) { return hexDigitValue(C) != -1U; } + +/// Checks if character \p C is a valid letter as classified by "C" locale. +inline bool isAlpha(char C) { + return ('a' <= C && C <= 'z') || ('A' <= C && C <= 'Z'); +} + +/// Checks whether character \p C is either a decimal digit or an uppercase or +/// lowercase letter as classified by "C" locale. +inline bool isAlnum(char C) { return isAlpha(C) || isDigit(C); } + +/// Returns the corresponding lowercase character if \p x is uppercase. +inline char toLower(char x) { + if (x >= 'A' && x <= 'Z') + return x - 'A' + 'a'; + return x; +} + +/// Returns the corresponding uppercase character if \p x is lowercase. +inline char toUpper(char x) { + if (x >= 'a' && x <= 'z') + return x - 'a' + 'A'; + return x; +} + +inline std::string utohexstr(uint64_t X, bool LowerCase = false) { char Buffer[17]; char *BufPtr = std::end(Buffer); @@ -94,7 +127,7 @@ inline std::string toHex(ArrayRef<uint8_t> Input) { return toHex(toStringRef(Input)); } -static inline uint8_t hexFromNibbles(char MSB, char LSB) { +inline uint8_t hexFromNibbles(char MSB, char LSB) { unsigned U1 = hexDigitValue(MSB); unsigned U2 = hexDigitValue(LSB); assert(U1 != -1U && U2 != -1U); @@ -104,7 +137,7 @@ static inline uint8_t hexFromNibbles(char MSB, char LSB) { /// Convert hexadecimal string \p Input to its binary representation. /// The return string is half the size of \p Input. -static inline std::string fromHex(StringRef Input) { +inline std::string fromHex(StringRef Input) { if (Input.empty()) return std::string(); @@ -157,7 +190,7 @@ inline bool to_float(const Twine &T, long double &Num) { return detail::to_float(T, Num, strtold); } -static inline std::string utostr(uint64_t X, bool isNeg = false) { +inline std::string utostr(uint64_t X, bool isNeg = false) { char Buffer[21]; char *BufPtr = std::end(Buffer); @@ -172,7 +205,7 @@ static inline std::string utostr(uint64_t X, bool isNeg = false) { return std::string(BufPtr, std::end(Buffer)); } -static inline std::string itostr(int64_t X) { +inline std::string itostr(int64_t X) { if (X < 0) return utostr(static_cast<uint64_t>(-X), true); else @@ -206,14 +239,14 @@ void SplitString(StringRef Source, // FIXME: Investigate whether a modified bernstein hash function performs // better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx // X*33+c -> X*33^c -static inline unsigned HashString(StringRef Str, unsigned Result = 0) { +inline unsigned HashString(StringRef Str, unsigned Result = 0) { for (StringRef::size_type i = 0, e = Str.size(); i != e; ++i) Result = Result * 33 + (unsigned char)Str[i]; return Result; } /// Returns the English suffix for an ordinal integer (-st, -nd, -rd, -th). -static inline StringRef getOrdinalSuffix(unsigned Val) { +inline StringRef getOrdinalSuffix(unsigned Val) { // It is critically important that we do this perfectly for // user-written sequences with over 100 elements. switch (Val % 100) { @@ -235,6 +268,9 @@ static inline StringRef getOrdinalSuffix(unsigned Val) { /// it if it is not printable or if it is an escape char. void PrintEscapedString(StringRef Name, raw_ostream &Out); +/// printLowerCase - Print each character as lowercase if it is uppercase. +void printLowerCase(StringRef String, raw_ostream &Out); + namespace detail { template <typename IteratorT> |