Closed firthm01 closed 1 year ago
Looks good, couple of things I thought of:
#include <regex>
std::vector<std::string> get_vst_chunks(std::string input) {
std::smatch matches{};
// regex grabs the least number of chars between '<VST', and the '>' char on a new line.
// Having <VST in the plugin name should be fine as it'll always be inside a match at that point.
// checking for newline before the '>' should guard against that character appearing in the plugin name
auto const static regex = std::regex{"<VST ((?:\\n|.|\\r)*?)\n\r?>"};
std::vector<std::string> plugin_chunks;
while(std::regex_search(input, matches, regex)) {
plugin_chunks.push_back(matches[1].str());
input = matches.suffix();
}
return plugin_chunks;
}
bool is_quote(char const c) {
return c == '\'' || c == '`' || c == '"';
}
bool is_space(unsigned char c) { return std::isspace(c); }
std::vector
for(auto const c : input) { if(is_quote(c)) { if(!outer_quote) { outer_quote = c; if(previous_char_was_whitespace) { add_token(); } } else if(outer_quote == c) { outer_quote = {}; } else { ss << c; } } if(is_space(c)) { if(outer_quote) { ss << c; } else { previous_char_was_whitespace = true; } } if(!is_space(c) && !is_quote(c)) { if(previous_char_was_whitespace) { add_token(); } ss << c; } } if(!ss.str().empty()) { add_token(); } return tokens; }
Thanks for that :)
If the ability to rename is new, does that mean the state chunk has changed? What happens if this is run in a version with an old state chunk
Good point! I will check. I guess that does add some fragility to this solution in that we are relying on a consistent layout of the state chunk.
The first part of the parsing (stripping out the VST chunk) can be done with a regex I think, it's probably a bit slower and debatable as to whether it's clearer than doing it your way, not sure what you think?
Regex is scary. But other than that, I think I doing it step by step makes it easier to debug if it does need to be changed later on. I guess it's much more concise with regex though.
I wound up rewriting the second bit to get my head around it, I’m not sure if it’s any more clear and it’s certainly less flexible (it always strips out enclosing cruft rather than having that configurable)
Ideally I'd want the option to keep bounding quotes and separators. It just means that if we want to Set in future, we can just change the element we're interested in and concat all of the vector elements to reconstruct the chunk. This was my original motivation - I wanted to see how easy/not it would be to automatically rename plugins to show object names - then I realised the renaming functionality probably breaks export (which it did) and needs fixing anyway.
Great, that all seems fair. Most of the regex scaryness is dealing with the fact that the c++ std lib regex implementation doesn't handle multi-line matches very well so you have to handle the line endings explicitly. I probably should have just given up when I realised that.
If the ability to rename is new, does that mean the state chunk has changed? What happens if this is run in a version with an old state chunk
Good point! I will check.
Turns out it's not that new. It's available at least as far back as v5.99 and the state chunk layout is the same.
Closes #240