aboutsummaryrefslogtreecommitdiff
path: root/include/cru
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-10-17 09:46:56 +0800
committerYuqian Yang <crupest@crupest.life>2025-10-17 09:46:56 +0800
commit9e4419826b3e23c63567591701a2834a837da98e (patch)
tree763838a83850f5cb07cff43915aa6be3a689bef0 /include/cru
parenta6b5b8b879a9a587ec0ad605722d5d6428d5e68c (diff)
downloadcru-9e4419826b3e23c63567591701a2834a837da98e.tar.gz
cru-9e4419826b3e23c63567591701a2834a837da98e.tar.bz2
cru-9e4419826b3e23c63567591701a2834a837da98e.zip
Toml remove String.
Diffstat (limited to 'include/cru')
-rw-r--r--include/cru/base/StringUtil.h42
-rw-r--r--include/cru/toml/TomlDocument.h21
-rw-r--r--include/cru/toml/TomlParser.h4
3 files changed, 18 insertions, 49 deletions
diff --git a/include/cru/base/StringUtil.h b/include/cru/base/StringUtil.h
index f2373444..d79c0c23 100644
--- a/include/cru/base/StringUtil.h
+++ b/include/cru/base/StringUtil.h
@@ -5,6 +5,7 @@
#include <compare>
#include <functional>
#include <stdexcept>
+#include <string>
#include <string_view>
#include <type_traits>
#include <vector>
@@ -16,7 +17,7 @@ std::weak_ordering CaseInsensitiveCompare(std::string_view left,
std::string TrimBegin(std::string_view str);
std::string TrimEnd(std::string_view str);
std::string Trim(std::string_view str);
-} // namespace string
+bool IsSpace(std::string_view str);
namespace details {
struct SplitOptionsTag {};
@@ -24,43 +25,12 @@ struct SplitOptionsTag {};
using SplitOption = Bitmask<details::SplitOptionsTag>;
struct SplitOptions {
static constexpr SplitOption RemoveEmpty = SplitOption::FromOffset(1);
+ static constexpr SplitOption RemoveSpace = SplitOption::FromOffset(2);
};
-template <typename TString>
-std::vector<TString> Split(const TString& str, const TString& sep,
- SplitOption options = {}) {
- using size_type = typename TString::size_type;
-
- if (sep.empty()) throw std::invalid_argument("Sep can't be empty.");
- if (str.empty()) return {};
-
- size_type current_pos = 0;
- std::vector<TString> result;
-
- while (current_pos != TString::npos) {
- if (current_pos == str.size()) {
- if (!options.Has(SplitOptions::RemoveEmpty)) {
- result.push_back({});
- }
- break;
- }
-
- auto next_pos = str.find(sep, current_pos);
- auto sub = str.substr(current_pos, next_pos - current_pos);
- if (!(options.Has(SplitOptions::RemoveEmpty) && sub.empty())) {
- result.push_back(sub);
- }
- current_pos =
- next_pos == TString::npos ? TString::npos : next_pos + sep.size();
- }
-
- return result;
-}
-
-inline std::vector<std::string> Split(const char* str, const std::string& sep,
- SplitOption options = {}) {
- return Split<std::string>(std::string(str), sep, options);
-}
+std::vector<std::string> Split(std::string_view str, std::string_view sep,
+ SplitOption options = {});
+} // namespace string
using CodePoint = std::int32_t;
constexpr CodePoint k_invalid_code_point = -1;
diff --git a/include/cru/toml/TomlDocument.h b/include/cru/toml/TomlDocument.h
index d072d384..4cd3ab3e 100644
--- a/include/cru/toml/TomlDocument.h
+++ b/include/cru/toml/TomlDocument.h
@@ -3,7 +3,6 @@
#include "Base.h"
#include "cru/base/Base.h"
-#include "cru/base/String.h"
#include <optional>
#include <unordered_map>
@@ -16,9 +15,9 @@ class CRU_TOML_API TomlSection {
CRU_DEFAULT_MOVE(TomlSection)
public:
- std::optional<String> GetValue(const String& key) const;
- void SetValue(const String& key, String value);
- void DeleteValue(const String& key);
+ std::optional<std::string> GetValue(const std::string& key) const;
+ void SetValue(const std::string& key, std::string value);
+ void DeleteValue(const std::string& key);
auto begin() { return values_.begin(); }
auto end() { return values_.end(); }
@@ -28,7 +27,7 @@ class CRU_TOML_API TomlSection {
auto cend() const { return values_.cend(); }
private:
- std::unordered_map<String, String> values_;
+ std::unordered_map<std::string, std::string> values_;
};
class CRU_TOML_API TomlDocument {
@@ -38,11 +37,11 @@ class CRU_TOML_API TomlDocument {
CRU_DEFAULT_MOVE(TomlDocument)
public:
- TomlSection* GetSection(const String& name);
- TomlSection* GetSectionOrCreate(const String& name);
- const TomlSection* GetSection(const String& name) const;
- void SetSection(const String& name, TomlSection section);
- void DeleteSection(const String& name);
+ TomlSection* GetSection(const std::string& name);
+ TomlSection* GetSectionOrCreate(const std::string& name);
+ const TomlSection* GetSection(const std::string& name) const;
+ void SetSection(const std::string& name, TomlSection section);
+ void DeleteSection(const std::string& name);
auto begin() { return sections_.begin(); }
auto end() { return sections_.end(); }
@@ -52,6 +51,6 @@ class CRU_TOML_API TomlDocument {
auto cend() const { return sections_.cend(); }
private:
- std::unordered_map<String, TomlSection> sections_;
+ std::unordered_map<std::string, TomlSection> sections_;
};
} // namespace cru::toml
diff --git a/include/cru/toml/TomlParser.h b/include/cru/toml/TomlParser.h
index 76c270c3..b6532931 100644
--- a/include/cru/toml/TomlParser.h
+++ b/include/cru/toml/TomlParser.h
@@ -14,7 +14,7 @@ class CRU_TOML_API TomlParsingException : public Exception {
class CRU_TOML_API TomlParser {
public:
- explicit TomlParser(String input);
+ explicit TomlParser(std::string input);
CRU_DELETE_COPY(TomlParser)
CRU_DELETE_MOVE(TomlParser)
@@ -29,7 +29,7 @@ class CRU_TOML_API TomlParser {
void DoParse(TomlDocument& document);
private:
- String input_;
+ std::string input_;
std::optional<TomlDocument> cache_;
};