str.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2009 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <config.h>
22 
23 #include "str.h"
24 
25 #include "omassert.h"
26 
27 #include <cstdio> // For snprintf() or sprintf().
28 #include <cstdlib> // For abort().
29 #include <string>
30 
31 using namespace std;
32 
33 // Much faster than snprintf() - also less generated code!
34 template<class T>
35 inline string
37 {
39  // We need a special case for 0, and we might as well handle all single
40  // digit numbers with it too.
41  if (value < 10) return string(1, '0' + char(value));
42  char buf[(sizeof(T) * 5 + 1) / 2];
43  char * p = buf + sizeof(buf);
44  do {
45  AssertRel(p,>,buf);
46  char ch(value % 10);
47  value /= 10;
48  *(--p) = ch + '0';
49  } while (value);
50  return string(p, buf + sizeof(buf) - p);
51 }
52 
53 template<class T>
54 inline string
55 tostring(T value)
56 {
57  // We need a special case for 0, and we might as well handle all single
58  // digit positive numbers with it too.
59  if (value < 10 && value >= 0) return string(1, '0' + char(value));
60 
61  bool negative = (value < 0);
62  if (negative) value = -value;
63 
64  char buf[(sizeof(T) * 5 + 1) / 2 + 1];
65  char * p = buf + sizeof(buf);
66  do {
67  AssertRel(p,>,buf);
68  char ch(value % 10);
69  value /= 10;
70  *(--p) = ch + '0';
71  } while (value);
72 
73  if (negative) {
74  AssertRel(p,>,buf);
75  *--p = '-';
76  }
77  return string(p, buf + sizeof(buf) - p);
78 }
79 
80 namespace Xapian {
81 namespace Internal {
82 
83 string
84 str(int value)
85 {
86  return tostring(value);
87 }
88 
89 string
90 str(unsigned int value)
91 {
92  return tostring_unsigned(value);
93 }
94 
95 string
96 str(long value)
97 {
98  return tostring(value);
99 }
100 
101 string
102 str(unsigned long value)
103 {
104  return tostring_unsigned(value);
105 }
106 
107 string
108 str(long long value)
109 {
110  return tostring(value);
111 }
112 
113 string
114 str(unsigned long long value)
115 {
116  return tostring_unsigned(value);
117 }
118 
119 template<class T>
120 inline string
121 format(const char * fmt, T value)
122 {
123  char buf[128];
124 #ifdef SNPRINTF
125  // If -1 is returned (as pre-ISO snprintf does if the buffer is too small,
126  // it will be cast to > sizeof(buf) and handled appropriately.
127  size_t size = SNPRINTF_ISO(buf, sizeof(buf), fmt, value);
128  AssertRel(size,<=,sizeof(buf));
129  if (size > sizeof(buf)) size = sizeof(buf);
130 #else
131  size_t size = sprintf(buf, fmt, value);
132  // Buffer overflow.
133  if (size >= sizeof(buf)) abort();
134 #endif
135  return string(buf, size);
136 }
137 
138 string
139 str(double value)
140 {
141  return format("%.20g", value);
142 }
143 
144 string
145 str(const void * value)
146 {
147  return format("%p", value);
148 }
149 
150 }
151 }

Documentation for Xapian (version 1.2.19).
Generated on Tue Oct 21 2014 by Doxygen 1.8.1.2.