[ci] Update macos jobs
[xapian.git] / xapian-core / common / str.cc
blobd14add2adc288b7b9d6a1c97f9d4a64e0c1c9d85
1 /** @file
2 * @brief Convert types to std::string
3 */
4 /* Copyright (C) 2009,2012,2015,2017 Olly Betts
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.
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.
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
21 #include <config.h>
23 #include "str.h"
25 #include "negate_unsigned.h"
26 #include "omassert.h"
28 #include <cstdio> // For snprintf().
29 #include <cstdlib> // For abort().
30 #include <string>
31 #include <type_traits>
33 using namespace std;
35 // Much faster than snprintf() - also less generated code!
36 template<class T>
37 static inline string
38 tostring_unsigned(T value)
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);
55 template<class T>
56 static inline string
57 tostring(T value)
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));
63 bool negative = (value < 0);
65 typedef typename std::make_unsigned_t<T> unsigned_type;
66 unsigned_type val(value);
67 if (negative) {
68 val = negate_unsigned(val);
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);
80 if (negative) {
81 AssertRel(p,>,buf);
82 *--p = '-';
84 return string(p, buf + sizeof(buf) - p);
87 namespace Xapian {
88 namespace Internal {
90 string
91 str(int value)
93 return tostring(value);
96 string
97 str(unsigned int value)
99 return tostring_unsigned(value);
102 string
103 str(long value)
105 return tostring(value);
108 string
109 str(unsigned long value)
111 return tostring_unsigned(value);
114 string
115 str(long long value)
117 return tostring(value);
120 string
121 str(unsigned long long value)
123 return tostring_unsigned(value);
126 template<class T>
127 static inline string
128 format(const char * fmt, T value)
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);
139 string
140 str(double value)
142 return format("%.20g", value);
145 string
146 str(const void * value)
148 return format("%p", value);