Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sequential parse firstly #191

Merged
merged 7 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions benchmark/xml_bench.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ struct item_t {
std::string_view itunes_explicit;
std::string_view itunes_duration;
std::string_view dc_creator;
std::string_view author;
std::optional<std::string_view> media_thumbnail;
std::string_view author;
std::optional<std::string_view> media_content;
std::string_view itunes_keywords;
};
REFLECTION(item_t, title, link, description, pubDate, enclosure, guid,
itunes_author, itunes_subtitle, itunes_summary, itunes_explicit,
itunes_duration, dc_creator, author, media_thumbnail, media_content,
itunes_duration, dc_creator, media_thumbnail, author, media_content,
itunes_keywords);

struct image_t {
Expand All @@ -73,7 +73,6 @@ struct channel_t {
image_t image;
std::string_view itunes_author;
std::string_view itunes_subtitle;
std::string_view itunes_summary;
std::string_view itunes_keywords;
std::optional<std::string_view> itunes_image;
std::string_view itunes_explicit;
Expand All @@ -85,8 +84,8 @@ struct channel_t {
};
REFLECTION(channel_t, title, link, description, generator, docs, language,
pubDate, lastBuildDate, image, itunes_author, itunes_subtitle,
itunes_summary, itunes_keywords, itunes_image, itunes_explicit,
itunes_block, item, media_credit, media_rating, media_description);
itunes_keywords, itunes_image, itunes_explicit, itunes_block, item,
media_credit, media_rating, media_description);

struct rss_t {
channel_t channel;
Expand Down
72 changes: 36 additions & 36 deletions data/sample_rss.xml

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions example/xml_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,59 @@ void cdata_example() {
std::cout << ss << "\n\n";
}

struct city_t {
iguana::xml_attr_t<long> area;
iguana::xml_cdata_t<> cd;
std::string name;
};
REFLECTION(city_t, area, cd, name);
struct cities_t {
std::vector<city_t> city;
};
REFLECTION(cities_t, city);
struct province {
iguana::xml_attr_t<long> area;
iguana::xml_cdata_t<> cd;
std::unique_ptr<cities_t> cities;
std::string capital;
};
REFLECTION(province, area, cd, cities, capital);

void province_example() {
std::string str = R"(
<province name="Sichuan Province">
<capital>Chengdu</capital>
<![CDATA[ sichuan <> ]]>
<area unit="km^2">485000</area>
<cities>
<city>
<name>Chengdu City</name>
<![CDATA[ chengdu <> ]]>
<area unit="km^2">14412</area>
</city>
<city>
<name>Suining City</name>
<![CDATA[ Suining <> ]]>
<area unit="km^2">20788</area>
</city>
<!-- More cities -->
</cities>
</province>
)";
province p;
iguana::from_xml(p, str);
std::cout << "========= serialize province ========\n";
std::string ss;
iguana::to_xml(p, ss);
std::cout << ss << "n\n";
}

int main(void) {
some_type_example();
lib_example();
package_example();
derived_object();
cdata_example();
province_example();
return 0;
}
122 changes: 84 additions & 38 deletions iguana/xml_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,11 @@ IGUANA_INLINE void parse_attr(U &&value, It &&it, It &&end) {
template <plain_t U, typename It>
IGUANA_INLINE void parse_item(U &value, It &&it, It &&end,
std::string_view name) {
skip_till_greater(it, end);
skip_till<'>'>(it, end);
++it;
skip_sapces_and_newline(it, end);
auto value_begin = it;
auto value_end = skip_pass_smaller(it, end);
;
auto value_end = skip_pass<'<'>(it, end);
parse_value(value, value_begin, value_end);
match_close_tag(it, end, name);
}
Expand All @@ -97,7 +96,7 @@ IGUANA_INLINE void parse_item(U &value, It &&it, It &&end,
--it;
return;
} else {
skip_till_greater(it, end);
skip_till<'>'>(it, end);
++it;
skip_sapces_and_newline(it, end);
continue;
Expand All @@ -120,7 +119,7 @@ template <optional_t U, typename It>
IGUANA_INLINE void parse_item(U &value, It &&it, It &&end,
std::string_view name) {
using value_type = typename std::remove_reference_t<U>::value_type;
skip_till_greater(it, end);
skip_till<'>'>(it, end);
if (*(it - 1) == '/') [[likely]] {
++it;
return;
Expand All @@ -129,11 +128,11 @@ IGUANA_INLINE void parse_item(U &value, It &&it, It &&end,
if constexpr (plain_t<value_type>) {
// The following code is for option not to be emplaced
// when parse "...> <..."
skip_till_greater(it, end);
skip_till<'>'>(it, end);
++it;
skip_sapces_and_newline(it, end);
auto value_begin = it;
auto value_end = skip_pass_smaller(it, end);
auto value_end = skip_pass<'<'>(it, end);
if (value_begin == value_end) {
match_close_tag(it, end, name);
return;
Expand Down Expand Up @@ -163,7 +162,7 @@ IGUANA_INLINE void parse_item(U &value, It &&it, It &&end,
// loose inspection here
template <typename It>
IGUANA_INLINE void skip_object_value(It &&it, It &&end, std::string_view name) {
skip_till_greater(it, end);
skip_till<'>'>(it, end);
++it;
if (*(it - 2) == '/') {
// .../>
Expand All @@ -172,34 +171,30 @@ IGUANA_INLINE void skip_object_value(It &&it, It &&end, std::string_view name) {
// </name>
size_t size = name.size();
while (it != end) {
skip_till_greater(it, end);
skip_till<'>'>(it, end);
if (*(it - size - 2) == '<' && *(it - size - 1) == '/' &&
(std::string_view{&*(it - size), size} == name)) {
++it;
return;
}
++it;
skip_sapces_and_newline(it, end);
}
throw std::runtime_error("unclosed tag: " + std::string(name));
}

template <refletable T, typename It>
IGUANA_INLINE void parse_item(T &value, It &&it, It &&end,
std::string_view name) {
constexpr auto cdata_idx = get_type_index<is_cdata_t, std::decay_t<T>>();
skip_till_greater(it, end);
++it;
// return true means reach the close tag
template <size_t cdata_idx, refletable T, typename It>
IGUANA_INLINE auto skip_till_key(T &value, It &&it, It &&end) {
skip_sapces_and_newline(it, end);
while (it != end) {
while (true) {
match<'<'>(it, end);

if (*it == '/') [[unlikely]] {
// </tag>
match_close_tag(it, end, name);
return; // reach the close tag
return true; // reach the close tag
} else if (*it == '?') [[unlikely]] {
// <? ... ?>
skip_till_greater(it, end);
skip_till<'>'>(it, end);
++it;
skip_sapces_and_newline(it, end);
continue;
Expand All @@ -209,7 +204,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_square_bracket(it, end);
skip_till<']'>(it, end);
++it;
match<"]>">(it, end);
skip_sapces_and_newline(it, end);
Expand All @@ -222,7 +217,7 @@ IGUANA_INLINE void parse_item(T &value, It &&it, It &&end,
auto &cdata_value = get<cdata_idx>(value);
using VT = typename std::decay_t<decltype(cdata_value)>::value_type;
auto vb = it;
auto ve = skip_pass_square_bracket(it, end);
auto ve = skip_pass<']'>(it, end);
if constexpr (str_view_t<VT>) {
cdata_value.value() =
VT(&*vb, static_cast<size_t>(std::distance(vb, ve)));
Expand All @@ -234,27 +229,72 @@ IGUANA_INLINE void parse_item(T &value, It &&it, It &&end,
skip_sapces_and_newline(it, end);
continue;
}
} else if (*it == 'D') {
// <!D
++it;
skip_till_greater(it, end);
++it;
skip_sapces_and_newline(it, end);
continue;
} else {
// <!-- -->
++it;
skip_till_greater(it, end);
// <!D
skip_till<'>'>(it, end);
++it;
skip_sapces_and_newline(it, end);
continue;
}
}
return false;
}
}

auto start = it;
template <refletable T, typename It>
IGUANA_INLINE void parse_item(T &value, It &&it, It &&end,
std::string_view name) {
constexpr auto cdata_idx = get_type_index<is_cdata_t, std::decay_t<T>>();
skip_till<'>'>(it, end);
++it;
if (skip_till_key<cdata_idx>(value, it, end)) {
match_close_tag(it, end, name);
return;
}
auto start = it;
skip_till_greater_or_space(it, end);
std::string_view key =
std::string_view{&*start, static_cast<size_t>(std::distance(start, it))};
bool parse_done = false;
// sequential parse
for_each(value, [&](const auto member_ptr, auto i) IGUANA__INLINE_LAMBDA {
static_assert(std::is_member_pointer_v<std::decay_t<decltype(member_ptr)>>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this assert is not necessary.

"type must be member ptr");
using M = decltype(iguana_reflect_members(value));
using item_type = std::remove_reference_t<decltype(value.*member_ptr)>;
constexpr auto Idx = decltype(i)::value;
constexpr auto Count = M::value();
static_assert(Idx < Count);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not necessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i will update it later

constexpr auto mkey = M::arr()[Idx];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simplify: constexpr auto mkey = iguana::get_name<T, i::value>();

constexpr std::string_view st_key(mkey.data(), mkey.size());
if constexpr (cdata_t<item_type>) {
return;
}
if (key != st_key) [[unlikely]] {
return;
}
if constexpr (!cdata_t<item_type>) {
parse_item(value.*member_ptr, it, end, key);
}
if (skip_till_key<cdata_idx>(value, it, end)) {
match_close_tag(it, end, name);
parse_done = true;
return;
}
if (parse_done) [[unlikely]] {
return;
}
start = it;
skip_till_greater_or_space(it, end);
std::string_view key = std::string_view{
&*start, static_cast<size_t>(std::distance(start, it))};
key = std::string_view{&*start,
static_cast<size_t>(std::distance(start, it))};
});
if (parse_done) [[unlikely]] {
return;
}
// map parse
while (true) {
static constexpr auto frozen_map = get_iguana_struct_map<T>();
const auto &member_it = frozen_map.find(key);
if (member_it != frozen_map.end()) [[likely]] {
Expand All @@ -276,7 +316,14 @@ IGUANA_INLINE void parse_item(T &value, It &&it, It &&end,
skip_object_value(it, end, key);
#endif
}
skip_sapces_and_newline(it, end);
if (skip_till_key<cdata_idx>(value, it, end)) {
match_close_tag(it, end, name);
return;
}
start = it;
skip_till_greater_or_space(it, end);
key = std::string_view{&*start,
static_cast<size_t>(std::distance(start, it))};
}
}

Expand All @@ -288,7 +335,7 @@ IGUANA_INLINE void from_xml(U &value, It &&it, It &&end) {
skip_sapces_and_newline(it, end);
match<'<'>(it, end);
if (*it == '?') {
skip_till_greater(it, end);
skip_till<'>'>(it, end);
++it;
} else {
break;
Expand All @@ -301,14 +348,13 @@ IGUANA_INLINE void from_xml(U &value, It &&it, It &&end) {
detail::parse_attr(value.attr(), it, end);
detail::parse_item(value.value(), it, end, key);
}

template <refletable U, typename It>
IGUANA_INLINE void from_xml(U &value, It &&it, It &&end) {
while (it != end) {
skip_sapces_and_newline(it, end);
match<'<'>(it, end);
if (*it == '?') {
skip_till_greater(it, end);
skip_till<'>'>(it, end);
++it; // skip >
} else {
break;
Expand Down
Loading