- Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathm_string.h
290 lines (248 loc) · 8.43 KB
/
m_string.h
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
Copyright (c) 2000, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
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
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#ifndef M_STRING_INCLUDED
#defineM_STRING_INCLUDED
/**
@file include/m_string.h
*/
/*
This file is for trivial constant definitions and in-line wrappers only.
Please don't add new stuff to this file unnecessarily.
*/
#include<float.h>
#include<limits.h>
#include<stdbool.h>// IWYU pragma: keep
#include<stdint.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<cstdint>
#include"lex_string.h"
#include"my_config.h"
/*
bchange(dst, old_length, src, new_length, tot_length)
replaces old_length characters at dst to new_length characters from
src in a buffer with tot_length bytes.
*/
staticinlinevoidbchange(uint8_t *dst, size_t old_length, constuint8_t *src,
size_t new_length, size_t tot_length) {
memmove(dst + new_length, dst + old_length, tot_length - old_length);
memcpy(dst, src, new_length);
}
/*
strend(s) returns a character pointer to the NUL which ends s. That
is, strend(s)-s == strlen(s). This is useful for adding things at
the end of strings. It is redundant, because strchr(s,'\0') could
be used instead, but this is clearer and faster.
*/
staticinlineconstchar *strend(constchar *s) {
while (*s++) {
}
return s - 1;
}
staticinlinechar *strend(char *s) {
while (*s++) {
}
return s - 1;
}
/*
strcend(s, c) returns a pointer to the first place in s where c
occurs, or a pointer to the end-null of s if c does not occur in s.
*/
staticinlineconstchar *strcend(constchar *s, char c) {
for (;;) {
if (*s == c) return s;
if (!*s++) return s - 1;
}
}
/*
strfill(dest, len, fill) makes a string of fill-characters. The result
string is of length == len. The des+len character is always set to NULL.
strfill() returns pointer to dest+len;
*/
staticinlinechar *strfill(char *s, size_t len, char fill) {
while (len--) *s++ = fill;
*(s) = '\0';
return (s);
}
/*
my_stpmov(dst, src) moves all the characters of src (including the
closing NUL) to dst, and returns a pointer to the new closing NUL in
dst. The similar UNIX routine strcpy returns the old value of dst,
which I have never found useful. my_stpmov(my_stpmov(dst,a),b) moves a//b
into dst, which seems useful.
*/
staticinlinechar *my_stpmov(char *dst, constchar *src) {
while ((*dst++ = *src++)) {
}
return dst - 1;
}
/*
my_stpnmov(dst,src,length) moves length characters, or until end, of src to
dst and appends a closing NUL to dst if src is shorter than length.
The result is a pointer to the first NUL in dst, or is dst+n if dst was
truncated.
*/
staticinlinechar *my_stpnmov(char *dst, constchar *src, size_t n) {
while (n-- != 0) {
if (!(*dst++ = *src++)) return (char *)dst - 1;
}
return dst;
}
/**
Copy a string from src to dst until (and including) terminating null byte.
@param dst Destination
@param src Source
@note src and dst cannot overlap.
Use my_stpmov() if src and dst overlaps.
@note Unsafe, consider using my_stpnpy() instead.
@return pointer to terminating null byte.
*/
staticinlinechar *my_stpcpy(char *dst, constchar *src) {
#if defined(HAVE_BUILTIN_STPCPY)
/*
If __builtin_stpcpy() is available, use it instead of stpcpy(), since GCC in
some situations is able to transform __builtin_stpcpy() into more efficient
strcpy() or memcpy() calls. It does not perform these transformations for a
plain call to stpcpy() when the compiler runs in strict mode. See GCC bug
82429.
*/
return__builtin_stpcpy(dst, src);
#elif defined(HAVE_STPCPY)
returnstpcpy(dst, src);
#else
/* Fallback to implementation supporting overlap. */
returnmy_stpmov(dst, src);
#endif
}
/**
Copy fixed-size string from src to dst.
@param dst Destination
@param src Source
@param n Maximum number of characters to copy.
@note src and dst cannot overlap
Use my_stpnmov() if src and dst overlaps.
@return pointer to terminating null byte.
*/
staticinlinechar *my_stpncpy(char *dst, constchar *src, size_t n) {
#if defined(HAVE_STPNCPY)
returnstpncpy(dst, src, n);
#else
/* Fallback to implementation supporting overlap. */
returnmy_stpnmov(dst, src, n);
#endif
}
staticinlinelonglongmy_strtoll(constchar *nptr, char **endptr, int base) {
#if defined _WIN32
return_strtoi64(nptr, endptr, base);
#else
returnstrtoll(nptr, endptr, base);
#endif
}
staticinlineunsignedlonglongmy_strtoull(constchar *nptr, char **endptr,
int base) {
#if defined _WIN32
return_strtoui64(nptr, endptr, base);
#else
returnstrtoull(nptr, endptr, base);
#endif
}
staticinlinechar *my_strtok_r(char *str, constchar *delim, char **saveptr) {
#if defined _WIN32
returnstrtok_s(str, delim, saveptr);
#else
returnstrtok_r(str, delim, saveptr);
#endif
}
/* native_ rather than my_ since my_strcasecmp already exists */
staticinlineintnative_strcasecmp(constchar *s1, constchar *s2) {
#if defined _WIN32
return_stricmp(s1, s2);
#else
returnstrcasecmp(s1, s2);
#endif
}
/* native_ rather than my_ for consistency with native_strcasecmp */
staticinlineintnative_strncasecmp(constchar *s1, constchar *s2, size_t n) {
#if defined _WIN32
return_strnicmp(s1, s2, n);
#else
returnstrncasecmp(s1, s2, n);
#endif
}
/*
is_prefix(s, t) returns 1 if s starts with t.
A empty t is always a prefix.
*/
staticinlineintis_prefix(constchar *s, constchar *t) {
while (*t)
if (*s++ != *t++) return0;
return1; /* WRONG */
}
/**
Skip trailing space (ASCII spaces only).
@return New end of the string.
*/
staticinlineconstuint8_t *skip_trailing_space(constuint8_t *ptr,
size_t len) {
constuint8_t *end = ptr + len;
while (end - ptr >= 8) {
uint64_t chunk;
memcpy(&chunk, end - 8, sizeof(chunk));
if (chunk != 0x2020202020202020ULL) break;
end -= 8;
}
while (end > ptr && end[-1] == 0x20) end--;
return (end);
}
/*
Format a double (representing number of bytes) into a human-readable string.
@param buf Buffer used for printing
@param buf_len Length of buffer
@param dbl_val Value to be formatted
@note
Sample output format: 42 1K 234M 2G
If we exceed ULLONG_MAX YiB we give up, and convert to "+INF".
@todo Consider writing KiB GiB etc, since we use 1024 rather than 1000
*/
staticinlinevoidhuman_readable_num_bytes(char *buf, int buf_len,
double dbl_val) {
constchar size[] = {'\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'};
unsignedint i;
for (i = 0; dbl_val > 1024 && i < sizeof(size) - 1; i++) dbl_val /= 1024;
constchar mult = size[i];
// 18446744073709551615 Yottabytes should be enough for most ...
// ULLONG_MAX is not exactly representable as a double. This is the largest
// double that is still below ULLONG_MAX.
if (dbl_val > 18446744073709549568.0)
snprintf(buf, buf_len, "+INF");
else
snprintf(buf, buf_len, "%llu%c", (unsignedlonglong)dbl_val, mult);
}
staticinlinevoidlex_string_set(LEX_STRING *lex_str, char *c_str) {
lex_str->str = c_str;
lex_str->length = strlen(c_str);
}
staticinlinevoidlex_cstring_set(LEX_CSTRING *lex_str, constchar *c_str) {
lex_str->str = c_str;
lex_str->length = strlen(c_str);
}
#endif// M_STRING_INCLUDED