util.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2014 Google Inc. All rights reserved.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef FLATBUFFERS_UTIL_H_
00018 #define FLATBUFFERS_UTIL_H_
00019 
00020 #include "flatbuffers/base.h"
00021 
00022 #include <errno.h>
00023 
00024 #ifndef FLATBUFFERS_PREFER_PRINTF
00025 #  include <sstream>
00026 #else  // FLATBUFFERS_PREFER_PRINTF
00027 #  include <float.h>
00028 #  include <stdio.h>
00029 #endif  // FLATBUFFERS_PREFER_PRINTF
00030 
00031 #include <iomanip>
00032 #include <string>
00033 
00034 namespace flatbuffers {
00035 
00036 // @locale-independent functions for ASCII characters set.
00037 
00038 // Check that integer scalar is in closed range: (a <= x <= b)
00039 // using one compare (conditional branch) operator.
00040 template<typename T> inline bool check_in_range(T x, T a, T b) {
00041   // (Hacker's Delight): `a <= x <= b` <=> `(x-a) <={u} (b-a)`.
00042   FLATBUFFERS_ASSERT(a <= b);  // static_assert only if 'a' & 'b' templated
00043   typedef typename flatbuffers::make_unsigned<T>::type U;
00044   return (static_cast<U>(x - a) <= static_cast<U>(b - a));
00045 }
00046 
00047 // Case-insensitive isalpha
00048 inline bool is_alpha(char c) {
00049   // ASCII only: alpha to upper case => reset bit 0x20 (~0x20 = 0xDF).
00050   return check_in_range(c & 0xDF, 'a' & 0xDF, 'z' & 0xDF);
00051 }
00052 
00053 // Check (case-insensitive) that `c` is equal to alpha.
00054 inline bool is_alpha_char(char c, char alpha) {
00055   FLATBUFFERS_ASSERT(is_alpha(alpha));
00056   // ASCII only: alpha to upper case => reset bit 0x20 (~0x20 = 0xDF).
00057   return ((c & 0xDF) == (alpha & 0xDF));
00058 }
00059 
00060 // https://en.cppreference.com/w/cpp/string/byte/isxdigit
00061 // isdigit and isxdigit are the only standard narrow character classification
00062 // functions that are not affected by the currently installed C locale. although
00063 // some implementations (e.g. Microsoft in 1252 codepage) may classify
00064 // additional single-byte characters as digits.
00065 inline bool is_digit(char c) { return check_in_range(c, '0', '9'); }
00066 
00067 inline bool is_xdigit(char c) {
00068   // Replace by look-up table.
00069   return is_digit(c) || check_in_range(c & 0xDF, 'a' & 0xDF, 'f' & 0xDF);
00070 }
00071 
00072 // Case-insensitive isalnum
00073 inline bool is_alnum(char c) { return is_alpha(c) || is_digit(c); }
00074 
00075 // @end-locale-independent functions for ASCII character set
00076 
00077 #ifdef FLATBUFFERS_PREFER_PRINTF
00078 template<typename T> size_t IntToDigitCount(T t) {
00079   size_t digit_count = 0;
00080   // Count the sign for negative numbers
00081   if (t < 0) digit_count++;
00082   // Count a single 0 left of the dot for fractional numbers
00083   if (-1 < t && t < 1) digit_count++;
00084   // Count digits until fractional part
00085   T eps = std::numeric_limits<float>::epsilon();
00086   while (t <= (-1 + eps) || (1 - eps) <= t) {
00087     t /= 10;
00088     digit_count++;
00089   }
00090   return digit_count;
00091 }
00092 
00093 template<typename T> size_t NumToStringWidth(T t, int precision = 0) {
00094   size_t string_width = IntToDigitCount(t);
00095   // Count the dot for floating point numbers
00096   if (precision) string_width += (precision + 1);
00097   return string_width;
00098 }
00099 
00100 template<typename T>
00101 std::string NumToStringImplWrapper(T t, const char *fmt, int precision = 0) {
00102   size_t string_width = NumToStringWidth(t, precision);
00103   std::string s(string_width, 0x00);
00104   // Allow snprintf to use std::string trailing null to detect buffer overflow
00105   snprintf(const_cast<char *>(s.data()), (s.size() + 1), fmt, precision, t);
00106   return s;
00107 }
00108 #endif  // FLATBUFFERS_PREFER_PRINTF
00109 
00110 // Convert an integer or floating point value to a string.
00111 // In contrast to std::stringstream, "char" values are
00112 // converted to a string of digits, and we don't use scientific notation.
00113 template<typename T> std::string NumToString(T t) {
00114   // clang-format off
00115 
00116   #ifndef FLATBUFFERS_PREFER_PRINTF
00117     std::stringstream ss;
00118     ss << t;
00119     return ss.str();
00120   #else // FLATBUFFERS_PREFER_PRINTF
00121     auto v = static_cast<long long>(t);
00122     return NumToStringImplWrapper(v, "%.*lld");
00123   #endif // FLATBUFFERS_PREFER_PRINTF
00124   // clang-format on
00125 }
00126 // Avoid char types used as character data.
00127 template<> inline std::string NumToString<signed char>(signed char t) {
00128   return NumToString(static_cast<int>(t));
00129 }
00130 template<> inline std::string NumToString<unsigned char>(unsigned char t) {
00131   return NumToString(static_cast<int>(t));
00132 }
00133 #if defined(FLATBUFFERS_CPP98_STL)
00134 template<> inline std::string NumToString<long long>(long long t) {
00135   char buf[21];  // (log((1 << 63) - 1) / log(10)) + 2
00136   snprintf(buf, sizeof(buf), "%lld", t);
00137   return std::string(buf);
00138 }
00139 
00140 template<>
00141 inline std::string NumToString<unsigned long long>(unsigned long long t) {
00142   char buf[22];  // (log((1 << 63) - 1) / log(10)) + 1
00143   snprintf(buf, sizeof(buf), "%llu", t);
00144   return std::string(buf);
00145 }
00146 #endif  // defined(FLATBUFFERS_CPP98_STL)
00147 
00148 // Special versions for floats/doubles.
00149 template<typename T> std::string FloatToString(T t, int precision) {
00150   // clang-format off
00151 
00152   #ifndef FLATBUFFERS_PREFER_PRINTF
00153     // to_string() prints different numbers of digits for floats depending on
00154     // platform and isn't available on Android, so we use stringstream
00155     std::stringstream ss;
00156     // Use std::fixed to suppress scientific notation.
00157     ss << std::fixed;
00158     // Default precision is 6, we want that to be higher for doubles.
00159     ss << std::setprecision(precision);
00160     ss << t;
00161     auto s = ss.str();
00162   #else // FLATBUFFERS_PREFER_PRINTF
00163     auto v = static_cast<double>(t);
00164     auto s = NumToStringImplWrapper(v, "%0.*f", precision);
00165   #endif // FLATBUFFERS_PREFER_PRINTF
00166   // clang-format on
00167   // Sadly, std::fixed turns "1" into "1.00000", so here we undo that.
00168   auto p = s.find_last_not_of('0');
00169   if (p != std::string::npos) {
00170     // Strip trailing zeroes. If it is a whole number, keep one zero.
00171     s.resize(p + (s[p] == '.' ? 2 : 1));
00172   }
00173   return s;
00174 }
00175 
00176 template<> inline std::string NumToString<double>(double t) {
00177   return FloatToString(t, 12);
00178 }
00179 template<> inline std::string NumToString<float>(float t) {
00180   return FloatToString(t, 6);
00181 }
00182 
00183 // Convert an integer value to a hexadecimal string.
00184 // The returned string length is always xdigits long, prefixed by 0 digits.
00185 // For example, IntToStringHex(0x23, 8) returns the string "00000023".
00186 inline std::string IntToStringHex(int i, int xdigits) {
00187   FLATBUFFERS_ASSERT(i >= 0);
00188   // clang-format off
00189 
00190   #ifndef FLATBUFFERS_PREFER_PRINTF
00191     std::stringstream ss;
00192     ss << std::setw(xdigits) << std::setfill('0') << std::hex << std::uppercase
00193        << i;
00194     return ss.str();
00195   #else // FLATBUFFERS_PREFER_PRINTF
00196     return NumToStringImplWrapper(i, "%.*X", xdigits);
00197   #endif // FLATBUFFERS_PREFER_PRINTF
00198   // clang-format on
00199 }
00200 
00201 // clang-format off
00202 // Use locale independent functions {strtod_l, strtof_l, strtoll_l, strtoull_l}.
00203 #if defined(FLATBUFFERS_LOCALE_INDEPENDENT) && (FLATBUFFERS_LOCALE_INDEPENDENT > 0)
00204   class ClassicLocale {
00205     #ifdef _MSC_VER
00206       typedef _locale_t locale_type;
00207     #else
00208       typedef locale_t locale_type;  // POSIX.1-2008 locale_t type
00209     #endif
00210     ClassicLocale();
00211     ~ClassicLocale();
00212     locale_type locale_;
00213     static ClassicLocale instance_;
00214   public:
00215     static locale_type Get() { return instance_.locale_; }
00216   };
00217 
00218   #ifdef _MSC_VER
00219     #define __strtoull_impl(s, pe, b) _strtoui64_l(s, pe, b, ClassicLocale::Get())
00220     #define __strtoll_impl(s, pe, b) _strtoi64_l(s, pe, b, ClassicLocale::Get())
00221     #define __strtod_impl(s, pe) _strtod_l(s, pe, ClassicLocale::Get())
00222     #define __strtof_impl(s, pe) _strtof_l(s, pe, ClassicLocale::Get())
00223   #else
00224     #define __strtoull_impl(s, pe, b) strtoull_l(s, pe, b, ClassicLocale::Get())
00225     #define __strtoll_impl(s, pe, b) strtoll_l(s, pe, b, ClassicLocale::Get())
00226     #define __strtod_impl(s, pe) strtod_l(s, pe, ClassicLocale::Get())
00227     #define __strtof_impl(s, pe) strtof_l(s, pe, ClassicLocale::Get())
00228   #endif
00229 #else
00230   #define __strtod_impl(s, pe) strtod(s, pe)
00231   #define __strtof_impl(s, pe) static_cast<float>(strtod(s, pe))
00232   #ifdef _MSC_VER
00233     #define __strtoull_impl(s, pe, b) _strtoui64(s, pe, b)
00234     #define __strtoll_impl(s, pe, b) _strtoi64(s, pe, b)
00235   #else
00236     #define __strtoull_impl(s, pe, b) strtoull(s, pe, b)
00237     #define __strtoll_impl(s, pe, b) strtoll(s, pe, b)
00238   #endif
00239 #endif
00240 
00241 inline void strtoval_impl(int64_t *val, const char *str, char **endptr,
00242                                  int base) {
00243     *val = __strtoll_impl(str, endptr, base);
00244 }
00245 
00246 inline void strtoval_impl(uint64_t *val, const char *str, char **endptr,
00247                                  int base) {
00248   *val = __strtoull_impl(str, endptr, base);
00249 }
00250 
00251 inline void strtoval_impl(double *val, const char *str, char **endptr) {
00252   *val = __strtod_impl(str, endptr);
00253 }
00254 
00255 // UBSAN: double to float is safe if numeric_limits<float>::is_iec559 is true.
00256 __supress_ubsan__("float-cast-overflow")
00257 inline void strtoval_impl(float *val, const char *str, char **endptr) {
00258   *val = __strtof_impl(str, endptr);
00259 }
00260 #undef __strtoull_impl
00261 #undef __strtoll_impl
00262 #undef __strtod_impl
00263 #undef __strtof_impl
00264 // clang-format on
00265 
00266 // Adaptor for strtoull()/strtoll().
00267 // Flatbuffers accepts numbers with any count of leading zeros (-009 is -9),
00268 // while strtoll with base=0 interprets first leading zero as octal prefix.
00269 // In future, it is possible to add prefixed 0b0101.
00270 // 1) Checks errno code for overflow condition (out of range).
00271 // 2) If base <= 0, function try to detect base of number by prefix.
00272 //
00273 // Return value (like strtoull and strtoll, but reject partial result):
00274 // - If successful, an integer value corresponding to the str is returned.
00275 // - If full string conversion can't be performed, 0 is returned.
00276 // - If the converted value falls out of range of corresponding return type, a
00277 // range error occurs. In this case value MAX(T)/MIN(T) is returned.
00278 template<typename T>
00279 inline bool StringToIntegerImpl(T *val, const char *const str,
00280                                 const int base = 0,
00281                                 const bool check_errno = true) {
00282   // T is int64_t or uint64_T
00283   FLATBUFFERS_ASSERT(str);
00284   if (base <= 0) {
00285     auto s = str;
00286     while (*s && !is_digit(*s)) s++;
00287     if (s[0] == '0' && is_alpha_char(s[1], 'X'))
00288       return StringToIntegerImpl(val, str, 16, check_errno);
00289     // if a prefix not match, try base=10
00290     return StringToIntegerImpl(val, str, 10, check_errno);
00291   } else {
00292     if (check_errno) errno = 0;  // clear thread-local errno
00293     auto endptr = str;
00294     strtoval_impl(val, str, const_cast<char **>(&endptr), base);
00295     if ((*endptr != '\0') || (endptr == str)) {
00296       *val = 0;      // erase partial result
00297       return false;  // invalid string
00298     }
00299     // errno is out-of-range, return MAX/MIN
00300     if (check_errno && errno) return false;
00301     return true;
00302   }
00303 }
00304 
00305 template<typename T>
00306 inline bool StringToFloatImpl(T *val, const char *const str) {
00307   // Type T must be either float or double.
00308   FLATBUFFERS_ASSERT(str && val);
00309   auto end = str;
00310   strtoval_impl(val, str, const_cast<char **>(&end));
00311   auto done = (end != str) && (*end == '\0');
00312   if (!done) *val = 0;  // erase partial result
00313   return done;
00314 }
00315 
00316 // Convert a string to an instance of T.
00317 // Return value (matched with StringToInteger64Impl and strtod):
00318 // - If successful, a numeric value corresponding to the str is returned.
00319 // - If full string conversion can't be performed, 0 is returned.
00320 // - If the converted value falls out of range of corresponding return type, a
00321 // range error occurs. In this case value MAX(T)/MIN(T) is returned.
00322 template<typename T> inline bool StringToNumber(const char *s, T *val) {
00323   FLATBUFFERS_ASSERT(s && val);
00324   int64_t i64;
00325   // The errno check isn't needed, will return MAX/MIN on overflow.
00326   if (StringToIntegerImpl(&i64, s, 0, false)) {
00327     const int64_t max = flatbuffers::numeric_limits<T>::max();
00328     const int64_t min = flatbuffers::numeric_limits<T>::lowest();
00329     if (i64 > max) {
00330       *val = static_cast<T>(max);
00331       return false;
00332     }
00333     if (i64 < min) {
00334       // For unsigned types return max to distinguish from
00335       // "no conversion can be performed" when 0 is returned.
00336       *val = static_cast<T>(flatbuffers::is_unsigned<T>::value ? max : min);
00337       return false;
00338     }
00339     *val = static_cast<T>(i64);
00340     return true;
00341   }
00342   *val = 0;
00343   return false;
00344 }
00345 
00346 template<> inline bool StringToNumber<int64_t>(const char *str, int64_t *val) {
00347   return StringToIntegerImpl(val, str);
00348 }
00349 
00350 template<>
00351 inline bool StringToNumber<uint64_t>(const char *str, uint64_t *val) {
00352   if (!StringToIntegerImpl(val, str)) return false;
00353   // The strtoull accepts negative numbers:
00354   // If the minus sign was part of the input sequence, the numeric value
00355   // calculated from the sequence of digits is negated as if by unary minus
00356   // in the result type, which applies unsigned integer wraparound rules.
00357   // Fix this behaviour (except -0).
00358   if (*val) {
00359     auto s = str;
00360     while (*s && !is_digit(*s)) s++;
00361     s = (s > str) ? (s - 1) : s;  // step back to one symbol
00362     if (*s == '-') {
00363       // For unsigned types return the max to distinguish from
00364       // "no conversion can be performed".
00365       *val = flatbuffers::numeric_limits<uint64_t>::max();
00366       return false;
00367     }
00368   }
00369   return true;
00370 }
00371 
00372 template<> inline bool StringToNumber(const char *s, float *val) {
00373   return StringToFloatImpl(val, s);
00374 }
00375 
00376 template<> inline bool StringToNumber(const char *s, double *val) {
00377   return StringToFloatImpl(val, s);
00378 }
00379 
00380 inline int64_t StringToInt(const char *s, int base = 10) {
00381   int64_t val;
00382   return StringToIntegerImpl(&val, s, base) ? val : 0;
00383 }
00384 
00385 inline uint64_t StringToUInt(const char *s, int base = 10) {
00386   uint64_t val;
00387   return StringToIntegerImpl(&val, s, base) ? val : 0;
00388 }
00389 
00390 typedef bool (*LoadFileFunction)(const char *filename, bool binary,
00391                                  std::string *dest);
00392 typedef bool (*FileExistsFunction)(const char *filename);
00393 
00394 LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function);
00395 
00396 FileExistsFunction SetFileExistsFunction(
00397     FileExistsFunction file_exists_function);
00398 
00399 // Check if file "name" exists.
00400 bool FileExists(const char *name);
00401 
00402 // Check if "name" exists and it is also a directory.
00403 bool DirExists(const char *name);
00404 
00405 // Load file "name" into "buf" returning true if successful
00406 // false otherwise.  If "binary" is false data is read
00407 // using ifstream's text mode, otherwise data is read with
00408 // no transcoding.
00409 bool LoadFile(const char *name, bool binary, std::string *buf);
00410 
00411 // Save data "buf" of length "len" bytes into a file
00412 // "name" returning true if successful, false otherwise.
00413 // If "binary" is false data is written using ifstream's
00414 // text mode, otherwise data is written with no
00415 // transcoding.
00416 bool SaveFile(const char *name, const char *buf, size_t len, bool binary);
00417 
00418 // Save data "buf" into file "name" returning true if
00419 // successful, false otherwise.  If "binary" is false
00420 // data is written using ifstream's text mode, otherwise
00421 // data is written with no transcoding.
00422 inline bool SaveFile(const char *name, const std::string &buf, bool binary) {
00423   return SaveFile(name, buf.c_str(), buf.size(), binary);
00424 }
00425 
00426 // Functionality for minimalistic portable path handling.
00427 
00428 // The functions below behave correctly regardless of whether posix ('/') or
00429 // Windows ('/' or '\\') separators are used.
00430 
00431 // Any new separators inserted are always posix.
00432 FLATBUFFERS_CONSTEXPR char kPathSeparator = '/';
00433 
00434 // Returns the path with the extension, if any, removed.
00435 std::string StripExtension(const std::string &filepath);
00436 
00437 // Returns the extension, if any.
00438 std::string GetExtension(const std::string &filepath);
00439 
00440 // Return the last component of the path, after the last separator.
00441 std::string StripPath(const std::string &filepath);
00442 
00443 // Strip the last component of the path + separator.
00444 std::string StripFileName(const std::string &filepath);
00445 
00446 // Concatenates a path with a filename, regardless of wether the path
00447 // ends in a separator or not.
00448 std::string ConCatPathFileName(const std::string &path,
00449                                const std::string &filename);
00450 
00451 // Replaces any '\\' separators with '/'
00452 std::string PosixPath(const char *path);
00453 
00454 // This function ensure a directory exists, by recursively
00455 // creating dirs for any parts of the path that don't exist yet.
00456 void EnsureDirExists(const std::string &filepath);
00457 
00458 // Obtains the absolute path from any other path.
00459 // Returns the input path if the absolute path couldn't be resolved.
00460 std::string AbsolutePath(const std::string &filepath);
00461 
00462 // To and from UTF-8 unicode conversion functions
00463 
00464 // Convert a unicode code point into a UTF-8 representation by appending it
00465 // to a string. Returns the number of bytes generated.
00466 inline int ToUTF8(uint32_t ucc, std::string *out) {
00467   FLATBUFFERS_ASSERT(!(ucc & 0x80000000));  // Top bit can't be set.
00468   // 6 possible encodings: http://en.wikipedia.org/wiki/UTF-8
00469   for (int i = 0; i < 6; i++) {
00470     // Max bits this encoding can represent.
00471     uint32_t max_bits = 6 + i * 5 + static_cast<int>(!i);
00472     if (ucc < (1u << max_bits)) {  // does it fit?
00473       // Remaining bits not encoded in the first byte, store 6 bits each
00474       uint32_t remain_bits = i * 6;
00475       // Store first byte:
00476       (*out) += static_cast<char>((0xFE << (max_bits - remain_bits)) |
00477                                   (ucc >> remain_bits));
00478       // Store remaining bytes:
00479       for (int j = i - 1; j >= 0; j--) {
00480         (*out) += static_cast<char>(((ucc >> (j * 6)) & 0x3F) | 0x80);
00481       }
00482       return i + 1;  // Return the number of bytes added.
00483     }
00484   }
00485   FLATBUFFERS_ASSERT(0);  // Impossible to arrive here.
00486   return -1;
00487 }
00488 
00489 // Converts whatever prefix of the incoming string corresponds to a valid
00490 // UTF-8 sequence into a unicode code. The incoming pointer will have been
00491 // advanced past all bytes parsed.
00492 // returns -1 upon corrupt UTF-8 encoding (ignore the incoming pointer in
00493 // this case).
00494 inline int FromUTF8(const char **in) {
00495   int len = 0;
00496   // Count leading 1 bits.
00497   for (int mask = 0x80; mask >= 0x04; mask >>= 1) {
00498     if (**in & mask) {
00499       len++;
00500     } else {
00501       break;
00502     }
00503   }
00504   if ((static_cast<unsigned char>(**in) << len) & 0x80)
00505     return -1;  // Bit after leading 1's must be 0.
00506   if (!len) return *(*in)++;
00507   // UTF-8 encoded values with a length are between 2 and 4 bytes.
00508   if (len < 2 || len > 4) { return -1; }
00509   // Grab initial bits of the code.
00510   int ucc = *(*in)++ & ((1 << (7 - len)) - 1);
00511   for (int i = 0; i < len - 1; i++) {
00512     if ((**in & 0xC0) != 0x80) return -1;  // Upper bits must 1 0.
00513     ucc <<= 6;
00514     ucc |= *(*in)++ & 0x3F;  // Grab 6 more bits of the code.
00515   }
00516   // UTF-8 cannot encode values between 0xD800 and 0xDFFF (reserved for
00517   // UTF-16 surrogate pairs).
00518   if (ucc >= 0xD800 && ucc <= 0xDFFF) { return -1; }
00519   // UTF-8 must represent code points in their shortest possible encoding.
00520   switch (len) {
00521     case 2:
00522       // Two bytes of UTF-8 can represent code points from U+0080 to U+07FF.
00523       if (ucc < 0x0080 || ucc > 0x07FF) { return -1; }
00524       break;
00525     case 3:
00526       // Three bytes of UTF-8 can represent code points from U+0800 to U+FFFF.
00527       if (ucc < 0x0800 || ucc > 0xFFFF) { return -1; }
00528       break;
00529     case 4:
00530       // Four bytes of UTF-8 can represent code points from U+10000 to U+10FFFF.
00531       if (ucc < 0x10000 || ucc > 0x10FFFF) { return -1; }
00532       break;
00533   }
00534   return ucc;
00535 }
00536 
00537 #ifndef FLATBUFFERS_PREFER_PRINTF
00538 // Wraps a string to a maximum length, inserting new lines where necessary. Any
00539 // existing whitespace will be collapsed down to a single space. A prefix or
00540 // suffix can be provided, which will be inserted before or after a wrapped
00541 // line, respectively.
00542 inline std::string WordWrap(const std::string in, size_t max_length,
00543                             const std::string wrapped_line_prefix,
00544                             const std::string wrapped_line_suffix) {
00545   std::istringstream in_stream(in);
00546   std::string wrapped, line, word;
00547 
00548   in_stream >> word;
00549   line = word;
00550 
00551   while (in_stream >> word) {
00552     if ((line.length() + 1 + word.length() + wrapped_line_suffix.length()) <
00553         max_length) {
00554       line += " " + word;
00555     } else {
00556       wrapped += line + wrapped_line_suffix + "\n";
00557       line = wrapped_line_prefix + word;
00558     }
00559   }
00560   wrapped += line;
00561 
00562   return wrapped;
00563 }
00564 #endif  // !FLATBUFFERS_PREFER_PRINTF
00565 
00566 inline bool EscapeString(const char *s, size_t length, std::string *_text,
00567                          bool allow_non_utf8, bool natural_utf8) {
00568   std::string &text = *_text;
00569   text += "\"";
00570   for (uoffset_t i = 0; i < length; i++) {
00571     char c = s[i];
00572     switch (c) {
00573       case '\n': text += "\\n"; break;
00574       case '\t': text += "\\t"; break;
00575       case '\r': text += "\\r"; break;
00576       case '\b': text += "\\b"; break;
00577       case '\f': text += "\\f"; break;
00578       case '\"': text += "\\\""; break;
00579       case '\\': text += "\\\\"; break;
00580       default:
00581         if (c >= ' ' && c <= '~') {
00582           text += c;
00583         } else {
00584           // Not printable ASCII data. Let's see if it's valid UTF-8 first:
00585           const char *utf8 = s + i;
00586           int ucc = FromUTF8(&utf8);
00587           if (ucc < 0) {
00588             if (allow_non_utf8) {
00589               text += "\\x";
00590               text += IntToStringHex(static_cast<uint8_t>(c), 2);
00591             } else {
00592               // There are two cases here:
00593               //
00594               // 1) We reached here by parsing an IDL file. In that case,
00595               // we previously checked for non-UTF-8, so we shouldn't reach
00596               // here.
00597               //
00598               // 2) We reached here by someone calling GenerateText()
00599               // on a previously-serialized flatbuffer. The data might have
00600               // non-UTF-8 Strings, or might be corrupt.
00601               //
00602               // In both cases, we have to give up and inform the caller
00603               // they have no JSON.
00604               return false;
00605             }
00606           } else {
00607             if (natural_utf8) {
00608               // utf8 points to past all utf-8 bytes parsed
00609               text.append(s + i, static_cast<size_t>(utf8 - s - i));
00610             } else if (ucc <= 0xFFFF) {
00611               // Parses as Unicode within JSON's \uXXXX range, so use that.
00612               text += "\\u";
00613               text += IntToStringHex(ucc, 4);
00614             } else if (ucc <= 0x10FFFF) {
00615               // Encode Unicode SMP values to a surrogate pair using two \u
00616               // escapes.
00617               uint32_t base = ucc - 0x10000;
00618               auto high_surrogate = (base >> 10) + 0xD800;
00619               auto low_surrogate = (base & 0x03FF) + 0xDC00;
00620               text += "\\u";
00621               text += IntToStringHex(high_surrogate, 4);
00622               text += "\\u";
00623               text += IntToStringHex(low_surrogate, 4);
00624             }
00625             // Skip past characters recognized.
00626             i = static_cast<uoffset_t>(utf8 - s - 1);
00627           }
00628         }
00629         break;
00630     }
00631   }
00632   text += "\"";
00633   return true;
00634 }
00635 
00636 // Remove paired quotes in a string: "text"|'text' -> text.
00637 std::string RemoveStringQuotes(const std::string &s);
00638 
00639 // Change th global C-locale to locale with name <locale_name>.
00640 // Returns an actual locale name in <_value>, useful if locale_name is "" or
00641 // null.
00642 bool SetGlobalTestLocale(const char *locale_name,
00643                          std::string *_value = nullptr);
00644 
00645 // Read (or test) a value of environment variable.
00646 bool ReadEnvironmentVariable(const char *var_name,
00647                              std::string *_value = nullptr);
00648 
00649 }  // namespace flatbuffers
00650 
00651 #endif  // FLATBUFFERS_UTIL_H_


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 20:17:15