Skip to content

Commit 5dbcdf1

Browse files
committed
Ok
1 parent f03a6ab commit 5dbcdf1

File tree

6 files changed

+54
-2
lines changed

6 files changed

+54
-2
lines changed

include/simdjson/implementation.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@
1010

1111
namespace simdjson {
1212

13+
/**
14+
* Validate the UTF-8 string.
15+
*
16+
* @param buf the string to validate.
17+
* @param len the length of the string in bytes.
18+
* @return true if the string is valid UTF-8.
19+
*/
20+
WARN_UNUSED bool validate_utf8(const char * buf, size_t length) noexcept;
21+
22+
23+
/**
24+
* Validate the UTF-8 string.
25+
*
26+
* @param p the string_view to validate.
27+
* @return true if the string is valid UTF-8.
28+
*/
29+
WARN_UNUSED bool validate_utf8(std::string_view& p) noexcept;
30+
1331
namespace dom {
1432
class document;
1533
} // namespace dom

include/simdjson/simdjson.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@
77

88
#include "simdjson/compiler_check.h"
99
#include "simdjson/error.h"
10-
1110
#endif // SIMDJSON_H

src/arm64/dom_parser_implementation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ WARN_UNUSED error_code dom_parser_implementation::stage1(const uint8_t *_buf, si
106106
return arm64::stage1::json_structural_indexer::index<64>(buf, len, *this, streaming);
107107
}
108108
#include "generic/stage1/utf8_validator.h"
109+
WARN_UNUSED bool implementation::validate_utf8(const char *buf, size_t len) const noexcept {
110+
return simdjson::arm64::stage1::utf8_validate(buf,len);
111+
}
109112
} // namespace arm64
110113
} // namespace simdjson
111114

src/implementation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ SIMDJSON_DLLIMPORTEXPORT internal::atomic_ptr<const implementation> active_imple
141141
WARN_UNUSED error_code minify(const char *buf, size_t len, char *dst, size_t &dst_len) noexcept {
142142
return active_implementation->minify((const uint8_t *)buf, len, (uint8_t *)dst, dst_len);
143143
}
144+
WARN_UNUSED bool validate_utf8(const char *buf, size_t len) noexcept {
145+
return active_implementation->validate_utf8(buf, len);
146+
}
144147

145148

146149
} // namespace simdjson

src/westmere/dom_parser_implementation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ WARN_UNUSED error_code dom_parser_implementation::stage1(const uint8_t *_buf, si
9595
return westmere::stage1::json_structural_indexer::index<64>(_buf, _len, *this, streaming);
9696
}
9797
#include "generic/stage1/utf8_validator.h"
98+
WARN_UNUSED bool implementation::validate_utf8(const char *buf, size_t len) const noexcept {
99+
return simdjson::westmere::stage1::utf8_validate(buf,len);
100+
}
98101
} // namespace westmere
99102
} // namespace simdjson
100103
UNTARGET_REGION

tests/basictests.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,31 @@ namespace type_tests {
16491649
}
16501650

16511651

1652+
namespace validate_tests {
1653+
bool test_validate() {
1654+
std::cout << "Running " << __func__ << std::endl;
1655+
const std::string test = R"({ "foo" : 1, "bar" : [ 1, 2, 3 ], "baz": { "a": 1, "b": 2, "c": 3 } })";
1656+
if(!simdjson::validate_utf8(test.data(), test.size())) {
1657+
return false;
1658+
}
1659+
return true;
1660+
}
1661+
1662+
bool test_bad_validate() {
1663+
std::cout << "Running " << __func__ << std::endl;
1664+
const std::string test = "\x80\x81";
1665+
if(simdjson::validate_utf8(test.data(), test.size())) {
1666+
return false;
1667+
}
1668+
return true;
1669+
}
1670+
bool run() {
1671+
return test_validate() &&
1672+
test_bad_validate();
1673+
}
1674+
}
1675+
1676+
16521677

16531678
namespace minify_tests {
16541679

@@ -1960,7 +1985,8 @@ int main(int argc, char *argv[]) {
19601985
printf("unsupported CPU\n");
19611986
}
19621987
std::cout << "Running basic tests." << std::endl;
1963-
if (minify_tests::run() &&
1988+
if (validate_tests::run() &&
1989+
minify_tests::run() &&
19641990
parse_api_tests::run() &&
19651991
dom_api_tests::run() &&
19661992
type_tests::run() &&

0 commit comments

Comments
 (0)