Skip to content

Commit

Permalink
Made String >> hasSubstring:at: more robust for a variety of situatio…
Browse files Browse the repository at this point in the history
…ns detailed in the tests.
  • Loading branch information
marcor committed May 13, 2021
1 parent bdd2b23 commit 1118024
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/Collections-Strings-Tests/StringTest.class.st
Expand Up @@ -1260,6 +1260,24 @@ StringTest >> testFormatFailures [
]
{ #category : #test }
StringTest >> testHasSubstringAt [
"Test empty strings"
self assert: ('' hasSubstring: '' at: 1).
self assert: ('test' hasSubstring: '' at: 1).
self deny: ('' hasSubstring: 'test' at: 1).
"Test out of bounds indexes"
self deny: ('test' hasSubstring: 't' at: 0).
self deny: ('test' hasSubstring: 't' at: 10).
"Test matching substring"
self assert: ('test' hasSubstring: 'st' at: 3).
"Test partially matching substring"
self deny: ('test' hasSubstring: 'sty' at: 3).
]
{ #category : #tests }
StringTest >> testHasWideCharacterFromTo [
Expand Down
8 changes: 5 additions & 3 deletions src/Collections-Strings/String.class.st
Expand Up @@ -1601,12 +1601,14 @@ String >> hasSubstring: str at: index [
"Answer true if the receiver contains the substring str exactly at index, false otherwise."
| pos |
(index < 1) ifTrue: [ ^ false ].
pos := index - 1.
^ str size <= (self size - pos) and:
[ str allSatisfy: [ :char |
[ str noneSatisfy: [ :char |
pos := pos + 1.
(self at: pos) = char ] ]
(self at: pos) ~= char ] ]
]
{ #category : #testing }
Expand Down

0 comments on commit 1118024

Please sign in to comment.