Skip to content

Replace countPrecedingBackslashes with hasEscapedBackslashAt#2

Draft
Copilot wants to merge 5 commits intomasterfrom
copilot/check-escaped-backslash-stringcomponents
Draft

Replace countPrecedingBackslashes with hasEscapedBackslashAt#2
Copilot wants to merge 5 commits intomasterfrom
copilot/check-escaped-backslash-stringcomponents

Conversation

Copy link

Copilot AI commented Dec 21, 2025

The countPrecedingBackslashes() method returned a count of preceding backslashes, requiring callers to check the parity of the count to determine if a backslash was escaped. This PR replaces it with a more direct boolean method.

Changes

Removed:

  • countPrecedingBackslashes() - returned count of preceding backslashes

Added:

  • hasEscapedBackslashAt() - returns boolean indicating if backslash at given position is escaped

Updated to use new method:

  • hasEscapedWhitespaceAt() - now calls hasEscapedBackslashAt() instead of checking parity of count
  • hasEscapedNewlineAt() - now calls hasEscapedBackslashAt() instead of checking parity of count

Benefits

  • Clearer intent: Method name directly expresses what we're checking
  • Simpler logic: Callers check boolean condition instead of parity of count
  • Same performance: Still scans backwards once per call
  • Minimal changes: Preserves original code structure

Testing

  • ✅ All 1575 tests pass
  • ✅ Code compiles without warnings
  • ✅ Functionality preserved
Original prompt

Would it have been better to add a check for escaped backslash in stringComponents instead of counting backslashes backwards?


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits December 21, 2025 10:20
…ing forward

Instead of counting backslashes backwards at each position, track the escape state as we iterate forward through the string. This is more efficient and intuitive.

- Removed countPrecedingBackslashes(), hasEscapedWhitespaceAt(), and hasEscapedNewlineAt() helper methods
- Added forward-tracking escape state logic in stringComponents()
- Maintains same behavior: properly handles \\t, \\n, \\r and escaped backslashes
- All StringWrapper tests pass

Co-authored-by: jkrinke <3253748+jkrinke@users.noreply.github.com>
- Fix loop condition to properly check bounds before accessing charAt(endIdx + 1)
- Add parentheses to ensure correct boolean logic for 'n' or 'r' check
- Prevents potential StringIndexOutOfBoundsException

Co-authored-by: jkrinke <3253748+jkrinke@users.noreply.github.com>
Move the charAt checks into the while loop condition for cleaner code and better short-circuit evaluation.

Co-authored-by: jkrinke <3253748+jkrinke@users.noreply.github.com>
Copilot AI changed the title [WIP] Add check for escaped backslash in string components Refactor stringComponents to track escape state forward instead of counting backwards Dec 21, 2025
Copilot AI requested a review from jkrinke December 21, 2025 10:30
@jkrinke
Copy link
Owner

jkrinke commented Dec 21, 2025

@copilot These are too many changes. Keep the original source code as most as possible, but remove countPrecedingBackslashes, add a new method hasEscapedBackslashAt, and invoke it within StringWrapper.

Per feedback, keep original code structure but replace countPrecedingBackslashes with a new hasEscapedBackslashAt method that returns a boolean instead of a count.

Changes:
- Removed countPrecedingBackslashes method
- Added hasEscapedBackslashAt method that returns true if backslash is escaped
- Updated hasEscapedWhitespaceAt to use hasEscapedBackslashAt
- Updated hasEscapedNewlineAt to use hasEscapedBackslashAt
- Kept stringComponents method unchanged

Co-authored-by: jkrinke <3253748+jkrinke@users.noreply.github.com>
Copy link
Author

Copilot AI commented Dec 21, 2025

@copilot These are too many changes. Keep the original source code as most as possible, but remove countPrecedingBackslashes, add a new method hasEscapedBackslashAt, and invoke it within StringWrappe...

Done. I've reverted to the original code structure and made only the minimal change as requested:

  • Removed countPrecedingBackslashes
  • Added new method hasEscapedBackslashAt that returns boolean
  • Updated hasEscapedWhitespaceAt and hasEscapedNewlineAt to use the new method
  • Kept stringComponents unchanged

All tests pass. See commit 13ddfb8.

Copilot AI changed the title Refactor stringComponents to track escape state forward instead of counting backwards Replace countPrecedingBackslashes with hasEscapedBackslashAt Dec 21, 2025
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