Christoph Thiede uploaded a new version of Regex-Core to project The Inbox: http://source.squeak.org/inbox/Regex-Core-ct.88.mcz
==================== Summary ====================
Name: Regex-Core-ct.88 Author: ct Time: 2 March 2024, 5:54:50.709512 pm UUID: 9dc8a041-9d03-994f-8c9b-4cdb24b5a303 Ancestors: Regex-Core-ct.87
Proposal: Optionally pass matcher to matchBlock in match enumerators.
E.g., this allows: 'Squeak! Is! Great!' copyWithRegex: '(\w+)\p{P}' matchesTranslatedUsing: [:match :matcher | matcher subexpression: 2] Instead of: | matcher | matcher := '(\w+)\p{P}' asRegex. 'Squeak! Is! Great!' copyWithRegex: matcher matchesTranslatedUsing: [:match | matcher subexpression: 2]
=============== Diff against Regex-Core-ct.87 ===============
Item was changed: ----- Method: RxMatcher>>copyStream:to:translatingMatchesUsing: (in category 'match enumeration') ----- copyStream: aStream to: writeStream translatingMatchesUsing: aBlock "Copy the contents of <aStream> on the <writeStream>, except for the matches. For each match, evaluate <aBlock> passing the matched substring as the argument. Expect the block to answer a String, and write the answer to <writeStream> in place of the match."
| searchStart matchStart matchEnd match | stream := aStream. firstTryMatch := true. [searchStart := aStream position. self proceedSearchingStream: aStream] whileTrue: [matchStart := (self subBeginning: 1) first. matchEnd := (self subEnd: 1) first. aStream position: searchStart. searchStart to: matchStart - 1 do: [:ignoredPos | writeStream nextPut: aStream next]. match := (String new: matchEnd - matchStart + 1) writeStream. matchStart to: matchEnd - 1 do: [:ignoredPos | match nextPut: aStream next]. + writeStream nextPutAll: (aBlock cull: match contents cull: self). - writeStream nextPutAll: (aBlock value: match contents). "Be extra careful about successful matches which consume no input. After those, make sure to advance or finish if already at end." matchEnd = searchStart ifTrue: [aStream atEnd ifTrue: [^self "rest after end of whileTrue: block is a no-op if atEnd"] ifFalse: [writeStream nextPut: aStream next]]]. aStream position: searchStart. [aStream atEnd] whileFalse: [writeStream nextPut: aStream next]!
Item was changed: ----- Method: RxMatcher>>matchesIn:collect: (in category 'match enumeration') ----- matchesIn: aString collect: aBlock "Search aString repeatedly for the matches of the receiver. Evaluate aBlock for each match passing the matched substring as the argument, collect evaluation results in an OrderedCollection, and return in. The following example shows how to use this message to split a string into words." "'\w+' asRegex matchesIn: 'Now is the Time' collect: [:each | each asLowercase]"
| result | result := OrderedCollection new. self matchesOnStream: aString readStream + do: [:match | result add: (aBlock cull: match cull: self)]. - do: [:match | result add: (aBlock value: match)]. ^result!
Item was changed: ----- Method: RxMatcher>>matchesOnStream:collect: (in category 'match enumeration') ----- matchesOnStream: aStream collect: aBlock
| result | result := OrderedCollection new. self matchesOnStream: aStream + do: [:match | result add: (aBlock cull: match cull: self)]. - do: [:match | result add: (aBlock value: match)]. ^result!
Item was changed: ----- Method: RxMatcher>>matchesOnStream:do: (in category 'match enumeration') ----- matchesOnStream: aStream do: aBlock "Be extra careful about successful matches which consume no input. After those, make sure to advance or finish if already at end."
| position subexpression | [ position := aStream position. self searchStream: aStream ] whileTrue: [ subexpression := self subexpression: 1. + aBlock cull: subexpression cull: self. - aBlock value: subexpression. subexpression size = 0 ifTrue: [ aStream atEnd ifTrue: [^self] ifFalse: [aStream next]]]!
squeak-dev@lists.squeakfoundation.org