2 * Copyright 2014 Google Inc. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FLATBUFFERS_UTIL_H_
18 #define FLATBUFFERS_UTIL_H_
22 #include "flatbuffers/base.h"
24 #ifndef FLATBUFFERS_PREFER_PRINTF
26 #else // FLATBUFFERS_PREFER_PRINTF
29 #endif // FLATBUFFERS_PREFER_PRINTF
34 namespace flatbuffers {
36 // @locale-independent functions for ASCII characters set.
38 // Fast checking that character lies in closed range: [a <= x <= b]
39 // using one compare (conditional branch) operator.
40 inline bool check_ascii_range(char x, char a, char b) {
41 FLATBUFFERS_ASSERT(a <= b);
42 // (Hacker's Delight): `a <= x <= b` <=> `(x-a) <={u} (b-a)`.
43 // The x, a, b will be promoted to int and subtracted without overflow.
44 return static_cast<unsigned int>(x - a) <= static_cast<unsigned int>(b - a);
47 // Case-insensitive isalpha
48 inline bool is_alpha(char c) {
49 // ASCII only: alpha to upper case => reset bit 0x20 (~0x20 = 0xDF).
50 return check_ascii_range(c & 0xDF, 'a' & 0xDF, 'z' & 0xDF);
53 // Check (case-insensitive) that `c` is equal to alpha.
54 inline bool is_alpha_char(char c, char alpha) {
55 FLATBUFFERS_ASSERT(is_alpha(alpha));
56 // ASCII only: alpha to upper case => reset bit 0x20 (~0x20 = 0xDF).
57 return ((c & 0xDF) == (alpha & 0xDF));
60 // https://en.cppreference.com/w/cpp/string/byte/isxdigit
61 // isdigit and isxdigit are the only standard narrow character classification
62 // functions that are not affected by the currently installed C locale. although
63 // some implementations (e.g. Microsoft in 1252 codepage) may classify
64 // additional single-byte characters as digits.
65 inline bool is_digit(char c) { return check_ascii_range(c, '0', '9'); }
67 inline bool is_xdigit(char c) {
68 // Replace by look-up table.
69 return is_digit(c) || check_ascii_range(c & 0xDF, 'a' & 0xDF, 'f' & 0xDF);
72 // Case-insensitive isalnum
73 inline bool is_alnum(char c) { return is_alpha(c) || is_digit(c); }
75 // @end-locale-independent functions for ASCII character set
77 #ifdef FLATBUFFERS_PREFER_PRINTF
78 template<typename T> size_t IntToDigitCount(T t) {
79 size_t digit_count = 0;
80 // Count the sign for negative numbers
81 if (t < 0) digit_count++;
82 // Count a single 0 left of the dot for fractional numbers
83 if (-1 < t && t < 1) digit_count++;
84 // Count digits until fractional part
85 T eps = std::numeric_limits<float>::epsilon();
86 while (t <= (-1 + eps) || (1 - eps) <= t) {
93 template<typename T> size_t NumToStringWidth(T t, int precision = 0) {
94 size_t string_width = IntToDigitCount(t);
95 // Count the dot for floating point numbers
96 if (precision) string_width += (precision + 1);
101 std::string NumToStringImplWrapper(T t, const char *fmt, int precision = 0) {
102 size_t string_width = NumToStringWidth(t, precision);
103 std::string s(string_width, 0x00);
104 // Allow snprintf to use std::string trailing null to detect buffer overflow
105 snprintf(const_cast<char *>(s.data()), (s.size() + 1), fmt, string_width, t);
108 #endif // FLATBUFFERS_PREFER_PRINTF
110 // Convert an integer or floating point value to a string.
111 // In contrast to std::stringstream, "char" values are
112 // converted to a string of digits, and we don't use scientific notation.
113 template<typename T> std::string NumToString(T t) {
116 #ifndef FLATBUFFERS_PREFER_PRINTF
117 std::stringstream ss;
120 #else // FLATBUFFERS_PREFER_PRINTF
121 auto v = static_cast<long long>(t);
122 return NumToStringImplWrapper(v, "%.*lld");
123 #endif // FLATBUFFERS_PREFER_PRINTF
126 // Avoid char types used as character data.
127 template<> inline std::string NumToString<signed char>(signed char t) {
128 return NumToString(static_cast<int>(t));
130 template<> inline std::string NumToString<unsigned char>(unsigned char t) {
131 return NumToString(static_cast<int>(t));
133 template<> inline std::string NumToString<char>(char t) {
134 return NumToString(static_cast<int>(t));
136 #if defined(FLATBUFFERS_CPP98_STL)
137 template<> inline std::string NumToString<long long>(long long t) {
138 char buf[21]; // (log((1 << 63) - 1) / log(10)) + 2
139 snprintf(buf, sizeof(buf), "%lld", t);
140 return std::string(buf);
144 inline std::string NumToString<unsigned long long>(unsigned long long t) {
145 char buf[22]; // (log((1 << 63) - 1) / log(10)) + 1
146 snprintf(buf, sizeof(buf), "%llu", t);
147 return std::string(buf);
149 #endif // defined(FLATBUFFERS_CPP98_STL)
151 // Special versions for floats/doubles.
152 template<typename T> std::string FloatToString(T t, int precision) {
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.
161 // Default precision is 6, we want that to be higher for doubles.
162 ss << std::setprecision(precision);
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
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));
179 template<> inline std::string NumToString<double>(double t) {
180 return FloatToString(t, 12);
182 template<> inline std::string NumToString<float>(float t) {
183 return FloatToString(t, 6);
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".
189 inline std::string IntToStringHex(int i, int xdigits) {
190 FLATBUFFERS_ASSERT(i >= 0);
193 #ifndef FLATBUFFERS_PREFER_PRINTF
194 std::stringstream ss;
195 ss << std::setw(xdigits) << std::setfill('0') << std::hex << std::uppercase
198 #else // FLATBUFFERS_PREFER_PRINTF
199 return NumToStringImplWrapper(i, "%.*X", xdigits);
200 #endif // FLATBUFFERS_PREFER_PRINTF
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 {
209 typedef _locale_t locale_type;
211 typedef locale_t locale_type; // POSIX.1-2008 locale_t type
216 static ClassicLocale instance_;
218 static locale_type Get() { return instance_.locale_; }
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())
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())
233 #define __strtod_impl(s, pe) strtod(s, pe)
234 #define __strtof_impl(s, pe) static_cast<float>(strtod(s, pe))
236 #define __strtoull_impl(s, pe, b) _strtoui64(s, pe, b)
237 #define __strtoll_impl(s, pe, b) _strtoi64(s, pe, b)
239 #define __strtoull_impl(s, pe, b) strtoull(s, pe, b)
240 #define __strtoll_impl(s, pe, b) strtoll(s, pe, b)
244 inline void strtoval_impl(int64_t *val, const char *str, char **endptr,
246 *val = __strtoll_impl(str, endptr, base);
249 inline void strtoval_impl(uint64_t *val, const char *str, char **endptr,
251 *val = __strtoull_impl(str, endptr, base);
254 inline void strtoval_impl(double *val, const char *str, char **endptr) {
255 *val = __strtod_impl(str, endptr);
258 // UBSAN: double to float is safe if numeric_limits<float>::is_iec559 is true.
259 __supress_ubsan__("float-cast-overflow")
260 inline void strtoval_impl(float *val, const char *str, char **endptr) {
261 *val = __strtof_impl(str, endptr);
263 #undef __strtoull_impl
264 #undef __strtoll_impl
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.
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.
282 inline bool StringToIntegerImpl(T *val, const char *const str,
284 const bool check_errno = true) {
285 // T is int64_t or uint64_T
286 FLATBUFFERS_ASSERT(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);
295 if (check_errno) errno = 0; // clear thread-local errno
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
302 // errno is out-of-range, return MAX/MIN
303 if (check_errno && errno) return false;
309 inline bool StringToFloatImpl(T *val, const char *const str) {
310 // Type T must be either float or double.
311 FLATBUFFERS_ASSERT(str && val);
313 strtoval_impl(val, str, const_cast<char **>(&end));
314 auto done = (end != str) && (*end == '\0');
315 if (!done) *val = 0; // erase partial result
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.
325 template<typename T> inline bool StringToNumber(const char *s, T *val) {
326 FLATBUFFERS_ASSERT(s && val);
328 // The errno check isn't needed, will return MAX/MIN on overflow.
329 if (StringToIntegerImpl(&i64, s, 0, false)) {
330 const int64_t max = (flatbuffers::numeric_limits<T>::max)();
331 const int64_t min = flatbuffers::numeric_limits<T>::lowest();
333 *val = static_cast<T>(max);
337 // For unsigned types return max to distinguish from
338 // "no conversion can be performed" when 0 is returned.
339 *val = static_cast<T>(flatbuffers::is_unsigned<T>::value ? max : min);
342 *val = static_cast<T>(i64);
349 template<> inline bool StringToNumber<int64_t>(const char *str, int64_t *val) {
350 return StringToIntegerImpl(val, str);
354 inline bool StringToNumber<uint64_t>(const char *str, uint64_t *val) {
355 if (!StringToIntegerImpl(val, str)) return false;
356 // The strtoull accepts negative numbers:
357 // If the minus sign was part of the input sequence, the numeric value
358 // calculated from the sequence of digits is negated as if by unary minus
359 // in the result type, which applies unsigned integer wraparound rules.
360 // Fix this behaviour (except -0).
363 while (*s && !is_digit(*s)) s++;
364 s = (s > str) ? (s - 1) : s; // step back to one symbol
366 // For unsigned types return the max to distinguish from
367 // "no conversion can be performed".
368 *val = (flatbuffers::numeric_limits<uint64_t>::max)();
375 template<> inline bool StringToNumber(const char *s, float *val) {
376 return StringToFloatImpl(val, s);
379 template<> inline bool StringToNumber(const char *s, double *val) {
380 return StringToFloatImpl(val, s);
383 inline int64_t StringToInt(const char *s, int base = 10) {
385 return StringToIntegerImpl(&val, s, base) ? val : 0;
388 inline uint64_t StringToUInt(const char *s, int base = 10) {
390 return StringToIntegerImpl(&val, s, base) ? val : 0;
393 typedef bool (*LoadFileFunction)(const char *filename, bool binary,
395 typedef bool (*FileExistsFunction)(const char *filename);
397 LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function);
399 FileExistsFunction SetFileExistsFunction(
400 FileExistsFunction file_exists_function);
402 // Check if file "name" exists.
403 bool FileExists(const char *name);
405 // Check if "name" exists and it is also a directory.
406 bool DirExists(const char *name);
408 // Load file "name" into "buf" returning true if successful
409 // false otherwise. If "binary" is false data is read
410 // using ifstream's text mode, otherwise data is read with
412 bool LoadFile(const char *name, bool binary, std::string *buf);
414 // Save data "buf" of length "len" bytes into a file
415 // "name" returning true if successful, false otherwise.
416 // If "binary" is false data is written using ifstream's
417 // text mode, otherwise data is written with no
419 bool SaveFile(const char *name, const char *buf, size_t len, bool binary);
421 // Save data "buf" into file "name" returning true if
422 // successful, false otherwise. If "binary" is false
423 // data is written using ifstream's text mode, otherwise
424 // data is written with no transcoding.
425 inline bool SaveFile(const char *name, const std::string &buf, bool binary) {
426 return SaveFile(name, buf.c_str(), buf.size(), binary);
429 // Functionality for minimalistic portable path handling.
431 // The functions below behave correctly regardless of whether posix ('/') or
432 // Windows ('/' or '\\') separators are used.
434 // Any new separators inserted are always posix.
435 FLATBUFFERS_CONSTEXPR char kPathSeparator = '/';
437 // Returns the path with the extension, if any, removed.
438 std::string StripExtension(const std::string &filepath);
440 // Returns the extension, if any.
441 std::string GetExtension(const std::string &filepath);
443 // Return the last component of the path, after the last separator.
444 std::string StripPath(const std::string &filepath);
446 // Strip the last component of the path + separator.
447 std::string StripFileName(const std::string &filepath);
449 // Concatenates a path with a filename, regardless of wether the path
450 // ends in a separator or not.
451 std::string ConCatPathFileName(const std::string &path,
452 const std::string &filename);
454 // Replaces any '\\' separators with '/'
455 std::string PosixPath(const char *path);
457 // This function ensure a directory exists, by recursively
458 // creating dirs for any parts of the path that don't exist yet.
459 void EnsureDirExists(const std::string &filepath);
461 // Obtains the absolute path from any other path.
462 // Returns the input path if the absolute path couldn't be resolved.
463 std::string AbsolutePath(const std::string &filepath);
465 // To and from UTF-8 unicode conversion functions
467 // Convert a unicode code point into a UTF-8 representation by appending it
468 // to a string. Returns the number of bytes generated.
469 inline int ToUTF8(uint32_t ucc, std::string *out) {
470 FLATBUFFERS_ASSERT(!(ucc & 0x80000000)); // Top bit can't be set.
471 // 6 possible encodings: http://en.wikipedia.org/wiki/UTF-8
472 for (int i = 0; i < 6; i++) {
473 // Max bits this encoding can represent.
474 uint32_t max_bits = 6 + i * 5 + static_cast<int>(!i);
475 if (ucc < (1u << max_bits)) { // does it fit?
476 // Remaining bits not encoded in the first byte, store 6 bits each
477 uint32_t remain_bits = i * 6;
479 (*out) += static_cast<char>((0xFE << (max_bits - remain_bits)) |
480 (ucc >> remain_bits));
481 // Store remaining bytes:
482 for (int j = i - 1; j >= 0; j--) {
483 (*out) += static_cast<char>(((ucc >> (j * 6)) & 0x3F) | 0x80);
485 return i + 1; // Return the number of bytes added.
488 FLATBUFFERS_ASSERT(0); // Impossible to arrive here.
492 // Converts whatever prefix of the incoming string corresponds to a valid
493 // UTF-8 sequence into a unicode code. The incoming pointer will have been
494 // advanced past all bytes parsed.
495 // returns -1 upon corrupt UTF-8 encoding (ignore the incoming pointer in
497 inline int FromUTF8(const char **in) {
499 // Count leading 1 bits.
500 for (int mask = 0x80; mask >= 0x04; mask >>= 1) {
507 if ((static_cast<unsigned char>(**in) << len) & 0x80)
508 return -1; // Bit after leading 1's must be 0.
509 if (!len) return *(*in)++;
510 // UTF-8 encoded values with a length are between 2 and 4 bytes.
511 if (len < 2 || len > 4) { return -1; }
512 // Grab initial bits of the code.
513 int ucc = *(*in)++ & ((1 << (7 - len)) - 1);
514 for (int i = 0; i < len - 1; i++) {
515 if ((**in & 0xC0) != 0x80) return -1; // Upper bits must 1 0.
517 ucc |= *(*in)++ & 0x3F; // Grab 6 more bits of the code.
519 // UTF-8 cannot encode values between 0xD800 and 0xDFFF (reserved for
520 // UTF-16 surrogate pairs).
521 if (ucc >= 0xD800 && ucc <= 0xDFFF) { return -1; }
522 // UTF-8 must represent code points in their shortest possible encoding.
525 // Two bytes of UTF-8 can represent code points from U+0080 to U+07FF.
526 if (ucc < 0x0080 || ucc > 0x07FF) { return -1; }
529 // Three bytes of UTF-8 can represent code points from U+0800 to U+FFFF.
530 if (ucc < 0x0800 || ucc > 0xFFFF) { return -1; }
533 // Four bytes of UTF-8 can represent code points from U+10000 to U+10FFFF.
534 if (ucc < 0x10000 || ucc > 0x10FFFF) { return -1; }
540 #ifndef FLATBUFFERS_PREFER_PRINTF
541 // Wraps a string to a maximum length, inserting new lines where necessary. Any
542 // existing whitespace will be collapsed down to a single space. A prefix or
543 // suffix can be provided, which will be inserted before or after a wrapped
544 // line, respectively.
545 inline std::string WordWrap(const std::string in, size_t max_length,
546 const std::string wrapped_line_prefix,
547 const std::string wrapped_line_suffix) {
548 std::istringstream in_stream(in);
549 std::string wrapped, line, word;
554 while (in_stream >> word) {
555 if ((line.length() + 1 + word.length() + wrapped_line_suffix.length()) <
559 wrapped += line + wrapped_line_suffix + "\n";
560 line = wrapped_line_prefix + word;
567 #endif // !FLATBUFFERS_PREFER_PRINTF
569 inline bool EscapeString(const char *s, size_t length, std::string *_text,
570 bool allow_non_utf8, bool natural_utf8) {
571 std::string &text = *_text;
573 for (uoffset_t i = 0; i < length; i++) {
576 case '\n': text += "\\n"; break;
577 case '\t': text += "\\t"; break;
578 case '\r': text += "\\r"; break;
579 case '\b': text += "\\b"; break;
580 case '\f': text += "\\f"; break;
581 case '\"': text += "\\\""; break;
582 case '\\': text += "\\\\"; break;
584 if (c >= ' ' && c <= '~') {
587 // Not printable ASCII data. Let's see if it's valid UTF-8 first:
588 const char *utf8 = s + i;
589 int ucc = FromUTF8(&utf8);
591 if (allow_non_utf8) {
593 text += IntToStringHex(static_cast<uint8_t>(c), 2);
595 // There are two cases here:
597 // 1) We reached here by parsing an IDL file. In that case,
598 // we previously checked for non-UTF-8, so we shouldn't reach
601 // 2) We reached here by someone calling GenerateText()
602 // on a previously-serialized flatbuffer. The data might have
603 // non-UTF-8 Strings, or might be corrupt.
605 // In both cases, we have to give up and inform the caller
606 // they have no JSON.
611 // utf8 points to past all utf-8 bytes parsed
612 text.append(s + i, static_cast<size_t>(utf8 - s - i));
613 } else if (ucc <= 0xFFFF) {
614 // Parses as Unicode within JSON's \uXXXX range, so use that.
616 text += IntToStringHex(ucc, 4);
617 } else if (ucc <= 0x10FFFF) {
618 // Encode Unicode SMP values to a surrogate pair using two \u
620 uint32_t base = ucc - 0x10000;
621 auto high_surrogate = (base >> 10) + 0xD800;
622 auto low_surrogate = (base & 0x03FF) + 0xDC00;
624 text += IntToStringHex(high_surrogate, 4);
626 text += IntToStringHex(low_surrogate, 4);
628 // Skip past characters recognized.
629 i = static_cast<uoffset_t>(utf8 - s - 1);
639 // Remove paired quotes in a string: "text"|'text' -> text.
640 std::string RemoveStringQuotes(const std::string &s);
642 // Change th global C-locale to locale with name <locale_name>.
643 // Returns an actual locale name in <_value>, useful if locale_name is "" or
645 bool SetGlobalTestLocale(const char *locale_name,
646 std::string *_value = nullptr);
648 // Read (or test) a value of environment variable.
649 bool ReadEnvironmentVariable(const char *var_name,
650 std::string *_value = nullptr);
652 // MSVC specific: Send all assert reports to STDOUT to prevent CI hangs.
653 void SetupDefaultCRTReportMode();
655 } // namespace flatbuffers
657 #endif // FLATBUFFERS_UTIL_H_