summaryrefslogtreecommitdiff
path: root/src/mongo/util/ctype.h
blob: 78ee57e3d0cc83602b1cb75032d7e515a8c8d481 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
 *    Copyright (C) 2020-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

/**
 * Replacements for <cctype> or <ctype.h> functions and macros.
 * These should be used instead of the corresponding standard functions.
 * Note the camel-case spelling to distinguish these from the C++ functions
 * and especially the C macros.
 *
 * Regarding the capitalization of these functions: POSIX defines standard
 * identifiers for the 12 character classes. Each "is"- function here directly
 * references and evokes such a POSIX identifier, so they are not
 * camel-cased as ordinary English phrases (so `isAlnum` not `isAlNum`).
 *
 * <https://en.wikipedia.org/wiki/Regular_expression#Character_classes>
 *
 * Problems with the standard functions:
 *
 *   - They accept int (to accept the EOF of -1 and integrate with cstdio).
 *     Passing negative char values other than EOF is undefined behavior!
 *     They cannot be used directly in std algorithms operating on char
 *     arguments because of this, say to `std::transform` or `std::find_if`
 *     on a `std::string`. You need a lambda and it has to do a cast.
 *   - Most are locale dependent, so they have to be slow. Dropping
 *     locale makes the "is"- functions 200% faster.
 *   - They return int instead of bool for C compatibility. Undesirable in C++.
 *   - In C they are macros, so they are very different entities depending on
 *     the subtle choice of #include <cctype> vs #include <ctype.h>.
 *   - Support for the EOF value bloats the lookup tables and carves out a
 *     surprising special case.
 *
 * The `<cctype>` character classification functions are a subtle source of bugs.
 * See warnings at <https://en.cppreference.com/w/cpp/header/cctype>.
 *
 * The proper call sequence is often not done, creating bugs. So
 * here are some more suitable C++17 implementations. We can make our versions
 * constexpr and noexcept because they don't depend on the locale or other
 * dynamic program state.
 */

#pragma once

#include <array>
#include <cstdint>

namespace mongo::ctype {
namespace detail {

/** Define a bit position for each character class queryable with this API. */
enum ClassBit : uint16_t {
    kUpper = 1 << 0,   //< [upper] UPPERCASE
    kLower = 1 << 1,   //< [lower] lowercase
    kAlpha = 1 << 2,   //< [alpha] Alphabetic (upper case or lower case)
    kDigit = 1 << 3,   //< [digit] Decimal digit
    kXdigit = 1 << 4,  //< [xdigit] Hexadecimal digit (upper case or lower case: [0-9A-Fa-f])
    kSpace = 1 << 5,   //< [space] Whitespace ([ \t\r\n\f\v])
    kPrint = 1 << 6,   //< [print] Printing (non-control chars)
    kGraph = 1 << 7,   //< [graph] Graphical (non-control, non-whitespace)
    kBlank = 1 << 8,   //< [blank] Blank (' ', '\t')
    kCntrl = 1 << 9,   //< [cntrl] Control character: 0x00-0x1f, and 0x7f (DEL)
    kPunct = 1 << 10,  //< [punct] Punctuation (graphical, but not alphanumeric)
    kAlnum = 1 << 11   //< [alnum] Alphanumeric (letter or digit)
};

/** Returns the bitwise-or of all `ClassBit` pertinent to character `c`.  */
constexpr uint16_t calculateClassBits(unsigned char c) {
    if (c >= 0x80)
        return 0;
    uint16_t r = 0;
    if (c <= 0x1f || c == 0x7f)
        r |= kCntrl;
    if (!(r & kCntrl))
        r |= kPrint;
    if (c == '\t' || c == ' ')
        r |= kBlank;
    if ((r & kBlank) || c == '\n' || c == '\v' || c == '\f' || c == '\r')
        r |= kSpace;
    if (c >= 'A' && c <= 'Z')
        r |= kUpper;
    if (c >= 'a' && c <= 'z')
        r |= kLower;
    if (c >= '0' && c <= '9')
        r |= kDigit;
    if ((r & kUpper) || (r & kLower))
        r |= kAlpha;
    if ((r & kDigit) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))
        r |= kXdigit;
    if ((r & kAlpha) || (r & kDigit))
        r |= kAlnum;
    if ((r & kPrint) && !(r & kSpace))
        r |= kGraph;
    if ((r & kGraph) && !(r & kAlnum))
        r |= kPunct;
    return r;
}

/** The character class memberships for each char. */
constexpr auto chClassTable = [] {
    std::array<uint16_t, 256> arr{};
    for (size_t i = 0; i < arr.size(); ++i)
        arr[i] = calculateClassBits(i);
    return arr;
}();

constexpr bool isMember(char c, uint16_t mask) {
    return chClassTable[static_cast<unsigned char>(c)] & mask;
}

/** Lookup table for `toUpper`. */
constexpr auto chUpperTable = [] {
    std::array<char, 256> arr{};
    for (size_t i = 0; i < arr.size(); ++i)
        arr[i] = isMember(i, kLower) ? 'A' + (i - 'a') : i;
    return arr;
}();

/** Lookup table for `toLower`. */
constexpr auto chLowerTable = [] {
    std::array<char, 256> arr{};
    for (size_t i = 0; i < arr.size(); ++i)
        arr[i] = isMember(i, kUpper) ? 'a' + (i - 'A') : i;
    return arr;
}();

}  // namespace detail


/**
 * These 12 "is"- functions exactly match the <cctype> definitions for the
 * POSIX (or C) locale. See the corresponding definitions in <cctype>.
 * <https://en.cppreference.com/w/cpp/header/cctype>
 * See notes above.
 */
constexpr bool isAlnum(char c) noexcept {
    return detail::isMember(c, detail::kAlnum);
}
constexpr bool isAlpha(char c) noexcept {
    return detail::isMember(c, detail::kAlpha);
}
constexpr bool isLower(char c) noexcept {
    return detail::isMember(c, detail::kLower);
}
constexpr bool isUpper(char c) noexcept {
    return detail::isMember(c, detail::kUpper);
}
constexpr bool isDigit(char c) noexcept {
    return detail::isMember(c, detail::kDigit);
}
constexpr bool isXdigit(char c) noexcept {
    return detail::isMember(c, detail::kXdigit);
}
constexpr bool isCntrl(char c) noexcept {
    return detail::isMember(c, detail::kCntrl);
}
constexpr bool isGraph(char c) noexcept {
    return detail::isMember(c, detail::kGraph);
}
constexpr bool isSpace(char c) noexcept {
    return detail::isMember(c, detail::kSpace);
}
constexpr bool isBlank(char c) noexcept {
    return detail::isMember(c, detail::kBlank);
}
constexpr bool isPrint(char c) noexcept {
    return detail::isMember(c, detail::kPrint);
}
constexpr bool isPunct(char c) noexcept {
    return detail::isMember(c, detail::kPunct);
}

/**
 * Returns the upper case of `c` if `c` is lower case, otherwise `c`.
 * Unlike `std::toupper`, is not affected by locale. See notes above.
 */
constexpr char toUpper(char c) noexcept {
    return detail::chUpperTable[static_cast<unsigned char>(c)];
}

/**
 * Returns the lower case of `c` if `c` is upper case, otherwise `c`.
 * Unlike `std::tolower`, is not affected by locale. See notes above.
 */
constexpr char toLower(char c) noexcept {
    return detail::chLowerTable[static_cast<unsigned char>(c)];
}

}  // namespace mongo::ctype