| 39 | namespace libtorrent { |
| 40 | |
| 41 | void xml_parse(string_view input |
| 42 | , std::function<void(int, string_view, string_view)> callback) |
| 43 | { |
| 44 | char const* p = input.data(); |
| 45 | char const* end = input.data() + input.size(); |
| 46 | for (;p != end; ++p) |
| 47 | { |
| 48 | char const* start = p; |
| 49 | // look for tag start |
| 50 | for (; p != end && *p != '<'; ++p); |
| 51 | |
| 52 | if (p != start) |
| 53 | { |
| 54 | callback(xml_string, {start, std::size_t(p - start)}, {}); |
| 55 | } |
| 56 | |
| 57 | if (p == end) break; |
| 58 | |
| 59 | // skip '<' |
| 60 | ++p; |
| 61 | if (p != end && p + 8 < end && string_begins_no_case("![CDATA[", p)) |
| 62 | { |
| 63 | // CDATA. match '![CDATA[' |
| 64 | p += 8; |
| 65 | start = p; |
| 66 | while (p != end && !string_begins_no_case("]]>", p - 2)) ++p; |
| 67 | |
| 68 | // parse error |
| 69 | if (p == end) |
| 70 | { |
| 71 | callback(xml_parse_error, "unexpected end of file", {}); |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | callback(xml_string, {start, std::size_t(p - start - 2)}, {}); |
| 76 | continue; |
| 77 | } |
| 78 | |
| 79 | // parse the name of the tag. |
| 80 | for (start = p; p != end && *p != '>' && !is_space(*p); ++p); |
| 81 | |
| 82 | char const* tag_name_end = p; |
| 83 | |
| 84 | // skip the attributes for now |
| 85 | for (; p != end && *p != '>'; ++p); |
| 86 | |
| 87 | // parse error |
| 88 | if (p == end) |
| 89 | { |
| 90 | callback(xml_parse_error, "unexpected end of file", {}); |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | TORRENT_ASSERT(*p == '>'); |
| 95 | |
| 96 | char const* tag_end = p; |
| 97 | if (*start == '/') |
| 98 | { |