[squeak-dev] The Trunk: Regex-Core-ct.69.mcz
commits at source.squeak.org
commits at source.squeak.org
Wed Oct 5 18:05:41 UTC 2022
Christoph Thiede uploaded a new version of Regex-Core to project The Trunk:
http://source.squeak.org/trunk/Regex-Core-ct.69.mcz
==================== Summary ====================
Name: Regex-Core-ct.69
Author: ct
Time: 23 August 2021, 10:32:19.392748 pm
UUID: 03218e64-f6d8-7349-84be-00f61d947856
Ancestors: Regex-Core-mt.61
Adds String >> #escapeRegex to escape special characters in a string before composing it into another regex.
Usage:
':-)' matchesRegex: ':-)' escapeRegex
Supersedes Regex-Core-ct.61. Reuploaded to optimize #escapeString:. Thanks to Levente (ul) for the detailed instructions!
=============== Diff against Regex-Core-mt.61 ===============
Item was added:
+ ----- Method: RxParser class>>escapeString: (in category 'utilities') -----
+ escapeString: aString
+ "Answer a copy of aString which does not contain any unescaped characters. This is the inverse function of String >> #matchesRegex:.
+ NB: Basically, we could simply escape every single character in the string, but this would not produce human-readable outputs."
+
+ | special lastIndex nextIndex |
+ special := self specialCharacters.
+ nextIndex := aString indexOfAnyOf: special startingAt: (lastIndex := 1) ifAbsent: [^ aString].
+ ^ String new: aString size * 11 // 10 "+10%" streamContents: [:stream |
+ [stream
+ next: nextIndex - lastIndex putAll: aString startingAt: lastIndex;
+ nextPut: $\;
+ nextPut: (aString at: nextIndex)]
+ doWhileTrue: [(nextIndex := aString indexOfAnyOf: special startingAt: (lastIndex := nextIndex + 1)) > 0].
+ stream next: aString size - lastIndex + 1 putAll: aString startingAt: lastIndex]!
Item was added:
+ ----- Method: RxParser class>>specialCharacters (in category 'utilities') -----
+ specialCharacters
+
+ ^ '()[]*+?{}.^$:\'!
Item was added:
+ ----- Method: String>>escapeRegex (in category '*Regex-Core') -----
+ escapeRegex
+
+ ^ RxParser escapeString: self!
More information about the Squeak-dev
mailing list
|