diff --git a/include/parse.hpp b/include/parse.hpp index ab2381e..cce2f05 100644 --- a/include/parse.hpp +++ b/include/parse.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #define SPACES " \t" @@ -37,9 +38,9 @@ struct list_parse_options { }; // globals - -extern const std::vector posix_cmdvar; -extern const std::vector bash_cmdvar; +extern const std::set all_reserved_words; +extern const std::set posix_cmdvar; +extern const std::set bash_cmdvar; std::string import_file(std::string const& path); diff --git a/include/util.hpp b/include/util.hpp index f1fc52d..0533bd5 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -24,8 +24,6 @@ inline bool is_dev_file(std::string const& filename) { return filename.substr(0, std::string indent(int n); -bool is_among(std::string const& in, std::vector const& values); - std::vector split(std::string const& in, const char* splitters); std::vector split(std::string const& in, char c); @@ -137,6 +135,12 @@ bool is_in_vector(T el, std::vector vec) return false; } +template +bool is_in_set(T el, std::set ss) +{ + return ss.find(el) != ss.end(); +} + std::set prune_matching(std::set& in, std::regex re); std::string delete_brackets(std::string const& in); diff --git a/src/minify.cpp b/src/minify.cpp index 5c142e4..4c5295e 100644 --- a/src/minify.cpp +++ b/src/minify.cpp @@ -310,6 +310,7 @@ void minify_fct(_obj* in, std::regex const& exclude) concat_sets(allcmds, m_excluded_fct); concat_sets(allcmds, unsets); // create mapping + concat_sets(allcmds, all_reserved_words); fctmap=gen_minimal_map(m_fcts, allcmds); // perform replace recurse(r_replace_fct, in, &fctmap); diff --git a/src/parse.cpp b/src/parse.cpp index 13d88cb..e8a64a0 100644 --- a/src/parse.cpp +++ b/src/parse.cpp @@ -14,14 +14,14 @@ // macro // constants -const std::vector posix_cmdvar = { "export", "unset", "local", "read", "getopts" }; -const std::vector bash_cmdvar = { "readonly", "declare", "typeset" }; +const std::set posix_cmdvar = { "export", "unset", "local", "read", "getopts" }; +const std::set bash_cmdvar = { "readonly", "declare", "typeset" }; -const std::vector arithmetic_precedence_operators = { "!", "~", "+", "-" }; -const std::vector arithmetic_operators = { "+", "-", "*", "/", "+=", "-=", "*=", "/=", "=", "==", "!=", "&", "|", "^", "<<", ">>", "&&", "||" }; +const std::set arithmetic_precedence_operators = { "!", "~", "+", "-" }; +const std::set arithmetic_operators = { "+", "-", "*", "/", "+=", "-=", "*=", "/=", "=", "==", "!=", "&", "|", "^", "<<", ">>", "&&", "||" }; -const std::vector all_reserved_words = { "if", "then", "else", "fi", "case", "esac", "for", "while", "do", "done", "{", "}" }; -const std::vector out_reserved_words = { "then", "else", "fi", "esac", "do", "done", "}" }; +const std::set all_reserved_words = { "if", "then", "else", "fi", "case", "esac", "for", "while", "do", "done", "{", "}" }; +const std::set out_reserved_words = { "then", "else", "fi", "esac", "do", "done", "}" }; // stuff @@ -276,7 +276,7 @@ std::pair parse_arithmetic(parse_context ctx) } auto po = get_operator(ctx); - if(is_among(po.first, arithmetic_precedence_operators)) + if(is_in_set(po.first, arithmetic_precedence_operators)) { ctx.i = po.second; auto pa = parse_arithmetic(ctx); @@ -335,7 +335,7 @@ std::pair parse_arithmetic(parse_context ctx) auto po = get_operator(ctx); if(po.first != "") { - if(!is_among(po.first, arithmetic_operators)) + if(!is_in_set(po.first, arithmetic_operators)) { parse_error( "Unknown arithmetic operator: "+po.first, ctx); } @@ -1289,9 +1289,10 @@ std::pair parse_cmd(parse_context ctx) ctx = parse_cmd_varassigns(ret, ctx); auto wp=get_word(ctx, ARG_END); - if(is_in_vector(wp.first, posix_cmdvar) || is_in_vector(wp.first, bash_cmdvar)) + bool is_bash_cmdvar=false; + if(is_in_set(wp.first, posix_cmdvar) || (is_bash_cmdvar=is_in_set(wp.first, bash_cmdvar)) ) { - if(!ctx.bash && is_in_vector(wp.first, bash_cmdvar)) + if(!ctx.bash && (is_bash_cmdvar || is_in_set(wp.first, bash_cmdvar))) { parse_error("bash specific: "+wp.first, ctx); } @@ -1627,7 +1628,7 @@ std::pair parse_block(parse_context ctx) ret = pp.first; ctx = pp.second; } - else if(is_in_vector(word, out_reserved_words)) // is a reserved word + else if(is_in_set(word, out_reserved_words)) // is a reserved word { parse_error( strf("Unexpected '%s'", word.c_str())+expecting(ctx.expecting) , ctx); ctx.i+=word.size(); diff --git a/src/processing.cpp b/src/processing.cpp index 217e98a..ace54c4 100644 --- a/src/processing.cpp +++ b/src/processing.cpp @@ -160,7 +160,7 @@ std::string get_varname(arg* in) bool cmd_is_argvar(std::string const& in) { - return is_in_vector(in, posix_cmdvar) || is_in_vector(in, bash_cmdvar); + return is_in_set(in, posix_cmdvar) || is_in_set(in, bash_cmdvar); } bool cmd::is_argvar() diff --git a/src/util.cpp b/src/util.cpp index 18a1520..bea1a46 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -45,14 +45,6 @@ std::string dirname(std::string const& in) return "."; } -bool is_among(std::string const& in, std::vector const& values) -{ - for(auto it: values) - if(in == it) - return true; - return false; -} - std::vector split(std::string const& in, const char* splitters) { uint32_t i=0,j=0;