Skip to content

Commit

Permalink
Moving the pair to basic_string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryn Aspestrand committed Aug 26, 2024
1 parent f1e095c commit 5991601
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 42 deletions.
29 changes: 6 additions & 23 deletions adobe/implementation/lex_shared_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include <functional>
#include <iterator>
#include <string_view>
#include <type_traits>
#include <utility>

#include <boost/operators.hpp>
Expand Down Expand Up @@ -140,26 +142,7 @@ and second value are equivalent, but any value. Typically this value is
0, but this is not required.
*/

using token_range_t = std::pair<uchar_ptr_t, uchar_ptr_t>;

/**************************************************************************************************/

/*!
\ingroup asl_xml_parser
Determines the size of a token range.
Determines the character count represented by the range (does not consider
interstitial null terminators).
\param r the range we are determining the size of
\return the distance between the first and second members of the range
*/

inline auto token_range_size(const token_range_t& r) {
return std::distance(r.first, r.second);
}
using token_range_t = std::basic_string_view<std::remove_pointer_t<uchar_ptr_t>>;

/**************************************************************************************************/

Expand All @@ -179,7 +162,7 @@ in equality for each set of characters. false otherwise.
*/

inline bool token_range_equal(const token_range_t& x, const token_range_t& y) {
return adobe::token_range_size(x) == adobe::token_range_size(y) && adobe::bounded_equal(x, y);
return x.size() == y.size() && adobe::bounded_equal(x, y);
}

/**************************************************************************************************/
Expand Down Expand Up @@ -208,8 +191,8 @@ as adobe::mismatch.
*/

inline bool token_range_less(const token_range_t& x, const token_range_t& y) {
const auto sizex{adobe::token_range_size(x)};
const auto sizey{adobe::token_range_size(y)};
const auto sizex{x.size()};
const auto sizey{y.size()};

if (sizex < sizey) {
return true;
Expand Down
18 changes: 8 additions & 10 deletions adobe/xml_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ inline token_range_t xml_element_linefeed(const token_range_t& /*entire_element_
const attribute_set_t& attribute_set,
const token_range_t& value) {
if (token_range_equal(name, static_token_range("br")) && attribute_set.empty() &&
adobe::token_range_size(value) == 0) {
value.size() == 0) {
#if ADOBE_PLATFORM_WIN
return static_token_range("&cr;&lf;");
#elif ADOBE_PLATFORM_MAC
Expand Down Expand Up @@ -869,7 +869,7 @@ bool xml_parser_t<O>::is_content(token_range_t& content) {
token_range_t result;

if (is_token(xml_token_reference_k, result)) {
if (adobe::token_range_size(result)) {
if (result.size()) {
if (preorder_mode_m) {
// Again, if we're in preorder mode we're not outputting
// but extending (possibly even starting, too) the token_range
Expand All @@ -888,7 +888,7 @@ bool xml_parser_t<O>::is_content(token_range_t& content) {
}
}
} else if (is_element(result)) {
if (adobe::token_range_size(result)) {
if (result.size()) {
if (preorder_mode_m) {
// Again, if we're in preorder mode we're not outputting
// but extending (possibly even starting, too) the token_range
Expand Down Expand Up @@ -995,17 +995,15 @@ bool xml_parser_t<O>::is_bom(token_range_t& bom) {
token_stream_m.set_skip_white_space(false);

if (is_token(xml_token_char_data_k, bom)) {
if (adobe::token_range_size(utf8_bom) <= adobe::token_range_size(bom) && adobe::equal(utf8_bom, bom.first)) {
if (utf8_bom.size() <= bom.size() && adobe::equal(utf8_bom, bom.first)) {
bom.second = bom.first;
std::advance(bom.second, adobe::token_range_size(utf8_bom));
std::advance(bom.second, utf8_bom.size());

result = true;
} else if (adobe::token_range_size(utf16_be_bom) <= adobe::token_range_size(bom) &&
adobe::equal(utf16_be_bom, bom.first)) {
} else if (utf16_be_bom.size() <= bom.size() && adobe::equal(utf16_be_bom, bom.first)) {
// it's a bom, but it's not a format the parser supports
throw_exception("utf16be bom encountered; xml_parser_t only supports utf8 encoding");
} else if (adobe::token_range_size(utf16_le_bom) <= adobe::token_range_size(bom) &&
adobe::equal(utf16_le_bom, bom.first)) {
} else if (utf16_le_bom.size() <= bom.size() && adobe::equal(utf16_le_bom, bom.first)) {
// it's a bom, but it's not a format the parser supports
throw_exception("utf16le bom encountered; xml_parser_t only supports utf8 encoding");
}
Expand Down Expand Up @@ -1071,7 +1069,7 @@ void xml_parser_t<O>::parse_content() {
// always returns true; have to test results
is_content(content);

if (adobe::token_range_size(content)) {
if (content.size()) {
token_range_t result(
this->callback_m(content, token_range_t(), attribute_set_t(), content));

Expand Down
2 changes: 1 addition & 1 deletion adobe/xstring.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ struct xstring_context_t : boost::noncopyable {
void glossary_parse() {
implementation::context_frame_t& context(implementation::top_frame());

if (context.parsed_m || !adobe::token_range_size(context.slurp_m)) {
if (context.parsed_m || !context.slurp_m.size()) {
return;
}

Expand Down
12 changes: 6 additions & 6 deletions documentation/asl/xml_parser.dox
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ token_range_t my_content_callback(
static const token_range_t id_attr_k( static_token_range("id") );

const token_range_t id( attribute_set[id_addr_k] );
if (0 == adobe::token_range_size(id)) {
if (0 == id.size()) {
throw std::runtime_error("replace-me tags require an id attribute");
}

Expand Down Expand Up @@ -301,23 +301,23 @@ token_range_t my_content_callback(
}
else if (token_range_equal(rect_tag_k, name))
{
if (0 != adobe::token_range_size(value)) {
if (0 != value.size()) {
throw std::runtime_error("rect elements must be empty");
}

draw_rectangle(attribute_set, graphics);
}
else if (token_range_equal(circle_tag_k, name))
{
if (0 != adobe::token_range_size(value)) {
if (0 != value.size()) {
throw std::runtime_error("circle elements must be empty");
}

draw_circle(attribute_set, graphics);
}
else if (token_range_equal(polygon_tag_k, name))
{
if (0 != adobe::token_range_size(value)) {
if (0 != value.size()) {
throw std::runtime_error("polygon elements must be empty");
}

Expand Down Expand Up @@ -424,15 +424,15 @@ token_range_t my_group_callback(
}
else if (token_range_equal(rect_tag_k, name))
{
if (0 != adobe::token_range_size(value)) {
if (0 != value.size()) {
throw std::runtime_error("rect elements must be empty");
}

draw_rectangle(attribute_set, graphics);
}
else if (token_range_equal(circle_tag_k, name))
{
if (0 != adobe::token_range_size(value)) {
if (0 != value.size()) {
throw std::runtime_error("circle elements must be empty");
}

Expand Down
4 changes: 2 additions & 2 deletions test/xml_parser/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ adobe::token_range_t test_content(const adobe::token_range_t& /*entire_element_r
} else if (adobe::token_range_equal(name, result_token_k)) {
const adobe::token_range_t value_attr(attribute_set[value_attr_k]);

if (0 == adobe::token_range_size(value_attr)) {
if (0 == value_attr.size()) {
throw std::runtime_error("result element is missing value attribute");
}

Expand Down Expand Up @@ -221,7 +221,7 @@ adobe::token_range_t expression_content(const adobe::token_range_t& /*entire_ele
} else if (adobe::token_range_equal(name, operand_token_k)) {
const adobe::token_range_t value_attr(attribute_set[value_attr_k]);

if (0 == adobe::token_range_size(value_attr)) {
if (0 == value_attr.size()) {
throw std::runtime_error("result element is missing value attribute");
}

Expand Down

0 comments on commit 5991601

Please sign in to comment.