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