From 20e0b3efa58d680af1cb821286bc5574d1c3b2c4 Mon Sep 17 00:00:00 2001 From: Joachim Schoeberl Date: Tue, 16 Jul 2024 12:44:04 +0200 Subject: [PATCH] replace const string& by string_view in Flags and SymbolTable --- libsrc/core/flags.cpp | 33 +++++++++++++++++---------------- libsrc/core/flags.hpp | 20 ++++++++++---------- libsrc/core/symboltable.hpp | 23 ++++++++++++++++------- 3 files changed, 43 insertions(+), 33 deletions(-) diff --git a/libsrc/core/flags.cpp b/libsrc/core/flags.cpp index e968ce9c..dc311124 100644 --- a/libsrc/core/flags.cpp +++ b/libsrc/core/flags.cpp @@ -16,6 +16,7 @@ namespace ngcore { using std::string; + using std::string_view; using std::endl; Flags :: Flags () { ; } @@ -209,18 +210,18 @@ namespace ngcore } - double Flags :: GetNumFlag (const string & name, double def) const + double Flags :: GetNumFlag (string_view name, double def) const { if (numflags.Used (name)) - return numflags[name]; + return numflags[string(name)]; else return def; } - const double * Flags :: GetNumFlagPtr (const string & name) const + const double * Flags :: GetNumFlagPtr (string_view name) const { if (numflags.Used (name)) - return & ((SymbolTable&)numflags)[name]; + return & ((SymbolTable&)numflags)[string(name)]; else return NULL; } @@ -239,16 +240,16 @@ namespace ngcore return defflags.Used (name); } */ - bool Flags :: GetDefineFlag (const string & name) const throw() + bool Flags :: GetDefineFlag (string_view name) const throw() { - if (!defflags.Used (name)) return false; - return defflags[name]; + if (!defflags.Used (string(name))) return false; + return defflags[string(name)]; } - xbool Flags :: GetDefineFlagX (const string & name) const throw() + xbool Flags :: GetDefineFlagX (string_view name) const throw() { - if (!defflags.Used (name)) return maybe; - return bool(defflags[name]); + if (!defflags.Used (string(name))) return maybe; + return bool(defflags[string(name)]); } @@ -296,32 +297,32 @@ namespace ngcore return empty; } - bool Flags :: StringFlagDefined (const string & name) const + bool Flags :: StringFlagDefined (string_view name) const noexcept { return strflags.Used (name); } - bool Flags :: NumFlagDefined (const string &name) const + bool Flags :: NumFlagDefined (string_view name) const noexcept { return numflags.Used (name); } - bool Flags :: FlagsFlagDefined (const string &name) const + bool Flags :: FlagsFlagDefined (string_view name) const noexcept { return flaglistflags.Used (name); } - bool Flags :: StringListFlagDefined (const string & name) const + bool Flags :: StringListFlagDefined (string_view name) const noexcept { return strlistflags.Used (name); } - bool Flags :: NumListFlagDefined (const string & name) const + bool Flags :: NumListFlagDefined (string_view name) const noexcept { return numlistflags.Used (name); } - bool Flags :: AnyFlagDefined (const string& name) const + bool Flags :: AnyFlagDefined (string_view name) const noexcept { return anyflags.Used(name); } diff --git a/libsrc/core/flags.hpp b/libsrc/core/flags.hpp index 938fb789..2d3d52ac 100644 --- a/libsrc/core/flags.hpp +++ b/libsrc/core/flags.hpp @@ -125,15 +125,15 @@ namespace ngcore /// Returns std::string flag, default value if not exists std::string GetStringFlag (const std::string & name, std::string def = "") const; /// Returns numerical flag, default value if not exists - double GetNumFlag (const std::string & name, double def) const; + double GetNumFlag (std::string_view name, double def) const; /// Returns address of numerical flag, null if not exists - const double * GetNumFlagPtr (const std::string & name) const; + const double * GetNumFlagPtr (std::string_view name) const; /// Returns address of numerical flag, null if not exists double * GetNumFlagPtr (const std::string & name); /// Returns boolean flag // int GetDefineFlag (const char * name) const; - bool GetDefineFlag (const std::string & name) const throw(); - xbool GetDefineFlagX (const std::string & name) const throw(); + bool GetDefineFlag (std::string_view name) const noexcept; + xbool GetDefineFlagX (std::string_view name) const noexcept; /// Returns string list flag, empty array if not exist const Array & GetStringListFlag (const std::string & name) const; /// Returns num list flag, empty array if not exist @@ -144,16 +144,16 @@ namespace ngcore /// Test, if string flag is defined - bool StringFlagDefined (const std::string & name) const; + bool StringFlagDefined (std::string_view name) const noexcept; /// Test, if num flag is defined - bool NumFlagDefined (const std::string & name) const; + bool NumFlagDefined (std::string_view name) const noexcept; /// Test, if num flag is defined - bool FlagsFlagDefined (const std::string & name) const; + bool FlagsFlagDefined (std::string_view name) const noexcept; /// Test, if string list flag is defined - bool StringListFlagDefined (const std::string & name) const; + bool StringListFlagDefined (std::string_view name) const noexcept; /// Test, if num list flag is defined - bool NumListFlagDefined (const std::string & name) const; - bool AnyFlagDefined (const std::string& name) const; + bool NumListFlagDefined (std::string_view name) const noexcept; + bool AnyFlagDefined (std::string_view name) const noexcept; /// number of string flags int GetNStringFlags () const { return strflags.Size(); } diff --git a/libsrc/core/symboltable.hpp b/libsrc/core/symboltable.hpp index 62a943e9..d384d70f 100644 --- a/libsrc/core/symboltable.hpp +++ b/libsrc/core/symboltable.hpp @@ -45,7 +45,7 @@ namespace ngcore } /// INDEX of symbol name, throws exception if unused - size_t Index (const std::string & name) const + size_t Index (std::string_view name) const { for (size_t i = 0; i < names.size(); i++) if (names[i] == name) return i; @@ -53,7 +53,7 @@ namespace ngcore } /// Index of symbol name, returns -1 if unused - int CheckIndex (const std::string & name) const + int CheckIndex (std::string_view name) const { for (int i = 0; i < names.size(); i++) if (names[i] == name) return i; @@ -67,12 +67,12 @@ namespace ngcore } /// Returns reference to element. exception for unused identifier - reference operator[] (const std::string & name) + reference operator[] (std::string_view name) { return data[Index (name)]; } - const_reference operator[] (const std::string & name) const + const_reference operator[] (std::string_view name) const { return data[Index (name)]; } @@ -99,7 +99,7 @@ namespace ngcore } /// Associates el to the string name, overrides if name is used - void Set (const std::string & name, const T & el) + void Set (std::string_view name, const T & el) { int i = CheckIndex (name); if (i >= 0) @@ -107,15 +107,24 @@ namespace ngcore else { data.push_back(el); - names.push_back(name); + names.push_back(std::string(name)); } } + + + /* bool Used (const std::string & name) const { return CheckIndex(name) >= 0; } - + */ + + bool Used (std::string_view name) const + { + return CheckIndex(name) >= 0; + } + /// Deletes symboltable inline void DeleteAll () {