CharacterArray.st
changeset 19746 a08dda93b89a
parent 19743 b5c295a51c56
child 19747 f9e14d358927
--- a/CharacterArray.st	Sat May 07 13:49:32 2016 +0200
+++ b/CharacterArray.st	Sat May 07 15:26:02 2016 +0200
@@ -4736,6 +4736,72 @@
     "Modified: 13.9.1997 / 06:31:22 / cg"
 !
 
+globPatternAsRegexPattern
+    "taking the receiver as a GLOB pattern,
+     return a corresponding regex pattern.
+     As regex does provide information about the matching substring,
+     it may be useful to apply a regex after a GLOB match, 
+     in order to highlight matching substrings (eg. in a CodeView after a search).
+     If it turns out to be better, we may convert all GLOB searches to regex and use it right away.
+     (currently, it is not sure, if GLOB is not better for most simple searches, as they are encountered
+      in typical real life)"
+
+    ^ self species streamContents:[:s |
+        |addCharacter|
+        
+        addCharacter := 
+            [:ch |
+                ch isLetterOrDigit ifFalse:[
+                    s nextPut:$\.
+                ].    
+                s nextPut:ch
+            ].
+                   
+        (String matchScanArrayFrom:self) do:[:matchEntry |
+            matchEntry isCharacter ifTrue:[
+                addCharacter value:matchEntry
+            ] ifFalse:[
+                matchEntry == #anyString ifTrue:[
+                    s nextPutAll:'.*'
+                ] ifFalse:[    
+                    matchEntry == #any ifTrue:[
+                        s nextPut:$.
+                    ] ifFalse:[
+                        matchEntry isString ifTrue:[
+                            |set min max|
+                            
+                            s nextPut:$[.
+                            set := matchEntry copy sort.
+                            min := set min.
+                            max := set max.
+                            set asSet = (min to:max) asSet ifTrue:[
+                                addCharacter value:min.
+                                s nextPut:$-.
+                                addCharacter value:max.
+                            ] ifFalse:[    
+                                set do:addCharacter.
+                            ].    
+                            s nextPut:$].
+                        ] ifFalse:[    
+                            self halt.
+                        ].    
+                    ].    
+                ].    
+            ]
+        ].
+    ].    
+
+    "
+     'hello' globPatternAsRegexPattern
+     'hello*' globPatternAsRegexPattern
+     '*hello*' globPatternAsRegexPattern
+     'h###' globPatternAsRegexPattern
+     'h[0-9]' globPatternAsRegexPattern
+     'h[0-9][0-9][0-9]' globPatternAsRegexPattern
+     'h[0-9]*' globPatternAsRegexPattern
+    "
+!
+
 includesMatchString:matchString
     "like includesString, but allowing GLOB match patterns.
      find matchstring; if found, return true, otherwise return false.