Home | History | Annotate | Download | only in ADT
      1 //===-- llvm/ADT/StringExtras.h - Useful string functions -------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file contains some functions that are useful when dealing with strings.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_ADT_STRINGEXTRAS_H
     15 #define LLVM_ADT_STRINGEXTRAS_H
     16 
     17 #include "llvm/ADT/StringRef.h"
     18 #include "llvm/Support/DataTypes.h"
     19 
     20 namespace llvm {
     21 template<typename T> class SmallVectorImpl;
     22 
     23 /// hexdigit - Return the hexadecimal character for the
     24 /// given number \p X (which should be less than 16).
     25 static inline char hexdigit(unsigned X, bool LowerCase = false) {
     26   const char HexChar = LowerCase ? 'a' : 'A';
     27   return X < 10 ? '0' + X : HexChar + X - 10;
     28 }
     29 
     30 /// Interpret the given character \p C as a hexadecimal digit and return its
     31 /// value.
     32 ///
     33 /// If \p C is not a valid hex digit, -1U is returned.
     34 static inline unsigned hexDigitValue(char C) {
     35   if (C >= '0' && C <= '9') return C-'0';
     36   if (C >= 'a' && C <= 'f') return C-'a'+10U;
     37   if (C >= 'A' && C <= 'F') return C-'A'+10U;
     38   return -1U;
     39 }
     40 
     41 /// utohex_buffer - Emit the specified number into the buffer specified by
     42 /// BufferEnd, returning a pointer to the start of the string.  This can be used
     43 /// like this: (note that the buffer must be large enough to handle any number):
     44 ///    char Buffer[40];
     45 ///    printf("0x%s", utohex_buffer(X, Buffer+40));
     46 ///
     47 /// This should only be used with unsigned types.
     48 ///
     49 template<typename IntTy>
     50 static inline char *utohex_buffer(IntTy X, char *BufferEnd) {
     51   char *BufPtr = BufferEnd;
     52   *--BufPtr = 0;      // Null terminate buffer.
     53   if (X == 0) {
     54     *--BufPtr = '0';  // Handle special case.
     55     return BufPtr;
     56   }
     57 
     58   while (X) {
     59     unsigned char Mod = static_cast<unsigned char>(X) & 15;
     60     *--BufPtr = hexdigit(Mod);
     61     X >>= 4;
     62   }
     63   return BufPtr;
     64 }
     65 
     66 static inline std::string utohexstr(uint64_t X) {
     67   char Buffer[17];
     68   return utohex_buffer(X, Buffer+17);
     69 }
     70 
     71 static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
     72   char Buffer[11];
     73   char *BufPtr = Buffer+11;
     74 
     75   if (X == 0) *--BufPtr = '0';  // Handle special case...
     76 
     77   while (X) {
     78     *--BufPtr = '0' + char(X % 10);
     79     X /= 10;
     80   }
     81 
     82   if (isNeg) *--BufPtr = '-';   // Add negative sign...
     83 
     84   return std::string(BufPtr, Buffer+11);
     85 }
     86 
     87 static inline std::string utostr(uint64_t X, bool isNeg = false) {
     88   char Buffer[21];
     89   char *BufPtr = Buffer+21;
     90 
     91   if (X == 0) *--BufPtr = '0';  // Handle special case...
     92 
     93   while (X) {
     94     *--BufPtr = '0' + char(X % 10);
     95     X /= 10;
     96   }
     97 
     98   if (isNeg) *--BufPtr = '-';   // Add negative sign...
     99   return std::string(BufPtr, Buffer+21);
    100 }
    101 
    102 
    103 static inline std::string itostr(int64_t X) {
    104   if (X < 0)
    105     return utostr(static_cast<uint64_t>(-X), true);
    106   else
    107     return utostr(static_cast<uint64_t>(X));
    108 }
    109 
    110 /// StrInStrNoCase - Portable version of strcasestr.  Locates the first
    111 /// occurrence of string 's1' in string 's2', ignoring case.  Returns
    112 /// the offset of s2 in s1 or npos if s2 cannot be found.
    113 StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2);
    114 
    115 /// getToken - This function extracts one token from source, ignoring any
    116 /// leading characters that appear in the Delimiters string, and ending the
    117 /// token at any of the characters that appear in the Delimiters string.  If
    118 /// there are no tokens in the source string, an empty string is returned.
    119 /// The function returns a pair containing the extracted token and the
    120 /// remaining tail string.
    121 std::pair<StringRef, StringRef> getToken(StringRef Source,
    122                                          StringRef Delimiters = " \t\n\v\f\r");
    123 
    124 /// SplitString - Split up the specified string according to the specified
    125 /// delimiters, appending the result fragments to the output list.
    126 void SplitString(StringRef Source,
    127                  SmallVectorImpl<StringRef> &OutFragments,
    128                  StringRef Delimiters = " \t\n\v\f\r");
    129 
    130 /// HashString - Hash function for strings.
    131 ///
    132 /// This is the Bernstein hash function.
    133 //
    134 // FIXME: Investigate whether a modified bernstein hash function performs
    135 // better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
    136 //   X*33+c -> X*33^c
    137 static inline unsigned HashString(StringRef Str, unsigned Result = 0) {
    138   for (unsigned i = 0, e = Str.size(); i != e; ++i)
    139     Result = Result * 33 + (unsigned char)Str[i];
    140   return Result;
    141 }
    142 
    143 /// Returns the English suffix for an ordinal integer (-st, -nd, -rd, -th).
    144 static inline StringRef getOrdinalSuffix(unsigned Val) {
    145   // It is critically important that we do this perfectly for
    146   // user-written sequences with over 100 elements.
    147   switch (Val % 100) {
    148   case 11:
    149   case 12:
    150   case 13:
    151     return "th";
    152   default:
    153     switch (Val % 10) {
    154       case 1: return "st";
    155       case 2: return "nd";
    156       case 3: return "rd";
    157       default: return "th";
    158     }
    159   }
    160 }
    161 
    162 } // End llvm namespace
    163 
    164 #endif
    165