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