MySQL 8.4.2
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#include "mysql/attribute.h"
35
36/**
37 @file
38
39 This file contains utilities for accessing digits of integers, and for
40 converting them to strings.
41*/
42
43/**
44 Helper class for #write_two_digits(), which creates a table that maps every
45 integer from 0 to 99 to a two-char sequence that represents its two base 10
46 digits.
47*/
49 public:
50 constexpr TwoDigitWriter() {
51 for (int i = 0; i < 100; ++i) {
52 m_digits[i][0] = '0' + i / 10;
53 m_digits[i][1] = '0' + i % 10;
54 }
55 }
56
57 char *Write(int value, char *to) const {
58 assert(value >= 0 && value < 100);
59 return std::copy_n(m_digits[value], 2, to);
60 }
61
62 private:
63 char m_digits[100][2]{};
64};
65
66/**
67 Writes an integer, which is between 0 (inclusive) and 100 (exclusive), to a
68 string in base 10. Always writes two digits, zero-padded if necessary. The
69 string is not zero-terminated.
70
71 @param value the number to write
72 @param[in,out] to the destination string
73 @return pointer to the character just after the last digit
74*/
75MY_ATTRIBUTE((visibility("default")))
76inline char *write_two_digits(int value, char *to) {
77 static constexpr TwoDigitWriter writer;
78 return writer.Write(value, to);
79}
80
81/**
82 Functor that calculates the number of digits in an unsigned integer using
83 binary search. The code for doing the binary search is generated and unrolled
84 at compile time.
85
86 @tparam T the unsigned integer type of the input to the functor
87 @tparam MinDigits the minimum number of digits the integer is known to have
88 @tparam MaxDigits the maximum number of digits the integer is known to have
89*/
90template <typename T, int MinDigits, int MaxDigits, typename = void>
92 static_assert(MinDigits < MaxDigits, "");
93 static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value,
94 "The input should be an unsigned integer.");
95
96 constexpr int operator()(T x) const {
97 constexpr int mid = (MinDigits + MaxDigits) / 2;
98 constexpr T pivot = pow10(mid);
99 if (x < pivot)
101 else
103 }
104
105 private:
106 static constexpr T pow10(int n) {
107 T x = 1;
108 for (int i = 0; i < n; ++i) x *= 10;
109 return x;
110 }
111};
112
113/**
114 Counts the number of digits for the trivial case where the known minimum
115 number of digits is equal to the known maximum number of digits.
116*/
117template <typename T, int MinDigits, int MaxDigits>
118struct DigitCounter<T, MinDigits, MaxDigits,
119 typename std::enable_if<MinDigits == MaxDigits>::type> {
120 constexpr int operator()(T) const { return MinDigits; }
121};
122
123/**
124 Counts the number of base 10 digits in an unsigned integer.
125
126 @param x the number whose digits to count
127 @return the number of digits in the number
128*/
129template <typename T>
130constexpr int count_digits(T x) {
132}
133
134/**
135 Writes an unsigned integer of the specified length to a string. The string is
136 not zero-terminated.
137
138 @param number the number to write
139 @param digits the number of digits to write (the number is zero-padded if it
140 is shorter)
141 @param[in,out] to the destination string
142 @return pointer to the character just after the last digit
143*/
144template <typename T>
145inline char *write_digits(T number, int digits, char *to) {
146 assert(digits >= count_digits(number));
147
148 // The string is built from the end, starting with the least significant
149 // digits.
150 char *pos = to + digits;
151
152 // The digits are written in groups of two in order to reduce the number of
153 // the relatively expensive modulo and division by 10 operations. If it has an
154 // odd number of digits, write the leftover digit separately.
155 if (digits % 2 != 0) {
156 *--pos = '0' + number % 10;
157 number /= 10;
158 }
159
160 while (pos > to) {
161 pos -= 2;
162 write_two_digits(number % 100, pos);
163 number /= 100;
164 }
165
166 return to + digits;
167}
168
169#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:48
constexpr TwoDigitWriter()
Definition: integer_digits.h:50
char m_digits[100][2]
Definition: integer_digits.h:63
char * Write(int value, char *to) const
Definition: integer_digits.h:57
constexpr int count_digits(T x)
Counts the number of base 10 digits in an unsigned integer.
Definition: integer_digits.h:130
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:76
char * write_digits(T number, int digits, char *to)
Writes an unsigned integer of the specified length to a string.
Definition: integer_digits.h:145
Definition: gcs_xcom_synode.h:64
required string type
Definition: replication_group_member_actions.proto:34
static const char digits[]
Definition: stacktrace.cc:644
Functor that calculates the number of digits in an unsigned integer using binary search.
Definition: integer_digits.h:91
constexpr int operator()(T x) const
Definition: integer_digits.h:96
static constexpr T pow10(int n)
Definition: integer_digits.h:106
int n
Definition: xcom_base.cc:509