StringExtras.h revision 697954c15da58bd8b186dbafdedd8b06db770201
1//===-- Support/StringExtras.h - Useful string functions ---------*- C++ -*--=//
2//
3// This file contains some functions that are useful when dealing with strings.
4//
5//===----------------------------------------------------------------------===//
6
7#ifndef SUPPORT_STRING_EXTRAS_H
8#define SUPPORT_STRING_EXTRAS_H
9
10#include "Support/DataTypes.h"
11#include <string>
12#include <stdio.h>
13
14static inline std::string utostr(uint64_t X, bool isNeg = false) {
15  char Buffer[40];
16  char *BufPtr = Buffer+39;
17
18  *BufPtr = 0;                  // Null terminate buffer...
19  if (X == 0) *--BufPtr = '0';  // Handle special case...
20
21  while (X) {
22    *--BufPtr = '0' + (X % 10);
23    X /= 10;
24  }
25
26  if (isNeg) *--BufPtr = '-';   // Add negative sign...
27
28  return std::string(BufPtr);
29}
30
31static inline std::string itostr(int64_t X) {
32  if (X < 0)
33    return utostr((uint64_t)-X, true);
34  else
35    return utostr((uint64_t)X);
36}
37
38
39static inline std::string utostr(unsigned X, bool isNeg = false) {
40  char Buffer[20];
41  char *BufPtr = Buffer+19;
42
43  *BufPtr = 0;                  // Null terminate buffer...
44  if (X == 0) *--BufPtr = '0';  // Handle special case...
45
46  while (X) {
47    *--BufPtr = '0' + (X % 10);
48    X /= 10;
49  }
50
51  if (isNeg) *--BufPtr = '-';   // Add negative sign...
52
53  return std::string(BufPtr);
54}
55
56static inline std::string itostr(int X) {
57  if (X < 0)
58    return utostr((unsigned)-X, true);
59  else
60    return utostr((unsigned)X);
61}
62
63static inline std::string ftostr(double V) {
64  char Buffer[200];
65  snprintf(Buffer, 200, "%e", V);
66  return Buffer;
67}
68
69#endif
70