From dce9e17c34940904bc005a044e60441324abfae7 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Fri, 25 Aug 2017 14:49:44 -0600 Subject: [PATCH] More string_view tweaks --- .../language/chaiscript_algebraic.hpp | 2 +- .../chaiscript/language/chaiscript_common.hpp | 72 +++++++++---------- .../chaiscript/language/chaiscript_eval.hpp | 8 +-- .../language/chaiscript_optimizer.hpp | 6 +- include/chaiscript/utility/fnv1a.hpp | 4 ++ 5 files changed, 44 insertions(+), 48 deletions(-) diff --git a/include/chaiscript/language/chaiscript_algebraic.hpp b/include/chaiscript/language/chaiscript_algebraic.hpp index 06d242a8..d13607a9 100644 --- a/include/chaiscript/language/chaiscript_algebraic.hpp +++ b/include/chaiscript/language/chaiscript_algebraic.hpp @@ -50,7 +50,7 @@ namespace chaiscript return opers[static_cast(t_oper)]; } - constexpr static Opers to_operator(const char * const t_str, bool t_is_unary = false) noexcept + constexpr static Opers to_operator(const std::string_view &t_str, bool t_is_unary = false) noexcept { #ifdef CHAISCRIPT_MSVC #pragma warning(push) diff --git a/include/chaiscript/language/chaiscript_common.hpp b/include/chaiscript/language/chaiscript_common.hpp index 82aa478b..2c96efe3 100644 --- a/include/chaiscript/language/chaiscript_common.hpp +++ b/include/chaiscript/language/chaiscript_common.hpp @@ -32,54 +32,46 @@ struct AST_Node; namespace chaiscript { struct Name_Validator { - static bool is_reserved_word(const std::string &name) noexcept + template + static bool is_reserved_word(const T &s) noexcept { - static const std::unordered_set words { - "def", "fun", "while", "for", "if", "else", - "&&", "||", ",", "auto", "return", "break", - "true", "false", "class", "attr", "var", "global", - "GLOBAL", "_", - "__LINE__", "__FILE__", "__FUNC__", "__CLASS__" - }; + const static std::unordered_set words{ + utility::fnv1a_32("def"), + utility::fnv1a_32("fun"), + utility::fnv1a_32("while"), + utility::fnv1a_32("for"), + utility::fnv1a_32("if"), + utility::fnv1a_32("else"), + utility::fnv1a_32("&&"), + utility::fnv1a_32("||"), + utility::fnv1a_32(","), + utility::fnv1a_32("auto"), + utility::fnv1a_32("return"), + utility::fnv1a_32("break"), + utility::fnv1a_32("true"), + utility::fnv1a_32("false"), + utility::fnv1a_32("class"), + utility::fnv1a_32("attr"), + utility::fnv1a_32("var"), + utility::fnv1a_32("global"), + utility::fnv1a_32("GLOBAL"), + utility::fnv1a_32("_"), + utility::fnv1a_32("__LINE__"), + utility::fnv1a_32("__FILE__"), + utility::fnv1a_32("__FUNC__"), + utility::fnv1a_32("__CLASS__")}; - return words.count(name) == 1; + return words.count(utility::fnv1a_32(s)) == 1; } - static bool is_reserved_word(const std::string_view &name) noexcept - { - static const std::unordered_set words { - "def", "fun", "while", "for", "if", "else", - "&&", "||", ",", "auto", "return", "break", - "true", "false", "class", "attr", "var", "global", - "GLOBAL", "_", - "__LINE__", "__FILE__", "__FUNC__", "__CLASS__" - }; - - return words.count(name) == 1; - } - - static bool valid_object_name(const std::string &name) noexcept + template + static bool valid_object_name(const T &name) noexcept { return name.find("::") == std::string::npos && !is_reserved_word(name); } - static void validate_object_name(const std::string &name) - { - if (is_reserved_word(name)) { - throw exception::reserved_word_error(name); - } - - if (name.find("::") != std::string::npos) { - throw exception::illegal_name_error(name); - } - } - - static bool valid_object_name(const std::string_view &name) noexcept - { - return name.find("::") == std::string::npos && !is_reserved_word(name); - } - - static void validate_object_name(const std::string_view &name) + template + static void validate_object_name(const T &name) { if (is_reserved_word(name)) { throw exception::reserved_word_error(std::string(name)); diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 6716dd9e..23557ca1 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -159,7 +159,7 @@ namespace chaiscript struct Fold_Right_Binary_Operator_AST_Node : AST_Node_Impl { Fold_Right_Binary_Operator_AST_Node(const std::string &t_oper, Parse_Location t_loc, std::vector> t_children, Boxed_Value t_rhs) : AST_Node_Impl(t_oper, AST_Node_Type::Binary, std::move(t_loc), std::move(t_children)), - m_oper(Operators::to_operator(t_oper.c_str())), + m_oper(Operators::to_operator(t_oper)), m_rhs(std::move(t_rhs)) { } @@ -204,7 +204,7 @@ namespace chaiscript struct Binary_Operator_AST_Node : AST_Node_Impl { Binary_Operator_AST_Node(const std::string &t_oper, Parse_Location t_loc, std::vector> t_children) : AST_Node_Impl(t_oper, AST_Node_Type::Binary, std::move(t_loc), std::move(t_children)), - m_oper(Operators::to_operator(t_oper.c_str())) + m_oper(Operators::to_operator(t_oper)) { } Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override { @@ -420,7 +420,7 @@ namespace chaiscript struct Equation_AST_Node final : AST_Node_Impl { Equation_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector> t_children) : AST_Node_Impl(std::move(t_ast_node_text), AST_Node_Type::Equation, std::move(t_loc), std::move(t_children)), - m_oper(Operators::to_operator(this->text.c_str())) + m_oper(Operators::to_operator(this->text)) { assert(this->children.size() == 2); } @@ -1162,7 +1162,7 @@ namespace chaiscript struct Prefix_AST_Node final : AST_Node_Impl { Prefix_AST_Node(std::string t_ast_node_text, Parse_Location t_loc, std::vector> t_children) : AST_Node_Impl(std::move(t_ast_node_text), AST_Node_Type::Prefix, std::move(t_loc), std::move(t_children)), - m_oper(Operators::to_operator(this->text.c_str(), true)) + m_oper(Operators::to_operator(this->text, true)) { } Boxed_Value eval_internal(const chaiscript::detail::Dispatch_State &t_ss) const override{ diff --git a/include/chaiscript/language/chaiscript_optimizer.hpp b/include/chaiscript/language/chaiscript_optimizer.hpp index 877cb4a7..3df867f1 100644 --- a/include/chaiscript/language/chaiscript_optimizer.hpp +++ b/include/chaiscript/language/chaiscript_optimizer.hpp @@ -241,7 +241,7 @@ namespace chaiscript { { try { const auto &oper = node->text; - const auto parsed = Operators::to_operator(oper.c_str()); + const auto parsed = Operators::to_operator(oper); if (parsed != Operators::Opers::invalid) { const auto rhs = dynamic_cast *>(node->children[1].get())->m_value; if (rhs.get_type_info().is_arithmetic()) { @@ -268,7 +268,7 @@ namespace chaiscript { { try { const auto &oper = node->text; - const auto parsed = Operators::to_operator(oper.c_str(), true); + const auto parsed = Operators::to_operator(oper, true); const auto lhs = dynamic_cast *>(node->children[0].get())->m_value; const auto match = oper + node->children[0]->text; @@ -308,7 +308,7 @@ namespace chaiscript { { try { const auto &oper = node->text; - const auto parsed = Operators::to_operator(oper.c_str()); + const auto parsed = Operators::to_operator(oper); if (parsed != Operators::Opers::invalid) { const auto lhs = dynamic_cast &>(*node->children[0]).m_value; const auto rhs = dynamic_cast &>(*node->children[1]).m_value; diff --git a/include/chaiscript/utility/fnv1a.hpp b/include/chaiscript/utility/fnv1a.hpp index 8442764a..07f9955f 100644 --- a/include/chaiscript/utility/fnv1a.hpp +++ b/include/chaiscript/utility/fnv1a.hpp @@ -57,6 +57,10 @@ namespace chaiscript return fnv1a_32(sv.begin(), sv.end()); } + static std::uint32_t fnv1a_32(const std::string &s) noexcept { + return fnv1a_32(s.begin(), s.end()); + } + } }