Skip to content

fix: make Contains and collection helpers rune-safe for Unicode strings#1908

Open
HarshalPatel1972 wants to merge 3 commits into
stretchr:masterfrom
HarshalPatel1972:fix/elements-match-unicode
Open

fix: make Contains and collection helpers rune-safe for Unicode strings#1908
HarshalPatel1972 wants to merge 3 commits into
stretchr:masterfrom
HarshalPatel1972:fix/elements-match-unicode

Conversation

@HarshalPatel1972
Copy link
Copy Markdown

Summary

Makes assert.Contains and underlying collection validation routines rune-safe to prevent false positives and out-of-bounds slicing bugs on multi-byte Unicode strings/emojis.

Changes

  • Implemented runeSliceContains, a zero-dependency sliding window helper that evaluates []rune collections to preserve character boundary integrity.
  • Updated containsElement for reflect.String variants to convert target data into []rune slices before executing structural matches.
  • Added boundary validation regression tracking test scenarios inside assert/assertions_test.go.

Motivation

The previous implementation relied directly on strings.Contains, which works exclusively on raw byte tracking. If a searched token matched a specific sequence of middle-bytes within a multi-byte Unicode rune or emoji, the assertion returned invalid positive outcomes or triggered index runtime panics.

Related issues

Closes #1518

Copilot AI review requested due to automatic review settings June 5, 2026 16:35
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR updates string containment checks in assert.Contains to avoid matching invalid UTF-8 byte fragments that cross Unicode rune boundaries, and adds regression tests covering Unicode-heavy scenarios.

Changes:

  • Replace strings.Contains with a rune-based containment check for string inputs.
  • Add new helper runeSliceContains to compare contiguous rune sub-slices.
  • Add tests for Unicode behavior in Contains and ElementsMatch.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
assert/assertions.go Switch string containment to rune-based search and add runeSliceContains helper
assert/assertions_test.go Add Unicode-focused tests for Contains and ElementsMatch

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread assert/assertions_test.go Outdated
Comment thread assert/assertions_test.go
Comment thread assert/assertions.go Outdated
Comment thread assert/assertions.go
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