xapian-core  2.0.0
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, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #include <config.h>
22 
23 #include "str.h"
24 
25 #include "negate_unsigned.h"
26 #include "omassert.h"
27 
28 #include <cstdio> // For snprintf().
29 #include <cstdlib> // For abort().
30 #include <string>
31 #include <type_traits>
32 
33 using namespace std;
34 
35 // Much faster than snprintf() - also less generated code!
36 template<class T>
37 static inline string
39 {
40  static_assert(std::is_unsigned_v<T>, "Unsigned type required");
41  // Special case single digit positive numbers.
42  // FIXME: is this actually worthwhile?
43  if (value < 10) return string(1, '0' + char(value));
44  char buf[(sizeof(T) * 5 + 1) / 2];
45  char * p = buf + sizeof(buf);
46  do {
47  AssertRel(p,>,buf);
48  char ch = static_cast<char>(value % 10);
49  value /= 10;
50  *(--p) = ch + '0';
51  } while (value);
52  return string(p, buf + sizeof(buf) - p);
53 }
54 
55 template<class T>
56 static inline string
57 tostring(T value)
58 {
59  // Special case single digit positive numbers.
60  // FIXME: is this actually worthwhile?
61  if (value < 10 && value >= 0) return string(1, '0' + char(value));
62 
63  bool negative = (value < 0);
64 
65  typedef typename std::make_unsigned_t<T> unsigned_type;
66  unsigned_type val(value);
67  if (negative) {
68  val = negate_unsigned(val);
69  }
70 
71  char buf[(sizeof(unsigned_type) * 5 + 1) / 2 + 1];
72  char * p = buf + sizeof(buf);
73  do {
74  AssertRel(p,>,buf);
75  char ch = static_cast<char>(val % 10);
76  val /= 10;
77  *(--p) = ch + '0';
78  } while (val);
79 
80  if (negative) {
81  AssertRel(p,>,buf);
82  *--p = '-';
83  }
84  return string(p, buf + sizeof(buf) - p);
85 }
86 
87 namespace Xapian {
88 namespace Internal {
89 
90 string
91 str(int value)
92 {
93  return tostring(value);
94 }
95 
96 string
97 str(unsigned int value)
98 {
99  return tostring_unsigned(value);
100 }
101 
102 string
103 str(long value)
104 {
105  return tostring(value);
106 }
107 
108 string
109 str(unsigned long value)
110 {
111  return tostring_unsigned(value);
112 }
113 
114 string
115 str(long long value)
116 {
117  return tostring(value);
118 }
119 
120 string
121 str(unsigned long long value)
122 {
123  return tostring_unsigned(value);
124 }
125 
126 template<class T>
127 static inline string
128 format(const char * fmt, T value)
129 {
130  char buf[128];
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  return string(buf, size);
137 }
138 
139 string
140 str(double value)
141 {
142  return format("%.20g", value);
143 }
144 
145 string
146 str(const void * value)
147 {
148  return format("%p", value);
149 }
150 
151 }
152 }
PositionList * p
string str(const void *value)
Convert const void * to std::string.
Definition: str.cc:146
static string format(const char *fmt, T value)
Definition: str.cc:128
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:82
Negate unsigned integer, avoiding compiler warnings.
constexpr std::enable_if_t< std::is_unsigned_v< T >, T > negate_unsigned(T value)
Various assertion macros.
#define AssertRel(A, REL, B)
Definition: omassert.h:123
static string tostring_unsigned(T value)
Definition: str.cc:38
static string tostring(T value)
Definition: str.cc:57
Convert types to std::string.