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