StringExtras.h revision 913e1b4bddb9674a3c03e6726579217ede134775
1//===-- Support/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 SUPPORT_STRINGEXTRAS_H
15#define SUPPORT_STRINGEXTRAS_H
16
17#include "Support/DataTypes.h"
18#include <cctype>
19#include <string>
20#include <stdio.h>
21
22namespace llvm {
23
24static inline std::string utohexstr(uint64_t X) {
25  char Buffer[40];
26  char *BufPtr = Buffer+39;
27
28  *BufPtr = 0;                  // Null terminate buffer...
29  if (X == 0) *--BufPtr = '0';  // Handle special case...
30
31  while (X) {
32    unsigned char Mod = (unsigned char)X & 15;
33    if (Mod < 10)
34      *--BufPtr = '0' + Mod;
35    else
36      *--BufPtr = 'A' + Mod-10;
37    X >>= 4;
38  }
39  return std::string(BufPtr);
40}
41
42static inline std::string utostr(unsigned long long X, bool isNeg = false) {
43  char Buffer[40];
44  char *BufPtr = Buffer+39;
45
46  *BufPtr = 0;                  // Null terminate buffer...
47  if (X == 0) *--BufPtr = '0';  // Handle special case...
48
49  while (X) {
50    *--BufPtr = '0' + char(X % 10);
51    X /= 10;
52  }
53
54  if (isNeg) *--BufPtr = '-';   // Add negative sign...
55  return std::string(BufPtr);
56}
57
58static inline std::string itostr(int64_t X) {
59  if (X < 0)
60    return utostr(static_cast<uint64_t>(-X), true);
61  else
62    return utostr(static_cast<uint64_t>(X));
63}
64
65
66static inline std::string utostr(unsigned long X, bool isNeg = false) {
67  return utostr(static_cast<unsigned long long>(X), isNeg);
68}
69
70static inline std::string utostr(unsigned X, bool isNeg = false) {
71  char Buffer[20];
72  char *BufPtr = Buffer+19;
73
74  *BufPtr = 0;                  // Null terminate buffer...
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);
85}
86
87static inline std::string itostr(int X) {
88  if (X < 0)
89    return utostr(static_cast<unsigned>(-X), true);
90  else
91    return utostr(static_cast<unsigned>(X));
92}
93
94static inline std::string ftostr(double V) {
95  char Buffer[200];
96  sprintf(Buffer, "%20.6e", V);
97  return Buffer;
98}
99
100static inline std::string LowercaseString(const std::string &S) {
101  std::string result(S);
102  for (unsigned i = 0; i < S.length(); ++i)
103    if (isupper(result[i]))
104      result[i] = (char)tolower(result[i]);
105  return result;
106}
107
108/// getToken - This function extracts one token from source, ignoring any
109/// leading characters that appear in the Delimiters string, and ending the
110/// token at any of the characters that appear in the Delimiters string.  If
111/// there are no tokens in the source string, an empty string is returned.
112/// The Source source string is updated in place to remove the returned string
113/// and any delimiter prefix from it.
114std::string getToken(std::string &Source,
115                     const char *Delimiters = " \t\n\v\f\r");
116
117} // End llvm namespace
118
119#endif
120