diff --git a/NEWS.md b/NEWS.md index d4cdf0d..ef5cd7f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # glyparse (development version) +* `parse_strucgp_struc()` rejects unmatched depth markers and malformed content instead of silently turning truncated input into valid structures. (#60) * `parse_pglyco_struc()` and `auto_parse()` reject structures with unclosed pGlyco branches instead of silently accepting truncated input. (#59) * `parse_iupac_short()` now preserves the base monosaccharide's donor carbon for modified residues, such as C2 for `Neu5Ac9Ac` and `Fru6S`, instead of incorrectly defaulting to C1. diff --git a/src/direct.cpp b/src/direct.cpp index 4cd9ff2..1393051 100644 --- a/src/direct.cpp +++ b/src/direct.cpp @@ -38,6 +38,28 @@ static Record pglyco(const S &x) { fail("Invalid pGlyco structure"); return r; } +static void validate_strucgp_markers(const S &x) { + std::vector stack; + for (size_t i = 0; i < x.size();) { + char c = x[i]; + if (c >= 'A' && c <= 'Z') { + if ((stack.empty() && i != 0) || i + 1 >= x.size() || + x[i + 1] < '1' || x[i + 1] > '5') + fail("Invalid StrucGP node"); + stack.push_back(c); + i += 2; + } else if (c >= 'a' && c <= 'z') { + if (stack.empty() || c != std::tolower(static_cast(stack.back()))) + fail("Unbalanced StrucGP depth marker"); + stack.pop_back(); + ++i; + } else { + fail("Invalid StrucGP depth marker"); + } + } + if (x.empty() || !stack.empty()) + fail("Unbalanced StrucGP depth marker"); +} static void strucgp_node(const S &x, Record &r, int parent, int depth = 0) { if (x.empty()) return; @@ -357,6 +379,7 @@ Record parse_direct(const S &x, const S &format, const Vocab &v) { if (format == "pglyco") return pglyco(x); if (format == "strucgp") { + validate_strucgp_markers(x); Record r; r.anomer = "??"; strucgp_node(x, r, 0); diff --git a/tests/testthat/test-native.R b/tests/testthat/test-native.R index e04df1b..55b9aee 100644 --- a/tests/testthat/test-native.R +++ b/tests/testthat/test-native.R @@ -79,10 +79,6 @@ test_that("native ports preserve established tokenization and recovery", { )), c(NA_character_, "GalNAc(a1-") ) - expect_identical( - as.character(parse_strucgp_struc("A1B1a")), - "Hex(??-?)Hex(??-" - ) expect_identical( as.character(parse_linucs("[][b-D-Glcp]{[(4+1)][a-D-Glcp\n]{}}")), "Glc(a1-4)Glc(b1-" diff --git a/tests/testthat/test-parse-strucgp.R b/tests/testthat/test-parse-strucgp.R index 848cf35..ed8b366 100644 --- a/tests/testthat/test-parse-strucgp.R +++ b/tests/testthat/test-parse-strucgp.R @@ -61,3 +61,26 @@ test_that("StrucGP array assembly preserves branched topology", { rep("??-?", 8) ) }) + +test_that("StrucGP rejects unmatched markers and skipped content", { + invalid <- c( + "A1B1a", + "A1B2bX", + "A1B2b", + "A1B2a", + "A1B2baB3b", + "A1B2bXB3ba" + ) + expect_error(parse_strucgp_struc(invalid), "Can't parse") + expect_true(all(is.na(parse_strucgp_struc(invalid, on_failure = "na")))) + expect_true(all(is.na(auto_parse(invalid, on_failure = "na")))) + + input <- c(valid = "A1B2bB3ba", invalid = "A1B2bXB3ba", missing = NA) + result <- parse_strucgp_struc(input, on_failure = "na") + expect_identical(names(result), names(input)) + expect_identical(unname(is.na(result)), c(FALSE, TRUE, TRUE)) + expect_identical( + unname(as.character(result[1])), + as.character(parse_strucgp_struc("A1B2bB3ba")) + ) +})