1 /*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef FLATBUFFERS_UTIL_H_
18 #define FLATBUFFERS_UTIL_H_
19
20 #include <ctype.h>
21 #include <errno.h>
22
23 #include "flatbuffers/base.h"
24 #include "flatbuffers/stl_emulation.h"
25
26 #ifndef FLATBUFFERS_PREFER_PRINTF
27 # include <iomanip>
28 # include <sstream>
29 #else // FLATBUFFERS_PREFER_PRINTF
30 # include <float.h>
31 # include <stdio.h>
32 #endif // FLATBUFFERS_PREFER_PRINTF
33
34 #include <string>
35
36 namespace flatbuffers {
37
38 // @locale-independent functions for ASCII characters set.
39
40 // Fast checking that character lies in closed range: [a <= x <= b]
41 // using one compare (conditional branch) operator.
check_ascii_range(char x,char a,char b)42 inline bool check_ascii_range(char x, char a, char b) {
43 FLATBUFFERS_ASSERT(a <= b);
44 // (Hacker's Delight): `a <= x <= b` <=> `(x-a) <={u} (b-a)`.
45 // The x, a, b will be promoted to int and subtracted without overflow.
46 return static_cast<unsigned int>(x - a) <= static_cast<unsigned int>(b - a);
47 }
48
49 // Case-insensitive isalpha
is_alpha(char c)50 inline bool is_alpha(char c) {
51 // ASCII only: alpha to upper case => reset bit 0x20 (~0x20 = 0xDF).
52 return check_ascii_range(c & 0xDF, 'a' & 0xDF, 'z' & 0xDF);
53 }
54
55 // Check for uppercase alpha
is_alpha_upper(char c)56 inline bool is_alpha_upper(char c) { return check_ascii_range(c, 'A', 'Z'); }
57
58 // Check (case-insensitive) that `c` is equal to alpha.
is_alpha_char(char c,char alpha)59 inline bool is_alpha_char(char c, char alpha) {
60 FLATBUFFERS_ASSERT(is_alpha(alpha));
61 // ASCII only: alpha to upper case => reset bit 0x20 (~0x20 = 0xDF).
62 return ((c & 0xDF) == (alpha & 0xDF));
63 }
64
65 // https://en.cppreference.com/w/cpp/string/byte/isxdigit
66 // isdigit and isxdigit are the only standard narrow character classification
67 // functions that are not affected by the currently installed C locale. although
68 // some implementations (e.g. Microsoft in 1252 codepage) may classify
69 // additional single-byte characters as digits.
is_digit(char c)70 inline bool is_digit(char c) { return check_ascii_range(c, '0', '9'); }
71
is_xdigit(char c)72 inline bool is_xdigit(char c) {
73 // Replace by look-up table.
74 return is_digit(c) || check_ascii_range(c & 0xDF, 'a' & 0xDF, 'f' & 0xDF);
75 }
76
77 // Case-insensitive isalnum
is_alnum(char c)78 inline bool is_alnum(char c) { return is_alpha(c) || is_digit(c); }
79
CharToUpper(char c)80 inline char CharToUpper(char c) {
81 return static_cast<char>(::toupper(static_cast<unsigned char>(c)));
82 }
83
CharToLower(char c)84 inline char CharToLower(char c) {
85 return static_cast<char>(::tolower(static_cast<unsigned char>(c)));
86 }
87
88 // @end-locale-independent functions for ASCII character set
89
90 #ifdef FLATBUFFERS_PREFER_PRINTF
IntToDigitCount(T t)91 template<typename T> size_t IntToDigitCount(T t) {
92 size_t digit_count = 0;
93 // Count the sign for negative numbers
94 if (t < 0) digit_count++;
95 // Count a single 0 left of the dot for fractional numbers
96 if (-1 < t && t < 1) digit_count++;
97 // Count digits until fractional part
98 T eps = std::numeric_limits<T>::epsilon();
99 while (t <= (-1 + eps) || (1 - eps) <= t) {
100 t /= 10;
101 digit_count++;
102 }
103 return digit_count;
104 }
105
106 template<typename T> size_t NumToStringWidth(T t, int precision = 0) {
107 size_t string_width = IntToDigitCount(t);
108 // Count the dot for floating point numbers
109 if (precision) string_width += (precision + 1);
110 return string_width;
111 }
112
113 template<typename T>
114 std::string NumToStringImplWrapper(T t, const char *fmt, int precision = 0) {
115 size_t string_width = NumToStringWidth(t, precision);
116 std::string s(string_width, 0x00);
117 // Allow snprintf to use std::string trailing null to detect buffer overflow
118 snprintf(const_cast<char *>(s.data()), (s.size() + 1), fmt, string_width, t);
119 return s;
120 }
121 #endif // FLATBUFFERS_PREFER_PRINTF
122
123 // Convert an integer or floating point value to a string.
124 // In contrast to std::stringstream, "char" values are
125 // converted to a string of digits, and we don't use scientific notation.
NumToString(T t)126 template<typename T> std::string NumToString(T t) {
127 // clang-format off
128
129 #ifndef FLATBUFFERS_PREFER_PRINTF
130 std::stringstream ss;
131 ss << t;
132 return ss.str();
133 #else // FLATBUFFERS_PREFER_PRINTF
134 auto v = static_cast<long long>(t);
135 return NumToStringImplWrapper(v, "%.*lld");
136 #endif // FLATBUFFERS_PREFER_PRINTF
137 // clang-format on
138 }
139 // Avoid char types used as character data.
140 template<> inline std::string NumToString<signed char>(signed char t) {
141 return NumToString(static_cast<int>(t));
142 }
143 template<> inline std::string NumToString<unsigned char>(unsigned char t) {
144 return NumToString(static_cast<int>(t));
145 }
146 template<> inline std::string NumToString<char>(char t) {
147 return NumToString(static_cast<int>(t));
148 }
149
150 // Special versions for floats/doubles.
FloatToString(T t,int precision)151 template<typename T> std::string FloatToString(T t, int precision) {
152 // clang-format off
153
154 #ifndef FLATBUFFERS_PREFER_PRINTF
155 // to_string() prints different numbers of digits for floats depending on
156 // platform and isn't available on Android, so we use stringstream
157 std::stringstream ss;
158 // Use std::fixed to suppress scientific notation.
159 ss << std::fixed;
160 // Default precision is 6, we want that to be higher for doubles.
161 ss << std::setprecision(precision);
162 ss << t;
163 auto s = ss.str();
164 #else // FLATBUFFERS_PREFER_PRINTF
165 auto v = static_cast<double>(t);
166 auto s = NumToStringImplWrapper(v, "%0.*f", precision);
167 #endif // FLATBUFFERS_PREFER_PRINTF
168 // clang-format on
169 // Sadly, std::fixed turns "1" into "1.00000", so here we undo that.
170 auto p = s.find_last_not_of('0');
171 if (p != std::string::npos) {
172 // Strip trailing zeroes. If it is a whole number, keep one zero.
173 s.resize(p + (s[p] == '.' ? 2 : 1));
174 }
175 return s;
176 }
177
178 template<> inline std::string NumToString<double>(double t) {
179 return FloatToString(t, 12);
180 }
181 template<> inline std::string NumToString<float>(float t) {
182 return FloatToString(t, 6);
183 }
184
185 // Convert an integer value to a hexadecimal string.
186 // The returned string length is always xdigits long, prefixed by 0 digits.
187 // For example, IntToStringHex(0x23, 8) returns the string "00000023".
IntToStringHex(int i,int xdigits)188 inline std::string IntToStringHex(int i, int xdigits) {
189 FLATBUFFERS_ASSERT(i >= 0);
190 // clang-format off
191
192 #ifndef FLATBUFFERS_PREFER_PRINTF
193 std::stringstream ss;
194 ss << std::setw(xdigits) << std::setfill('0') << std::hex << std::uppercase
195 << i;
196 return ss.str();
197 #else // FLATBUFFERS_PREFER_PRINTF
198 return NumToStringImplWrapper(i, "%.*X", xdigits);
199 #endif // FLATBUFFERS_PREFER_PRINTF
200 // clang-format on
201 }
202
203 // clang-format off
204 // Use locale independent functions {strtod_l, strtof_l, strtoll_l, strtoull_l}.
205 #if defined(FLATBUFFERS_LOCALE_INDEPENDENT) && (FLATBUFFERS_LOCALE_INDEPENDENT > 0)
206 class ClassicLocale {
207 #ifdef _MSC_VER
208 typedef _locale_t locale_type;
209 #else
210 typedef locale_t locale_type; // POSIX.1-2008 locale_t type
211 #endif
212 ClassicLocale();
213 ~ClassicLocale();
214 locale_type locale_;
215 static ClassicLocale instance_;
216 public:
Get()217 static locale_type Get() { return instance_.locale_; }
218 };
219
220 #ifdef _MSC_VER
221 #define __strtoull_impl(s, pe, b) _strtoui64_l(s, pe, b, ClassicLocale::Get())
222 #define __strtoll_impl(s, pe, b) _strtoi64_l(s, pe, b, ClassicLocale::Get())
223 #define __strtod_impl(s, pe) _strtod_l(s, pe, ClassicLocale::Get())
224 #define __strtof_impl(s, pe) _strtof_l(s, pe, ClassicLocale::Get())
225 #else
226 #define __strtoull_impl(s, pe, b) strtoull_l(s, pe, b, ClassicLocale::Get())
227 #define __strtoll_impl(s, pe, b) strtoll_l(s, pe, b, ClassicLocale::Get())
228 #define __strtod_impl(s, pe) strtod_l(s, pe, ClassicLocale::Get())
229 #define __strtof_impl(s, pe) strtof_l(s, pe, ClassicLocale::Get())
230 #endif
231 #else
232 #define __strtod_impl(s, pe) strtod(s, pe)
233 #define __strtof_impl(s, pe) static_cast<float>(strtod(s, pe))
234 #ifdef _MSC_VER
235 #define __strtoull_impl(s, pe, b) _strtoui64(s, pe, b)
236 #define __strtoll_impl(s, pe, b) _strtoi64(s, pe, b)
237 #else
238 #define __strtoull_impl(s, pe, b) strtoull(s, pe, b)
239 #define __strtoll_impl(s, pe, b) strtoll(s, pe, b)
240 #endif
241 #endif
242
strtoval_impl(int64_t * val,const char * str,char ** endptr,int base)243 inline void strtoval_impl(int64_t *val, const char *str, char **endptr,
244 int base) {
245 *val = __strtoll_impl(str, endptr, base);
246 }
247
strtoval_impl(uint64_t * val,const char * str,char ** endptr,int base)248 inline void strtoval_impl(uint64_t *val, const char *str, char **endptr,
249 int base) {
250 *val = __strtoull_impl(str, endptr, base);
251 }
252
strtoval_impl(double * val,const char * str,char ** endptr)253 inline void strtoval_impl(double *val, const char *str, char **endptr) {
254 *val = __strtod_impl(str, endptr);
255 }
256
257 // UBSAN: double to float is safe if numeric_limits<float>::is_iec559 is true.
258 __supress_ubsan__("float-cast-overflow")
strtoval_impl(float * val,const char * str,char ** endptr)259 inline void strtoval_impl(float *val, const char *str, char **endptr) {
260 *val = __strtof_impl(str, endptr);
261 }
262 #undef __strtoull_impl
263 #undef __strtoll_impl
264 #undef __strtod_impl
265 #undef __strtof_impl
266 // clang-format on
267
268 // Adaptor for strtoull()/strtoll().
269 // Flatbuffers accepts numbers with any count of leading zeros (-009 is -9),
270 // while strtoll with base=0 interprets first leading zero as octal prefix.
271 // In future, it is possible to add prefixed 0b0101.
272 // 1) Checks errno code for overflow condition (out of range).
273 // 2) If base <= 0, function try to detect base of number by prefix.
274 //
275 // Return value (like strtoull and strtoll, but reject partial result):
276 // - If successful, an integer value corresponding to the str is returned.
277 // - If full string conversion can't be performed, 0 is returned.
278 // - If the converted value falls out of range of corresponding return type, a
279 // range error occurs. In this case value MAX(T)/MIN(T) is returned.
280 template<typename T>
281 inline bool StringToIntegerImpl(T *val, const char *const str,
282 const int base = 0,
283 const bool check_errno = true) {
284 // T is int64_t or uint64_T
285 FLATBUFFERS_ASSERT(str);
286 if (base <= 0) {
287 auto s = str;
288 while (*s && !is_digit(*s)) s++;
289 if (s[0] == '0' && is_alpha_char(s[1], 'X'))
290 return StringToIntegerImpl(val, str, 16, check_errno);
291 // if a prefix not match, try base=10
292 return StringToIntegerImpl(val, str, 10, check_errno);
293 } else {
294 if (check_errno) errno = 0; // clear thread-local errno
295 auto endptr = str;
296 strtoval_impl(val, str, const_cast<char **>(&endptr), base);
297 if ((*endptr != '\0') || (endptr == str)) {
298 *val = 0; // erase partial result
299 return false; // invalid string
300 }
301 // errno is out-of-range, return MAX/MIN
302 if (check_errno && errno) return false;
303 return true;
304 }
305 }
306
307 template<typename T>
StringToFloatImpl(T * val,const char * const str)308 inline bool StringToFloatImpl(T *val, const char *const str) {
309 // Type T must be either float or double.
310 FLATBUFFERS_ASSERT(str && val);
311 auto end = str;
312 strtoval_impl(val, str, const_cast<char **>(&end));
313 auto done = (end != str) && (*end == '\0');
314 if (!done) *val = 0; // erase partial result
315 return done;
316 }
317
318 // Convert a string to an instance of T.
319 // Return value (matched with StringToInteger64Impl and strtod):
320 // - If successful, a numeric value corresponding to the str is returned.
321 // - If full string conversion can't be performed, 0 is returned.
322 // - If the converted value falls out of range of corresponding return type, a
323 // range error occurs. In this case value MAX(T)/MIN(T) is returned.
StringToNumber(const char * s,T * val)324 template<typename T> inline bool StringToNumber(const char *s, T *val) {
325 // Assert on `unsigned long` and `signed long` on LP64.
326 // If it is necessary, it could be solved with flatbuffers::enable_if<B,T>.
327 static_assert(sizeof(T) < sizeof(int64_t), "unexpected type T");
328 FLATBUFFERS_ASSERT(s && val);
329 int64_t i64;
330 // The errno check isn't needed, will return MAX/MIN on overflow.
331 if (StringToIntegerImpl(&i64, s, 0, false)) {
332 const int64_t max = (flatbuffers::numeric_limits<T>::max)();
333 const int64_t min = flatbuffers::numeric_limits<T>::lowest();
334 if (i64 > max) {
335 *val = static_cast<T>(max);
336 return false;
337 }
338 if (i64 < min) {
339 // For unsigned types return max to distinguish from
340 // "no conversion can be performed" when 0 is returned.
341 *val = static_cast<T>(flatbuffers::is_unsigned<T>::value ? max : min);
342 return false;
343 }
344 *val = static_cast<T>(i64);
345 return true;
346 }
347 *val = 0;
348 return false;
349 }
350
351 template<> inline bool StringToNumber<int64_t>(const char *str, int64_t *val) {
352 return StringToIntegerImpl(val, str);
353 }
354
355 template<>
356 inline bool StringToNumber<uint64_t>(const char *str, uint64_t *val) {
357 if (!StringToIntegerImpl(val, str)) return false;
358 // The strtoull accepts negative numbers:
359 // If the minus sign was part of the input sequence, the numeric value
360 // calculated from the sequence of digits is negated as if by unary minus
361 // in the result type, which applies unsigned integer wraparound rules.
362 // Fix this behaviour (except -0).
363 if (*val) {
364 auto s = str;
365 while (*s && !is_digit(*s)) s++;
366 s = (s > str) ? (s - 1) : s; // step back to one symbol
367 if (*s == '-') {
368 // For unsigned types return the max to distinguish from
369 // "no conversion can be performed".
370 *val = (flatbuffers::numeric_limits<uint64_t>::max)();
371 return false;
372 }
373 }
374 return true;
375 }
376
StringToNumber(const char * s,float * val)377 template<> inline bool StringToNumber(const char *s, float *val) {
378 return StringToFloatImpl(val, s);
379 }
380
StringToNumber(const char * s,double * val)381 template<> inline bool StringToNumber(const char *s, double *val) {
382 return StringToFloatImpl(val, s);
383 }
384
385 inline int64_t StringToInt(const char *s, int base = 10) {
386 int64_t val;
387 return StringToIntegerImpl(&val, s, base) ? val : 0;
388 }
389
390 inline uint64_t StringToUInt(const char *s, int base = 10) {
391 uint64_t val;
392 return StringToIntegerImpl(&val, s, base) ? val : 0;
393 }
394
395 typedef bool (*LoadFileFunction)(const char *filename, bool binary,
396 std::string *dest);
397 typedef bool (*FileExistsFunction)(const char *filename);
398
399 LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function);
400
401 FileExistsFunction SetFileExistsFunction(
402 FileExistsFunction file_exists_function);
403
404 // Check if file "name" exists.
405 bool FileExists(const char *name);
406
407 // Check if "name" exists and it is also a directory.
408 bool DirExists(const char *name);
409
410 // Load file "name" into "buf" returning true if successful
411 // false otherwise. If "binary" is false data is read
412 // using ifstream's text mode, otherwise data is read with
413 // no transcoding.
414 bool LoadFile(const char *name, bool binary, std::string *buf);
415
416 // Save data "buf" of length "len" bytes into a file
417 // "name" returning true if successful, false otherwise.
418 // If "binary" is false data is written using ifstream's
419 // text mode, otherwise data is written with no
420 // transcoding.
421 bool SaveFile(const char *name, const char *buf, size_t len, bool binary);
422
423 // Save data "buf" into file "name" returning true if
424 // successful, false otherwise. If "binary" is false
425 // data is written using ifstream's text mode, otherwise
426 // data is written with no transcoding.
SaveFile(const char * name,const std::string & buf,bool binary)427 inline bool SaveFile(const char *name, const std::string &buf, bool binary) {
428 return SaveFile(name, buf.c_str(), buf.size(), binary);
429 }
430
431 // Functionality for minimalistic portable path handling.
432
433 // The functions below behave correctly regardless of whether posix ('/') or
434 // Windows ('/' or '\\') separators are used.
435
436 // Any new separators inserted are always posix.
437 FLATBUFFERS_CONSTEXPR char kPathSeparator = '/';
438
439 // Returns the path with the extension, if any, removed.
440 std::string StripExtension(const std::string &filepath);
441
442 // Returns the extension, if any.
443 std::string GetExtension(const std::string &filepath);
444
445 // Return the last component of the path, after the last separator.
446 std::string StripPath(const std::string &filepath);
447
448 // Strip the last component of the path + separator.
449 std::string StripFileName(const std::string &filepath);
450
451 std::string StripPrefix(const std::string &filepath,
452 const std::string &prefix_to_remove);
453
454 // Concatenates a path with a filename, regardless of whether the path
455 // ends in a separator or not.
456 std::string ConCatPathFileName(const std::string &path,
457 const std::string &filename);
458
459 // Replaces any '\\' separators with '/'
460 std::string PosixPath(const char *path);
461 std::string PosixPath(const std::string &path);
462
463 // This function ensure a directory exists, by recursively
464 // creating dirs for any parts of the path that don't exist yet.
465 void EnsureDirExists(const std::string &filepath);
466
467 // Obtains the absolute path from any other path.
468 // Returns the input path if the absolute path couldn't be resolved.
469 std::string AbsolutePath(const std::string &filepath);
470
471 // Returns files relative to the --project_root path, prefixed with `//`.
472 std::string RelativeToRootPath(const std::string &project,
473 const std::string &filepath);
474
475 // To and from UTF-8 unicode conversion functions
476
477 // Convert a unicode code point into a UTF-8 representation by appending it
478 // to a string. Returns the number of bytes generated.
ToUTF8(uint32_t ucc,std::string * out)479 inline int ToUTF8(uint32_t ucc, std::string *out) {
480 FLATBUFFERS_ASSERT(!(ucc & 0x80000000)); // Top bit can't be set.
481 // 6 possible encodings: http://en.wikipedia.org/wiki/UTF-8
482 for (int i = 0; i < 6; i++) {
483 // Max bits this encoding can represent.
484 uint32_t max_bits = 6 + i * 5 + static_cast<int>(!i);
485 if (ucc < (1u << max_bits)) { // does it fit?
486 // Remaining bits not encoded in the first byte, store 6 bits each
487 uint32_t remain_bits = i * 6;
488 // Store first byte:
489 (*out) += static_cast<char>((0xFE << (max_bits - remain_bits)) |
490 (ucc >> remain_bits));
491 // Store remaining bytes:
492 for (int j = i - 1; j >= 0; j--) {
493 (*out) += static_cast<char>(((ucc >> (j * 6)) & 0x3F) | 0x80);
494 }
495 return i + 1; // Return the number of bytes added.
496 }
497 }
498 FLATBUFFERS_ASSERT(0); // Impossible to arrive here.
499 return -1;
500 }
501
502 // Converts whatever prefix of the incoming string corresponds to a valid
503 // UTF-8 sequence into a unicode code. The incoming pointer will have been
504 // advanced past all bytes parsed.
505 // returns -1 upon corrupt UTF-8 encoding (ignore the incoming pointer in
506 // this case).
FromUTF8(const char ** in)507 inline int FromUTF8(const char **in) {
508 int len = 0;
509 // Count leading 1 bits.
510 for (int mask = 0x80; mask >= 0x04; mask >>= 1) {
511 if (**in & mask) {
512 len++;
513 } else {
514 break;
515 }
516 }
517 if ((static_cast<unsigned char>(**in) << len) & 0x80)
518 return -1; // Bit after leading 1's must be 0.
519 if (!len) return *(*in)++;
520 // UTF-8 encoded values with a length are between 2 and 4 bytes.
521 if (len < 2 || len > 4) { return -1; }
522 // Grab initial bits of the code.
523 int ucc = *(*in)++ & ((1 << (7 - len)) - 1);
524 for (int i = 0; i < len - 1; i++) {
525 if ((**in & 0xC0) != 0x80) return -1; // Upper bits must 1 0.
526 ucc <<= 6;
527 ucc |= *(*in)++ & 0x3F; // Grab 6 more bits of the code.
528 }
529 // UTF-8 cannot encode values between 0xD800 and 0xDFFF (reserved for
530 // UTF-16 surrogate pairs).
531 if (ucc >= 0xD800 && ucc <= 0xDFFF) { return -1; }
532 // UTF-8 must represent code points in their shortest possible encoding.
533 switch (len) {
534 case 2:
535 // Two bytes of UTF-8 can represent code points from U+0080 to U+07FF.
536 if (ucc < 0x0080 || ucc > 0x07FF) { return -1; }
537 break;
538 case 3:
539 // Three bytes of UTF-8 can represent code points from U+0800 to U+FFFF.
540 if (ucc < 0x0800 || ucc > 0xFFFF) { return -1; }
541 break;
542 case 4:
543 // Four bytes of UTF-8 can represent code points from U+10000 to U+10FFFF.
544 if (ucc < 0x10000 || ucc > 0x10FFFF) { return -1; }
545 break;
546 }
547 return ucc;
548 }
549
550 #ifndef FLATBUFFERS_PREFER_PRINTF
551 // Wraps a string to a maximum length, inserting new lines where necessary. Any
552 // existing whitespace will be collapsed down to a single space. A prefix or
553 // suffix can be provided, which will be inserted before or after a wrapped
554 // line, respectively.
WordWrap(const std::string in,size_t max_length,const std::string wrapped_line_prefix,const std::string wrapped_line_suffix)555 inline std::string WordWrap(const std::string in, size_t max_length,
556 const std::string wrapped_line_prefix,
557 const std::string wrapped_line_suffix) {
558 std::istringstream in_stream(in);
559 std::string wrapped, line, word;
560
561 in_stream >> word;
562 line = word;
563
564 while (in_stream >> word) {
565 if ((line.length() + 1 + word.length() + wrapped_line_suffix.length()) <
566 max_length) {
567 line += " " + word;
568 } else {
569 wrapped += line + wrapped_line_suffix + "\n";
570 line = wrapped_line_prefix + word;
571 }
572 }
573 wrapped += line;
574
575 return wrapped;
576 }
577 #endif // !FLATBUFFERS_PREFER_PRINTF
578
EscapeString(const char * s,size_t length,std::string * _text,bool allow_non_utf8,bool natural_utf8)579 inline bool EscapeString(const char *s, size_t length, std::string *_text,
580 bool allow_non_utf8, bool natural_utf8) {
581 std::string &text = *_text;
582 text += "\"";
583 for (uoffset_t i = 0; i < length; i++) {
584 char c = s[i];
585 switch (c) {
586 case '\n': text += "\\n"; break;
587 case '\t': text += "\\t"; break;
588 case '\r': text += "\\r"; break;
589 case '\b': text += "\\b"; break;
590 case '\f': text += "\\f"; break;
591 case '\"': text += "\\\""; break;
592 case '\\': text += "\\\\"; break;
593 default:
594 if (c >= ' ' && c <= '~') {
595 text += c;
596 } else {
597 // Not printable ASCII data. Let's see if it's valid UTF-8 first:
598 const char *utf8 = s + i;
599 int ucc = FromUTF8(&utf8);
600 if (ucc < 0) {
601 if (allow_non_utf8) {
602 text += "\\x";
603 text += IntToStringHex(static_cast<uint8_t>(c), 2);
604 } else {
605 // There are two cases here:
606 //
607 // 1) We reached here by parsing an IDL file. In that case,
608 // we previously checked for non-UTF-8, so we shouldn't reach
609 // here.
610 //
611 // 2) We reached here by someone calling GenerateText()
612 // on a previously-serialized flatbuffer. The data might have
613 // non-UTF-8 Strings, or might be corrupt.
614 //
615 // In both cases, we have to give up and inform the caller
616 // they have no JSON.
617 return false;
618 }
619 } else {
620 if (natural_utf8) {
621 // utf8 points to past all utf-8 bytes parsed
622 text.append(s + i, static_cast<size_t>(utf8 - s - i));
623 } else if (ucc <= 0xFFFF) {
624 // Parses as Unicode within JSON's \uXXXX range, so use that.
625 text += "\\u";
626 text += IntToStringHex(ucc, 4);
627 } else if (ucc <= 0x10FFFF) {
628 // Encode Unicode SMP values to a surrogate pair using two \u
629 // escapes.
630 uint32_t base = ucc - 0x10000;
631 auto high_surrogate = (base >> 10) + 0xD800;
632 auto low_surrogate = (base & 0x03FF) + 0xDC00;
633 text += "\\u";
634 text += IntToStringHex(high_surrogate, 4);
635 text += "\\u";
636 text += IntToStringHex(low_surrogate, 4);
637 }
638 // Skip past characters recognized.
639 i = static_cast<uoffset_t>(utf8 - s - 1);
640 }
641 }
642 break;
643 }
644 }
645 text += "\"";
646 return true;
647 }
648
BufferToHexText(const void * buffer,size_t buffer_size,size_t max_length,const std::string & wrapped_line_prefix,const std::string & wrapped_line_suffix)649 inline std::string BufferToHexText(const void *buffer, size_t buffer_size,
650 size_t max_length,
651 const std::string &wrapped_line_prefix,
652 const std::string &wrapped_line_suffix) {
653 std::string text = wrapped_line_prefix;
654 size_t start_offset = 0;
655 const char *s = reinterpret_cast<const char *>(buffer);
656 for (size_t i = 0; s && i < buffer_size; i++) {
657 // Last iteration or do we have more?
658 bool have_more = i + 1 < buffer_size;
659 text += "0x";
660 text += IntToStringHex(static_cast<uint8_t>(s[i]), 2);
661 if (have_more) { text += ','; }
662 // If we have more to process and we reached max_length
663 if (have_more &&
664 text.size() + wrapped_line_suffix.size() >= start_offset + max_length) {
665 text += wrapped_line_suffix;
666 text += '\n';
667 start_offset = text.size();
668 text += wrapped_line_prefix;
669 }
670 }
671 text += wrapped_line_suffix;
672 return text;
673 }
674
675 // Remove paired quotes in a string: "text"|'text' -> text.
676 std::string RemoveStringQuotes(const std::string &s);
677
678 // Change th global C-locale to locale with name <locale_name>.
679 // Returns an actual locale name in <_value>, useful if locale_name is "" or
680 // null.
681 bool SetGlobalTestLocale(const char *locale_name,
682 std::string *_value = nullptr);
683
684 // Read (or test) a value of environment variable.
685 bool ReadEnvironmentVariable(const char *var_name,
686 std::string *_value = nullptr);
687
688 // MSVC specific: Send all assert reports to STDOUT to prevent CI hangs.
689 void SetupDefaultCRTReportMode();
690
691 enum class Case {
692 kUnknown = 0,
693 // TheQuickBrownFox
694 kUpperCamel = 1,
695 // theQuickBrownFox
696 kLowerCamel = 2,
697 // the_quick_brown_fox
698 kSnake = 3,
699 // THE_QUICK_BROWN_FOX
700 kScreamingSnake = 4,
701 // THEQUICKBROWNFOX
702 kAllUpper = 5,
703 // thequickbrownfox
704 kAllLower = 6,
705 // the-quick-brown-fox
706 kDasher = 7,
707 // THEQuiCKBr_ownFox (or whatever you want, we won't change it)
708 kKeep = 8,
709 // the_quick_brown_fox123 (as opposed to the_quick_brown_fox_123)
710 kSnake2 = 9,
711 };
712
713 // Convert the `input` string of case `input_case` to the specified `output_case`.
714 std::string ConvertCase(const std::string &input, Case output_case,
715 Case input_case = Case::kSnake);
716
717 } // namespace flatbuffers
718
719 #endif // FLATBUFFERS_UTIL_H_
720