From 9aa2dfc9a366df851d1eea05a58d5e171adcb8fd Mon Sep 17 00:00:00 2001 From: Neil Date: Thu, 3 May 2018 07:44:56 +1000 Subject: Simplify PropSetSimple, avoid casts, use nullptr. --- lexlib/LexerBase.cxx | 2 +- lexlib/PropSetSimple.cxx | 28 ++++++++++++++++------------ lexlib/PropSetSimple.h | 2 +- 3 files changed, 18 insertions(+), 14 deletions(-) (limited to 'lexlib') diff --git a/lexlib/LexerBase.cxx b/lexlib/LexerBase.cxx index 80558504f..f9e6292a1 100644 --- a/lexlib/LexerBase.cxx +++ b/lexlib/LexerBase.cxx @@ -62,7 +62,7 @@ const char * SCI_METHOD LexerBase::DescribeProperty(const char *) { Sci_Position SCI_METHOD LexerBase::PropertySet(const char *key, const char *val) { const char *valOld = props.Get(key); if (strcmp(val, valOld) != 0) { - props.Set(key, val); + props.Set(key, val, strlen(key), strlen(val)); return 0; } else { return -1; diff --git a/lexlib/PropSetSimple.cxx b/lexlib/PropSetSimple.cxx index cbcbac5bd..ba76019be 100644 --- a/lexlib/PropSetSimple.cxx +++ b/lexlib/PropSetSimple.cxx @@ -17,27 +17,31 @@ using namespace Scintilla; +namespace { + typedef std::map mapss; +mapss *PropsFromPointer(void *impl) { + return static_cast(impl); +} + +} + PropSetSimple::PropSetSimple() { mapss *props = new mapss; impl = static_cast(props); } PropSetSimple::~PropSetSimple() { - mapss *props = static_cast(impl); + mapss *props = PropsFromPointer(impl); delete props; impl = 0; } -void PropSetSimple::Set(const char *key, const char *val, int lenKey, int lenVal) { - mapss *props = static_cast(impl); +void PropSetSimple::Set(const char *key, const char *val, size_t lenKey, size_t lenVal) { + mapss *props = PropsFromPointer(impl); if (!*key) // Empty keys are not supported return; - if (lenKey == -1) - lenKey = static_cast(strlen(key)); - if (lenVal == -1) - lenVal = static_cast(strlen(val)); (*props)[std::string(key, lenKey)] = std::string(val, lenVal); } @@ -53,10 +57,10 @@ void PropSetSimple::Set(const char *keyVal) { endVal++; const char *eqAt = strchr(keyVal, '='); if (eqAt) { - Set(keyVal, eqAt + 1, static_cast(eqAt-keyVal), - static_cast(endVal - eqAt - 1)); + Set(keyVal, eqAt + 1, eqAt-keyVal, + endVal - eqAt - 1); } else if (*keyVal) { // No '=' so assume '=1' - Set(keyVal, "1", static_cast(endVal-keyVal), 1); + Set(keyVal, "1", endVal-keyVal, 1); } } @@ -71,7 +75,7 @@ void PropSetSimple::SetMultiple(const char *s) { } const char *PropSetSimple::Get(const char *key) const { - mapss *props = static_cast(impl); + mapss *props = PropsFromPointer(impl); mapss::const_iterator keyPos = props->find(std::string(key)); if (keyPos != props->end()) { return keyPos->second.c_str(); @@ -86,7 +90,7 @@ const char *PropSetSimple::Get(const char *key) const { // for that, through a recursive function and a simple chain of pointers. struct VarChain { - VarChain(const char *var_=NULL, const VarChain *link_=NULL): var(var_), link(link_) {} + VarChain(const char *var_=nullptr, const VarChain *link_= nullptr): var(var_), link(link_) {} bool contains(const char *testVar) const { return (var && (0 == strcmp(var, testVar))) diff --git a/lexlib/PropSetSimple.h b/lexlib/PropSetSimple.h index 91b170df3..ba4e42446 100644 --- a/lexlib/PropSetSimple.h +++ b/lexlib/PropSetSimple.h @@ -16,7 +16,7 @@ class PropSetSimple { public: PropSetSimple(); virtual ~PropSetSimple(); - void Set(const char *key, const char *val, int lenKey=-1, int lenVal=-1); + void Set(const char *key, const char *val, size_t lenKey, size_t lenVal); void SetMultiple(const char *); const char *Get(const char *key) const; int GetExpanded(const char *key, char *result) const; -- cgit v1.2.3