From 5d6f8aad8aaa3cdc3c52848c8adbc271dbe8e5e2 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sun, 11 Oct 2009 14:17:04 +0200 Subject: Use a set instead of pre-sorted array for keywords The pre-sorted array approach depends on the character encoding. --- cli/context.cxx | 65 ++++++++++++++++++++++++++++++--------------------------- cli/context.hxx | 5 +++++ 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/cli/context.cxx b/cli/context.cxx index 3f3261c..5ee27aa 100644 --- a/cli/context.cxx +++ b/cli/context.cxx @@ -9,34 +9,6 @@ using namespace std; -context:: -context (ostream& os_, semantics::cli_unit& unit_, options_type const& ops) - : data_ (new (shared) data), - os (os_), - unit (unit_), - options (ops), - inl (data_->inl_), - opt_prefix (options.option_prefix ()), - opt_sep (options.option_separator ()), - reserved_name_map (options.reserved_name ()) -{ - if (!options.suppress_inline ()) - data_->inl_ = "inline "; -} - -context:: -context (context& c) - : data_ (c.data_), - os (c.os), - unit (c.unit), - options (c.options), - inl (c.inl), - opt_prefix (c.opt_prefix), - opt_sep (c.opt_sep), - reserved_name_map (c.reserved_name_map) -{ -} - namespace { char const* keywords[] = @@ -119,6 +91,39 @@ namespace }; } +context:: +context (ostream& os_, semantics::cli_unit& unit_, options_type const& ops) + : data_ (new (shared) data), + os (os_), + unit (unit_), + options (ops), + inl (data_->inl_), + opt_prefix (options.option_prefix ()), + opt_sep (options.option_separator ()), + reserved_name_map (options.reserved_name ()), + keyword_set (data_->keyword_set_) +{ + if (!options.suppress_inline ()) + data_->inl_ = "inline "; + + for (size_t i (0); i < sizeof (keywords) / sizeof (char*); ++i) + data_->keyword_set_.insert (keywords[i]); +} + +context:: +context (context& c) + : data_ (c.data_), + os (c.os), + unit (c.unit), + options (c.options), + inl (c.inl), + opt_prefix (c.opt_prefix), + opt_sep (c.opt_sep), + reserved_name_map (c.reserved_name_map), + keyword_set (c.keyword_set) +{ +} + string context:: escape (string const& name) const { @@ -169,9 +174,7 @@ escape (string const& name) const // Keywords // - size const ks (sizeof (keywords) / sizeof (char*)); - - if (std::binary_search (keywords, keywords + ks, r)) + if (keyword_set.find (r) != keyword_set.end ()) { r += '_'; diff --git a/cli/context.hxx b/cli/context.hxx index 37864af..1f2facb 100644 --- a/cli/context.hxx +++ b/cli/context.hxx @@ -6,6 +6,7 @@ #ifndef CLI_CONTEXT_HXX #define CLI_CONTEXT_HXX +#include #include #include #include @@ -42,10 +43,14 @@ public: typedef std::map reserved_name_map_type; reserved_name_map_type const& reserved_name_map; + typedef std::set keyword_set_type; + keyword_set_type const& keyword_set; + private: struct data { string inl_; + keyword_set_type keyword_set_; }; public: -- cgit v1.1