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