17 #ifndef FLATBUFFERS_UTIL_H_ 18 #define FLATBUFFERS_UTIL_H_ 20 #include "flatbuffers/base.h" 24 #ifndef FLATBUFFERS_PREFER_PRINTF 26 #else // FLATBUFFERS_PREFER_PRINTF 29 #endif // FLATBUFFERS_PREFER_PRINTF 44 return (static_cast<U>(x - a) <= static_cast<U>(b - a));
57 return ((c & 0xDF) == (alpha & 0xDF));
77 #ifdef FLATBUFFERS_PREFER_PRINTF 78 template<
typename T>
size_t IntToDigitCount(T t) {
79 size_t digit_count = 0;
81 if (t < 0) digit_count++;
83 if (-1 < t && t < 1) digit_count++;
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);
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);
105 snprintf(const_cast<char *>(s.data()), (s.size() + 1), fmt, precision, t);
108 #endif // FLATBUFFERS_PREFER_PRINTF 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 133 #if defined(FLATBUFFERS_CPP98_STL) 134 template<>
inline std::string NumToString<long long>(
long long t) {
136 snprintf(buf,
sizeof(buf),
"%lld", t);
137 return std::string(buf);
141 inline std::string NumToString<unsigned long long>(
unsigned long long t) {
143 snprintf(buf,
sizeof(buf),
"%llu", t);
144 return std::string(buf);
146 #endif // defined(FLATBUFFERS_CPP98_STL) 152 #ifndef FLATBUFFERS_PREFER_PRINTF 155 std::stringstream ss;
159 ss << std::setprecision(precision);
162 #else // FLATBUFFERS_PREFER_PRINTF 163 auto v =
static_cast<double>(t);
164 auto s = NumToStringImplWrapper(v,
"%0.*f", precision);
165 #endif // FLATBUFFERS_PREFER_PRINTF 168 auto p = s.find_last_not_of(
'0');
169 if (p != std::string::npos) {
171 s.resize(p + (s[p] ==
'.' ? 2 : 1));
190 #ifndef FLATBUFFERS_PREFER_PRINTF 191 std::stringstream ss;
192 ss << std::setw(xdigits) << std::setfill(
'0') << std::hex << std::uppercase
195 #else // FLATBUFFERS_PREFER_PRINTF 196 return NumToStringImplWrapper(i,
"%.*X", xdigits);
197 #endif // FLATBUFFERS_PREFER_PRINTF 203 #if defined(FLATBUFFERS_LOCALE_INDEPENDENT) && (FLATBUFFERS_LOCALE_INDEPENDENT > 0) 204 class ClassicLocale {
206 typedef _locale_t locale_type;
208 typedef locale_t locale_type;
213 static ClassicLocale instance_;
215 static locale_type Get() {
return instance_.locale_; }
219 #define __strtoull_impl(s, pe, b) _strtoui64_l(s, pe, b, ClassicLocale::Get()) 220 #define __strtoll_impl(s, pe, b) _strtoi64_l(s, pe, b, ClassicLocale::Get()) 221 #define __strtod_impl(s, pe) _strtod_l(s, pe, ClassicLocale::Get()) 222 #define __strtof_impl(s, pe) _strtof_l(s, pe, ClassicLocale::Get()) 224 #define __strtoull_impl(s, pe, b) strtoull_l(s, pe, b, ClassicLocale::Get()) 225 #define __strtoll_impl(s, pe, b) strtoll_l(s, pe, b, ClassicLocale::Get()) 226 #define __strtod_impl(s, pe) strtod_l(s, pe, ClassicLocale::Get()) 227 #define __strtof_impl(s, pe) strtof_l(s, pe, ClassicLocale::Get()) 230 #define __strtod_impl(s, pe) strtod(s, pe) 231 #define __strtof_impl(s, pe) static_cast<float>(strtod(s, pe)) 233 #define __strtoull_impl(s, pe, b) _strtoui64(s, pe, b) 234 #define __strtoll_impl(s, pe, b) _strtoi64(s, pe, b) 236 #define __strtoull_impl(s, pe, b) strtoull(s, pe, b) 237 #define __strtoll_impl(s, pe, b) strtoll(s, pe, b) 260 #undef __strtoull_impl 261 #undef __strtoll_impl 281 const bool check_errno =
true) {
292 if (check_errno) errno = 0;
300 if (check_errno && errno)
return false;
311 auto done = (end !=
str) && (*end ==
'\0');
330 *val =
static_cast<T
>(max);
339 *val =
static_cast<T
>(i64);
361 s = (s >
str) ? (s - 1) : s;
409 bool LoadFile(
const char *name,
bool binary, std::string *buf);
416 bool SaveFile(
const char *name,
const char *buf,
size_t len,
bool binary);
422 inline bool SaveFile(
const char *name,
const std::string &buf,
bool binary) {
423 return SaveFile(name, buf.c_str(), buf.size(), binary);
441 std::string
StripPath(
const std::string &filepath);
449 const std::string &filename);
466 inline int ToUTF8(uint32_t ucc, std::string *out) {
469 for (
int i = 0; i < 6; i++) {
471 uint32_t max_bits = 6 + i * 5 +
static_cast<int>(!i);
472 if (ucc < (1u << max_bits)) {
474 uint32_t remain_bits = i * 6;
476 (*out) +=
static_cast<char>((0xFE << (max_bits - remain_bits)) |
477 (ucc >> remain_bits));
479 for (
int j = i - 1; j >= 0; j--) {
480 (*out) +=
static_cast<char>(((ucc >> (j * 6)) & 0x3F) | 0x80);
497 for (
int mask = 0x80; mask >= 0x04; mask >>= 1) {
504 if ((static_cast<unsigned char>(**in) << len) & 0x80)
506 if (!len)
return *(*in)++;
508 if (len < 2 || len > 4) {
return -1; }
510 int ucc = *(*in)++ & ((1 << (7 - len)) - 1);
511 for (
int i = 0; i < len - 1; i++) {
512 if ((**in & 0xC0) != 0x80)
return -1;
514 ucc |= *(*in)++ & 0x3F;
518 if (ucc >= 0xD800 && ucc <= 0xDFFF) {
return -1; }
523 if (ucc < 0x0080 || ucc > 0x07FF) {
return -1; }
527 if (ucc < 0x0800 || ucc > 0xFFFF) {
return -1; }
531 if (ucc < 0x10000 || ucc > 0x10FFFF) {
return -1; }
537 #ifndef FLATBUFFERS_PREFER_PRINTF 542 inline std::string
WordWrap(
const std::string in,
size_t max_length,
543 const std::string wrapped_line_prefix,
544 const std::string wrapped_line_suffix) {
545 std::istringstream in_stream(in);
546 std::string wrapped, line, word;
551 while (in_stream >> word) {
552 if ((line.length() + 1 + word.length() + wrapped_line_suffix.length()) <
556 wrapped += line + wrapped_line_suffix +
"\n";
557 line = wrapped_line_prefix + word;
564 #endif // !FLATBUFFERS_PREFER_PRINTF 566 inline bool EscapeString(
const char *s,
size_t length, std::string *_text,
567 bool allow_non_utf8,
bool natural_utf8) {
568 std::string &text = *_text;
570 for (uoffset_t i = 0; i < length; i++) {
573 case '\n': text +=
"\\n";
break;
574 case '\t': text +=
"\\t";
break;
575 case '\r': text +=
"\\r";
break;
576 case '\b': text +=
"\\b";
break;
577 case '\f': text +=
"\\f";
break;
578 case '\"': text +=
"\\\"";
break;
579 case '\\': text +=
"\\\\";
break;
581 if (c >=
' ' && c <=
'~') {
585 const char *utf8 = s + i;
588 if (allow_non_utf8) {
609 text.append(s + i, static_cast<size_t>(utf8 - s - i));
610 }
else if (ucc <= 0xFFFF) {
614 }
else if (ucc <= 0x10FFFF) {
617 uint32_t base = ucc - 0x10000;
618 auto high_surrogate = (base >> 10) + 0xD800;
619 auto low_surrogate = (base & 0x03FF) + 0xDC00;
626 i =
static_cast<uoffset_t
>(utf8 - s - 1);
643 std::string *_value =
nullptr);
647 std::string *_value =
nullptr);
651 #endif // FLATBUFFERS_UTIL_H_ bool ReadEnvironmentVariable(const char *var_name, std::string *_value=nullptr)
bool SetGlobalTestLocale(const char *locale_name, std::string *_value=nullptr)
std::string IntToStringHex(int i, int xdigits)
std::string AbsolutePath(const std::string &filepath)
bool(* FileExistsFunction)(const char *filename)
bool check_in_range(T x, T a, T b)
bool FileExists(const char *name)
#define FLATBUFFERS_ASSERT
bool DirExists(const char *name)
std::string NumToString< signed char >(signed char t)
void strtoval_impl(int64_t *val, const char *str, char **endptr, int base)
bool LoadFile(const char *name, bool binary, std::string *buf)
bool(* LoadFileFunction)(const char *filename, bool binary, std::string *dest)
std::string GetExtension(const std::string &filepath)
bool StringToIntegerImpl(T *val, const char *const str, const int base=0, const bool check_errno=true)
bool EscapeString(const char *s, size_t length, std::string *_text, bool allow_non_utf8, bool natural_utf8)
bool is_alpha_char(char c, char alpha)
std::string ConCatPathFileName(const std::string &path, const std::string &filename)
std::string PosixPath(const char *path)
__supress_ubsan__("float-cast-overflow") inline void strtoval_impl(float *val
bool StringToNumber(const char *s, T *val)
FLATBUFFERS_CONSTEXPR char kPathSeparator
const char char ** endptr
std::string RemoveStringQuotes(const std::string &s)
#define __strtoull_impl(s, pe, b)
bool StringToFloatImpl(T *val, const char *const str)
LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function)
std::string WordWrap(const std::string in, size_t max_length, const std::string wrapped_line_prefix, const std::string wrapped_line_suffix)
void EnsureDirExists(const std::string &filepath)
#define __strtof_impl(s, pe)
FileExistsFunction SetFileExistsFunction(FileExistsFunction file_exists_function)
Simple class for manipulating paths on Linux/Windows/Mac OS.
int FromUTF8(const char **in)
std::string NumToString(T t)
#define __strtoll_impl(s, pe, b)
std::string NumToString< unsigned char >(unsigned char t)
std::string NumToString< float >(float t)
std::string StripPath(const std::string &filepath)
int64_t StringToInt(const char *s, int base=10)
#define __strtod_impl(s, pe)
uint64_t StringToUInt(const char *s, int base=10)
bool StringToNumber< int64_t >(const char *str, int64_t *val)
std::string FloatToString(T t, int precision)
bool StringToNumber< uint64_t >(const char *str, uint64_t *val)
std::string NumToString< double >(double t)
std::string StripExtension(const std::string &filepath)
std::string StripFileName(const std::string &filepath)
int ToUTF8(uint32_t ucc, std::string *out)
bool SaveFile(const char *name, const char *buf, size_t len, bool binary)