Skip to content

Commit

Permalink
Copy some improvements from "Initial cleanup", Jonas Bülow (Sep 14, 2…
Browse files Browse the repository at this point in the history
  • Loading branch information
parsley72 committed Dec 16, 2023
1 parent 6df8b24 commit 82fa6a0
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 78 deletions.
23 changes: 16 additions & 7 deletions byte_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace TinyXPath {
*/
class byte_stream {
/// Length of the total string, + 1
unsigned _u_length;
size_t _u_length;
/// Total string
_byte_* _bp_in;
/// Current read position
Expand All @@ -49,12 +49,21 @@ class byte_stream {
public:
/// constructor
byte_stream(const char* cp_in) {
_u_length = strlen(cp_in) + 1;
_bp_in = new _byte_[_u_length];
memcpy(_bp_in, cp_in, _u_length);
_bp_current = _bp_in;
_bp_end = _bp_in + _u_length - 1;
_o_valid = (_bp_current != _bp_end);
if (cp_in) {
_u_length = strlen(cp_in) + 1;
_bp_in = new _byte_[_u_length];
memcpy(_bp_in, cp_in, _u_length);
_bp_current = _bp_in;
_bp_end = _bp_in + _u_length - 1;
_o_valid = (_bp_current != _bp_end);
} else {
_u_length = 1;
_bp_in = new _byte_[_u_length];
_bp_in[0] = 0;
_bp_current = _bp_in;
_bp_end = _bp_in + _u_length - 1;
_o_valid = (_bp_current != _bp_end);
}
}
/// destructor
~byte_stream() {
Expand Down
4 changes: 1 addition & 3 deletions lex_token.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,9 @@ class lex_token {

/// Set the string value of a lexical element
void v_set(lexico lex_in, const char* cp_repre) {
unsigned u_length;

_l_enum = lex_in;
delete[] _cp_value;
u_length = strlen(cp_repre);
const size_t u_length = strlen(cp_repre);
_cp_value = new char[u_length + 1];
strcpy(_cp_value, cp_repre);
}
Expand Down
42 changes: 24 additions & 18 deletions tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,48 +39,54 @@ void token_list::v_tokenize_expression() {
v_set_current_top();
while (ltp_get(1)) {
switch (ltp_get(0)->lex_get_value()) {
case lex_colon:
if (ltp_get(1)->lex_get_value() == lex_colon) {
case lex_colon: {
lex_token* token = ltp_get(1);
if (token != nullptr && token->lex_get_value() == lex_colon) {
v_replace_current(lex_2_colon, "::");
v_delete_next();
} else
v_inc_current(1);
break;
case lex_slash:
if (ltp_get(1)->lex_get_value() == lex_slash) {
} break;
case lex_slash: {
lex_token* token = ltp_get(1);
if (token != nullptr && token->lex_get_value() == lex_slash) {
v_replace_current(lex_2_slash, "//");
v_delete_next();
} else
v_inc_current(1);
break;
case lex_exclam:
if (ltp_get(1)->lex_get_value() == lex_equal) {
} break;
case lex_exclam: {
lex_token* token = ltp_get(1);
if (token != nullptr && token->lex_get_value() == lex_equal) {
v_replace_current(lex_not_equal, "!=");
v_delete_next();
} else
v_inc_current(1);
break;
case lex_lt:
if (ltp_get(1)->lex_get_value() == lex_equal) {
} break;
case lex_lt: {
lex_token* token = ltp_get(1);
if (token != nullptr && token->lex_get_value() == lex_equal) {
v_replace_current(lex_lt_equal, "<=");
v_delete_next();
} else
v_inc_current(1);
break;
case lex_gt:
if (ltp_get(1)->lex_get_value() == lex_equal) {
} break;
case lex_gt: {
lex_token* token = ltp_get(1);
if (token != nullptr && token->lex_get_value() == lex_equal) {
v_replace_current(lex_gt_equal, ">=");
v_delete_next();
} else
v_inc_current(1);
break;
case lex_dot:
if (ltp_get(1)->lex_get_value() == lex_dot) {
} break;
case lex_dot: {
lex_token* token = ltp_get(1);
if (token != nullptr && token->lex_get_value() == lex_dot) {
v_replace_current(lex_2_dot, "..");
v_delete_next();
} else
v_inc_current(1);
break;
} break;
case lex_space:
v_delete_current();
break;
Expand Down
4 changes: 4 additions & 0 deletions xpath_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ string expression_result::S_get_string() {
else
S_res = "false";
break;
case e_invalid:
throw std::runtime_error("Invalid XPath expression");
}
return S_res;
}
Expand Down Expand Up @@ -127,6 +129,8 @@ bool expression_result::o_get_bool() {
return nsp_ptr->u_get_nb_node_in_set() != 0;
case e_bool:
return _o_content;
case e_invalid:
throw std::runtime_error("Invalid XPath expression");
}
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions xpath_expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ distribution.
#ifndef __XPATH_EXPR_H
#define __XPATH_EXPR_H

#include <stdexcept>

#include "node_set.h"
#include "tinyxml2.h"
#include "tinyxpath_conf.h"
Expand Down Expand Up @@ -101,6 +103,8 @@ class expression_result {
case e_node_set:
_ns_set = er_2._ns_set;
break;
case e_invalid:
throw std::runtime_error("Invalid XPath expression");
}
#ifdef TINYXPATH_DEBUG
_S_comment = er_2._S_comment;
Expand Down
15 changes: 8 additions & 7 deletions xpath_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ using namespace TinyXPath;
/// xpath_processor constructor
xpath_processor::xpath_processor(const XMLNode* XNp_source_tree, ///< Source XML tree
const char* cp_xpath_expr) ///< XPath expression
: xpath_stream(cp_xpath_expr) {
: xpath_stream(cp_xpath_expr), _e_error(e_no_error) {
if (XNp_source_tree && cp_xpath_expr)
_XNp_base = XNp_source_tree;
else
Expand Down Expand Up @@ -874,7 +874,7 @@ void xpath_processor::v_execute_absolute_path(

if (o_with_rel) {
int i_1, i_2, i_3;
int i_bak_position, i_current, i_first, i_relative;
int i_current, i_first, i_relative;
string S_lit;

// compute position of the first (absolute) step
Expand All @@ -892,7 +892,6 @@ void xpath_processor::v_execute_absolute_path(
i_first = i_relative;
}
// i_first = i_3 - 1;
i_bak_position = _as_action_store.i_get_position();
_as_action_store.v_set_position(i_first);
if (o_everywhere)
i_relative_action = -1;
Expand Down Expand Up @@ -1411,11 +1410,13 @@ void xpath_processor::v_function_name(unsigned u_nb_arg, ///< Nb of arguments
S_res = "";
if (erpp_arg[0]->_e_type == e_node_set) {
nsp_set = erpp_arg[0]->nsp_get_node_set();
if (nsp_set->u_get_nb_node_in_set())
if (nsp_set->o_is_attrib(0))
if (nsp_set->u_get_nb_node_in_set()) {
if (nsp_set->o_is_attrib(0)) {
S_res = nsp_set->XAp_get_attribute_in_set(0)->Name();
else
} else {
S_res = nsp_set->XNp_get_node_in_set(0)->Value();
}
}
}
break;
default:
Expand Down Expand Up @@ -1482,7 +1483,7 @@ void xpath_processor::v_function_string_length(unsigned u_nb_arg, ///< Nb of ar
if (u_nb_arg != 1)
throw execution_error(28);
S_arg = erpp_arg[0]->S_get_string();
v_push_int(S_arg.length(), "string-length");
v_push_int(static_cast<int>(S_arg.length()), "string-length");
}

/**
Expand Down
3 changes: 1 addition & 2 deletions xpath_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ class xpath_processor : public xpath_stream {
public:
/// Constructor
xpath_processor(const tinyxml2::XMLNode* XNp_source_tree, const char* cp_xpath_expr);
virtual ~xpath_processor() {
}
virtual ~xpath_processor() = default;
expression_result er_compute_xpath();
std::string S_compute_xpath();
int i_compute_xpath();
Expand Down
1 change: 1 addition & 0 deletions xpath_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ void result_and_next::v_dump() {
xpath_stack::xpath_stack() {
_rnp_first = nullptr;
_u_size = 0;
_XNp_root = nullptr;
}

/// Destructor
Expand Down
4 changes: 4 additions & 0 deletions xpath_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ distribution.
#ifndef __XPATH_STREAM_H
#define __XPATH_STREAM_H

#include <stdexcept>

#include "byte_stream.h"
#include "lex_util.h"
#include "tinyxml2.h"
Expand Down Expand Up @@ -189,6 +191,8 @@ class xpath_stream : public byte_stream {
break;
}
break;
case s_end:
throw std::runtime_error("Invalid XPath expression");
}
if (lex_next == lex_null)
state = s_end;
Expand Down
80 changes: 45 additions & 35 deletions xpath_syntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,40 +111,46 @@ bool token_syntax_decoder::o_recognize(xpath_construct xc_current, ///< XPath c
}
break;

case xpath_absolute_location_path:
case xpath_absolute_location_path: {
// [2] AbsoluteLocationPath ::= '/' RelativeLocationPath?
// | AbbreviatedAbsoluteLocationPath
if (!ltp_get(0))
return false;
i_action_counter = i_get_action_counter();
switch (ltp_get(0)->lex_get_value()) {
case lex_slash:
v_inc_current(1);
ltp_freeze = ltp_get(0);
o_temp = o_recognize(xpath_relative_location_path, false);
if (o_temp) {
v_set_current(ltp_freeze);
o_recognize(xpath_relative_location_path, o_final);
if (o_final)
v_action(
xpath_absolute_location_path, xpath_absolute_location_path_slash_rel, i_action_counter);
} else {
v_set_current(ltp_freeze);
lex_token* token = ltp_get(0);
if (token) {
switch (token->lex_get_value()) {
case lex_slash:
v_inc_current(1);
ltp_freeze = ltp_get(0);
o_temp = o_recognize(xpath_relative_location_path, false);
if (o_temp) {
v_set_current(ltp_freeze);
o_recognize(xpath_relative_location_path, o_final);
if (o_final)
v_action(xpath_absolute_location_path, xpath_absolute_location_path_slash_rel,
i_action_counter);
} else {
v_set_current(ltp_freeze);
if (o_final)
v_action(
xpath_absolute_location_path, xpath_absolute_location_path_slash, i_action_counter);
}
break;
case lex_2_slash:
if (!o_recognize(xpath_abbreviated_absolute_location_path, o_final))
return false;
if (o_final)
v_action(
xpath_absolute_location_path, xpath_absolute_location_path_slash, i_action_counter);
}
break;
case lex_2_slash:
if (!o_recognize(xpath_abbreviated_absolute_location_path, o_final))
xpath_absolute_location_path, xpath_absolute_location_path_abbrev, i_action_counter);
break;
default:
return false;
if (o_final)
v_action(xpath_absolute_location_path, xpath_absolute_location_path_abbrev, i_action_counter);
break;
default:
return false;
}
} else {
return false;
}
break;
} break;

case xpath_relative_location_path:
//
Expand Down Expand Up @@ -292,8 +298,9 @@ bool token_syntax_decoder::o_recognize(xpath_construct xc_current, ///< XPath c
v_action(xpath_node_test, xpath_node_test_reserved_keyword, ltp_get(0)->lex_get_value());
v_inc_current(3);
break;
case lex_processing_instruction:
if (ltp_get(2) && ltp_get(2)->lex_get_value() == lex_cparen) {
case lex_processing_instruction: {
lex_token* token = ltp_get(2);
if (token && token->lex_get_value() == lex_cparen) {
// single
v_inc_current(3);
if (o_final)
Expand All @@ -306,7 +313,7 @@ bool token_syntax_decoder::o_recognize(xpath_construct xc_current, ///< XPath c
ltp_get(0)->cp_get_literal());
v_inc_current(1);
}
break;
} break;
default:
if (!o_recognize(xpath_name_test, o_final))
return false;
Expand Down Expand Up @@ -838,16 +845,19 @@ bool token_syntax_decoder::o_recognize(xpath_construct xc_current, ///< XPath c
// | QName
if (!ltp_get(0))
return false;
switch (ltp_get(0)->lex_get_value()) {
case lex_star:

{
lexico lexVal = ltp_get(0)->lex_get_value();
if (lexVal == lex_star) {
v_inc_current(1);
if (o_final)
v_action(xpath_name_test, xpath_name_test_star);
break;
case lex_ncname:
} else if (lexVal == lex_ncname) {
o_qname = false;
if (ltp_get(1) && ltp_get(2) && ltp_get(1)->lex_get_value() == lex_colon) {
if (ltp_get(2)->lex_get_value() == lex_star) {
lex_token* token1 = ltp_get(1);
lex_token* token2 = ltp_get(2);
if (token1 && token2 && token1->lex_get_value() == lex_colon) {
if (token2->lex_get_value() == lex_star) {
v_inc_current(3);
if (o_final)
v_action(xpath_name_test, xpath_name_test_ncname);
Expand All @@ -861,7 +871,7 @@ bool token_syntax_decoder::o_recognize(xpath_construct xc_current, ///< XPath c
if (o_final)
v_action(xpath_name_test, xpath_name_test_qname);
}
break;
}
}
break;

Expand Down
Loading

0 comments on commit 82fa6a0

Please sign in to comment.