Panda3D
string_utils.I
1 // Filename: string_utils.I
2 // Created by: drose (14Jul00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 template<class Thing>
17 INLINE string
18 format_string(const Thing &thing) {
19  ostringstream str;
20  str << thing;
21  return str.str();
22 }
23 
24 INLINE string
25 format_string(const string &value) {
26  return value;
27 }
28 
29 INLINE string
30 format_string(float value) {
31  char buffer[32];
32  pdtoa(value, buffer);
33  return string(buffer);
34 }
35 
36 INLINE string
37 format_string(double value) {
38  char buffer[32];
39  pdtoa(value, buffer);
40  return string(buffer);
41 }
42 
43 INLINE string
44 format_string(unsigned int value) {
45  char buffer[11];
46  char *p = buffer + 10;
47  *p = 0;
48  do {
49  *--p = '0' + (value % 10);
50  value /= 10;
51  } while (value > 0);
52 
53  return string(p);
54 }
55 
56 INLINE string
57 format_string(int value) {
58  char buffer[12];
59  char *p = buffer + 11;
60  *p = 0;
61 
62  if (value < 0) {
63  unsigned int posv = (unsigned int)-value;
64  do {
65  *--p = '0' + (posv % 10);
66  posv /= 10;
67  } while (posv > 0);
68  *--p = '-';
69  } else {
70  do {
71  *--p = '0' + (value % 10);
72  value /= 10;
73  } while (value > 0);
74  }
75 
76  return string(p);
77 }
78 
79 INLINE string
80 format_string(PN_int64 value) {
81  char buffer[21];
82  char *p = buffer + 20;
83  *p = 0;
84 
85  if (value < 0) {
86  PN_uint64 posv = (PN_uint64)-value;
87  do {
88  *--p = '0' + (posv % 10);
89  posv /= 10;
90  } while (posv > 0);
91  *--p = '-';
92  } else {
93  do {
94  *--p = '0' + (value % 10);
95  value /= 10;
96  } while (value > 0);
97  }
98 
99  return string(p);
100 }