Skip to content

Commit

Permalink
[strcut_xml][fix] fix parse Chinese text in xml (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Dec 20, 2023
1 parent 3850f60 commit 72238a5
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 29 deletions.
5 changes: 3 additions & 2 deletions include/ylt/thirdparty/iguana/define.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#pragma once

#include <array>
#if __cplusplus >= 202002L
#if (defined(_MSC_VER) && _MSC_VER >= 1920 && _MSVC_LANG >= 202002L) || \
(!defined(_MSC_VER) && defined(__cplusplus) && __cplusplus >= 202002L)
#include <bit>
#endif
#include <array>
#include <string>
#include <string_view>
#include <type_traits>
Expand Down
11 changes: 11 additions & 0 deletions include/ylt/thirdparty/iguana/enum_reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include "frozen/unordered_map.h"

namespace iguana {

#if defined(__clang__) || defined(_MSC_VER) || \
(defined(__GNUC__) && __GNUC__ > 8)

template <typename T>
constexpr std::string_view get_raw_name() {
#ifdef _MSC_VER
Expand Down Expand Up @@ -136,6 +140,8 @@ constexpr inline auto get_str_to_enum_map(
{enum_names[Is], enum_values[Is]}...};
}

#endif

// the default generic enum_value
// if the user has not defined a specialization template, this will be called
template <typename T>
Expand All @@ -145,6 +151,8 @@ struct enum_value {

template <bool str_to_enum, typename E>
constexpr inline auto get_enum_map() {
#if defined(__clang__) || defined(_MSC_VER) || \
(defined(__GNUC__) && __GNUC__ > 8)
constexpr auto &arr = enum_value<E>::value;
constexpr auto arr_size = arr.size();
if constexpr (arr_size > 0) {
Expand All @@ -166,6 +174,9 @@ constexpr inline auto get_enum_map() {
else {
return false;
}
#else
return false;
#endif
}

#if defined(__clang__) && (__clang_major__ >= 17)
Expand Down
10 changes: 0 additions & 10 deletions include/ylt/thirdparty/iguana/json_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,6 @@ IGUANA_INLINE void skip_ws_no_comments(It &&it, It &&end) {
}
}

inline constexpr auto has_zero = [](uint64_t chunk) IGUANA__INLINE_LAMBDA {
return (((chunk - 0x0101010101010101) & ~chunk) & 0x8080808080808080);
};

inline constexpr auto has_qoute = [](uint64_t chunk) IGUANA__INLINE_LAMBDA {
return has_zero(
chunk ^
0b0010001000100010001000100010001000100010001000100010001000100010);
};

inline constexpr auto has_escape = [](uint64_t chunk) IGUANA__INLINE_LAMBDA {
return has_zero(
chunk ^
Expand Down
3 changes: 3 additions & 0 deletions include/ylt/thirdparty/iguana/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,9 @@ constexpr std::array<frozen::string, N> get_alias_arr(Args... pairs) {
using size_type = std::integral_constant< \
size_t, std::tuple_size_v<decltype(std::make_tuple(__VA_ARGS__))>>; \
constexpr static std::string_view name() { return ALIAS; } \
constexpr static std::string_view struct_name() { \
return std::string_view(#STRUCT_NAME, sizeof(#STRUCT_NAME) - 1); \
} \
constexpr static size_t value() { return size_type::value; } \
constexpr static std::array<frozen::string, size_type::value> arr() { \
return iguana::detail::get_alias_arr<size_type::value>(__VA_ARGS__); \
Expand Down
10 changes: 10 additions & 0 deletions include/ylt/thirdparty/iguana/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,14 @@ IGUANA_INLINE void match(It &&it, It &&end) {
}
}

inline constexpr auto has_zero = [](uint64_t chunk) IGUANA__INLINE_LAMBDA {
return (((chunk - 0x0101010101010101) & ~chunk) & 0x8080808080808080);
};

inline constexpr auto has_qoute = [](uint64_t chunk) IGUANA__INLINE_LAMBDA {
return has_zero(
chunk ^
0b0010001000100010001000100010001000100010001000100010001000100010);
};

} // namespace iguana
12 changes: 1 addition & 11 deletions include/ylt/thirdparty/iguana/xml_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ struct is_cdata_t<xml_cdata_t<T>> : std::true_type {};
template <typename T>
constexpr inline bool cdata_v = is_cdata_t<std::remove_cvref_t<T>>::value;

inline constexpr auto has_zero = [](uint64_t chunk) IGUANA__INLINE_LAMBDA {
return (((chunk - 0x0101010101010101) & ~chunk) & 0x8080808080808080);
};

inline constexpr auto has_greater = [](uint64_t chunk) IGUANA__INLINE_LAMBDA {
return has_zero(
chunk ^
Expand Down Expand Up @@ -82,15 +78,9 @@ inline constexpr auto has_equal = [](uint64_t chunk) IGUANA__INLINE_LAMBDA {
0b0011110100111101001111010011110100111101001111010011110100111101);
};

inline constexpr auto has_qoute = [](uint64_t chunk) IGUANA__INLINE_LAMBDA {
return has_zero(
chunk ^
0b0010001000100010001000100010001000100010001000100010001000100010);
};

template <typename It>
IGUANA_INLINE void skip_sapces_and_newline(It &&it, It &&end) {
while (it != end && (*it < 33)) {
while (it != end && (static_cast<uint8_t>(*it) < 33)) {
++it;
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/ylt/thirdparty/iguana/xml_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ IGUANA_INLINE void render_xml_value(Stream &ss, T &&t, std::string_view name) {
using value_type = underline_type_t<decltype(t.*v)>;
constexpr auto Idx = decltype(i)::value;
constexpr auto Count = M::value();
constexpr std::string_view tag_name =
[[maybe_unused]] constexpr std::string_view tag_name =
std::string_view(get_name<std::decay_t<T>, Idx>().data(),
get_name<std::decay_t<T>, Idx>().size());
static_assert(Idx < Count);
Expand Down
9 changes: 5 additions & 4 deletions include/ylt/thirdparty/iguana/yaml_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,8 @@ IGUANA_INLINE void parse_item(U &value, It &&it, It &&end, size_t min_spaces) {
using value_type = std::decay_t<decltype(v)>;
skip_space_and_lines(it, end, spaces);
match<'-'>(it, end);
auto subspaces = skip_space_and_lines(it, end, spaces + 1);
[[maybe_unused]] auto subspaces =
skip_space_and_lines(it, end, spaces + 1);
if constexpr (string_v<value_type>) {
parse_item(v, it, end, spaces + 1);
}
Expand All @@ -361,10 +362,10 @@ IGUANA_INLINE void parse_item(U &value, It &&it, It &&end, size_t min_spaces) {
using T = std::remove_reference_t<U>;
using key_type = typename T::key_type;
using value_type = typename T::mapped_type;
auto spaces = skip_space_and_lines(it, end, min_spaces);
[[maybe_unused]] auto spaces = skip_space_and_lines(it, end, min_spaces);
if (*it == '{') {
++it;
auto subspaces = skip_space_and_lines(it, end, min_spaces);
[[maybe_unused]] auto subspaces = skip_space_and_lines(it, end, min_spaces);
while (it != end) {
if (*it == '}')
IGUANA_UNLIKELY {
Expand Down Expand Up @@ -470,7 +471,7 @@ IGUANA_INLINE void parse_item(U &value, It &&it, It &&end, size_t min_spaces) {

template <typename It>
IGUANA_INLINE void skip_object_value(It &&it, It &&end, size_t min_spaces) {
int subspace = min_spaces;
size_t subspace = min_spaces;
while (it != end) {
while (it != end && *it != '\n') {
++it;
Expand Down
2 changes: 1 addition & 1 deletion include/ylt/thirdparty/iguana/yaml_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace iguana {
// return true when it==end
template <typename It>
IGUANA_INLINE bool skip_space_till_end(It &&it, It &&end) {
while (it != end && *it < 33) ++it;
while (it != end && (static_cast<uint8_t>(*it) < 33)) ++it;
return it == end;
}

Expand Down
7 changes: 7 additions & 0 deletions src/struct_xml/examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ void test_inner_object() {
iguana::from_xml(obj1, str);
assert(obj1.get_id() == 20);
assert(obj1.get_name() == "tom");

std::string xml_str =
R"(<some_object><id>20</id><name>小强</name></some_object>)";
some_object obj2;
iguana::from_xml(obj2, xml_str);
assert(obj2.get_id() == 20);
assert(obj2.get_name() == "小强");
}

struct shared_object {
Expand Down

0 comments on commit 72238a5

Please sign in to comment.