2004-09-01 22:55:40 +00:00
|
|
|
//===-- llvm/ADT/StringExtras.h - Useful string functions -------*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 19:46:57 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 19:46:57 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// This file contains some functions that are useful when dealing with strings.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-01 22:55:40 +00:00
|
|
|
#ifndef LLVM_ADT_STRINGEXTRAS_H
|
|
|
|
#define LLVM_ADT_STRINGEXTRAS_H
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2007-08-31 04:03:46 +00:00
|
|
|
#include "llvm/ADT/APFloat.h"
|
2004-07-20 02:18:25 +00:00
|
|
|
#include <cctype>
|
2004-07-20 16:14:06 +00:00
|
|
|
#include <cstdio>
|
2001-06-06 20:29:01 +00:00
|
|
|
#include <string>
|
2006-11-28 22:32:35 +00:00
|
|
|
#include <vector>
|
2001-10-29 13:24:31 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2002-04-07 08:36:19 +00:00
|
|
|
static inline std::string utohexstr(uint64_t X) {
|
|
|
|
char Buffer[40];
|
|
|
|
char *BufPtr = Buffer+39;
|
|
|
|
|
|
|
|
*BufPtr = 0; // Null terminate buffer...
|
|
|
|
if (X == 0) *--BufPtr = '0'; // Handle special case...
|
|
|
|
|
|
|
|
while (X) {
|
2005-01-28 07:22:20 +00:00
|
|
|
unsigned char Mod = static_cast<unsigned char>(X) & 15;
|
2002-04-07 08:36:19 +00:00
|
|
|
if (Mod < 10)
|
|
|
|
*--BufPtr = '0' + Mod;
|
|
|
|
else
|
|
|
|
*--BufPtr = 'A' + Mod-10;
|
|
|
|
X >>= 4;
|
|
|
|
}
|
|
|
|
return std::string(BufPtr);
|
|
|
|
}
|
|
|
|
|
2006-05-31 21:25:50 +00:00
|
|
|
static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
|
|
|
|
char Buffer[20];
|
|
|
|
char *BufPtr = Buffer+19;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
*BufPtr = 0; // Null terminate buffer...
|
|
|
|
if (X == 0) *--BufPtr = '0'; // Handle special case...
|
|
|
|
|
|
|
|
while (X) {
|
2004-06-04 19:10:30 +00:00
|
|
|
*--BufPtr = '0' + char(X % 10);
|
2001-06-06 20:29:01 +00:00
|
|
|
X /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isNeg) *--BufPtr = '-'; // Add negative sign...
|
2006-05-31 21:25:50 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
return std::string(BufPtr);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2006-05-31 21:25:50 +00:00
|
|
|
static inline std::string utostr(uint64_t X, bool isNeg = false) {
|
2006-06-01 07:03:53 +00:00
|
|
|
if (X == uint32_t(X))
|
|
|
|
return utostr_32(uint32_t(X), isNeg);
|
2006-05-31 21:25:50 +00:00
|
|
|
|
|
|
|
char Buffer[40];
|
|
|
|
char *BufPtr = Buffer+39;
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
*BufPtr = 0; // Null terminate buffer...
|
|
|
|
if (X == 0) *--BufPtr = '0'; // Handle special case...
|
2006-05-31 21:25:50 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
while (X) {
|
2004-06-04 19:10:30 +00:00
|
|
|
*--BufPtr = '0' + char(X % 10);
|
2001-06-06 20:29:01 +00:00
|
|
|
X /= 10;
|
|
|
|
}
|
2006-05-31 21:25:50 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
if (isNeg) *--BufPtr = '-'; // Add negative sign...
|
2002-01-20 22:54:45 +00:00
|
|
|
return std::string(BufPtr);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2006-05-31 21:25:50 +00:00
|
|
|
|
2006-05-24 19:21:13 +00:00
|
|
|
static inline std::string itostr(int64_t X) {
|
2005-04-21 20:19:05 +00:00
|
|
|
if (X < 0)
|
2004-08-18 22:56:12 +00:00
|
|
|
return utostr(static_cast<uint64_t>(-X), true);
|
|
|
|
else
|
|
|
|
return utostr(static_cast<uint64_t>(X));
|
|
|
|
}
|
2005-04-21 20:19:05 +00:00
|
|
|
|
2008-02-15 23:05:48 +00:00
|
|
|
static inline std::string itohexstr(int64_t X) {
|
|
|
|
return utohexstr(static_cast<uint64_t>(X));
|
|
|
|
}
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
static inline std::string ftostr(double V) {
|
2001-07-15 00:16:38 +00:00
|
|
|
char Buffer[200];
|
2004-06-04 19:10:30 +00:00
|
|
|
sprintf(Buffer, "%20.6e", V);
|
2005-01-04 01:56:28 +00:00
|
|
|
char *B = Buffer;
|
|
|
|
while (*B == ' ') ++B;
|
|
|
|
return B;
|
2001-07-15 00:16:38 +00:00
|
|
|
}
|
|
|
|
|
2007-08-31 17:03:33 +00:00
|
|
|
static inline std::string ftostr(const APFloat& V) {
|
2007-09-04 17:32:27 +00:00
|
|
|
if (&V.getSemantics() == &APFloat::IEEEdouble)
|
2007-08-31 04:03:46 +00:00
|
|
|
return ftostr(V.convertToDouble());
|
2007-09-04 17:32:27 +00:00
|
|
|
else if (&V.getSemantics() == &APFloat::IEEEsingle)
|
2007-08-31 04:03:46 +00:00
|
|
|
return ftostr((double)V.convertToFloat());
|
2008-01-18 18:54:31 +00:00
|
|
|
return "<unknown format in ftostr>"; // error
|
2007-08-31 04:03:46 +00:00
|
|
|
}
|
|
|
|
|
2005-04-21 20:19:05 +00:00
|
|
|
static inline std::string LowercaseString(const std::string &S) {
|
2004-07-20 00:52:16 +00:00
|
|
|
std::string result(S);
|
2004-06-24 23:38:52 +00:00
|
|
|
for (unsigned i = 0; i < S.length(); ++i)
|
2004-07-20 00:52:16 +00:00
|
|
|
if (isupper(result[i]))
|
2005-01-28 07:22:20 +00:00
|
|
|
result[i] = char(tolower(result[i]));
|
2004-06-24 23:38:52 +00:00
|
|
|
return result;
|
|
|
|
}
|
2003-12-29 05:06:38 +00:00
|
|
|
|
2007-10-18 19:31:38 +00:00
|
|
|
static inline std::string UppercaseString(const std::string &S) {
|
|
|
|
std::string result(S);
|
|
|
|
for (unsigned i = 0; i < S.length(); ++i)
|
|
|
|
if (islower(result[i]))
|
|
|
|
result[i] = char(toupper(result[i]));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-01-26 20:36:29 +00:00
|
|
|
/// StringsEqualNoCase - Return true if the two strings are equal, ignoring
|
|
|
|
/// case.
|
|
|
|
static inline bool StringsEqualNoCase(const std::string &LHS,
|
|
|
|
const std::string &RHS) {
|
|
|
|
if (LHS.size() != RHS.size()) return false;
|
2008-05-05 18:30:58 +00:00
|
|
|
for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i)
|
2006-01-26 20:36:29 +00:00
|
|
|
if (tolower(LHS[i]) != tolower(RHS[i])) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// StringsEqualNoCase - Return true if the two strings are equal, ignoring
|
|
|
|
/// case.
|
|
|
|
static inline bool StringsEqualNoCase(const std::string &LHS,
|
|
|
|
const char *RHS) {
|
2008-05-05 18:30:58 +00:00
|
|
|
for (unsigned i = 0, e = static_cast<unsigned>(LHS.size()); i != e; ++i) {
|
2006-01-26 20:36:29 +00:00
|
|
|
if (RHS[i] == 0) return false; // RHS too short.
|
|
|
|
if (tolower(LHS[i]) != tolower(RHS[i])) return false;
|
|
|
|
}
|
|
|
|
return RHS[LHS.size()] == 0; // Not too long?
|
|
|
|
}
|
2008-05-07 18:35:46 +00:00
|
|
|
|
|
|
|
/// CStrInCStrNoCase - Portable version of strcasestr. Locates the first
|
2008-05-07 20:04:18 +00:00
|
|
|
/// occurance of c-string 's2' in string 's1', ignoring case. Returns
|
|
|
|
/// NULL if 's2' cannot be found.
|
2008-05-07 18:35:46 +00:00
|
|
|
static inline const char* CStrInCStrNoCase(const char *s1, const char *s2) {
|
|
|
|
|
2008-05-07 18:49:31 +00:00
|
|
|
// Are either strings NULL or empty?
|
|
|
|
if (!s1 || !s2 || s1[0] == '\0' || s2[0] == '\0')
|
2008-05-07 18:35:46 +00:00
|
|
|
return 0;
|
|
|
|
|
2008-05-07 19:22:36 +00:00
|
|
|
if (s1 == s2)
|
|
|
|
return s1;
|
|
|
|
|
2008-05-07 18:35:46 +00:00
|
|
|
const char *I1=s1, *I2=s2;
|
|
|
|
|
|
|
|
while (*I1 != '\0' || *I2 != '\0' )
|
|
|
|
if (tolower(*I1) != tolower(*I2)) { // No match. Start over.
|
2008-05-07 20:04:18 +00:00
|
|
|
++s1; I1 = s1; I2 = s2;
|
2008-05-07 18:35:46 +00:00
|
|
|
}
|
|
|
|
else { // Character match. Advance to the next character.
|
|
|
|
++I1; ++I2;
|
|
|
|
}
|
|
|
|
|
2008-05-07 20:04:18 +00:00
|
|
|
// If we exhausted all of the characters in 's2', then 's2' appears in 's1'.
|
|
|
|
return *I2 == '\0' ? s1 : 0;
|
2008-05-07 18:35:46 +00:00
|
|
|
}
|
2006-01-26 20:36:29 +00:00
|
|
|
|
2003-12-29 05:06:38 +00:00
|
|
|
/// getToken - This function extracts one token from source, ignoring any
|
|
|
|
/// leading characters that appear in the Delimiters string, and ending the
|
|
|
|
/// token at any of the characters that appear in the Delimiters string. If
|
|
|
|
/// there are no tokens in the source string, an empty string is returned.
|
|
|
|
/// The Source source string is updated in place to remove the returned string
|
|
|
|
/// and any delimiter prefix from it.
|
|
|
|
std::string getToken(std::string &Source,
|
|
|
|
const char *Delimiters = " \t\n\v\f\r");
|
|
|
|
|
2006-11-28 22:32:35 +00:00
|
|
|
/// SplitString - Split up the specified string according to the specified
|
|
|
|
/// delimiters, appending the result fragments to the output list.
|
|
|
|
void SplitString(const std::string &Source,
|
|
|
|
std::vector<std::string> &OutFragments,
|
|
|
|
const char *Delimiters = " \t\n\v\f\r");
|
|
|
|
|
2006-07-14 22:54:06 +00:00
|
|
|
/// UnescapeString - Modify the argument string, turning two character sequences
|
|
|
|
/// like '\\' 'n' into '\n'. This handles: \e \a \b \f \n \r \t \v \' \\ and
|
|
|
|
/// \num (where num is a 1-3 byte octal value).
|
|
|
|
void UnescapeString(std::string &Str);
|
|
|
|
|
|
|
|
/// EscapeString - Modify the argument string, turning '\\' and anything that
|
|
|
|
/// doesn't satisfy std::isprint into an escape sequence.
|
|
|
|
void EscapeString(std::string &Str);
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
#endif
|