CharacterArray.st
changeset 308 f04744ef7b5d
parent 282 94f5c3a6230d
child 326 d2902942491d
--- a/CharacterArray.st	Thu Mar 09 11:52:57 1995 +0100
+++ b/CharacterArray.st	Sat Mar 18 06:06:59 1995 +0100
@@ -21,7 +21,7 @@
 COPYRIGHT (c) 1994 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.15 1995-02-24 16:35:55 claus Exp $
+$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.16 1995-03-18 05:03:15 claus Exp $
 '!
 
 !CharacterArray class methodsFor:'documentation'!
@@ -42,7 +42,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.15 1995-02-24 16:35:55 claus Exp $
+$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.16 1995-03-18 05:03:15 claus Exp $
 "
 !
 
@@ -805,12 +805,13 @@
 
 !CharacterArray methodsFor:'pattern matching'!
 
-from:matchStart to:matchStop match:aString from:start to:stop
+from:matchStart to:matchStop match:aString from:start to:stop ignoreCase:ignoreCase
     "helper for match; return true if the characters from start to stop in
      aString are matching the receivers characters from matchStart to matchStop.
      The receiver may contain meta-match characters $* (to match any string) 
      or $# (to match any character) or [...] (to match from a set of characters).
-     This algorithm is not very efficient; for heavy duty pattern matching,
+
+     This algorithm is not at all efficient; for heavy duty pattern matching,
      an interface (primitive) to the regex pattern matching package should be
      added."
 
@@ -822,7 +823,7 @@
      mSize  "{ Class: SmallInteger }"
      sSize  "{ Class: SmallInteger }"
      index cont matchLast
-     matchSet c1 c2|
+     matchSet c1 c2 checkChar included|
 
     mStart := matchStart.
     mStop := matchStop.
@@ -851,7 +852,8 @@
 		"testString empty -> no match"
 		(sSize == 0) ifTrue:[^ false].
 
-		matchSet := Set new.
+		included := false.
+		checkChar := aString at:sStart.
 		c1 := nil.
 		[matchChar == $]] whileFalse:[
 		    mStart := mStart + 1.
@@ -860,19 +862,53 @@
 		    ((matchChar == $-) and:[c1 notNil]) ifTrue:[
 			mStart := mStart + 1.
 			mSize := mSize - 1.
-			c2 := self at:mStart.
-			(c1 to:c2) do:[:c | matchSet add:c].
+			included ifFalse:[
+			    c2 := self at:mStart.
+			    ignoreCase ifTrue:[
+				included := checkChar asUppercase asciiValue
+						between:c1 asUppercase asciiValue
+						and:c2 asUppercase asciiValue.
+			    ] ifFalse:[
+				included := checkChar asciiValue
+						between:c1 asciiValue
+						and:c2 asciiValue.
+			    ]
+			].
 			c1 := nil
 		    ] ifFalse:[
 			(matchChar == $]) ifFalse:[
-			    matchSet add:matchChar.
-			    c1 := matchChar.
-			].
-		    ].
+			    included := (checkChar == matchChar).
+			    c1 := matchChar
+			]
+		    ]
 		].
 		mStart := mStart + 1.
 		mSize := mSize - 1.
-		(matchSet includes:(aString at:sStart)) ifFalse:[^ false].
+		included ifFalse:[^ false].
+
+"/                matchSet := Set new.
+"/                c1 := nil.
+"/                [matchChar == $]] whileFalse:[
+"/                    mStart := mStart + 1.
+"/                    mSize := mSize - 1.
+"/                    matchChar := self at:mStart.
+"/                    ((matchChar == $-) and:[c1 notNil]) ifTrue:[
+"/                        mStart := mStart + 1.
+"/                        mSize := mSize - 1.
+"/                        c2 := self at:mStart.
+"/                        (c1 to:c2) do:[:c | matchSet add:c].
+"/                        c1 := nil
+"/                    ] ifFalse:[
+"/                        (matchChar == $]) ifFalse:[
+"/                            matchSet add:matchChar.
+"/                            c1 := matchChar.
+"/                        ].
+"/                    ].
+"/                ].
+"/                mStart := mStart + 1.
+"/                mSize := mSize - 1.
+"/                (matchSet includes:(aString at:sStart)) ifFalse:[^ false].
+
 		((sSize == 1) and:[mSize == 0]) ifTrue:[^ true].
 		"cut off 1st char and continue"
 		sStart := sStart + 1
@@ -880,7 +916,7 @@
 		(matchChar == $*) ifTrue:[
 		    "* matches anything"
 		    (mSize == 1) ifTrue:[^ true].
-		    "testString empty -> matchString not we have no match"
+		    "testString empty & matchString not empty -> we have no match"
 		    (sSize == 0) ifTrue:[^ false].
 
 		    "try to avoid some of the recursion by checking last
@@ -917,7 +953,11 @@
 		    (sSize == 0) ifTrue:[^ false].
 
 		    "first characters equal ?"
-		    ((aString at:sStart) ~~ matchChar) ifTrue:[^ false].
+		    ignoreCase ifTrue:[
+			((aString at:sStart) asUppercase ~~ matchChar asUppercase) ifTrue:[^ false].
+		    ] ifFalse:[
+			((aString at:sStart) ~~ matchChar) ifTrue:[^ false].
+		    ].
 
 		    "avoid recursion if possible"
 		    ((sSize == mSize) and:[self = aString]) ifTrue:[^ true].
@@ -931,11 +971,31 @@
     ]
 !
 
+from:matchStart to:matchStop match:aString from:start to:stop
+    ^ self from:matchStart to:matchStop match:aString from:start to:stop ignoreCase:false
+!
+
+match:aString ignoreCase:ignoreCase
+    "return true if aString matches self, where self may contain meta-match 
+     characters $* (to match any string) or $# (to match any character)."
+
+    ^ self from:1 to:(self size) match:aString from:1 to:(aString size) ignoreCase:ignoreCase
+
+    "
+     '*ute*' match:'COMPUTER' ignoreCase:true
+     '*uter' match:'COMPUTER' ignoreCase:false 
+     '[abcd]*' match:'computer' 
+     '[a-k]*' match:'komputer' 
+     '*some*compl*ern*' match:'this is some more complicated pattern match' 
+     '*some*compl*ern*' match:'this is another complicated pattern match' 
+    "
+!
+
 match:aString
     "return true if aString matches self, where self may contain meta-match 
      characters $* (to match any string) or $# (to match any character)."
 
-    ^ self from:1 to:(self size) match:aString from:1 to:(aString size)
+    ^ self match:aString ignoreCase:false
 
     "
      '*ute*' match:'computer'