blob: 569ae4240b913080d6925aade746be73a7258ce6 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- StringExtras.h - Useful string functions -----------------*- C++ -*--=//
2//
3// This file contains some functions that are useful when dealing with strings.
Chris Lattner00950542001-06-06 20:29:01 +00004//
5//===----------------------------------------------------------------------===//
6
7#ifndef LLVM_TOOLS_STRING_EXTRAS_H
8#define LLVM_TOOLS_STRING_EXTRAS_H
9
10#include <string>
Chris Lattner2e35bed2001-07-15 00:16:38 +000011#include <stdio.h>
Chris Lattner57dbb3a2001-07-23 17:46:59 +000012#include "llvm/Support/DataTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner5f621912001-10-29 13:24:31 +000014class ConstPoolArray;
15
Chris Lattner00950542001-06-06 20:29:01 +000016static inline string utostr(uint64_t X, bool isNeg = false) {
17 char Buffer[40];
18 char *BufPtr = Buffer+39;
19
20 *BufPtr = 0; // Null terminate buffer...
21 if (X == 0) *--BufPtr = '0'; // Handle special case...
22
23 while (X) {
24 *--BufPtr = '0' + (X % 10);
25 X /= 10;
26 }
27
28 if (isNeg) *--BufPtr = '-'; // Add negative sign...
29
30 return string(BufPtr);
31}
32
33static inline string itostr(int64_t X) {
34 if (X < 0)
35 return utostr((uint64_t)-X, true);
36 else
37 return utostr((uint64_t)X);
38}
39
40
41static inline string utostr(unsigned X, bool isNeg = false) {
42 char Buffer[20];
43 char *BufPtr = Buffer+19;
44
45 *BufPtr = 0; // Null terminate buffer...
46 if (X == 0) *--BufPtr = '0'; // Handle special case...
47
48 while (X) {
49 *--BufPtr = '0' + (X % 10);
50 X /= 10;
51 }
52
53 if (isNeg) *--BufPtr = '-'; // Add negative sign...
54
55 return string(BufPtr);
56}
57
58static inline string itostr(int X) {
59 if (X < 0)
60 return utostr((unsigned)-X, true);
61 else
62 return utostr((unsigned)X);
63}
64
Chris Lattner2e35bed2001-07-15 00:16:38 +000065static inline string ftostr(double V) {
66 char Buffer[200];
Chris Lattner6eaa4572001-11-01 22:06:00 +000067 snprintf(Buffer, 200, "%e", V);
Chris Lattner2e35bed2001-07-15 00:16:38 +000068 return Buffer;
69}
70
Vikram S. Adve7d2a2512001-07-28 04:41:27 +000071static inline void
72printIndent(unsigned int indent, ostream& os=cout, const char* const istr=" ")
73{
74 for (unsigned i=0; i < indent; i++)
75 os << istr;
76}
Chris Lattner5f621912001-10-29 13:24:31 +000077
78// Can we treat the specified array as a string? Only if it is an array of
79// ubytes or non-negative sbytes.
80//
81bool isStringCompatible(ConstPoolArray *CPA);
82
83// getAsCString - Return the specified array as a C compatible string, only if
84// the predicate isStringCompatible is true.
85//
86string getAsCString(ConstPoolArray *CPA);
87
Chris Lattner00950542001-06-06 20:29:01 +000088#endif