diff --git a/src/coders/commons.cpp b/src/coders/commons.cpp index ff86b5a3..1f1b38a4 100644 --- a/src/coders/commons.cpp +++ b/src/coders/commons.cpp @@ -16,13 +16,18 @@ inline double power(double base, int64_t power) { parsing_error::parsing_error( std::string message, - std::string filename, - std::string source, + std::string_view filename, + std::string_view source, uint pos, uint line, uint linestart) - : std::runtime_error(message), filename(filename), source(source), + : std::runtime_error(message), filename(filename), pos(pos), line(line), linestart(linestart) { + size_t end = source.find("\n", linestart); + if (end == std::string::npos) { + end = source.length(); + } + source = source.substr(linestart, end-linestart); } std::string parsing_error::errorLog() const { @@ -30,11 +35,7 @@ std::string parsing_error::errorLog() const { uint linepos = pos - linestart; ss << "parsing error in file '" << filename; ss << "' at " << (line+1) << ":" << linepos << ": " << this->what() << "\n"; - size_t end = source.find("\n", linestart); - if (end == std::string::npos) { - end = source.length(); - } - ss << source.substr(linestart, end-linestart) << "\n"; + ss << source << "\n"; for (uint i = 0; i < linepos; i++) { ss << " "; } @@ -43,8 +44,8 @@ std::string parsing_error::errorLog() const { } BasicParser::BasicParser( - const std::string& file, - const std::string& source + std::string_view file, + std::string_view source ) : filename(file), source(source) { } @@ -164,7 +165,7 @@ char BasicParser::peek() { return source[pos]; } -std::string BasicParser::readUntil(char c) { +std::string_view BasicParser::readUntil(char c) { int start = pos; while (hasNext() && source[pos] != c) { pos++; @@ -185,7 +186,7 @@ std::string BasicParser::parseName() { while (hasNext() && is_identifier_part(source[pos])) { pos++; } - return source.substr(start, pos-start); + return std::string(source.substr(start, pos-start)); } int64_t BasicParser::parseSimpleInt(int base) { diff --git a/src/coders/commons.hpp b/src/coders/commons.hpp index afebda89..c45d4f28 100644 --- a/src/coders/commons.hpp +++ b/src/coders/commons.hpp @@ -61,8 +61,8 @@ public: parsing_error( std::string message, - std::string filename, - std::string source, + std::string_view filename, + std::string_view source, uint pos, uint line, uint linestart @@ -72,8 +72,8 @@ public: class BasicParser { protected: - const std::string& filename; - const std::string& source; + std::string_view filename; + std::string_view source; uint pos = 0; uint line = 1; uint linestart = 0; @@ -95,13 +95,13 @@ protected: parsing_error error(std::string message); public: - std::string readUntil(char c); + std::string_view readUntil(char c); std::string parseName(); bool hasNext(); char peek(); char nextChar(); - BasicParser(const std::string& file, const std::string& source); + BasicParser(std::string_view file, std::string_view source); }; #endif // CODERS_COMMONS_HPP_ diff --git a/src/coders/json.cpp b/src/coders/json.cpp index 36313314..b8db4402 100644 --- a/src/coders/json.cpp +++ b/src/coders/json.cpp @@ -11,6 +11,16 @@ using namespace json; using namespace dynamic; +class Parser : BasicParser { + std::unique_ptr parseList(); + std::unique_ptr parseObject(); + dynamic::Value parseValue(); +public: + Parser(std::string_view filename, std::string_view source); + + std::unique_ptr parse(); +}; + inline void newline( std::stringstream& ss, bool nice, uint indent, @@ -126,7 +136,7 @@ std::string json::stringify( return ss.str(); } -Parser::Parser(const std::string& filename, const std::string& source) +Parser::Parser(std::string_view filename, std::string_view source) : BasicParser(filename, source) { } diff --git a/src/coders/json.hpp b/src/coders/json.hpp index 1643ce35..6dfe9c8e 100644 --- a/src/coders/json.hpp +++ b/src/coders/json.hpp @@ -14,16 +14,6 @@ #include namespace json { - class Parser : public BasicParser { - std::unique_ptr parseList(); - std::unique_ptr parseObject(); - dynamic::Value parseValue(); - public: - Parser(const std::string& filename, const std::string& source); - - std::unique_ptr parse(); - }; - std::unique_ptr parse(const std::string& filename, const std::string& source); std::unique_ptr parse(const std::string& source); diff --git a/src/coders/toml.cpp b/src/coders/toml.cpp index 26bc5d17..dfa0e467 100644 --- a/src/coders/toml.cpp +++ b/src/coders/toml.cpp @@ -14,7 +14,7 @@ using namespace toml; -class Reader : public BasicParser { +class Reader : BasicParser { SettingsHandler& handler; void skipWhitespace() override { @@ -83,8 +83,8 @@ class Reader : public BasicParser { public: Reader( SettingsHandler& handler, - const std::string& file, - const std::string& source) + std::string_view file, + std::string_view source) : BasicParser(file, source), handler(handler) { } diff --git a/src/coders/xml.cpp b/src/coders/xml.cpp index 59842ff2..0f96efed 100644 --- a/src/coders/xml.cpp +++ b/src/coders/xml.cpp @@ -177,7 +177,7 @@ const std::string& Document::getEncoding() const { return encoding; } -Parser::Parser(const std::string& filename, const std::string& source) +Parser::Parser(std::string_view filename, std::string_view source) : BasicParser(filename, source) { } @@ -250,7 +250,7 @@ std::string Parser::parseText() { } nextChar(); } - return source.substr(start, pos-start); + return std::string(source.substr(start, pos-start)); } inline bool is_xml_identifier_start(char c) { @@ -270,7 +270,7 @@ std::string Parser::parseXMLName() { while (hasNext() && is_xml_identifier_part(source[pos])) { pos++; } - return source.substr(start, pos-start); + return std::string(source.substr(start, pos-start)); } xmlelement Parser::parseElement() { diff --git a/src/coders/xml.hpp b/src/coders/xml.hpp index f8da875d..aff5288c 100644 --- a/src/coders/xml.hpp +++ b/src/coders/xml.hpp @@ -108,7 +108,7 @@ namespace xml { const std::string& getEncoding() const; }; - class Parser : public BasicParser { + class Parser : BasicParser { xmldocument document; xmlelement parseOpenTag(); @@ -118,7 +118,7 @@ namespace xml { std::string parseText(); std::string parseXMLName(); public: - Parser(const std::string& filename, const std::string& source); + Parser(std::string_view filename, std::string_view source); xmldocument parse(); }; diff --git a/src/frontend/locale.cpp b/src/frontend/locale.cpp index 2afb23f6..1c9730b8 100644 --- a/src/frontend/locale.cpp +++ b/src/frontend/locale.cpp @@ -35,8 +35,8 @@ const std::string& langs::Lang::getId() const { return locale; } -/* Language key-value txt files parser */ -class Reader : public BasicParser { +// @brief Language key-value txt files parser +class Reader : BasicParser { void skipWhitespace() override { BasicParser::skipWhitespace(); if (hasNext() && source[pos] == '#') { @@ -47,7 +47,7 @@ class Reader : public BasicParser { } } public: - Reader(const std::string& file, const std::string& source) : BasicParser(file, source) { + Reader(std::string_view file, std::string_view source) : BasicParser(file, source) { } void read(langs::Lang& lang, std::string prefix) { diff --git a/src/frontend/menu.cpp b/src/frontend/menu.cpp index e8a9b5fc..3796f88c 100644 --- a/src/frontend/menu.cpp +++ b/src/frontend/menu.cpp @@ -46,11 +46,12 @@ gui::page_loader_func menus::create_page_loader(Engine* engine) { name = query.substr(0, index); auto map = std::make_shared(); - BasicParser parser("query for "+name, argstr); + auto filename = "query for "+name; + BasicParser parser(filename, argstr); while (parser.hasNext()) { - auto key = parser.readUntil('='); + auto key = std::string(parser.readUntil('=')); parser.nextChar(); - auto value = parser.readUntil('&'); + auto value = std::string(parser.readUntil('&')); map->put(key, value); } args.push_back(map);