Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame^] | 1 | //===-- Support/StringExtras.h - Useful string functions ---------*- C++ -*--=// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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 | |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame^] | 7 | #ifndef SUPPORT_STRING_EXTRAS_H |
| 8 | #define SUPPORT_STRING_EXTRAS_H |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame^] | 10 | #include "Support/DataTypes.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | #include <string> |
Chris Lattner | 2e35bed | 2001-07-15 00:16:38 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
Chris Lattner | 5f62191 | 2001-10-29 13:24:31 +0000 | [diff] [blame] | 13 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | static inline 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 string(BufPtr); |
| 29 | } |
| 30 | |
| 31 | static inline 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 | |
| 39 | static inline 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 string(BufPtr); |
| 54 | } |
| 55 | |
| 56 | static inline string itostr(int X) { |
| 57 | if (X < 0) |
| 58 | return utostr((unsigned)-X, true); |
| 59 | else |
| 60 | return utostr((unsigned)X); |
| 61 | } |
| 62 | |
Chris Lattner | 2e35bed | 2001-07-15 00:16:38 +0000 | [diff] [blame] | 63 | static inline string ftostr(double V) { |
| 64 | char Buffer[200]; |
Chris Lattner | 6eaa457 | 2001-11-01 22:06:00 +0000 | [diff] [blame] | 65 | snprintf(Buffer, 200, "%e", V); |
Chris Lattner | 2e35bed | 2001-07-15 00:16:38 +0000 | [diff] [blame] | 66 | return Buffer; |
| 67 | } |
| 68 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 69 | #endif |