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
|
|
|
|
2009-10-26 01:35:46 +00:00
|
|
|
#include "llvm/System/DataTypes.h"
|
2007-08-31 04:03:46 +00:00
|
|
|
#include "llvm/ADT/APFloat.h"
|
2009-10-17 18:21:06 +00:00
|
|
|
#include "llvm/ADT/StringRef.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>
|
2010-01-11 18:44:35 +00:00
|
|
|
#include <vector>
|
2001-10-29 13:24:31 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
2010-01-11 18:03:24 +00:00
|
|
|
template<typename T> class SmallVectorImpl;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2008-10-14 23:26:20 +00:00
|
|
|
/// hexdigit - Return the (uppercase) hexadecimal character for the
|
|
|
|
/// given number \arg X (which should be less than 16).
|
|
|
|
static inline char hexdigit(unsigned X) {
|
|
|
|
return X < 10 ? '0' + X : 'A' + X - 10;
|
|
|
|
}
|
|
|
|
|
2008-11-10 04:22:46 +00:00
|
|
|
/// utohex_buffer - Emit the specified number into the buffer specified by
|
|
|
|
/// BufferEnd, returning a pointer to the start of the string. This can be used
|
|
|
|
/// like this: (note that the buffer must be large enough to handle any number):
|
|
|
|
/// char Buffer[40];
|
|
|
|
/// printf("0x%s", utohex_buffer(X, Buffer+40));
|
|
|
|
///
|
|
|
|
/// This should only be used with unsigned types.
|
|
|
|
///
|
|
|
|
template<typename IntTy>
|
|
|
|
static inline char *utohex_buffer(IntTy X, char *BufferEnd) {
|
|
|
|
char *BufPtr = BufferEnd;
|
|
|
|
*--BufPtr = 0; // Null terminate buffer.
|
|
|
|
if (X == 0) {
|
|
|
|
*--BufPtr = '0'; // Handle special case.
|
|
|
|
return BufPtr;
|
|
|
|
}
|
2002-04-07 08:36:19 +00:00
|
|
|
|
|
|
|
while (X) {
|
2005-01-28 07:22:20 +00:00
|
|
|
unsigned char Mod = static_cast<unsigned char>(X) & 15;
|
2008-10-14 23:26:20 +00:00
|
|
|
*--BufPtr = hexdigit(Mod);
|
2002-04-07 08:36:19 +00:00
|
|
|
X >>= 4;
|
|
|
|
}
|
2008-11-10 04:22:46 +00:00
|
|
|
return BufPtr;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2008-11-10 04:22:46 +00:00
|
|
|
static inline std::string utohexstr(uint64_t X) {
|
2010-04-11 19:00:03 +00:00
|
|
|
char Buffer[17];
|
|
|
|
return utohex_buffer(X, Buffer+17);
|
2002-04-07 08:36:19 +00:00
|
|
|
}
|
|
|
|
|
2006-05-31 21:25:50 +00:00
|
|
|
static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
|
2010-04-11 19:00:03 +00:00
|
|
|
char Buffer[11];
|
|
|
|
char *BufPtr = Buffer+11;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2010-04-11 19:00:03 +00:00
|
|
|
return std::string(BufPtr, Buffer+11);
|
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) {
|
2010-04-11 19:00:03 +00:00
|
|
|
char Buffer[21];
|
|
|
|
char *BufPtr = Buffer+21;
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
if (X == 0) *--BufPtr = '0'; // Handle special case...
|
2009-01-09 19:25:42 +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;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
if (isNeg) *--BufPtr = '-'; // Add negative sign...
|
2010-04-11 19:00:03 +00:00
|
|
|
return std::string(BufPtr, Buffer+21);
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-01-11 19:45:18 +00:00
|
|
|
/// StrInStrNoCase - Portable version of strcasestr. Locates the first
|
|
|
|
/// occurrence of string 's1' in string 's2', ignoring case. Returns
|
|
|
|
/// the offset of s2 in s1 or npos if s2 cannot be found.
|
|
|
|
StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2);
|
|
|
|
|
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.
|
2010-01-11 18:03:24 +00:00
|
|
|
/// The function returns a pair containing the extracted token and the
|
|
|
|
/// remaining tail string.
|
|
|
|
std::pair<StringRef, StringRef> getToken(StringRef Source,
|
|
|
|
StringRef Delimiters = " \t\n\v\f\r");
|
2003-12-29 05:06:38 +00:00
|
|
|
|
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.
|
2010-01-11 18:03:24 +00:00
|
|
|
void SplitString(StringRef Source,
|
|
|
|
SmallVectorImpl<StringRef> &OutFragments,
|
|
|
|
StringRef Delimiters = " \t\n\v\f\r");
|
2006-11-28 22:32:35 +00:00
|
|
|
|
2009-10-17 18:21:06 +00:00
|
|
|
/// HashString - Hash funtion for strings.
|
|
|
|
///
|
|
|
|
/// This is the Bernstein hash function.
|
|
|
|
//
|
|
|
|
// 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) {
|
|
|
|
for (unsigned i = 0, e = Str.size(); i != e; ++i)
|
|
|
|
Result = Result * 33 + Str[i];
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
#endif
|