-
Notifications
You must be signed in to change notification settings - Fork 68
/
SequenceableCollection.extension.st
39 lines (33 loc) · 1.32 KB
/
SequenceableCollection.extension.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Extension { #name : #SequenceableCollection }
{ #category : #'*VMMaker-translation support' }
SequenceableCollection >> copyReplaceAll: oldSubstring with: newSubstring tokenish: tokenishBlock [
"Answer a copy of the receiver in which all occurrences of
oldSubstring have been replaced by newSubstring.
tokenishBlock specifies what elements are in a token."
| sequence startSearch currentIndex endIndex |
sequence := self.
startSearch := 1.
[(currentIndex := sequence indexOfSubCollection: oldSubstring startingAt: startSearch)
> 0] whileTrue:
[endIndex := currentIndex + oldSubstring size - 1.
((currentIndex = 1
or: [(tokenishBlock value: (sequence at: currentIndex-1)) not])
and: [endIndex = sequence size
or: [(tokenishBlock value: (sequence at: endIndex+1)) not]])
ifTrue: [sequence := sequence
copyReplaceFrom: currentIndex
to: endIndex
with: newSubstring.
startSearch := currentIndex + newSubstring size]
ifFalse: [startSearch := currentIndex + 1]].
^sequence
"Test case:
'pid_t (*pid)(pid_t pid)' copyReplaceAll: 'pid' with: 'foo' tokenish: [:ch| ch = $_ or: [ch isAlphaNumeric]]"
]
{ #category : #'*VMMaker' }
SequenceableCollection >> copyReplaceAt: index with: blockTransform [
^ self
copyReplaceFrom: index
to: index
with: { (blockTransform cull: (self at: index)) }
]