From c16936e22515084f44b8ce9cac65cdf535ff9142 Mon Sep 17 00:00:00 2001 From: Paul Ferrand Date: Wed, 18 Dec 2019 09:54:00 +0100 Subject: [PATCH] Moved the parsing functions to SfzHelpers --- src/CMakeLists.txt | 2 +- src/sfizz/Parser.cpp | 94 +---------------------------------- src/sfizz/SfzHelpers.cpp | 94 +++++++++++++++++++++++++++++++++++ src/sfizz/SfzHelpers.h | 4 ++ src/sfizz/StringViewHelpers.h | 4 +- 5 files changed, 101 insertions(+), 97 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2590162d..4214b319 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -27,7 +27,7 @@ include (SfizzSIMDSourceFilesCheck) # Parser core library add_library (sfizz_parser STATIC) -target_sources (sfizz_parser PRIVATE sfizz/Parser.cpp sfizz/Opcode.cpp) +target_sources (sfizz_parser PRIVATE sfizz/Parser.cpp sfizz/Opcode.cpp sfizz/SfzHelpers.cpp) target_include_directories (sfizz_parser PUBLIC sfizz) target_include_directories (sfizz_parser PUBLIC external) target_include_directories (sfizz_parser PRIVATE ${re2_SOURCE_DIR}) diff --git a/src/sfizz/Parser.cpp b/src/sfizz/Parser.cpp index 8b762cd2..a49cc2c6 100644 --- a/src/sfizz/Parser.cpp +++ b/src/sfizz/Parser.cpp @@ -24,6 +24,7 @@ #include "Parser.h" #include "Config.h" #include "StringViewHelpers.h" +#include "SfzHelpers.h" #include "absl/strings/str_join.h" #include "absl/strings/str_cat.h" #include @@ -36,55 +37,6 @@ void removeCommentOnLine(absl::string_view& line) line.remove_suffix(line.size() - position); } -bool findHeader(absl::string_view& source, absl::string_view& header, absl::string_view& members) -{ - auto openHeader = source.find("<"); - if (openHeader == absl::string_view::npos) - return false; - - auto closeHeader = source.find(">", openHeader); - if (openHeader == absl::string_view::npos) - return false; - - auto nextHeader = source.find("<", closeHeader); - header = source.substr(openHeader + 1, closeHeader - openHeader - 1); - if (nextHeader == absl::string_view::npos) { - members = trim(source.substr(closeHeader + 1)); - source.remove_prefix(source.length()); - } else { - members = trim(source.substr(closeHeader + 1, nextHeader - closeHeader - 1)); - source.remove_prefix(nextHeader); - } - - return true; -} - -bool findOpcode(absl::string_view& source, absl::string_view& opcode, absl::string_view& value) -{ - auto opcodeEnd = source.find("="); - if (opcodeEnd == absl::string_view::npos) - return false; - - const auto valueStart = opcodeEnd + 1; - const auto nextOpcodeEnd = source.find("=", valueStart); - - if (nextOpcodeEnd == absl::string_view::npos) { - opcode = source.substr(0, opcodeEnd); - value = source.substr(valueStart); - source.remove_prefix(source.length()); - return true; - } - - auto valueEnd = nextOpcodeEnd; - while (source[valueEnd] != ' ' && valueEnd != valueStart) - valueEnd--; - - opcode = source.substr(0, opcodeEnd); - value = source.substr(valueStart, valueEnd - valueStart); - source.remove_prefix(valueEnd); - return true; -} - bool sfz::Parser::loadSfzFile(const fs::path& file) { const auto sfzFile = file.is_absolute() ? file : originalDirectory / file; @@ -117,50 +69,6 @@ bool sfz::Parser::loadSfzFile(const fs::path& file) return true; } -bool findDefine(absl::string_view line, absl::string_view& variable, absl::string_view& value) -{ - const auto defPosition = line.find("#define"); - if (defPosition == absl::string_view::npos) - return false; - - const auto variableStart = line.find("$", 7); - if (variableStart == absl::string_view::npos) - return false; - - const auto variableEnd = line.find_first_of(" \r\t\n\f\v", variableStart); - if (variableEnd == absl::string_view::npos) - return false; - - const auto valueStart = line.find_first_not_of(" \r\t\n\f\v", variableEnd); - if (valueStart == absl::string_view::npos) - return false; - - const auto valueEnd = line.find_first_of(" \r\t\n\f\v", valueStart); - variable = line.substr(variableStart, variableEnd - variableStart); - value = valueEnd != absl::string_view::npos - ? line.substr(valueStart, valueEnd - valueStart) - : line.substr(valueStart); - return true; -} - -bool findInclude(absl::string_view line, std::string& path) -{ - const auto defPosition = line.find("#include"); - if (defPosition == absl::string_view::npos) - return false; - - const auto pathStart = line.find("\"", 8); - if (pathStart == absl::string_view::npos) - return false; - - const auto pathEnd = line.find("\"", pathStart + 1); - if (pathEnd == absl::string_view::npos) - return false; - - path = std::string(line.substr(pathStart + 1, pathEnd - pathStart - 1)); - return true; -} - void sfz::Parser::readSfzFile(const fs::path& fileName, std::vector& lines) noexcept { std::ifstream fileStream(fileName.c_str()); diff --git a/src/sfizz/SfzHelpers.cpp b/src/sfizz/SfzHelpers.cpp index f72278bd..7239b30c 100644 --- a/src/sfizz/SfzHelpers.cpp +++ b/src/sfizz/SfzHelpers.cpp @@ -169,3 +169,97 @@ absl::optional sfz::readNoteValue(const absl::string_view& value) default: return {}; } } + +bool sfz::findHeader(absl::string_view& source, absl::string_view& header, absl::string_view& members) +{ + auto openHeader = source.find("<"); + if (openHeader == absl::string_view::npos) + return false; + + auto closeHeader = source.find(">", openHeader); + if (openHeader == absl::string_view::npos) + return false; + + auto nextHeader = source.find("<", closeHeader); + header = source.substr(openHeader + 1, closeHeader - openHeader - 1); + if (nextHeader == absl::string_view::npos) { + members = trim(source.substr(closeHeader + 1)); + source.remove_prefix(source.length()); + } else { + members = trim(source.substr(closeHeader + 1, nextHeader - closeHeader - 1)); + source.remove_prefix(nextHeader); + } + + return true; +} + +bool sfz::findOpcode(absl::string_view& source, absl::string_view& opcode, absl::string_view& value) +{ + auto opcodeEnd = source.find("="); + if (opcodeEnd == absl::string_view::npos) + return false; + + const auto valueStart = opcodeEnd + 1; + const auto nextOpcodeEnd = source.find("=", valueStart); + + if (nextOpcodeEnd == absl::string_view::npos) { + opcode = source.substr(0, opcodeEnd); + value = source.substr(valueStart); + source.remove_prefix(source.length()); + return true; + } + + auto valueEnd = nextOpcodeEnd; + while (source[valueEnd] != ' ' && valueEnd != valueStart) + valueEnd--; + + opcode = source.substr(0, opcodeEnd); + value = source.substr(valueStart, valueEnd - valueStart); + source.remove_prefix(valueEnd); + return true; +} + + +bool sfz::findDefine(absl::string_view line, absl::string_view& variable, absl::string_view& value) +{ + const auto defPosition = line.find("#define"); + if (defPosition == absl::string_view::npos) + return false; + + const auto variableStart = line.find("$", 7); + if (variableStart == absl::string_view::npos) + return false; + + const auto variableEnd = line.find_first_of(" \r\t\n\f\v", variableStart); + if (variableEnd == absl::string_view::npos) + return false; + + const auto valueStart = line.find_first_not_of(" \r\t\n\f\v", variableEnd); + if (valueStart == absl::string_view::npos) + return false; + + const auto valueEnd = line.find_first_of(" \r\t\n\f\v", valueStart); + variable = line.substr(variableStart, variableEnd - variableStart); + value = valueEnd != absl::string_view::npos + ? line.substr(valueStart, valueEnd - valueStart) + : line.substr(valueStart); + return true; +} + +bool sfz::findInclude(absl::string_view line, std::string& path) +{ + const auto defPosition = line.find("#include"); + if (defPosition == absl::string_view::npos) + return false; + + const auto pathStart = line.find("\"", 8); + if (pathStart == absl::string_view::npos) + return false; + + const auto pathEnd = line.find("\"", pathStart + 1); + if (pathEnd == absl::string_view::npos) + return false; + + path = std::string(line.substr(pathStart + 1, pathEnd - pathStart - 1)); + return true; +} diff --git a/src/sfizz/SfzHelpers.h b/src/sfizz/SfzHelpers.h index a708fe32..bf8c9083 100644 --- a/src/sfizz/SfzHelpers.h +++ b/src/sfizz/SfzHelpers.h @@ -126,5 +126,9 @@ inline float ccSwitchedValue(const SfzCCArray& ccValues, const absl::optional readNoteValue(const absl::string_view& value); +bool findHeader(absl::string_view& source, absl::string_view& header, absl::string_view& members); +bool findOpcode(absl::string_view& source, absl::string_view& opcode, absl::string_view& value); +bool findDefine(absl::string_view line, absl::string_view& variable, absl::string_view& value); +bool findInclude(absl::string_view line, std::string& path); } // namespace sfz diff --git a/src/sfizz/StringViewHelpers.h b/src/sfizz/StringViewHelpers.h index 4248908e..f488d11e 100644 --- a/src/sfizz/StringViewHelpers.h +++ b/src/sfizz/StringViewHelpers.h @@ -76,12 +76,10 @@ constexpr uint64_t Fnv1aPrime = 0x01000193; * @param h the hashing seed to use * @return uint64_t */ -inline constexpr uint64_t hash(absl::string_view s, uint64_t h = Fnv1aBasis) +constexpr uint64_t hash(absl::string_view s, uint64_t h = Fnv1aBasis) { if (s.length() > 0) return hash( { s.data() + 1, s.length() - 1 }, (h ^ s.front()) * Fnv1aPrime ); return h; } - -