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