summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2009-10-11 14:45:21 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2009-10-11 14:45:21 +0200
commit48778e6ccb9dfac96f939584c336a1e8ca7a36ad (patch)
tree68825277d80883e6568ef7ec30bc63af722068e9
parent0e70bc69e1a686933a69ba6a60c1ea48f50b327c (diff)
Use a set instead of pre-sorted array for keywords
The pre-sorted array approach depends on the character encoding.
-rw-r--r--xsd/cxx/elements.cxx175
-rw-r--r--xsd/cxx/elements.hxx11
2 files changed, 98 insertions, 88 deletions
diff --git a/xsd/cxx/elements.cxx b/xsd/cxx/elements.cxx
index 2bd29bb..3cd5e9e 100644
--- a/xsd/cxx/elements.cxx
+++ b/xsd/cxx/elements.cxx
@@ -10,7 +10,6 @@
#include <cctype> // std::toupper
#include <sstream>
#include <iostream>
-#include <algorithm>
using std::wcerr;
using std::endl;
@@ -25,6 +24,87 @@ namespace CXX
return std::toupper (c);
}
+ namespace
+ {
+ WideChar const* keywords[] = {
+ L"NULL",
+ L"and",
+ L"asm",
+ L"auto",
+ L"bitand",
+ L"bitor",
+ L"bool",
+ L"break",
+ L"case",
+ L"catch",
+ L"char",
+ L"class",
+ L"compl",
+ L"const",
+ L"const_cast",
+ L"continue",
+ L"default",
+ L"delete",
+ L"do",
+ L"double",
+ L"dynamic_cast",
+ L"else",
+ L"end_eq",
+ L"enum",
+ L"explicit",
+ L"export",
+ L"extern",
+ L"false",
+ L"float",
+ L"for",
+ L"friend",
+ L"goto",
+ L"if",
+ L"inline",
+ L"int",
+ L"long",
+ L"mutable",
+ L"namespace",
+ L"new",
+ L"not",
+ L"not_eq",
+ L"operator",
+ L"or",
+ L"or_eq",
+ L"private",
+ L"protected",
+ L"public",
+ L"register",
+ L"reinterpret_cast",
+ L"return",
+ L"short",
+ L"signed",
+ L"sizeof",
+ L"static",
+ L"static_cast",
+ L"struct",
+ L"switch",
+ L"template",
+ L"this",
+ L"throw",
+ L"true",
+ L"try",
+ L"typedef",
+ L"typeid",
+ L"typename",
+ L"union",
+ L"unsigned",
+ L"using",
+ L"virtual",
+ L"void",
+ L"volatile",
+ L"wchar_t",
+ L"while",
+ L"xor",
+ L"xor_eq"
+ };
+ }
+
// Context
//
@@ -68,7 +148,8 @@ namespace CXX
nsm_mapping (nsm_mapping_),
include_mapping (include_mapping_),
trace_include_regex (trace_include_regex_),
- reserved_name_map (reserved_name_map_)
+ reserved_name_map (reserved_name_map_),
+ keyword_set (keyword_set_)
{
// Resolve and cache XML Schema namespace.
//
@@ -167,6 +248,11 @@ namespace CXX
else
reserved_name_map_[String (s, 0, pos)] = String (s, pos + 1);
}
+
+ // Populate the keyword set.
+ //
+ for (Size i (0); i < sizeof (keywords) / sizeof (char*); ++i)
+ keyword_set_.insert (keywords[i]);
}
String Context::
@@ -415,87 +501,6 @@ namespace CXX
return *b;
}
- namespace
- {
- WideChar const* keywords[] = {
- L"NULL",
- L"and",
- L"asm",
- L"auto",
- L"bitand",
- L"bitor",
- L"bool",
- L"break",
- L"case",
- L"catch",
- L"char",
- L"class",
- L"compl",
- L"const",
- L"const_cast",
- L"continue",
- L"default",
- L"delete",
- L"do",
- L"double",
- L"dynamic_cast",
- L"else",
- L"end_eq",
- L"enum",
- L"explicit",
- L"export",
- L"extern",
- L"false",
- L"float",
- L"for",
- L"friend",
- L"goto",
- L"if",
- L"inline",
- L"int",
- L"long",
- L"mutable",
- L"namespace",
- L"new",
- L"not",
- L"not_eq",
- L"operator",
- L"or",
- L"or_eq",
- L"private",
- L"protected",
- L"public",
- L"register",
- L"reinterpret_cast",
- L"return",
- L"short",
- L"signed",
- L"sizeof",
- L"static",
- L"static_cast",
- L"struct",
- L"switch",
- L"template",
- L"this",
- L"throw",
- L"true",
- L"try",
- L"typedef",
- L"typeid",
- L"typename",
- L"union",
- L"unsigned",
- L"using",
- L"virtual",
- L"void",
- L"volatile",
- L"wchar_t",
- L"while",
- L"xor",
- L"xor_eq"
- };
- }
-
String Context::
escape (String const& name)
{
@@ -546,9 +551,7 @@ namespace CXX
// Keywords
//
- Size const size (sizeof (keywords) / sizeof (WideChar*));
-
- if (std::binary_search (keywords, keywords + size, r))
+ if (keyword_set.find (r) != keyword_set.end ())
{
r += L'_';
diff --git a/xsd/cxx/elements.hxx b/xsd/cxx/elements.hxx
index dba9665..8ab2c87 100644
--- a/xsd/cxx/elements.hxx
+++ b/xsd/cxx/elements.hxx
@@ -7,6 +7,7 @@
#define CXX_ELEMENTS_HXX
#include <cult/types.hxx>
+#include <cult/containers/set.hxx>
#include <cult/containers/map.hxx>
#include <cult/containers/vector.hxx>
@@ -118,6 +119,7 @@ namespace CXX
typedef Cult::Containers::Map<String, String> MappingCache;
typedef Cult::Containers::Map<String, String> ReservedNameMap;
+ typedef Cult::Containers::Set<String> KeywordSet;
public:
Context (std::wostream& o,
@@ -154,7 +156,8 @@ namespace CXX
nsm_mapping (c.nsm_mapping),
include_mapping (c.include_mapping),
trace_include_regex (c.trace_include_regex),
- reserved_name_map (c.reserved_name_map)
+ reserved_name_map (c.reserved_name_map),
+ keyword_set (c.keyword_set)
{
}
@@ -177,7 +180,8 @@ namespace CXX
nsm_mapping (c.nsm_mapping),
include_mapping (c.include_mapping),
trace_include_regex (c.trace_include_regex),
- reserved_name_map (c.reserved_name_map)
+ reserved_name_map (c.reserved_name_map),
+ keyword_set (c.keyword_set)
{
}
@@ -346,6 +350,9 @@ namespace CXX
ReservedNameMap const& reserved_name_map;
ReservedNameMap reserved_name_map_;
+
+ KeywordSet const& keyword_set;
+ KeywordSet keyword_set_;
};
inline UnsignedLong Context::