diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/common/String.cpp | 12 | ||||
-rw-r--r-- | src/common/StringUtil.cpp | 2 | ||||
-rw-r--r-- | src/toml/TomlDocument.cpp | 9 | ||||
-rw-r--r-- | src/toml/TomlParser.cpp | 43 |
4 files changed, 63 insertions, 3 deletions
diff --git a/src/common/String.cpp b/src/common/String.cpp index ade2a72d..1c2ff022 100644 --- a/src/common/String.cpp +++ b/src/common/String.cpp @@ -206,7 +206,7 @@ String::iterator String::erase(const_iterator start, const_iterator end) { auto s = const_cast<iterator>(start); auto e = const_cast<iterator>(end); - std::memmove(e, s, (cend() - end) * sizeof(value_type)); + std::memmove(s, e, (cend() - end) * sizeof(value_type)); this->size_ = new_size; this->buffer_[new_size] = 0; @@ -298,6 +298,16 @@ std::vector<String> String::SplitToLines(bool remove_space_line) const { return result; } +bool String::StartWith(StringView str) const { + if (str.size() > size_) return false; + return std::memcmp(str.data(), buffer_, str.size()) == 0; +} + +bool String::EndWith(StringView str) const { + if (str.size() > size_) return false; + return std::memcmp(str.data(), buffer_ + size_ - str.size(), str.size()) == 0; +} + std::string String::ToUtf8() const { return cru::ToUtf8(buffer_, size_); } void String::AppendCodePoint(CodePoint code_point) { diff --git a/src/common/StringUtil.cpp b/src/common/StringUtil.cpp index d3948c6a..440db8c4 100644 --- a/src/common/StringUtil.cpp +++ b/src/common/StringUtil.cpp @@ -253,7 +253,7 @@ char16_t ToUpper(char16_t c) { return c; } -char16_t IsWhitespace(char16_t c) { +bool IsWhitespace(char16_t c) { return c == u' ' || c == u'\t' || c == u'\n' || c == u'\r'; } } // namespace cru diff --git a/src/toml/TomlDocument.cpp b/src/toml/TomlDocument.cpp index a785b4e4..052af170 100644 --- a/src/toml/TomlDocument.cpp +++ b/src/toml/TomlDocument.cpp @@ -31,6 +31,15 @@ const TomlSection* TomlDocument::GetSection(const String& name) const { return &it->second; } +TomlSection* TomlDocument::GetSectionOrCreate(const String& name) { + auto it = sections_.find(name); + if (it == sections_.end()) { + sections_[name] = TomlSection(); + return §ions_[name]; + } + return &it->second; +} + void TomlDocument::SetSection(const String& name, TomlSection section) { sections_[name] = std::move(section); } diff --git a/src/toml/TomlParser.cpp b/src/toml/TomlParser.cpp index d54624f6..7f19c711 100644 --- a/src/toml/TomlParser.cpp +++ b/src/toml/TomlParser.cpp @@ -17,6 +17,47 @@ TomlDocument TomlParser::Parse() { } void TomlParser::DoParse(TomlDocument& document) { - // TODO: Implement this. + std::vector<String> lines = input_.SplitToLines(true); + + String current_section_name; + + for (auto& line : lines) { + line.Trim(); + if (line.StartWith(u"[") && line.EndWith(u"]")) { + current_section_name = line.substr(1, line.size() - 2); + } else if (line.StartWith(u"#")) { + // Ignore comments. + } else { + auto equal_index = line.Find(u'='); + + if (equal_index == -1) { + throw TomlParsingException(u"Invalid TOML line: " + line); + } + + auto key = line.substr(0, equal_index).Trim(); + auto value = line.substr(equal_index + 1).Trim(); + + auto remove_quote = [](const String& str) -> String { + if (str.size() < 2) { + return str; + } + + if (str.StartWith(u"\"") && str.EndWith(u"\"")) { + return str.substr(1, str.size() - 2); + } + + if (str.StartWith(u"\'") && str.EndWith(u"\'")) { + return str.substr(1, str.size() - 2); + } + + return str; + }; + + key = remove_quote(key); + value = remove_quote(value); + + document.GetSectionOrCreate(current_section_name)->SetValue(key, value); + } + } } } // namespace cru::toml |