Added support for matching only a portion of a string and match case sensitive/insensitive.
authorJan Vrany <jan.vrany@fit.cvut.cz>
Wed, 26 Nov 2014 11:45:23 +0100
changeset 3449 21c891242b9d
parent 3448 d51b708eb5d7
child 3450 173128ed138a
Added support for matching only a portion of a string and match case sensitive/insensitive.
StringPattern.st
--- a/StringPattern.st	Wed Nov 26 09:50:35 2014 +0100
+++ b/StringPattern.st	Wed Nov 26 11:45:23 2014 +0100
@@ -99,11 +99,6 @@
     "Created: / 09-08-2011 / 13:42:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
-!StringPattern methodsFor:'accessing'!
-
-data
-    ^ data
-! !
 
 !StringPattern methodsFor:'initialization'!
 
@@ -117,19 +112,44 @@
 !StringPattern methodsFor:'matching'!
 
 match: string 
-
-    "Answers true if myself match the given string.
-     No relaxing done"
-
-    ^self match: string relax: 1.
+    ^self match: string from: 1 to: string size
 
     "Created: / 09-08-2011 / 13:51:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 26-11-2014 / 07:00:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+match: string case: case 
+    ^self match: string case: case relax: 1
+
+    "Created: / 26-11-2014 / 07:02:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+match: string case: case relax: relax 
+    ^self match: string from: 1 to: string size case: case relax: relax
+
+    "Created: / 26-11-2014 / 07:02:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
-match: string relax: relax
+match: string from: from to: to 
+    ^self match: string from: 1 to: string size case: true
+
+    "Created: / 26-11-2014 / 07:01:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+match: string from: from to: to case: case                                                              
+    ^self match: string from: from to: to case: case relax: 1
 
-    "Answers true if myself match the given string.
-     relax argument say how much the matching should
+    "Created: / 26-11-2014 / 07:01:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 26-11-2014 / 10:00:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+match: string from: from to: to case: case relax: relax
+    "Answers true if receiver matches the given `string`
+     portion starting at `from`, enditn at `to`.
+     If `case` is true, then perform natch case sensitive,
+     otherwise match case insensitive.
+
+     `relax` argument say how much the matching should
      be relaxed - relax is a number in <1..3>, where
      1 means no relaxing at all (aka exact match). 
      All patterns should support relax == 1. If the relax
@@ -138,7 +158,20 @@
 
     ^self subclassResponsibility
 
+    "Created: / 26-11-2014 / 06:35:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+match: string from: from to: to relax: relax                                                              
+    ^self match: string from: from to: string size case: true relax: relax
+
+    "Created: / 26-11-2014 / 07:01:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+match: string relax: relax
+    ^ self match: string case: true relax: relax
+
     "Created: / 09-08-2011 / 13:47:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 26-11-2014 / 07:03:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !StringPattern methodsFor:'printing & storing'!
@@ -187,40 +220,37 @@
 
 !StringPattern::Includes methodsFor:'matching'!
 
-match:string relax:relax
+match:string from: from to: to case: case relax:relax
 
     relax == 1 ifTrue:[
-        ^string includesString: data
-    ].
-    relax == 2 ifTrue:[
-        ^string matches: ('*', data, '*')
+        | i |
+        i := string indexOfSubCollection:data startingAt:from ifAbsent:0 caseSensitive:case.
+        ^ i between: from and: to - data size + 1 
     ].
     ^false.
 
-    "Modified: / 11-03-2013 / 14:11:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Created: / 26-11-2014 / 06:40:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !StringPattern::Matches methodsFor:'matching'!
 
-match:string relax:relax
+match:string from: from to: to case: case relax:relax
+    | pattern |
+
+    relax > 2 ifTrue:[ ^ false ].
 
-    relax == 1 ifTrue:[
-        ^string matches: data
-    ].
+    pattern := data.
     relax == 2 ifTrue:[
-        | relaxed |
-        relaxed := data.
         data first ~~  $* ifTrue:[
-            relaxed := '*' , relaxed.
+            pattern := '*' , pattern.
         ].
         data last ~~  $* ifTrue:[
-            relaxed := relaxed , '*'.
+            pattern := pattern , '*'.
         ].
-        ^string matches: relaxed
     ].
-    ^false.
+    ^ pattern match: string from: from to: to caseSensitive: case.
 
-    "Modified: / 18-10-2011 / 21:35:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Created: / 26-11-2014 / 06:43:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !StringPattern::Parser class methodsFor:'parsing'!
@@ -285,34 +315,66 @@
 
 !StringPattern::StartsWith methodsFor:'matching'!
 
-match: string relax: relax
+match: string from: from to: to case: case relax: relax
 
     relax == 1 ifTrue:[
-        ^string startsWith: data.
+        (case and:[from == 1]) ifTrue:[
+            ^ string startsWith: data.
+        ] ifFalse:[ 
+            | stringSz dataSz |
+
+            stringSz := string size.
+            dataSz := data size.
+            stringSz >= (from + dataSz - 1) ifTrue:[ 
+                | i |
+
+                i := dataSz.
+                case ifTrue:[
+                    [ i > 0 ] whileTrue:[ 
+                        (string at: from + i - 1) ~~ (data at: i) ifTrue:[  ^ false ].
+                        i := i - 1.
+                    ].
+                ] ifFalse:[
+                    [ i > 0 ] whileTrue:[ 
+                        (string at: from + i - 1) asLowercase ~~ (data at: i) asLowercase ifTrue:[  ^ false ].
+                        i := i - 1.
+                    ].
+                ].
+                ^ true
+            ].
+            ^ false
+        ].
     ].
     relax == 2 ifTrue:[
-        ^string includes: data
+        | i |
+        i := string indexOfSubCollection:data startingAt:from ifAbsent:0 caseSensitive:case.
+        ^ i between: from and: to - data size + 1 
     ].
     relax == 3 ifTrue:[
-        ^string matches: ('*', data, '*')
+        ^ ('*', data, '*') match: string from: from to: to caseSensitive: case.
     ].
     ^false.
 
     "
         (StringPattern startsWith: 'String') match: 'StringPattern'
         (StringPattern startsWith: 'STring') match: 'StringPattern' relax: 2
+        (StringPattern startsWith: 'Pattern') match: 'StringPattern' from: 7 to: 13 relax: 1   
+        (StringPattern startsWith: 'String') match: 'StringPattern' from: 7 to: 13 relax: 1   
+        (StringPattern startsWith: 'Pattern') match: 'StringPattern' relax: 2   
+
     "
 
-    "Created: / 09-08-2011 / 13:50:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Created: / 26-11-2014 / 06:45:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 26-11-2014 / 09:58:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !StringPattern class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/StringPattern.st,v 1.8 2014-04-22 09:29:04 vrany Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/StringPattern.st,v 1.9 2014-11-26 10:45:23 vrany Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic2/StringPattern.st,v 1.8 2014-04-22 09:29:04 vrany Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/StringPattern.st,v 1.9 2014-11-26 10:45:23 vrany Exp $'
 ! !