From cb776831040431f73307835044de251b30085336 Mon Sep 17 00:00:00 2001 From: LoveSy <631499712@qq.com> Date: Mon, 31 Jan 2022 13:58:54 +0800 Subject: [PATCH] Improve: strip sstream which is extremely heavy & update dependencies (#1) --- build.gradle.kts | 2 +- gradle-plugin/build.gradle.kts | 2 +- runtime/build.gradle.kts | 5 ++-- runtime/src/main/cpp/properties.cpp | 42 +++++++++++++---------------- runtime/src/main/cpp/properties.h | 2 +- 5 files changed, 24 insertions(+), 29 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 0d003bc..403d916 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ buildscript { mavenCentral() } dependencies { - classpath("com.android.tools.build:gradle:7.0.4") + classpath("com.android.tools.build:gradle:7.1.0") } } diff --git a/gradle-plugin/build.gradle.kts b/gradle-plugin/build.gradle.kts index a85d4d7..3a79b5a 100644 --- a/gradle-plugin/build.gradle.kts +++ b/gradle-plugin/build.gradle.kts @@ -14,7 +14,7 @@ java { dependencies { compileOnly(gradleApi()) - compileOnly("com.android.tools.build:gradle:7.0.0") + compileOnly("com.android.tools.build:gradle:7.1.0") } sourceSets { diff --git a/runtime/build.gradle.kts b/runtime/build.gradle.kts index ba52160..260e421 100644 --- a/runtime/build.gradle.kts +++ b/runtime/build.gradle.kts @@ -5,7 +5,7 @@ plugins { android { compileSdk = 31 - ndkVersion = "23.0.7599858" + ndkVersion = "23.1.7779620" defaultConfig { minSdk = 26 @@ -45,6 +45,7 @@ android { externalNativeBuild { cmake { path = file("src/main/cpp/CMakeLists.txt") + version = "3.18.1" } } buildFeatures { @@ -77,4 +78,4 @@ afterEvaluate { } } } -} \ No newline at end of file +} diff --git a/runtime/src/main/cpp/properties.cpp b/runtime/src/main/cpp/properties.cpp index e30d3f6..1fdcb4c 100644 --- a/runtime/src/main/cpp/properties.cpp +++ b/runtime/src/main/cpp/properties.cpp @@ -1,45 +1,39 @@ #include "properties.h" #include -#include -static std::string strip(const std::string &input) { - auto start_it = input.begin(); - auto end_it = input.rbegin(); - while (std::isspace(*start_it)) - ++start_it; - while (std::isspace(*end_it)) - ++end_it; +static std::string strip(const std::string_view &input) { + auto start_it = std::find_if_not(input.begin(), input.end(), std::isspace); + auto end_it = std::find_if_not(input.rbegin(), input.rend(), std::isspace); return std::string(start_it, end_it.base()); } -Properties::Properties(std::unordered_map const &properties) { - this->properties = properties; +Properties::Properties(std::unordered_map properties) : + properties(std::move(properties)) { } std::string Properties::get(const std::string &key) const { - auto value = properties.find(key); - if (value == properties.end()) { + if (auto value = properties.find(key); value == properties.end()) { return ""; + } else { + return value->second; } - return value->second; } Properties *Properties::load(Chunk *chunk) { - std::string text = std::string(static_cast(chunk->getData()), chunk->getLength()); - std::istringstream ss{text}; + std::string_view text{static_cast(chunk->getData()), + static_cast(chunk->getLength())}; std::unordered_map properties; - std::string line; - while (std::getline(ss, line)) { - size_t split = line.find('='); - if (split == std::string::npos) { - continue; + for (size_t start = 0, end; start != std::string::npos; start = end) { + end = text.find_first_of('\n', start + 1); + if (end != std::string::npos) { + auto line = text.substr(start, end); + if (size_t split = line.find('='); split != std::string::npos) { + properties[strip(line.substr(0, split))] = strip(line.substr(split + 1)); + } } - - properties[strip(line.substr(0, split))] = strip(line.substr(split + 1)); } - - return new Properties(properties); + return new Properties(std::move(properties)); } diff --git a/runtime/src/main/cpp/properties.h b/runtime/src/main/cpp/properties.h index ab0b48b..e33eecc 100644 --- a/runtime/src/main/cpp/properties.h +++ b/runtime/src/main/cpp/properties.h @@ -7,7 +7,7 @@ class Properties { private: - Properties(std::unordered_map const &properties); + Properties(std::unordered_map properties); public: std::string get(std::string const &key) const;