18#ifndef FLATBUFFERS_UTIL_H_
19#define FLATBUFFERS_UTIL_H_
26#ifndef FLATBUFFERS_PREFER_PRINTF
48 return static_cast<unsigned int>(x - a) <=
static_cast<unsigned int>(b - a);
66 return ((c & 0xDF) == (alpha & 0xDF));
87 return static_cast<char>(::toupper(
static_cast<unsigned char>(c)));
92 return static_cast<char>(::tolower(
static_cast<unsigned char>(c)));
97#ifdef FLATBUFFERS_PREFER_PRINTF
98template <
typename T>
size_t IntToDigitCount(T t)
100 size_t digit_count = 0;
108 T eps = std::numeric_limits<float>::epsilon();
109 while (t <= (-1 + eps) || (1 - eps) <= t)
117template <
typename T>
size_t NumToStringWidth(T t,
int precision = 0)
119 size_t string_width = IntToDigitCount(t);
122 string_width += (precision + 1);
126template <
typename T> std::string NumToStringImplWrapper(T t,
const char *fmt,
int precision = 0)
128 size_t string_width = NumToStringWidth(t, precision);
129 std::string
s(string_width, 0x00);
131 snprintf(
const_cast<char *
>(
s.data()), (
s.size() + 1), fmt, string_width, t);
143 #ifndef FLATBUFFERS_PREFER_PRINTF
144 std::stringstream ss;
148 auto v =
static_cast<long long>(t);
149 return NumToStringImplWrapper(v,
"%.*lld");
166#if defined(FLATBUFFERS_CPP98_STL)
167template <>
inline std::string NumToString<long long>(
long long t)
170 snprintf(buf,
sizeof(buf),
"%lld", t);
171 return std::string(buf);
174template <>
inline std::string NumToString<unsigned long long>(
unsigned long long t)
177 snprintf(buf,
sizeof(buf),
"%llu", t);
178 return std::string(buf);
187 #ifndef FLATBUFFERS_PREFER_PRINTF
190 std::stringstream ss;
194 ss << std::setprecision(precision);
198 auto v =
static_cast<double>(t);
199 auto s = NumToStringImplWrapper(v,
"%0.*f", precision);
203 auto p = s.find_last_not_of(
'0');
204 if (p != std::string::npos)
207 s.resize(p + (s[p] ==
'.' ? 2 : 1));
223 #ifndef FLATBUFFERS_PREFER_PRINTF
224 std::stringstream ss;
225 ss << std::setw(xdigits) << std::setfill(
'0') << std::hex << std::uppercase
229 return NumToStringImplWrapper(i,
"%.*X", xdigits);
236#if defined(FLATBUFFERS_LOCALE_INDEPENDENT) && (FLATBUFFERS_LOCALE_INDEPENDENT > 0)
237 class ClassicLocale {
239 typedef _locale_t locale_type;
241 typedef locale_t locale_type;
246 static ClassicLocale instance_;
248 static locale_type Get() {
return instance_.locale_; }
252 #define __strtoull_impl(s, pe, b) _strtoui64_l(s, pe, b, ClassicLocale::Get())
253 #define __strtoll_impl(s, pe, b) _strtoi64_l(s, pe, b, ClassicLocale::Get())
254 #define __strtod_impl(s, pe) _strtod_l(s, pe, ClassicLocale::Get())
255 #define __strtof_impl(s, pe) _strtof_l(s, pe, ClassicLocale::Get())
257 #define __strtoull_impl(s, pe, b) strtoull_l(s, pe, b, ClassicLocale::Get())
258 #define __strtoll_impl(s, pe, b) strtoll_l(s, pe, b, ClassicLocale::Get())
259 #define __strtod_impl(s, pe) strtod_l(s, pe, ClassicLocale::Get())
260 #define __strtof_impl(s, pe) strtof_l(s, pe, ClassicLocale::Get())
263 #define __strtod_impl(s, pe) strtod(s, pe)
264 #define __strtof_impl(s, pe) static_cast<float>(strtod(s, pe))
266 #define __strtoull_impl(s, pe, b) _strtoui64(s, pe, b)
267 #define __strtoll_impl(s, pe, b) _strtoi64(s, pe, b)
269 #define __strtoull_impl(s, pe, b) strtoull(s, pe, b)
270 #define __strtoll_impl(s, pe, b) strtoll(s, pe, b)
293#undef __strtoull_impl
313 const bool check_errno =
true)
339 if (check_errno && errno)
351 auto done = (end !=
str) && (*end ==
'\0');
367 static_assert(
sizeof(T) <
sizeof(int64_t),
"unexpected type T");
377 *val =
static_cast<T
>(max);
387 *val =
static_cast<T
>(i64);
413 s = (s >
str) ? (s - 1) : s;
464bool LoadFile(
const char *name,
bool binary, std::string *buf);
471bool SaveFile(
const char *name,
const char *buf,
size_t len,
bool binary);
477inline bool SaveFile(
const char *name,
const std::string &buf,
bool binary)
479 return SaveFile(name, buf.c_str(), buf.size(), binary);
521inline int ToUTF8(uint32_t ucc, std::string *out)
525 for (
int i = 0; i < 6; i++)
528 uint32_t max_bits = 6 + i * 5 +
static_cast<int>(!i);
529 if (ucc < (1u << max_bits))
532 uint32_t remain_bits = i * 6;
534 (*out) +=
static_cast<char>((0xFE << (max_bits - remain_bits)) | (ucc >> remain_bits));
536 for (
int j = i - 1; j >= 0; j--)
538 (*out) +=
static_cast<char>(((ucc >> (j * 6)) & 0x3F) | 0x80);
556 for (
int mask = 0x80; mask >= 0x04; mask >>= 1)
567 if ((
static_cast<unsigned char>(**in) << len) & 0x80)
572 if (len < 2 || len > 4)
577 int ucc = *(*in)++ & ((1 << (7 - len)) - 1);
578 for (
int i = 0; i < len - 1; i++)
580 if ((**in & 0xC0) != 0x80)
583 ucc |= *(*in)++ & 0x3F;
587 if (ucc >= 0xD800 && ucc <= 0xDFFF)
596 if (ucc < 0x0080 || ucc > 0x07FF)
603 if (ucc < 0x0800 || ucc > 0xFFFF)
610 if (ucc < 0x10000 || ucc > 0x10FFFF)
619#ifndef FLATBUFFERS_PREFER_PRINTF
624inline std::string
WordWrap(
const std::string in,
size_t max_length,
625 const std::string wrapped_line_prefix,
626 const std::string wrapped_line_suffix)
628 std::istringstream in_stream(in);
629 std::string wrapped, line, word;
634 while (in_stream >> word)
636 if ((line.length() + 1 + word.length() + wrapped_line_suffix.length()) < max_length)
642 wrapped += line + wrapped_line_suffix +
"\n";
643 line = wrapped_line_prefix + word;
652inline bool EscapeString(
const char *s,
size_t length, std::string *_text,
bool allow_non_utf8,
655 std::string &text = *_text;
657 for (uoffset_t i = 0; i < length; i++)
684 if (c >=
' ' && c <=
'~')
691 const char *utf8 = s + i;
722 text.append(s + i,
static_cast<size_t>(utf8 - s - i));
724 else if (ucc <= 0xFFFF)
730 else if (ucc <= 0x10FFFF)
734 uint32_t base = ucc - 0x10000;
735 auto high_surrogate = (base >> 10) + 0xD800;
736 auto low_surrogate = (base & 0x03FF) + 0xDC00;
743 i =
static_cast<uoffset_t
>(utf8 - s - 1);
753inline std::string
BufferToHexText(
const void *buffer,
size_t buffer_size,
size_t max_length,
754 const std::string &wrapped_line_prefix,
755 const std::string &wrapped_line_suffix)
757 std::string text = wrapped_line_prefix;
758 size_t start_offset = 0;
759 const char *s =
reinterpret_cast<const char *
>(buffer);
760 for (
size_t i = 0; s && i < buffer_size; i++)
763 bool have_more = i + 1 < buffer_size;
771 if (have_more && text.size() + wrapped_line_suffix.size() >= start_offset + max_length)
773 text += wrapped_line_suffix;
775 start_offset = text.size();
776 text += wrapped_line_prefix;
779 text += wrapped_line_suffix;
#define FLATBUFFERS_ASSERT
bool StringToNumber< int64_t >(const char *str, int64_t *val)
const char char ** endptr
std::string StripFileName(const std::string &filepath)
LoadFileFunction SetLoadFileFunction(LoadFileFunction load_file_function)
int ToUTF8(uint32_t ucc, std::string *out)
std::string GetExtension(const std::string &filepath)
bool SaveFile(const char *name, const char *buf, size_t len, bool binary)
void SetupDefaultCRTReportMode()
FLATBUFFERS_CONSTEXPR char kPathSeparator
bool check_ascii_range(char x, char a, char b)
std::string StripPath(const std::string &filepath)
std::string NumToString(T t)
bool(* FileExistsFunction)(const char *filename)
bool StringToIntegerImpl(T *val, const char *const str, const int base=0, const bool check_errno=true)
std::string IntToStringHex(int i, int xdigits)
std::string NumToString< double >(double t)
int64_t StringToInt(const char *s, int base=10)
std::string AbsolutePath(const std::string &filepath)
std::string BufferToHexText(const void *buffer, size_t buffer_size, size_t max_length, const std::string &wrapped_line_prefix, const std::string &wrapped_line_suffix)
std::string NumToString< float >(float t)
bool EscapeString(const char *s, size_t length, std::string *_text, bool allow_non_utf8, bool natural_utf8)
std::string NumToString< signed char >(signed char t)
std::string WordWrap(const std::string in, size_t max_length, const std::string wrapped_line_prefix, const std::string wrapped_line_suffix)
__supress_ubsan__("float-cast-overflow") inline void strtoval_impl(float *val
bool StringToNumber< uint64_t >(const char *str, uint64_t *val)
std::string ConCatPathFileName(const std::string &path, const std::string &filename)
std::string NumToString< char >(char t)
bool FileExists(const char *name)
bool DirExists(const char *name)
bool StringToNumber(const char *s, T *val)
std::string NumToString< unsigned char >(unsigned char t)
std::string RemoveStringQuotes(const std::string &s)
FileExistsFunction SetFileExistsFunction(FileExistsFunction file_exists_function)
int FromUTF8(const char **in)
bool StringToFloatImpl(T *val, const char *const str)
bool(* LoadFileFunction)(const char *filename, bool binary, std::string *dest)
bool is_alpha_char(char c, char alpha)
std::string StripExtension(const std::string &filepath)
uint64_t StringToUInt(const char *s, int base=10)
bool LoadFile(const char *name, bool binary, std::string *buf)
std::string FloatToString(T t, int precision)
std::string PosixPath(const char *path)
void strtoval_impl(int64_t *val, const char *str, char **endptr, int base)
bool is_alpha_upper(char c)
void EnsureDirExists(const std::string &filepath)
bool ReadEnvironmentVariable(const char *var_name, std::string *_value=nullptr)
bool SetGlobalTestLocale(const char *locale_name, std::string *_value=nullptr)
#define __strtoll_impl(s, pe, b)
#define __strtoull_impl(s, pe, b)
#define __strtod_impl(s, pe)
#define __strtof_impl(s, pe)