Skip to content

Commit

Permalink
Migrating token_range_t off of boost::size (#124)
Browse files Browse the repository at this point in the history
* Migrating token_range_t off of boost::size

* Moving the pair to basic_string_view

* fixing build

* fixing xml parser

* Revert "fixing xml parser"

This reverts commit efc28ab.

* Revert "fixing build"

This reverts commit 1331005.

* Revert "Moving the pair to basic_string_view"

This reverts commit 5991601.

---------

Co-authored-by: Bryn Aspestrand <aspestra@adobe.com>
  • Loading branch information
thinlang and Bryn Aspestrand authored Aug 27, 2024
1 parent 8b1150d commit c7f6b88
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 28 deletions.
35 changes: 28 additions & 7 deletions adobe/implementation/lex_shared_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,26 @@ and second value are equivalent, but any value. Typically this value is
0, but this is not required.
*/

typedef std::pair<uchar_ptr_t, uchar_ptr_t> token_range_t;
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);
}

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

Expand All @@ -160,7 +179,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 boost::size(x) == boost::size(y) && adobe::bounded_equal(x, y);
return adobe::token_range_size(x) == adobe::token_range_size(y) && adobe::bounded_equal(x, y);
}

/**************************************************************************************************/
Expand Down Expand Up @@ -189,18 +208,20 @@ as adobe::mismatch.
*/

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

if (sizex < sizey)
if (sizex < sizey) {
return true;
else if (sizey < sizex)
} else if (sizey < sizex) {
return false;
}

std::pair<uchar_ptr_t, uchar_ptr_t> diff(adobe::mismatch(x, boost::begin(y)));

if (diff.first == boost::end(x))
if (diff.first == boost::end(x)) {
return false;
}

return *diff.first < *diff.second;
}
Expand Down
19 changes: 10 additions & 9 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() &&
boost::size(value) == 0) {
adobe::token_range_size(value) == 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 (boost::size(result)) {
if (adobe::token_range_size(result)) {
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 (boost::size(result)) {
if (adobe::token_range_size(result)) {
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,16 +995,16 @@ 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 (boost::size(utf8_bom) <= boost::size(bom) && adobe::equal(utf8_bom, bom.first)) {
if (adobe::token_range_size(utf8_bom) <= adobe::token_range_size(bom) && adobe::equal(utf8_bom, bom.first)) {
bom.second = bom.first;
std::advance(bom.second, boost::size(utf8_bom));
std::advance(bom.second, adobe::token_range_size(utf8_bom));

result = true;
} else if (boost::size(utf16_be_bom) <= boost::size(bom) &&
} else if (adobe::token_range_size(utf16_be_bom) <= adobe::token_range_size(bom) &&
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 (boost::size(utf16_le_bom) <= boost::size(bom) &&
} else if (adobe::token_range_size(utf16_le_bom) <= adobe::token_range_size(bom) &&
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,13 +1071,14 @@ void xml_parser_t<O>::parse_content() {
// always returns true; have to test results
is_content(content);

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

adobe::copy(result, this->output_m);
} else
} else {
break;
}
}
}

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

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

make_xml_parser(context.slurp_m.first, context.slurp_m.second, context.parse_info_m,
implementation::xstring_preorder_predicate, &implementation::xml_xstr_store,
Expand Down
22 changes: 14 additions & 8 deletions documentation/asl/xml_parser.dox
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,9 @@ 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 == boost::size(id))
if (0 == adobe::token_range_size(id)) {
throw std::runtime_error("replace-me tags require an id attribute");
}

return lookup_replacement_text(id);
}\endverbatim
Expand Down Expand Up @@ -300,22 +301,25 @@ token_range_t my_content_callback(
}
else if (token_range_equal(rect_tag_k, name))
{
if (0 != boost::size(value))
if (0 != adobe::token_range_size(value)) {
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 != boost::size(value))
if (0 != adobe::token_range_size(value)) {
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 != boost::size(value))
if (0 != adobe::token_range_size(value)) {
throw std::runtime_error("polygon elements must be empty");
}

draw_polygon(attribute_set, graphics);
}
Expand Down Expand Up @@ -420,15 +424,17 @@ token_range_t my_group_callback(
}
else if (token_range_equal(rect_tag_k, name))
{
if (0 != boost::size(value))
if (0 != adobe::token_range_size(value)) {
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 != boost::size(value))
if (0 != adobe::token_range_size(value)) {
throw std::runtime_error("circle elements must be empty");
}

draw_circle(attribute_set, graphics);
}
Expand Down
1 change: 0 additions & 1 deletion source/xstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ context_frame_t::exact_match_exists(const adobe::attribute_set_t& attribute_set,
/**************************************************************************************************/

token_range_t context_frame_t::clone(const token_range_t& range) {
// std::size_t len(boost::size(range));
std::string str(boost::begin(range), boost::end(range));
const char* added(pool_m.add(str.c_str()));

Expand Down
6 changes: 4 additions & 2 deletions test/xml_parser/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ 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 == boost::size(value_attr))
if (0 == adobe::token_range_size(value_attr)) {
throw std::runtime_error("result element is missing value attribute");
}

test.expected_m = to_long(value_attr);
} else {
Expand All @@ -220,8 +221,9 @@ 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 == boost::size(value_attr))
if (0 == adobe::token_range_size(value_attr)) {
throw std::runtime_error("result element is missing value attribute");
}

stack.push_back(to_long(value_attr));
} else if (adobe::token_range_equal(name, add_token_k)) {
Expand Down

0 comments on commit c7f6b88

Please sign in to comment.