43concept FromChars =
requires(
const char* p, T& v) { std::from_chars(p, p, v); };
62 static std::string
ToLower(std::string_view str);
72 static std::string
ToUpper(std::string_view str);
80 const std::optional<bool> fast = AsciiEqualsIgnoreCase(lhs, rhs);
81 return fast.has_value() ? *fast : (ToLower(lhs) == ToLower(rhs));
91 if (prefix.size() <= str.size()) {
92 const std::optional<bool> fast =
93 AsciiEqualsIgnoreCase(str.substr(0, prefix.size()), prefix);
94 if (fast.has_value()) {
98 return ToLower(str).starts_with(ToLower(prefix));
105 if ((i & 0xC0) != 0x80) {
112 template <
typename T>
113 requires std::is_arithmetic_v<T> &&
FromChars<T> && (!std::same_as<T, bool>)
114 static Result<T> ParseNumber(std::string_view str) {
116 auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value);
117 if (ec == std::errc()) [[likely]] {
118 if (ptr != str.data() + str.size()) {
119 return InvalidArgument(
"Failed to parse {} from string '{}': trailing characters",
120 typeid(T).name(), str);
124 if (ec == std::errc::invalid_argument) {
125 return InvalidArgument(
"Failed to parse {} from string '{}': invalid argument",
126 typeid(T).name(), str);
128 if (ec == std::errc::result_out_of_range) {
129 return InvalidArgument(
"Failed to parse {} from string '{}': value out of range",
130 typeid(T).name(), str);
139 template <
typename T>
141 static Result<T> ParseNumber(std::string_view str) {
144 std::string owned(str);
145 const char* start = owned.c_str();
149 if constexpr (std::same_as<T, float>) {
150 value = std::strtof(start, &end);
151 }
else if constexpr (std::same_as<T, double>) {
152 value = std::strtod(start, &end);
154 value = std::strtold(start, &end);
157 if (end == start || end != start +
static_cast<std::ptrdiff_t
>(owned.size())) {
158 return InvalidArgument(
"Failed to parse {} from string '{}': invalid argument",
159 typeid(T).name(), str);
161 if (errno == ERANGE) {
162 return InvalidArgument(
"Failed to parse {} from string '{}': value out of range",
163 typeid(T).name(), str);
175 static bool ParseBoolean(std::string_view str) {
return EqualsIgnoreCase(str,
"true"); }
180 static constexpr char ToUpperAscii(
char c)
noexcept {
181 return (c >=
'a' && c <=
'z') ?
static_cast<char>(c -
'a' +
'A') : c;
183 static constexpr char ToLowerAscii(
char c)
noexcept {
184 return (c >=
'A' && c <=
'Z') ?
static_cast<char>(c -
'A' +
'a') : c;
189 static constexpr bool IsAsciiByte(
char c)
noexcept {
190 return (
static_cast<unsigned char>(c) & 0x80) == 0;
197 static std::optional<bool> AsciiEqualsIgnoreCase(std::string_view a,
198 std::string_view b) {
199 const size_t n = std::min(a.size(), b.size());
200 for (
size_t i = 0; i < n; ++i) {
201 if (!IsAsciiByte(a[i]) || !IsAsciiByte(b[i])) {
204 if (ToLowerAscii(a[i]) != ToLowerAscii(b[i])) {
208 return a.size() == b.size();
217 using hash_type = std::hash<std::string_view>;
218 using is_transparent = void;
220 std::size_t operator()(std::string_view str)
const {
return hash_type{}(str); }
221 std::size_t operator()(
const char* str)
const {
return hash_type{}(str); }
222 std::size_t operator()(
const std::string& str)
const {
return hash_type{}(str); }
227 using is_transparent = void;
229 bool operator()(std::string_view lhs, std::string_view rhs)
const {
return lhs == rhs; }
230 bool operator()(
const std::string& lhs,
const std::string& rhs)
const {
237 using is_transparent = void;
239 bool operator()(std::string_view lhs, std::string_view rhs)
const {
return lhs < rhs; }
Definition string_util.h:45
static bool StartsWithIgnoreCase(std::string_view str, std::string_view prefix)
Case-insensitive prefix test using Unicode simple (1:1) case mapping.
Definition string_util.h:90
static size_t CodePointCount(std::string_view str)
Count the number of code points in a UTF-8 string.
Definition string_util.h:102
static std::string ToUpper(std::string_view str)
Upper-case the ASCII letters (a-z) in a string; all other bytes, including multi-byte UTF-8 sequences...
static bool ParseBoolean(std::string_view str)
Parse a boolean from its string representation, ignoring case.
Definition string_util.h:175
static std::string ToLower(std::string_view str)
Lower-case a UTF-8 string using Unicode simple (1:1) case mapping.
static Result< std::vector< uint8_t > > HexStringToBytes(std::string_view hex)
Decode a hex string (upper or lower case) into bytes. Returns an error if the string has odd length o...
static bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs)
Case-insensitive equality using Unicode simple (1:1) case mapping.
Definition string_util.h:79
Definition expected.h:1199
Definition string_util.h:43
Define symbol visibility macros for core Iceberg APIs.
Core Apache Iceberg C++ APIs.
Definition arrow_io_util.h:34
void unreachable() noexcept
Marks a branch the program can never take.
Definition unreachable.h:35
Define Result, Status, and error helpers.
Transparent equality function that supports std::string_view as lookup key.
Definition string_util.h:226
Transparent hash function that supports std::string_view as lookup key.
Definition string_util.h:216
Transparent less function that supports std::string_view as lookup key.
Definition string_util.h:236
Portable stand-in for std::unreachable.