StringExtras.h revision d5b58c239e44fe4fa2322900b26c691f5a44bd87
1//===-- llvm/ADT/StringExtras.h - Useful string functions -------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by the LLVM research group and is distributed under 6// the University of Illinois Open Source 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/Support/DataTypes.h" 18#include <cctype> 19#include <cstdio> 20#include <string> 21#include <vector> 22 23namespace llvm { 24 25static inline std::string utohexstr(uint64_t X) { 26 char Buffer[40]; 27 char *BufPtr = Buffer+39; 28 29 *BufPtr = 0; // Null terminate buffer... 30 if (X == 0) *--BufPtr = '0'; // Handle special case... 31 32 while (X) { 33 unsigned char Mod = static_cast<unsigned char>(X) & 15; 34 if (Mod < 10) 35 *--BufPtr = '0' + Mod; 36 else 37 *--BufPtr = 'A' + Mod-10; 38 X >>= 4; 39 } 40 return std::string(BufPtr); 41} 42 43static inline std::string utostr_32(uint32_t X, bool isNeg = false) { 44 char Buffer[20]; 45 char *BufPtr = Buffer+19; 46 47 *BufPtr = 0; // Null terminate buffer... 48 if (X == 0) *--BufPtr = '0'; // Handle special case... 49 50 while (X) { 51 *--BufPtr = '0' + char(X % 10); 52 X /= 10; 53 } 54 55 if (isNeg) *--BufPtr = '-'; // Add negative sign... 56 57 return std::string(BufPtr); 58} 59 60static inline std::string utostr(uint64_t X, bool isNeg = false) { 61 if (X == uint32_t(X)) 62 return utostr_32(uint32_t(X), isNeg); 63 64 char Buffer[40]; 65 char *BufPtr = Buffer+39; 66 67 *BufPtr = 0; // Null terminate buffer... 68 if (X == 0) *--BufPtr = '0'; // Handle special case... 69 70 while (X) { 71 *--BufPtr = '0' + char(X % 10); 72 X /= 10; 73 } 74 75 if (isNeg) *--BufPtr = '-'; // Add negative sign... 76 return std::string(BufPtr); 77} 78 79 80static inline std::string itostr(int64_t X) { 81 if (X < 0) 82 return utostr(static_cast<uint64_t>(-X), true); 83 else 84 return utostr(static_cast<uint64_t>(X)); 85} 86 87static inline std::string ftostr(double V) { 88 char Buffer[200]; 89 sprintf(Buffer, "%20.6e", V); 90 char *B = Buffer; 91 while (*B == ' ') ++B; 92 return B; 93} 94 95static inline std::string LowercaseString(const std::string &S) { 96 std::string result(S); 97 for (unsigned i = 0; i < S.length(); ++i) 98 if (isupper(result[i])) 99 result[i] = char(tolower(result[i])); 100 return result; 101} 102 103/// StringsEqualNoCase - Return true if the two strings are equal, ignoring 104/// case. 105static inline bool StringsEqualNoCase(const std::string &LHS, 106 const std::string &RHS) { 107 if (LHS.size() != RHS.size()) return false; 108 for (unsigned i = 0, e = LHS.size(); i != e; ++i) 109 if (tolower(LHS[i]) != tolower(RHS[i])) return false; 110 return true; 111} 112 113/// StringsEqualNoCase - Return true if the two strings are equal, ignoring 114/// case. 115static inline bool StringsEqualNoCase(const std::string &LHS, 116 const char *RHS) { 117 for (unsigned i = 0, e = LHS.size(); i != e; ++i) { 118 if (RHS[i] == 0) return false; // RHS too short. 119 if (tolower(LHS[i]) != tolower(RHS[i])) return false; 120 } 121 return RHS[LHS.size()] == 0; // Not too long? 122} 123 124/// getToken - This function extracts one token from source, ignoring any 125/// leading characters that appear in the Delimiters string, and ending the 126/// token at any of the characters that appear in the Delimiters string. If 127/// there are no tokens in the source string, an empty string is returned. 128/// The Source source string is updated in place to remove the returned string 129/// and any delimiter prefix from it. 130std::string getToken(std::string &Source, 131 const char *Delimiters = " \t\n\v\f\r"); 132 133/// SplitString - Split up the specified string according to the specified 134/// delimiters, appending the result fragments to the output list. 135void SplitString(const std::string &Source, 136 std::vector<std::string> &OutFragments, 137 const char *Delimiters = " \t\n\v\f\r"); 138 139/// UnescapeString - Modify the argument string, turning two character sequences 140/// like '\\' 'n' into '\n'. This handles: \e \a \b \f \n \r \t \v \' \\ and 141/// \num (where num is a 1-3 byte octal value). 142void UnescapeString(std::string &Str); 143 144/// EscapeString - Modify the argument string, turning '\\' and anything that 145/// doesn't satisfy std::isprint into an escape sequence. 146void EscapeString(std::string &Str); 147 148} // End llvm namespace 149 150#endif 151