Skip to content

StringUtils: make prefix/suffix(char) return whole string when delimiter absent - #9501

Merged
timosachsenberg merged 5 commits into
developfrom
claude/stringutils-bug-eval-k6a83d
Jun 16, 2026
Merged

timosachsenberg merged 5 commits into
developfrom
claude/stringutils-bug-eval-k6a83d

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

prefix(s, delim) and suffix(s, delim) previously threw Exception::ElementNotFound
when the delimiter was not present. Throwing a container-lookup exception from a
string-slicing helper is surprising and forced callers to either guard every call
or wrap it in try/catch.

Now the missing-delimiter case returns the whole string ("nothing to cut off ->
you get the original back"), giving a simple, symmetric, no-surprises contract.
This does not change behavior for any valid input (a valid delimiter is always present).

Call sites that relied on the old throw as control flow were updated explicitly, so the
lenient contract does not silently change their behavior on malformed input:

  • FileHandler::getTypeByFileName: explicit has('.') guard (preserves Bruker
    fid -> XMASS and UNKNOWN handling).
  • ImageCreator: explicit has('.') guard (preserves "nosuffix" fallback).
  • TOPPBase::parseRange_: now requires the documented "[min]:[max]" separator and throws
    ConversionError when ':' is missing, instead of silently reading "400" as the degenerate
    range 400:400 (affects FileFilter, IDFilter, DTAExtractor, CometAdapter, ImageCreator).
  • RibonucleotideTSVDataProvider::parseRow_: a malformed ambiguity entry (10th field without
    the space separator) throws ParseError and the row is skipped, as before, instead of
    duplicating the single value into both alternative codes.
  • TargetedSpectraExtractor: an identifier without '_' leaves PeptideRef empty (as the
    previous throw-and-catch did) rather than using the whole identifier.

MSGFPlusAdapter was also switched to the lenient suffix('=') for scan-number extraction;
this is equivalent to (and replaces) the rfind('=') ternary already on develop, so it is a
cosmetic refactor with no behavior change. Updated String_test and TOPPBase_test accordingly.

https://claude.ai/code/session_01TzmuvTWtGMmc5hhH8kXK7Y

Summary by CodeRabbit

  • Bug Fixes
    • String utilities now return the original string when a requested character delimiter is missing (instead of raising an error).
    • Filenames without an extension are now consistently treated as unknown file type.
    • Image output format detection and scan number parsing are now more robust when expected separators/delimiters are absent.
    • Range parsing now validates the required min:max format and errors when the : separator is missing.
  • Tests
    • Updated and extended tests to cover delimiter-absent and colon-missing range inputs.

@github-actions

Copy link
Copy Markdown
Contributor

@timosachsenberg The following names might be missing from the AUTHORS file:

  • Claude

Please consider applying the following patch:

--- AUTHORS	2026-06-12 19:32:17.926427885 +0000
+++ AUTHORS.new	2026-06-12 19:32:23.105329884 +0000
@@ -22,8 +22,9 @@
  - Chirag Dave
  - Chris Bauer
  - Chris Bielow
- - Christie Mathews
  - Christian Ehrlich
+ - Christie Mathews
+ - Claude
  - Clemens Groepl
  - Cornelia Friedle
  - Daniel Jameson

@coderabbitai

coderabbitai Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

StringUtils prefix/suffix overloads now return the full input when a char delimiter is absent instead of throwing exceptions. Callers in FileHandler and ImageCreator replace exception handling with explicit delimiter checks. Tests updated to the new contract. MSGFPlusAdapter uses the new suffix semantics for scan number parsing. Separately, TOPPBase parseRange_ adds mandatory colon validation with corresponding test coverage.

Changes

StringUtils Delimiter Handling Refactor

Layer / File(s) Summary
StringUtils prefix and suffix contract change
src/openms/include/OpenMS/DATASTRUCTURES/StringUtils.h
prefix(const std::string&, char) and suffix(const std::string&, char) overloads now return the whole input string when the delimiter is absent instead of throwing Exception::ElementNotFound. The implementation uses find/rfind and conditional substr logic.
FileHandler extension parsing migration
src/openms/source/FORMAT/FileHandler.cpp
getTypeByFileName adds an explicit guard to check if the basename contains . and returns FileTypes::UNKNOWN when absent, then unconditionally calls StringUtils::suffix(basename, '.') for extension mapping.
ImageCreator format resolution migration
src/openms_gui/source/VISUAL/APPLICATIONS/GUITOOLS/ImageCreator.cpp
Output image format is determined by checking StringUtils::has(out, '.') and using the suffix when present, or falling back to "nosuffix" when absent, replacing prior try/catch exception handling.
StringUtils delimiter test assertions
src/tests/class_tests/openms/source/String_test.cpp
Test cases for prefix and suffix with absent delimiters are updated to assert that the full input string is returned, replacing prior Exception::ElementNotFound expectations.
MSGFPlusAdapter scan number parsing
src/topp/MSGFPlusAdapter.cpp
When ScanNum is missing or -1, scan_number is now parsed from the SpecID token by extracting the substring after the last = via StringUtils::suffix, handling SpecID strings that include key/value fields.

TOPPBase Range Parsing Validation

Layer / File(s) Summary
TOPPBase parseRange_ colon validation
src/openms/source/APPLICATIONS/TOPPBase.cpp
Both double and Int overloads of parseRange_ now check for the presence of : before attempting to parse min/max values. When the separator is absent, a ConversionError is thrown indicating the expected format [min]:[max] with an explicit message that the : separator is missing.
TOPPBase parseRange_ validation test
src/tests/class_tests/openms/source/TOPPBase_test.cpp
New test case verifies that parseRange_ throws Exception::ConversionError when the input string is missing the required colon separator, e.g., "400" without a colon.

🐰 Strings once threw, now they flow,
When delims hide, the whole returns below,
No more try/catch, just checks divine,
Guard clauses and ranges must align,
The refactor hops forward, crisp and clean!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and accurately summarizes the main change: StringUtils prefix/suffix functions now return the whole string when delimiter is absent instead of throwing an exception.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/stringutils-bug-eval-k6a83d

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/openms/include/OpenMS/DATASTRUCTURES/StringUtils.h`:
- Around line 503-504: Update the Doxygen comments for the functions that return
parts before/after a delimiter (the prefix(char) and suffix(char) overloads in
StringUtils.h) to use an `@brief` block followed by a blank line and then the
detailed description: move the current single-line /// descriptions into a
proper `@brief` line, insert a blank line, and then add the rest of the
explanation (e.g., behavior when delim is not found) in the detail section so
the comments conform to the header guideline requiring "`@brief`" plus a
blank-line-separated detail section.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 88a3d2e1-17ca-4204-bfb3-955c4596cb85

📥 Commits

Reviewing files that changed from the base of the PR and between 87e1314 and 3212eaa.

📒 Files selected for processing (5)
  • src/openms/include/OpenMS/DATASTRUCTURES/StringUtils.h
  • src/openms/source/FORMAT/FileHandler.cpp
  • src/openms_gui/source/VISUAL/APPLICATIONS/GUITOOLS/ImageCreator.cpp
  • src/tests/class_tests/openms/source/String_test.cpp
  • src/topp/MSGFPlusAdapter.cpp

claude added 3 commits June 15, 2026 10:05
…ter absent

prefix(s, delim) and suffix(s, delim) previously threw Exception::ElementNotFound
when the delimiter was not present. Throwing a container-lookup exception from a
string-slicing helper is surprising and forced callers to either guard every call
or wrap it in try/catch. It was also the root cause of the MSGFPlusAdapter
scan-number regression: the String->std::string migration dropped the implicit
suffix('=') extraction, and the strict throw turned a benign input into a crash.

Now the missing-delimiter case returns the whole string ("nothing to cut off ->
you get the original back"), giving a simple, symmetric, no-surprises contract.

Fixed the only two call sites that relied on the throw as control flow:
- FileHandler::getTypeByFileName: explicit has('.') guard (preserves Bruker
  fid -> XMASS and UNKNOWN handling).
- ImageCreator: explicit has('.') guard (preserves "nosuffix" fallback).

All other char-delim callers either guarantee the delimiter is present or had no
exception handling (so they would previously crash on missing input and now
degrade gracefully). Updated String_test accordingly.

https://claude.ai/code/session_01TzmuvTWtGMmc5hhH8kXK7Y
PR #9450 dropped the implicit suffix('=') when migrating String->std::string,
turning a SpecID like "controllerType=0 controllerNumber=1 scan=17" into a
ConversionError. With suffix(char) now returning the whole string when the
delimiter is absent, the original intent is expressed in a single line and also
handles a bare-integer SpecID gracefully.

https://claude.ai/code/session_01TzmuvTWtGMmc5hhH8kXK7Y
@timosachsenberg
timosachsenberg force-pushed the claude/stringutils-bug-eval-k6a83d branch from 3eb9fa0 to 672e4b5 Compare June 15, 2026 08:09
timosachsenberg and others added 2 commits June 15, 2026 10:24
…misreading

With StringUtils::prefix/suffix(.,':') now returning the whole string when the
delimiter is absent, a colon-less range like "400" was silently interpreted as the
degenerate range 400:400 instead of failing as before. Restore the loud failure:
parseRange_ now throws Exception::ConversionError when the mandatory ':' separator
(documented format "[min]:[max]") is missing. Valid input (which always contains ':',
e.g. ":", "0:0", "600:5000") is unaffected. Adds a regression test.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…hrowing

After making StringUtils::prefix/suffix(char) lenient, two broad-catch call sites
silently changed behavior on malformed input. Restore the intended behavior explicitly:

- RibonucleotideTSVDataProvider::parseRow_: an ambiguity entry whose 10th field lacks
  the space separator now throws ParseError (so the row is skipped, as before) instead
  of silently setting both alternative codes to the same single value.
- TargetedSpectraExtractor: an identifier without a '_' now leaves PeptideRef empty
  (as the previous throw-and-catch did) instead of silently using the whole identifier;
  the dead try/catch is removed.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

@timosachsenberg timosachsenberg left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found one API semantic-change regression risk.

@timosachsenberg
timosachsenberg merged commit 2565224 into develop Jun 16, 2026
41 checks passed
@timosachsenberg
timosachsenberg deleted the claude/stringutils-bug-eval-k6a83d branch June 16, 2026 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants