Skip to content

Commit

Permalink
fix: Adapt parse_comment_line to handle more MatchSpec
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Jerphanion <[email protected]>
  • Loading branch information
jjerphan committed Oct 2, 2024
1 parent 9fc7b99 commit 6711ca5
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion libmamba/src/core/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,54 @@ namespace mamba
{
needle[0] = value[idx_start];
idx_end = value.find_first_of(needle.c_str(), idx_search);

// Capturing `MatchSpecs` without internal quotes (e.g `libcurl`)
if (idx_end != std::string::npos && value[idx_end - 1] != '\\')
{
pkg_specs.push_back(value.substr(idx_start + 1, idx_end - 1 - idx_start));
auto start_string = idx_start + 1;
auto len_matchspec = idx_end - start_string;

// Quotes are excluded (e.g. `libcurl` is extracted from `'libcurl'` or
// `"libcurl"`)
auto match_spec = value.substr(start_string, len_matchspec);
std::cout << "Extracted MatchSpec: " << match_spec << std::endl;
pkg_specs.push_back(match_spec);
idx_start = value.find_first_of("\'\"", idx_end + 1);
idx_search = idx_start + 1;
}
// Capturing `MatchSpecs` with metadata (e.g `libcurl[version=\">=7.86,<8.10\"]`)
else if (idx_end != std::string::npos && value[idx_end - 1] == '\\')
{
// Find if "[" is present in between idx_search and idx_end
auto idx_bracket = value.find_first_of("[", idx_search);

// If "[" is present, then find the closing bracket
if (idx_bracket != std::string::npos && idx_bracket < idx_end)
{
auto idx_closing_bracket = value.find_first_of("]", idx_bracket);
if (idx_closing_bracket != std::string::npos)
{
auto start_string = idx_start + 1;
auto end_string = idx_closing_bracket;
auto len_matchspec = end_string - start_string;

// Quotes are excluded (e.g. `libcurl[version=\">=7.86,<8.10\"]` is
// extracted from
// `'libcurl[version=\">=7.86,<8.10\"]'` or
// `"libcurl[version=\">=7.86,<8.10\"]"`)
auto match_spec = value.substr(start_string, len_matchspec);
pkg_specs.push_back(match_spec);
idx_start = value.find_first_of("\'\"", end_string);
idx_search = std::min(idx_closing_bracket + 2, value.size());
}
}
// If "[" is not present, then there's a problem with the MatchSpec
else if (idx_bracket == std::string::npos || idx_bracket > idx_end)
{
throw std::runtime_error("Parsing of history file failed at: " + value);
}
}

else
{
idx_search = idx_end;
Expand Down

0 comments on commit 6711ca5

Please sign in to comment.