xapian-core  1.4.19
str.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2009,2012,2015,2017 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 #include <type_traits>
31 
32 using namespace std;
33 
34 // Much faster than snprintf() - also less generated code!
35 template<class T>
36 static inline string
38 {
39  static_assert(std::is_unsigned<T>::value, "Unsigned type required");
40  // Special case single digit positive numbers.
41  // FIXME: is this actually worthwhile?
42  if (value < 10) return string(1, '0' + char(value));
43  char buf[(sizeof(T) * 5 + 1) / 2];
44  char * p = buf + sizeof(buf);
45  do {
46  AssertRel(p,>,buf);
47  char ch = static_cast<char>(value % 10);
48  value /= 10;
49  *(--p) = ch + '0';
50  } while (value);
51  return string(p, buf + sizeof(buf) - p);
52 }
53 
54 template<class T>
55 static inline string
56 tostring(T value)
57 {
58  // Special case single digit positive numbers.
59  // FIXME: is this actually worthwhile?
60  if (value < 10 && value >= 0) return string(1, '0' + char(value));
61 
62  bool negative = (value < 0);
63 
64  typedef typename std::make_unsigned<T>::type unsigned_type;
65  unsigned_type val(value);
66  if (negative) {
67  val = -val;
68  }
69 
70  char buf[(sizeof(unsigned_type) * 5 + 1) / 2 + 1];
71  char * p = buf + sizeof(buf);
72  do {
73  AssertRel(p,>,buf);
74  char ch = static_cast<char>(val % 10);
75  val /= 10;
76  *(--p) = ch + '0';
77  } while (val);
78 
79  if (negative) {
80  AssertRel(p,>,buf);
81  *--p = '-';
82  }
83  return string(p, buf + sizeof(buf) - p);
84 }
85 
86 namespace Xapian {
87 namespace Internal {
88 
89 string
90 str(int value)
91 {
92  return tostring(value);
93 }
94 
95 string
96 str(unsigned int value)
97 {
98  return tostring_unsigned(value);
99 }
100 
101 string
102 str(long value)
103 {
104  return tostring(value);
105 }
106 
107 string
108 str(unsigned long value)
109 {
110  return tostring_unsigned(value);
111 }
112 
113 string
114 str(long long value)
115 {
116  return tostring(value);
117 }
118 
119 string
120 str(unsigned long long value)
121 {
122  return tostring_unsigned(value);
123 }
124 
125 template<class T>
126 static inline string
127 format(const char * fmt, T value)
128 {
129  char buf[128];
130 #ifdef SNPRINTF
131  // If -1 is returned (as pre-ISO snprintf does if the buffer is too small,
132  // it will be cast to > sizeof(buf) and handled appropriately.
133  size_t size = SNPRINTF(buf, sizeof(buf), fmt, value);
134  AssertRel(size,<=,sizeof(buf));
135  if (size > sizeof(buf)) size = sizeof(buf);
136 #else
137  size_t size = sprintf(buf, fmt, value);
138  // Buffer overflow.
139  if (size >= sizeof(buf)) abort();
140 #endif
141  return string(buf, size);
142 }
143 
144 string
145 str(double value)
146 {
147  return format("%.20g", value);
148 }
149 
150 string
151 str(const void * value)
152 {
153  return format("%p", value);
154 }
155 
156 }
157 }
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:80
string str(const void *value)
Convert const void * to std::string.
Definition: str.cc:151
#define AssertRel(A, REL, B)
Definition: omassert.h:123
static string tostring_unsigned(T value)
Definition: str.cc:37
STL namespace.
Convert types to std::string.
static string tostring(T value)
Definition: str.cc:56
static string format(const char *fmt, T value)
Definition: str.cc:127
Various assertion macros.
#define SNPRINTF
Definition: config.h:346