Skip to content

Commit

Permalink
add test; remove template skip_till
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbgan committed Jul 9, 2023
1 parent 155057d commit 42981d6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion iguana/xml_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ IGUANA_INLINE void parse_item(T &value, It &&it, It &&end,
// <![
if constexpr (cdata_idx == iguana::get_value<std::decay_t<T>>()) {
++it;
skip_till<']'>(it, end);
skip_till_square_bracket(it, end);
++it;
match<"]>">(it, end);
skip_sapces_and_newline(it, end);
Expand Down
46 changes: 35 additions & 11 deletions iguana/xml_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,17 +168,6 @@ template <size_t N> struct string_literal {
// TODO: get more information, now just skip it
IGUANA_INLINE void parse_declaration(auto &&it, auto &&end) {}

template <char... C> IGUANA_INLINE void skip_till(auto &&it, auto &&end) {
while ((it != end) && (!((... || (*it == C))))) {
++it;
}
if (it == end) [[unlikely]] {
static constexpr char b[] = {C..., '\0'};
std::string error = std::string("Expected one of these: ").append(b);
throw std::runtime_error(error);
}
}

IGUANA_INLINE void skip_sapces_and_newline(auto &&it, auto &&end) {
while (it != end && (*it < 33)) {
++it;
Expand Down Expand Up @@ -289,6 +278,7 @@ IGUANA_INLINE void skip_till_greater(auto &&it, auto &&end) {
throw std::runtime_error("Expected >");
}

// skip_till<'>', '<'>(it, end);
IGUANA_INLINE void skip_till_greater_or_space(auto &&it, auto &&end) {
static_assert(std::contiguous_iterator<std::decay_t<decltype(it)>>);

Expand Down Expand Up @@ -329,6 +319,7 @@ IGUANA_INLINE void skip_till_greater_or_space(auto &&it, auto &&end) {
throw std::runtime_error("Expected > or space");
}

// skip_till<'<'>(it, end);
IGUANA_INLINE void skip_till_smaller(auto &&it, auto &&end) {
static_assert(std::contiguous_iterator<std::decay_t<decltype(it)>>);

Expand Down Expand Up @@ -364,6 +355,39 @@ IGUANA_INLINE void skip_till_smaller(auto &&it, auto &&end) {
throw std::runtime_error("Expected >");
}

// skip_till<']'>(it, end);
IGUANA_INLINE void skip_till_square_bracket(auto &&it, auto &&end) {
static_assert(std::contiguous_iterator<std::decay_t<decltype(it)>>);

auto has_zero = [](uint64_t chunk) {
return (((chunk - 0x0101010101010101) & ~chunk) & 0x8080808080808080);
};
auto has_square_bracket = [&](uint64_t chunk) {
return has_zero(chunk ^ 0b010111010101110101011101010111010101110101011101);
};
if (std::distance(it, end) >= 7) [[likely]] {
const auto end_m7 = end - 7;
for (; it < end_m7; it += 8) {
const auto chunk = *reinterpret_cast<const uint64_t *>(&*it);
uint64_t test = has_square_bracket(chunk);
if (test != 0) {
it += (std::countr_zero(test) >> 3);
return;
}
}
}

// Tail end of buffer. Should be rare we even get here
while (it < end) {
switch (*it) {
case ']':
return;
}
++it;
}
throw std::runtime_error("Expected ]");
}

IGUANA_INLINE auto skip_pass_smaller(auto &&it, auto &&end) {
skip_till_smaller(it, end);
auto res = it++ - 1;
Expand Down
8 changes: 4 additions & 4 deletions test/test_xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,22 +319,22 @@ struct test_exception_t {
REFLECTION(test_exception_t, a, b, c);
TEST_CASE("test exception") {
{
std::string str = "<a>3.14</a>";
std::string str = "<root> <a>d3</a> </root>";
test_exception_t t;
CHECK_THROWS(iguana::from_xml(t, str));
}
{
std::string str = "<b>TURE</b>";
std::string str = "<root> <b>TURE</b> </root>";
test_exception_t t;
CHECK_THROWS(iguana::from_xml(t, str));
}
{
std::string str = "<c>ab</c>";
std::string str = "<root> <c>ab</c> </root>";
test_exception_t t;
CHECK_THROWS(iguana::from_xml(t, str));
}
{
std::string str = "<d>ab</d>";
std::string str = "<root> <d>ab</d> </root>";
test_exception_t t;
CHECK_THROWS(iguana::from_xml(t, str));
}
Expand Down

0 comments on commit 42981d6

Please sign in to comment.