Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame^] | 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 "flatbuffers/base.h" |
| 21 | |
| 22 | #include <errno.h> |
| 23 | |
| 24 | #ifndef FLATBUFFERS_PREFER_PRINTF |
| 25 | # include <sstream> |
| 26 | #else // FLATBUFFERS_PREFER_PRINTF |
| 27 | # include <float.h> |
| 28 | # include <stdio.h> |
| 29 | #endif // FLATBUFFERS_PREFER_PRINTF |
| 30 | |
| 31 | #include <iomanip> |
| 32 | #include <string> |
| 33 | |
| 34 | namespace flatbuffers { |
| 35 | |
| 36 | // @locale-independent functions for ASCII characters set. |
| 37 | |
| 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); |
| 45 | } |
| 46 | |
| 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); |
| 51 | } |
| 52 | |
| 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)); |
| 58 | } |
| 59 | |
| 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'); } |
| 66 | |
| 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); |
| 70 | } |
| 71 | |
| 72 | // Case-insensitive isalnum |
| 73 | inline bool is_alnum(char c) { return is_alpha(c) || is_digit(c); } |
| 74 | |
| 75 | // @end-locale-independent functions for ASCII character set |
| 76 | |
| 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) { |
| 87 | t /= 10; |
| 88 | digit_count++; |
| 89 | } |
| 90 | return digit_count; |
| 91 | } |
| 92 | |
| 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); |
| 97 | return string_width; |
| 98 | } |
| 99 | |
| 100 | template<typename T> |
| 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); |
| 106 | return s; |
| 107 | } |
| 108 | #endif // FLATBUFFERS_PREFER_PRINTF |
| 109 | |
| 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) { |
| 114 | // clang-format off |
| 115 | |
| 116 | #ifndef FLATBUFFERS_PREFER_PRINTF |
| 117 | std::stringstream ss; |
| 118 | ss << t; |
| 119 | return ss.str(); |
| 120 | #else // FLATBUFFERS_PREFER_PRINTF |
| 121 | auto v = static_cast<long long>(t); |
| 122 | return NumToStringImplWrapper(v, "%.*lld"); |
| 123 | #endif // FLATBUFFERS_PREFER_PRINTF |
| 124 | // clang-format on |
| 125 | } |
| 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)); |
| 129 | } |
| 130 | template<> inline std::string NumToString<unsigned char>(unsigned char t) { |
| 131 | return NumToString(static_cast<int>(t)); |
| 132 | } |
| 133 | template<> inline std::string NumToString<char>(char t) { |
| 134 | return NumToString(static_cast<int>(t)); |
| 135 | } |
| 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); |
| 141 | } |
| 142 | |
| 143 | template<> |
| 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); |
| 148 | } |
| 149 | #endif // defined(FLATBUFFERS_CPP98_STL) |
| 150 | |
| 151 | // Special versions for floats/doubles. |
| 152 | template<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 | |
| 179 | template<> inline std::string NumToString<double>(double t) { |
| 180 | return FloatToString(t, 12); |
| 181 | } |
| 182 | template<> 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". |
| 189 | inline 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 | |
| 244 | inline void strtoval_impl(int64_t *val, const char *str, char **endptr, |
| 245 | int base) { |
| 246 | *val = __strtoll_impl(str, endptr, base); |
| 247 | } |
| 248 | |
| 249 | inline void strtoval_impl(uint64_t *val, const char *str, char **endptr, |
| 250 | int base) { |
| 251 | *val = __strtoull_impl(str, endptr, base); |
| 252 | } |
| 253 | |
| 254 | inline 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. |
| 259 | __supress_ubsan__("float-cast-overflow") |
| 260 | inline 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. |
| 281 | template<typename T> |
| 282 | inline 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 | |
| 308 | template<typename T> |
| 309 | inline 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. |
| 325 | template<typename T> inline bool StringToNumber(const char *s, T *val) { |
| 326 | FLATBUFFERS_ASSERT(s && val); |
| 327 | int64_t i64; |
| 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(); |
| 332 | if (i64 > max) { |
| 333 | *val = static_cast<T>(max); |
| 334 | return false; |
| 335 | } |
| 336 | if (i64 < min) { |
| 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); |
| 340 | return false; |
| 341 | } |
| 342 | *val = static_cast<T>(i64); |
| 343 | return true; |
| 344 | } |
| 345 | *val = 0; |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | template<> inline bool StringToNumber<int64_t>(const char *str, int64_t *val) { |
| 350 | return StringToIntegerImpl(val, str); |
| 351 | } |
| 352 | |
| 353 | template<> |
| 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). |
| 361 | if (*val) { |
| 362 | auto s = str; |
| 363 | while (*s && !is_digit(*s)) s++; |
| 364 | s = (s > str) ? (s - 1) : s; // step back to one symbol |
| 365 | if (*s == '-') { |
| 366 | // For unsigned types return the max to distinguish from |
| 367 | // "no conversion can be performed". |
| 368 | *val = (flatbuffers::numeric_limits<uint64_t>::max)(); |
| 369 | return false; |
| 370 | } |
| 371 | } |
| 372 | return true; |
| 373 | } |
| 374 | |
| 375 | template<> inline bool StringToNumber(const char *s, float *val) { |
| 376 | return StringToFloatImpl(val, s); |
| 377 | } |
| 378 | |
| 379 | template<> inline bool StringToNumber(const char *s, double *val) { |
| 380 | return StringToFloatImpl(val, s); |
| 381 | } |
| 382 | |
| 383 | inline int64_t StringToInt(const char *s, int base = 10) { |
| 384 | int64_t val; |
| 385 | return StringToIntegerImpl(&val, s, base) ? val : 0; |
| 386 | } |
| 387 | |
| 388 | inline uint64_t StringToUInt(const char *s, int base = 10) { |
| 389 | uint64_t val; |
| 390 | return StringToIntegerImpl(&val, s, base) ? val : 0; |
| 391 | } |
| 392 | |
| 393 | typedef bool (*LoadFileFunction)(const char *filename, bool binary, |
| 394 | std::string *dest); |
| 395 | typedef bool (*FileExistsFunction)(const char *filename); |
| 396 | |
| 397 | LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function); |
| 398 | |
| 399 | FileExistsFunction SetFileExistsFunction( |
| 400 | FileExistsFunction file_exists_function); |
| 401 | |
| 402 | // Check if file "name" exists. |
| 403 | bool FileExists(const char *name); |
| 404 | |
| 405 | // Check if "name" exists and it is also a directory. |
| 406 | bool DirExists(const char *name); |
| 407 | |
| 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 |
| 411 | // no transcoding. |
| 412 | bool LoadFile(const char *name, bool binary, std::string *buf); |
| 413 | |
| 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 |
| 418 | // transcoding. |
| 419 | bool SaveFile(const char *name, const char *buf, size_t len, bool binary); |
| 420 | |
| 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); |
| 427 | } |
| 428 | |
| 429 | // Functionality for minimalistic portable path handling. |
| 430 | |
| 431 | // The functions below behave correctly regardless of whether posix ('/') or |
| 432 | // Windows ('/' or '\\') separators are used. |
| 433 | |
| 434 | // Any new separators inserted are always posix. |
| 435 | FLATBUFFERS_CONSTEXPR char kPathSeparator = '/'; |
| 436 | |
| 437 | // Returns the path with the extension, if any, removed. |
| 438 | std::string StripExtension(const std::string &filepath); |
| 439 | |
| 440 | // Returns the extension, if any. |
| 441 | std::string GetExtension(const std::string &filepath); |
| 442 | |
| 443 | // Return the last component of the path, after the last separator. |
| 444 | std::string StripPath(const std::string &filepath); |
| 445 | |
| 446 | // Strip the last component of the path + separator. |
| 447 | std::string StripFileName(const std::string &filepath); |
| 448 | |
| 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); |
| 453 | |
| 454 | // Replaces any '\\' separators with '/' |
| 455 | std::string PosixPath(const char *path); |
| 456 | |
| 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); |
| 460 | |
| 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); |
| 464 | |
| 465 | // To and from UTF-8 unicode conversion functions |
| 466 | |
| 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; |
| 478 | // Store first byte: |
| 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); |
| 484 | } |
| 485 | return i + 1; // Return the number of bytes added. |
| 486 | } |
| 487 | } |
| 488 | FLATBUFFERS_ASSERT(0); // Impossible to arrive here. |
| 489 | return -1; |
| 490 | } |
| 491 | |
| 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 |
| 496 | // this case). |
| 497 | inline int FromUTF8(const char **in) { |
| 498 | int len = 0; |
| 499 | // Count leading 1 bits. |
| 500 | for (int mask = 0x80; mask >= 0x04; mask >>= 1) { |
| 501 | if (**in & mask) { |
| 502 | len++; |
| 503 | } else { |
| 504 | break; |
| 505 | } |
| 506 | } |
| 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. |
| 516 | ucc <<= 6; |
| 517 | ucc |= *(*in)++ & 0x3F; // Grab 6 more bits of the code. |
| 518 | } |
| 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. |
| 523 | switch (len) { |
| 524 | case 2: |
| 525 | // Two bytes of UTF-8 can represent code points from U+0080 to U+07FF. |
| 526 | if (ucc < 0x0080 || ucc > 0x07FF) { return -1; } |
| 527 | break; |
| 528 | case 3: |
| 529 | // Three bytes of UTF-8 can represent code points from U+0800 to U+FFFF. |
| 530 | if (ucc < 0x0800 || ucc > 0xFFFF) { return -1; } |
| 531 | break; |
| 532 | case 4: |
| 533 | // Four bytes of UTF-8 can represent code points from U+10000 to U+10FFFF. |
| 534 | if (ucc < 0x10000 || ucc > 0x10FFFF) { return -1; } |
| 535 | break; |
| 536 | } |
| 537 | return ucc; |
| 538 | } |
| 539 | |
| 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; |
| 550 | |
| 551 | in_stream >> word; |
| 552 | line = word; |
| 553 | |
| 554 | while (in_stream >> word) { |
| 555 | if ((line.length() + 1 + word.length() + wrapped_line_suffix.length()) < |
| 556 | max_length) { |
| 557 | line += " " + word; |
| 558 | } else { |
| 559 | wrapped += line + wrapped_line_suffix + "\n"; |
| 560 | line = wrapped_line_prefix + word; |
| 561 | } |
| 562 | } |
| 563 | wrapped += line; |
| 564 | |
| 565 | return wrapped; |
| 566 | } |
| 567 | #endif // !FLATBUFFERS_PREFER_PRINTF |
| 568 | |
| 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; |
| 572 | text += "\""; |
| 573 | for (uoffset_t i = 0; i < length; i++) { |
| 574 | char c = s[i]; |
| 575 | switch (c) { |
| 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; |
| 583 | default: |
| 584 | if (c >= ' ' && c <= '~') { |
| 585 | text += c; |
| 586 | } else { |
| 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); |
| 590 | if (ucc < 0) { |
| 591 | if (allow_non_utf8) { |
| 592 | text += "\\x"; |
| 593 | text += IntToStringHex(static_cast<uint8_t>(c), 2); |
| 594 | } else { |
| 595 | // There are two cases here: |
| 596 | // |
| 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 |
| 599 | // here. |
| 600 | // |
| 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. |
| 604 | // |
| 605 | // In both cases, we have to give up and inform the caller |
| 606 | // they have no JSON. |
| 607 | return false; |
| 608 | } |
| 609 | } else { |
| 610 | if (natural_utf8) { |
| 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. |
| 615 | text += "\\u"; |
| 616 | text += IntToStringHex(ucc, 4); |
| 617 | } else if (ucc <= 0x10FFFF) { |
| 618 | // Encode Unicode SMP values to a surrogate pair using two \u |
| 619 | // escapes. |
| 620 | uint32_t base = ucc - 0x10000; |
| 621 | auto high_surrogate = (base >> 10) + 0xD800; |
| 622 | auto low_surrogate = (base & 0x03FF) + 0xDC00; |
| 623 | text += "\\u"; |
| 624 | text += IntToStringHex(high_surrogate, 4); |
| 625 | text += "\\u"; |
| 626 | text += IntToStringHex(low_surrogate, 4); |
| 627 | } |
| 628 | // Skip past characters recognized. |
| 629 | i = static_cast<uoffset_t>(utf8 - s - 1); |
| 630 | } |
| 631 | } |
| 632 | break; |
| 633 | } |
| 634 | } |
| 635 | text += "\""; |
| 636 | return true; |
| 637 | } |
| 638 | |
| 639 | // Remove paired quotes in a string: "text"|'text' -> text. |
| 640 | std::string RemoveStringQuotes(const std::string &s); |
| 641 | |
| 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 |
| 644 | // null. |
| 645 | bool SetGlobalTestLocale(const char *locale_name, |
| 646 | std::string *_value = nullptr); |
| 647 | |
| 648 | // Read (or test) a value of environment variable. |
| 649 | bool ReadEnvironmentVariable(const char *var_name, |
| 650 | std::string *_value = nullptr); |
| 651 | |
| 652 | // MSVC specific: Send all assert reports to STDOUT to prevent CI hangs. |
| 653 | void SetupDefaultCRTReportMode(); |
| 654 | |
| 655 | } // namespace flatbuffers |
| 656 | |
| 657 | #endif // FLATBUFFERS_UTIL_H_ |