Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- StringExtras.h - Useful string functions -----------------*- C++ -*--=// |
| 2 | // |
| 3 | // This file contains some functions that are useful when dealing with strings. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #ifndef LLVM_TOOLS_STRING_EXTRAS_H |
| 8 | #define LLVM_TOOLS_STRING_EXTRAS_H |
| 9 | |
| 10 | #include <string> |
Chris Lattner | 2e35bed | 2001-07-15 00:16:38 +0000 | [diff] [blame] | 11 | #include <stdio.h> |
Chris Lattner | 57dbb3a | 2001-07-23 17:46:59 +0000 | [diff] [blame] | 12 | #include "llvm/Support/DataTypes.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 13 | |
Chris Lattner | 5f62191 | 2001-10-29 13:24:31 +0000 | [diff] [blame] | 14 | class ConstPoolArray; |
| 15 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 16 | static 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 | |
| 33 | static 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 | |
| 41 | static 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 | |
| 58 | static 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 Lattner | 2e35bed | 2001-07-15 00:16:38 +0000 | [diff] [blame] | 65 | static inline string ftostr(double V) { |
| 66 | char Buffer[200]; |
Chris Lattner | 6eaa457 | 2001-11-01 22:06:00 +0000 | [diff] [blame^] | 67 | snprintf(Buffer, 200, "%e", V); |
Chris Lattner | 2e35bed | 2001-07-15 00:16:38 +0000 | [diff] [blame] | 68 | return Buffer; |
| 69 | } |
| 70 | |
Vikram S. Adve | 7d2a251 | 2001-07-28 04:41:27 +0000 | [diff] [blame] | 71 | static inline void |
| 72 | printIndent(unsigned int indent, ostream& os=cout, const char* const istr=" ") |
| 73 | { |
| 74 | for (unsigned i=0; i < indent; i++) |
| 75 | os << istr; |
| 76 | } |
Chris Lattner | 5f62191 | 2001-10-29 13:24:31 +0000 | [diff] [blame] | 77 | |
| 78 | // Can we treat the specified array as a string? Only if it is an array of |
| 79 | // ubytes or non-negative sbytes. |
| 80 | // |
| 81 | bool isStringCompatible(ConstPoolArray *CPA); |
| 82 | |
| 83 | // getAsCString - Return the specified array as a C compatible string, only if |
| 84 | // the predicate isStringCompatible is true. |
| 85 | // |
| 86 | string getAsCString(ConstPoolArray *CPA); |
| 87 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 88 | #endif |