Improve performance with string_view

This commit is contained in:
halx99 2022-06-09 16:19:33 +08:00
parent 4413728f6d
commit de9a56cdaf
1 changed files with 29 additions and 29 deletions

View File

@ -60,7 +60,7 @@ THE SOFTWARE.
#if CC_TARGET_PLATFORM != CC_PLATFORM_IOS && \ #if CC_TARGET_PLATFORM != CC_PLATFORM_IOS && \
(CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID || (defined(__NDK_MAJOR__) && __NDK_MAJOR__ >= 22)) (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID || (defined(__NDK_MAJOR__) && __NDK_MAJOR__ >= 22))
# define ADXE_HAVE_STDFS 1 # define AX_HAVE_STDFS 1
# include <filesystem> # include <filesystem>
namespace stdfs = std::filesystem; namespace stdfs = std::filesystem;
# if defined(_WIN32) # if defined(_WIN32)
@ -76,7 +76,7 @@ inline stdfs::path toFspath(const std::string_view& pathSV)
# endif # endif
#else #else
# include "tinydir/tinydir.h" # include "tinydir/tinydir.h"
# define ADXE_HAVE_STDFS 0 # define AX_HAVE_STDFS 0
#endif #endif
NS_CC_BEGIN NS_CC_BEGIN
@ -163,7 +163,7 @@ public:
void startElement(void* ctx, const char* name, const char** atts) override void startElement(void* ctx, const char* name, const char** atts) override
{ {
const std::string sName(name); const std::string sName(name);
if (sName == "dict") if (sName == "dict"sv)
{ {
if (_resultType == SAX_RESULT_DICT && _rootDict.empty()) if (_resultType == SAX_RESULT_DICT && _rootDict.empty())
{ {
@ -197,23 +197,23 @@ public:
_stateStack.push(_state); _stateStack.push(_state);
_dictStack.push(_curDict); _dictStack.push(_curDict);
} }
else if (sName == "key") else if (sName == "key"sv)
{ {
_state = SAX_KEY; _state = SAX_KEY;
} }
else if (sName == "integer") else if (sName == "integer"sv)
{ {
_state = SAX_INT; _state = SAX_INT;
} }
else if (sName == "real") else if (sName == "real"sv)
{ {
_state = SAX_REAL; _state = SAX_REAL;
} }
else if (sName == "string") else if (sName == "string"sv)
{ {
_state = SAX_STRING; _state = SAX_STRING;
} }
else if (sName == "array") else if (sName == "array"sv)
{ {
_state = SAX_ARRAY; _state = SAX_ARRAY;
@ -253,7 +253,7 @@ public:
{ {
SAXState curState = _stateStack.empty() ? SAX_DICT : _stateStack.top(); SAXState curState = _stateStack.empty() ? SAX_DICT : _stateStack.top();
const std::string sName((char*)name); const std::string sName((char*)name);
if (sName == "dict") if (sName == "dict"sv)
{ {
_stateStack.pop(); _stateStack.pop();
_dictStack.pop(); _dictStack.pop();
@ -262,7 +262,7 @@ public:
_curDict = _dictStack.top(); _curDict = _dictStack.top();
} }
} }
else if (sName == "array") else if (sName == "array"sv)
{ {
_stateStack.pop(); _stateStack.pop();
_arrayStack.pop(); _arrayStack.pop();
@ -271,7 +271,7 @@ public:
_curArray = _arrayStack.top(); _curArray = _arrayStack.top();
} }
} }
else if (sName == "true") else if (sName == "true"sv)
{ {
if (SAX_ARRAY == curState) if (SAX_ARRAY == curState)
{ {
@ -282,7 +282,7 @@ public:
(*_curDict)[_curKey] = Value(true); (*_curDict)[_curKey] = Value(true);
} }
} }
else if (sName == "false") else if (sName == "false"sv)
{ {
if (SAX_ARRAY == curState) if (SAX_ARRAY == curState)
{ {
@ -293,22 +293,22 @@ public:
(*_curDict)[_curKey] = Value(false); (*_curDict)[_curKey] = Value(false);
} }
} }
else if (sName == "string" || sName == "integer" || sName == "real") else if (sName == "string"sv || sName == "integer"sv || sName == "real"sv)
{ {
if (SAX_ARRAY == curState) if (SAX_ARRAY == curState)
{ {
if (sName == "string") if (sName == "string"sv)
_curArray->push_back(Value(_curValue)); _curArray->push_back(Value(_curValue));
else if (sName == "integer") else if (sName == "integer"sv)
_curArray->push_back(Value(atoi(_curValue.c_str()))); _curArray->push_back(Value(atoi(_curValue.c_str())));
else else
_curArray->push_back(Value(std::atof(_curValue.c_str()))); _curArray->push_back(Value(std::atof(_curValue.c_str())));
} }
else if (SAX_DICT == curState) else if (SAX_DICT == curState)
{ {
if (sName == "string") if (sName == "string"sv)
(*_curDict)[_curKey] = Value(_curValue); (*_curDict)[_curKey] = Value(_curValue);
else if (sName == "integer") else if (sName == "integer"sv)
(*_curDict)[_curKey] = Value(atoi(_curValue.c_str())); (*_curDict)[_curKey] = Value(atoi(_curValue.c_str()));
else else
(*_curDict)[_curKey] = Value(std::atof(_curValue.c_str())); (*_curDict)[_curKey] = Value(std::atof(_curValue.c_str()));
@ -423,25 +423,25 @@ static void generateElementForObject(const Value& value, pugi::xml_node& parent)
// object is String // object is String
if (value.getType() == Value::Type::STRING) if (value.getType() == Value::Type::STRING)
{ {
auto node = parent.append_child("string"); auto node = parent.append_child("string"sv);
node.append_child(pugi::xml_node_type::node_pcdata).set_value(value.asString().c_str()); node.append_child(pugi::xml_node_type::node_pcdata).set_value(value.asString());
} }
// object is integer // object is integer
else if (value.getType() == Value::Type::INTEGER) else if (value.getType() == Value::Type::INTEGER)
{ {
auto node = parent.append_child("integer"); auto node = parent.append_child("integer"sv);
node.append_child(pugi::xml_node_type::node_pcdata).set_value(value.asString().c_str()); node.append_child(pugi::xml_node_type::node_pcdata).set_value(value.asString());
} }
// object is real // object is real
else if (value.getType() == Value::Type::FLOAT || value.getType() == Value::Type::DOUBLE) else if (value.getType() == Value::Type::FLOAT || value.getType() == Value::Type::DOUBLE)
{ {
auto node = parent.append_child("real"); auto node = parent.append_child("real"sv);
node.append_child(pugi::xml_node_type::node_pcdata).set_value(value.asString().c_str()); node.append_child(pugi::xml_node_type::node_pcdata).set_value(value.asString());
} }
// object is bool // object is bool
else if (value.getType() == Value::Type::BOOLEAN) else if (value.getType() == Value::Type::BOOLEAN)
{ {
parent.append_child(value.asString().c_str()); parent.append_child(value.asString());
} }
// object is Array // object is Array
else if (value.getType() == Value::Type::VECTOR) else if (value.getType() == Value::Type::VECTOR)
@ -453,11 +453,11 @@ static void generateElementForObject(const Value& value, pugi::xml_node& parent)
static void generateElementForDict(const ValueMap& dict, pugi::xml_node& parent) static void generateElementForDict(const ValueMap& dict, pugi::xml_node& parent)
{ {
auto dictDS = parent.append_child("dict"); auto dictDS = parent.append_child("dict"sv);
for (const auto& iter : dict) for (const auto& iter : dict)
{ {
auto key = dictDS.append_child("key"); auto key = dictDS.append_child("key"sv);
key.append_child(pugi::xml_node_type::node_pcdata).set_value(iter.first.c_str()); key.append_child(pugi::xml_node_type::node_pcdata).set_value(iter.first);
generateElementForObject(iter.second, dictDS); generateElementForObject(iter.second, dictDS);
} }
@ -465,7 +465,7 @@ static void generateElementForDict(const ValueMap& dict, pugi::xml_node& parent)
static void generateElementForArray(const ValueVector& array, pugi::xml_node& parent) static void generateElementForArray(const ValueVector& array, pugi::xml_node& parent)
{ {
auto arrayDS = parent.append_child("array"); auto arrayDS = parent.append_child("array"sv);
for (const auto& value : array) for (const auto& value : array)
{ {
generateElementForObject(value, arrayDS); generateElementForObject(value, arrayDS);
@ -1179,7 +1179,7 @@ std::unique_ptr<FileStream> FileUtils::openFileStream(std::string_view filePath,
a. ios: require ios 13.0+ a. ios: require ios 13.0+
b. android: require ndk-r22+ b. android: require ndk-r22+
*/ */
#if ADXE_HAVE_STDFS #if AX_HAVE_STDFS
std::vector<std::string> FileUtils::listFiles(std::string_view dirPath) const std::vector<std::string> FileUtils::listFiles(std::string_view dirPath) const
{ {
const auto fullPath = fullPathForDirectory(dirPath); const auto fullPath = fullPathForDirectory(dirPath);