Skip to content

Commit

Permalink
Merge Allow any characters after expected MM header
Browse files Browse the repository at this point in the history
This merge allows any characters (esp. `\r`) after the expected matrix market header. Additionally, if the regex search failed, the description line is also printed.

Related PR: #1557
  • Loading branch information
MarcelKoch authored Feb 27, 2024
2 parents ba16a9b + cc20b0b commit 4b31772
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
8 changes: 5 additions & 3 deletions core/base/mtx_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,9 +679,9 @@ class mtx_io {

std::smatch match{};
GKO_CHECK_MATCH(
regex_match(
regex_search(
description_line, match,
std::regex("%%matrixmarket matrix "
std::regex("^%%matrixmarket matrix "
"(coordinate|array) "
"(real|integer|complex|pattern) "
"(general|symmetric|skew-symmetric|hermitian)")),
Expand All @@ -693,7 +693,9 @@ class mtx_io {
" <LAYOUT-TYPE> is one of: coordinate, array\n"
" <VALUE-TYPE> is one of: real, integer, complex, pattern\n"
" <LAYOUT-MODIFIER> is one of: general, symmetric, "
"skew-symmetric, hermitian\n");
"skew-symmetric, hermitian\n"
"Found the following header instead: " +
description_line);

data.layout = layout_map.at(match[1]);
data.entry = format_map.at(match[2]);
Expand Down
16 changes: 16 additions & 0 deletions core/test/base/mtx_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,22 @@ TEST(MtxReader, ReadsSparseComplexHermitianMtx)
}


TEST(MtxReader, ReadIgnoresExtraCharacters)
{
using tpl = gko::matrix_data<double, gko::int32>::nonzero_type;
std::istringstream iss(
"%%MatrixMarket matrix array real general ??? whateve\r\n"
"1 1\n"
"0.1\n");

auto data = gko::read_raw<double, gko::int32>(iss);

ASSERT_EQ(data.size, gko::dim<2>(1, 1));
auto& v = data.nonzeros;
ASSERT_EQ(v[0], tpl(0, 0, 0.1));
}


std::array<gko::uint64, 20> build_binary_complex_data()
{
gko::uint64 int_val{};
Expand Down

0 comments on commit 4b31772

Please sign in to comment.