Make trim a common file

This commit is contained in:
John Keiser
2023-07-17 12:24:43 -07:00
parent 7784aa86c2
commit fa629bd783
3 changed files with 21 additions and 32 deletions
+10
View File
@@ -96,6 +96,16 @@ static simdjson_inline uint64_t _umul128(uint64_t ab, uint64_t cd, uint64_t *hi)
}
#endif
inline std::string_view trim(const std::string_view str) noexcept {
// We can almost surely do better by rolling our own find_first_not_of function.
size_t first = str.find_first_not_of(" \t\n\r");
// If we have the empty string (just white space), then no trimming is possible, and
// we return the empty string_view.
if (std::string_view::npos == first) { return std::string_view(); }
size_t last = str.find_last_not_of(" \t\n\r");
return str.substr(first, (last - first + 1));
}
} // namespace jsoncharutils
} // unnamed namespace
} // namespace SIMDJSON_IMPLEMENTATION
@@ -13,29 +13,18 @@
namespace simdjson {
inline std::string_view trim(const std::string_view str) noexcept {
// We can almost surely do better by rolling our own find_first_not_of function.
size_t first = str.find_first_not_of(" \t\n\r");
// If we have the empty string (just white space), then no trimming is possible, and
// we return the empty string_view.
if (std::string_view::npos == first) { return std::string_view(); }
size_t last = str.find_last_not_of(" \t\n\r");
return str.substr(first, (last - first + 1));
}
inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION::ondemand::document& x) noexcept {
std::string_view v;
auto error = x.raw_json().get(v);
if(error) {return error; }
return trim(v);
return SIMDJSON_IMPLEMENTATION::jsoncharutils::trim(v);
}
inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION::ondemand::document_reference& x) noexcept {
std::string_view v;
auto error = x.raw_json().get(v);
if(error) {return error; }
return trim(v);
return SIMDJSON_IMPLEMENTATION::jsoncharutils::trim(v);
}
inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION::ondemand::value& x) noexcept {
@@ -66,7 +55,7 @@ inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION:
return to_json_string(object);
}
default:
return trim(x.raw_json_token());
return SIMDJSON_IMPLEMENTATION::jsoncharutils::trim(x.raw_json_token());
}
}
@@ -74,14 +63,14 @@ inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION:
std::string_view v;
auto error = x.raw_json().get(v);
if(error) {return error; }
return trim(v);
return SIMDJSON_IMPLEMENTATION::jsoncharutils::trim(v);
}
inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION::ondemand::array& x) noexcept {
std::string_view v;
auto error = x.raw_json().get(v);
if(error) {return error; }
return trim(v);
return SIMDJSON_IMPLEMENTATION::jsoncharutils::trim(v);
}
inline simdjson_result<std::string_view> to_json_string(simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::document> x) {
@@ -9,33 +9,23 @@
#include "simdjson/generic/singlestage/object.h"
#include "simdjson/generic/singlestage/serialization.h"
#include "simdjson/generic/singlestage/value.h"
#include "simdjson/generic/jsoncharutils.h"
#endif // SIMDJSON_AMALGAMATED
namespace simdjson {
inline std::string_view trim(const std::string_view str) noexcept {
// We can almost surely do better by rolling our own find_first_not_of function.
size_t first = str.find_first_not_of(" \t\n\r");
// If we have the empty string (just white space), then no trimming is possible, and
// we return the empty string_view.
if (std::string_view::npos == first) { return std::string_view(); }
size_t last = str.find_last_not_of(" \t\n\r");
return str.substr(first, (last - first + 1));
}
inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION::singlestage::document& x) noexcept {
std::string_view v;
auto error = x.raw_json().get(v);
if(error) {return error; }
return trim(v);
return SIMDJSON_IMPLEMENTATION::jsoncharutils::trim(v);
}
inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION::singlestage::document_reference& x) noexcept {
std::string_view v;
auto error = x.raw_json().get(v);
if(error) {return error; }
return trim(v);
return SIMDJSON_IMPLEMENTATION::jsoncharutils::trim(v);
}
inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION::singlestage::value& x) noexcept {
@@ -66,7 +56,7 @@ inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION:
return to_json_string(object);
}
default:
return trim(x.raw_json_token());
return SIMDJSON_IMPLEMENTATION::jsoncharutils::trim(x.raw_json_token());
}
}
@@ -74,14 +64,14 @@ inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION:
std::string_view v;
auto error = x.raw_json().get(v);
if(error) {return error; }
return trim(v);
return SIMDJSON_IMPLEMENTATION::jsoncharutils::trim(v);
}
inline simdjson_result<std::string_view> to_json_string(SIMDJSON_IMPLEMENTATION::singlestage::array& x) noexcept {
std::string_view v;
auto error = x.raw_json().get(v);
if(error) {return error; }
return trim(v);
return SIMDJSON_IMPLEMENTATION::jsoncharutils::trim(v);
}
inline simdjson_result<std::string_view> to_json_string(simdjson_result<SIMDJSON_IMPLEMENTATION::singlestage::document> x) {