MySQL 8.0.37
Source Code Documentation
integer_digits.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef INTEGER_DIGITS_INCLUDED
27#define INTEGER_DIGITS_INCLUDED
28
29#include <assert.h>
30#include <algorithm>
31#include <limits>
32#include <type_traits>
33
34/**
35 @file
36
37 This file contains utilities for accessing digits of integers, and for
38 converting them to strings.
39*/
40
41/**
42 Helper class for #write_two_digits(), which creates a table that maps every
43 integer from 0 to 99 to a two-char sequence that represents its two base 10
44 digits.
45*/
47 public:
48 constexpr TwoDigitWriter() {
49 for (int i = 0; i < 100; ++i) {
50 m_digits[i][0] = '0' + i / 10;
51 m_digits[i][1] = '0' + i % 10;
52 }
53 }
54
55 char *Write(int value, char *to) const {
56 assert(value >= 0 && value < 100);
57 return std::copy_n(m_digits[value], 2, to);
58 }
59
60 private:
61 char m_digits[100][2]{};
62};
63
64/**
65 Writes an integer, which is between 0 (inclusive) and 100 (exclusive), to a
66 string in base 10. Always writes two digits, zero-padded if necessary. The
67 string is not zero-terminated.
68
69 @param value the number to write
70 @param[in,out] to the destination string
71 @return pointer to the character just after the last digit
72*/
73inline char *write_two_digits(int value, char *to) {
74 static constexpr TwoDigitWriter writer;
75 return writer.Write(value, to);
76}
77
78/**
79 Functor that calculates the number of digits in an unsigned integer using
80 binary search. The code for doing the binary search is generated and unrolled
81 at compile time.
82
83 @tparam T the unsigned integer type of the input to the functor
84 @tparam MinDigits the minimum number of digits the integer is known to have
85 @tparam MaxDigits the maximum number of digits the integer is known to have
86*/
87template <typename T, int MinDigits, int MaxDigits, typename = void>
89 static_assert(MinDigits < MaxDigits, "");
90 static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value,
91 "The input should be an unsigned integer.");
92
93 constexpr int operator()(T x) const {
94 constexpr int mid = (MinDigits + MaxDigits) / 2;
95 constexpr T pivot = pow10(mid);
96 if (x < pivot)
98 else
100 }
101
102 private:
103 static constexpr T pow10(int n) {
104 T x = 1;
105 for (int i = 0; i < n; ++i) x *= 10;
106 return x;
107 }
108};
109
110/**
111 Counts the number of digits for the trivial case where the known minimum
112 number of digits is equal to the known maximum number of digits.
113*/
114template <typename T, int MinDigits, int MaxDigits>
115struct DigitCounter<T, MinDigits, MaxDigits,
116 typename std::enable_if<MinDigits == MaxDigits>::type> {
117 constexpr int operator()(T) const { return MinDigits; }
118};
119
120/**
121 Counts the number of base 10 digits in an unsigned integer.
122
123 @param x the number whose digits to count
124 @return the number of digits in the number
125*/
126template <typename T>
127constexpr int count_digits(T x) {
129}
130
131/**
132 Writes an unsigned integer of the specified length to a string. The string is
133 not zero-terminated.
134
135 @param number the number to write
136 @param digits the number of digits to write (the number is zero-padded if it
137 is shorter)
138 @param[in,out] to the destination string
139 @return pointer to the character just after the last digit
140*/
141template <typename T>
142inline char *write_digits(T number, int digits, char *to) {
143 assert(digits >= count_digits(number));
144
145 // The string is built from the end, starting with the least significant
146 // digits.
147 char *pos = to + digits;
148
149 // The digits are written in groups of two in order to reduce the number of
150 // the relatively expensive modulo and division by 10 operations. If it has an
151 // odd number of digits, write the leftover digit separately.
152 if (digits % 2 != 0) {
153 *--pos = '0' + number % 10;
154 number /= 10;
155 }
156
157 while (pos > to) {
158 pos -= 2;
159 write_two_digits(number % 100, pos);
160 number /= 100;
161 }
162
163 return to + digits;
164}
165
166#endif // INTEGER_DIGITS_INCLUDED
Helper class for write_two_digits(), which creates a table that maps every integer from 0 to 99 to a ...
Definition: integer_digits.h:46
constexpr TwoDigitWriter()
Definition: integer_digits.h:48
char m_digits[100][2]
Definition: integer_digits.h:61
char * Write(int value, char *to) const
Definition: integer_digits.h:55
constexpr int count_digits(T x)
Counts the number of base 10 digits in an unsigned integer.
Definition: integer_digits.h:127
char * write_two_digits(int value, char *to)
Writes an integer, which is between 0 (inclusive) and 100 (exclusive), to a string in base 10.
Definition: integer_digits.h:73
char * write_digits(T number, int digits, char *to)
Writes an unsigned integer of the specified length to a string.
Definition: integer_digits.h:142
Definition: gcs_xcom_synode.h:64
required string type
Definition: replication_group_member_actions.proto:34
static const char digits[]
Definition: stacktrace.cc:598
Functor that calculates the number of digits in an unsigned integer using binary search.
Definition: integer_digits.h:88
constexpr int operator()(T x) const
Definition: integer_digits.h:93
static constexpr T pow10(int n)
Definition: integer_digits.h:103
int n
Definition: xcom_base.cc:509