LLVM API Documentation

StringExtras.h

Go to the documentation of this file.
00001 //===-- llvm/ADT/StringExtras.h - Useful string functions -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains some functions that are useful when dealing with strings.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_ADT_STRINGEXTRAS_H
00015 #define LLVM_ADT_STRINGEXTRAS_H
00016 
00017 #include "llvm/Support/DataTypes.h"
00018 #include <cctype>
00019 #include <cstdio>
00020 #include <string>
00021 
00022 namespace llvm {
00023 
00024 static inline std::string utohexstr(uint64_t X) {
00025   char Buffer[40];
00026   char *BufPtr = Buffer+39;
00027 
00028   *BufPtr = 0;                  // Null terminate buffer...
00029   if (X == 0) *--BufPtr = '0';  // Handle special case...
00030 
00031   while (X) {
00032     unsigned char Mod = static_cast<unsigned char>(X) & 15;
00033     if (Mod < 10)
00034       *--BufPtr = '0' + Mod;
00035     else
00036       *--BufPtr = 'A' + Mod-10;
00037     X >>= 4;
00038   }
00039   return std::string(BufPtr);
00040 }
00041 
00042 static inline std::string utostr(unsigned long long X, bool isNeg = false) {
00043   char Buffer[40];
00044   char *BufPtr = Buffer+39;
00045 
00046   *BufPtr = 0;                  // Null terminate buffer...
00047   if (X == 0) *--BufPtr = '0';  // Handle special case...
00048 
00049   while (X) {
00050     *--BufPtr = '0' + char(X % 10);
00051     X /= 10;
00052   }
00053 
00054   if (isNeg) *--BufPtr = '-';   // Add negative sign...
00055   return std::string(BufPtr);
00056 }
00057 
00058 static inline std::string utostr(unsigned long X, bool isNeg = false) {
00059   return utostr(static_cast<unsigned long long>(X), isNeg);
00060 }
00061 
00062 static inline std::string utostr(unsigned X, bool isNeg = false) {
00063   char Buffer[20];
00064   char *BufPtr = Buffer+19;
00065 
00066   *BufPtr = 0;                  // Null terminate buffer...
00067   if (X == 0) *--BufPtr = '0';  // Handle special case...
00068 
00069   while (X) {
00070     *--BufPtr = '0' + char(X % 10);
00071     X /= 10;
00072   }
00073 
00074   if (isNeg) *--BufPtr = '-';   // Add negative sign...
00075 
00076   return std::string(BufPtr);
00077 }
00078 
00079 static inline std::string itostr(long long X) {
00080   if (X < 0)
00081     return utostr(static_cast<uint64_t>(-X), true);
00082   else
00083     return utostr(static_cast<uint64_t>(X));
00084 }
00085 
00086 static inline std::string itostr(long X) {
00087   if (X < 0)
00088     return utostr(static_cast<uint64_t>(-X), true);
00089   else
00090     return utostr(static_cast<uint64_t>(X));
00091 }
00092 
00093 static inline std::string itostr(int X) {
00094   if (X < 0)
00095     return utostr(static_cast<unsigned>(-X), true);
00096   else
00097     return utostr(static_cast<unsigned>(X));
00098 }
00099 
00100 static inline std::string ftostr(double V) {
00101   char Buffer[200];
00102   sprintf(Buffer, "%20.6e", V);
00103   char *B = Buffer;
00104   while (*B == ' ') ++B;
00105   return B;
00106 }
00107 
00108 static inline std::string LowercaseString(const std::string &S) {
00109   std::string result(S);
00110   for (unsigned i = 0; i < S.length(); ++i)
00111     if (isupper(result[i]))
00112       result[i] = char(tolower(result[i]));
00113   return result;
00114 }
00115 
00116 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
00117 /// case.
00118 static inline bool StringsEqualNoCase(const std::string &LHS, 
00119                                       const std::string &RHS) {
00120   if (LHS.size() != RHS.size()) return false;
00121   for (unsigned i = 0, e = LHS.size(); i != e; ++i)
00122     if (tolower(LHS[i]) != tolower(RHS[i])) return false;
00123   return true;
00124 }
00125 
00126 /// StringsEqualNoCase - Return true if the two strings are equal, ignoring
00127 /// case.
00128 static inline bool StringsEqualNoCase(const std::string &LHS, 
00129                                       const char *RHS) {
00130   for (unsigned i = 0, e = LHS.size(); i != e; ++i) {
00131     if (RHS[i] == 0) return false;  // RHS too short.
00132     if (tolower(LHS[i]) != tolower(RHS[i])) return false;
00133   }
00134   return RHS[LHS.size()] == 0;  // Not too long?
00135 }
00136 
00137 /// getToken - This function extracts one token from source, ignoring any
00138 /// leading characters that appear in the Delimiters string, and ending the
00139 /// token at any of the characters that appear in the Delimiters string.  If
00140 /// there are no tokens in the source string, an empty string is returned.
00141 /// The Source source string is updated in place to remove the returned string
00142 /// and any delimiter prefix from it.
00143 std::string getToken(std::string &Source,
00144                      const char *Delimiters = " \t\n\v\f\r");
00145 
00146 } // End llvm namespace
00147 
00148 #endif