From 9664a3369500d9c7cb93225ea9a6ce4ab372d3a4 Mon Sep 17 00:00:00 2001 From: "Joachim Wuttke (h)" <j.wuttke@fz-juelich.de> Date: Mon, 14 Aug 2023 22:58:07 +0200 Subject: [PATCH] replace regex in ParseUtil --- Device/IO/ParseUtil.cpp | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/Device/IO/ParseUtil.cpp b/Device/IO/ParseUtil.cpp index 5dbf01ad4ad..f36825a24db 100644 --- a/Device/IO/ParseUtil.cpp +++ b/Device/IO/ParseUtil.cpp @@ -18,10 +18,8 @@ #include "Base/Util/StringUtil.h" #include "Device/Data/Datafield.h" #include <cmath> // ignoreDenormalized -#include <functional> #include <iostream> #include <iterator> -#include <regex> namespace { @@ -64,13 +62,29 @@ Scale* Util::Parse::parseScale(std::istream& input_stream) { std::string line; std::getline(input_stream, line); - std::smatch matched; - std::regex_match(line, matched, std::regex("(\\w+)\\(\"(.*?)\",\\s*(.+?)\\)")); - if (matched.size() != 4) - throw std::runtime_error("Cannot read axis from input"); - const std::string type = matched[1]; - const std::string name = matched[2]; - const std::string body = matched[3]; + + size_t j = line.find_first_of('('); + if (j == std::string::npos) + throw std::runtime_error("Scale constructor has no '('"); + std::string type = line.substr(0, j); + + line = line.substr(j + 1); + if (line.back() != ')') + throw std::runtime_error("Scale constructor call not ending with ')'"); + line = line.substr(0, line.size() - 1); + + if (line[0] != '"') + throw std::runtime_error("Scale constructor arg 1 does not start with \""); + j = 1 + line.substr(1).find_first_of('"'); + if (j == std::string::npos) + throw std::runtime_error("Scale constructor arg 1 has no closing \""); + std::string name = line.substr(1, j - 1); + + if (line[j + 1] != ',') + throw std::runtime_error("Scale constructor arg 1 not followed by comma"); + std::string body = line.substr(j + 2); + body = Base::String::trimFront(body, " "); + if (type == "EquiDivision" || type == "FixedBinAxis" /* for compatibility with pre-21 */) { std::vector<std::string> arr = Base::String::split(body, ","); int nbins; -- GitLab