diff -r 9e36e4384637 -r 8f12aadd28bb CharacterArray.st --- a/CharacterArray.st Sun Dec 21 23:41:28 2014 +0100 +++ b/CharacterArray.st Mon Dec 22 01:19:04 2014 +0100 @@ -3820,6 +3820,96 @@ ^ newString. ]. ^ super copyWith:aCharacter +! + +restAfter:keyword withoutSeparators:strip + "compare the left of the receiver with keyword, + if it matches return the right. + Finally, if strip is true, remove whiteSpace. + This method is used to match and extract lines of the form: + something: rest + where we are interested in rest, but only if the receiver string + begins with something. + + You may wonder why such a specialized method exists here + - this is so common when processing mailboxes, + rcs files, nntp/pop3 responses, that is was considered worth + a special method here to avoid having the code below a hundred + times in variuos places." + + |rest| + + (self startsWith:keyword) ifTrue:[ + rest := self copyFrom:(keyword size + 1). + strip ifTrue:[ + rest := rest withoutSeparators + ]. + ^ rest + ]. + ^ nil + + " + 'foo: hello world' restAfter:'foo:' withoutSeparators:true + 'funny: something' restAfter:'foo:' withoutSeparators:true + + 'foo: hello world ' restAfter:'foo:' withoutSeparators:true + 'foo: hello world ' restAfter:'foo:' withoutSeparators:false + " + + "Created: 25.11.1995 / 11:04:18 / cg" +! + +split:separator + "splits into an array of substrings. + Separator may be a single character or a separating string." + + separator isCharacter ifTrue:[ + ^ self asCollectionOfSubstringsSeparatedBy:separator + ]. + (separator isString and:[separator size == 1]) ifTrue:[ + ^ self asCollectionOfSubstringsSeparatedBy:(separator first) + ]. + ^ self asCollectionOfSubstringsSeparatedByAll:separator + + " + JavaScriptParser + evaluate:'''bla-fasel-suelz''.split(''-'')' + + JavaScriptParser + evaluate:'''bla - fasel - suelz''.split('' - '')' + + 'bla - fasel - suelz' split:' - ' + " +! + +splitAtString:subString withoutSeparators:strip + "If the receiver is of the form: + + return a collection containing left and right only. + If strip is true, remove whiteSpace in the returned substrings." + + |idx left right| + + (idx := self indexOfSubCollection:subString) ~~ 0 ifTrue:[ + left := self copyTo:(idx - 1). + right := self copyFrom:(idx + subString size). + strip ifTrue:[ + left := left withoutSeparators. + right := right withoutSeparators. + ]. + ^ StringCollection with:left with:right + ]. + self error:'substring not present in receiver' mayProceed:true. + ^ self + + " + 'hello -> world' splitAtString:'->' withoutSeparators:false + 'hello -> world' splitAtString:'->' withoutSeparators:true + 'hello -> ' splitAtString:'->' withoutSeparators:true + 'hello > error' splitAtString:'->' withoutSeparators:true + " + + "Created: 25.11.1995 / 11:04:18 / cg" ! ! !CharacterArray methodsFor:'displaying'! @@ -5121,7 +5211,7 @@ The original code only looked at the first character being a vowel; this has been enhanced by some heuristics - not perfect, still." - |firstChar secondChar| + |firstChar secondChar thirdChar| firstChar := (self at:1) asLowercase. ((firstChar isVowel and:[firstChar ~~ $u]) or:[firstChar == $x]) ifTrue:[ @@ -5130,13 +5220,19 @@ (self size >= 3) ifTrue:[ secondChar := (self at:2) asLowercase. + thirdChar := (self at:3) asLowercase. (firstChar isVowel not and:[(secondChar isVowel or:[secondChar == $y]) not - and:[(self at:3) isVowel not ]]) ifTrue:[ + and:[thirdChar isVowel not ]]) ifTrue:[ "/ exceptions: 3 non-vowels in a row: looks like an abbreviation (self size > 4) ifTrue:[ - ((self asLowercase startsWith:'scr') and:[(self at:4) isVowel]) ifTrue:[ - ^ 'a' + (firstChar = $s) ifTrue:[ + ((secondChar = $c and:[thirdChar = $r]) + or:[ (secondChar = $t and:[thirdChar = $r]) ]) ifTrue:[ + (self at:4) isVowel ifTrue:[ + ^ 'a' + ] + ] ]. ]. "/ an abbreviation; treat x, s as vowels @@ -5158,6 +5254,7 @@ 'cvs' article. 'cvssource' article. 'symbol' article. + 'string' article. " ! @@ -6877,73 +6974,6 @@ 'hello world' findRangeOfString:'llo' 'hello world' findRangeOfString:'ole' " -! - -restAfter:keyword withoutSeparators:strip - "compare the left of the receiver with keyword, - if it matches return the right. - Finally, if strip is true, remove whiteSpace. - This method is used to match and extract lines of the form: - something: rest - where we are interested in rest, but only if the receiver string - begins with something. - - You may wonder why such a specialized method exists here - - this is so common when processing mailboxes, - rcs files, nntp/pop3 responses, that is was considered worth - a special method here to avoid having the code below a hundred - times in variuos places." - - |rest| - - (self startsWith:keyword) ifTrue:[ - rest := self copyFrom:(keyword size + 1). - strip ifTrue:[ - rest := rest withoutSeparators - ]. - ^ rest - ]. - ^ nil - - " - 'foo: hello world' restAfter:'foo:' withoutSeparators:true - 'funny: something' restAfter:'foo:' withoutSeparators:true - - 'foo: hello world ' restAfter:'foo:' withoutSeparators:true - 'foo: hello world ' restAfter:'foo:' withoutSeparators:false - " - - "Created: 25.11.1995 / 11:04:18 / cg" -! - -splitAtString:subString withoutSeparators:strip - "If the receiver is of the form: - - return a collection containing left and right only. - If strip is true, remove whiteSpace in the returned substrings." - - |idx left right| - - (idx := self indexOfSubCollection:subString) ~~ 0 ifTrue:[ - left := self copyTo:(idx - 1). - right := self copyFrom:(idx + subString size). - strip ifTrue:[ - left := left withoutSeparators. - right := right withoutSeparators. - ]. - ^ StringCollection with:left with:right - ]. - self error:'substring not present in receiver' mayProceed:true. - ^ self - - " - 'hello -> world' splitAtString:'->' withoutSeparators:false - 'hello -> world' splitAtString:'->' withoutSeparators:true - 'hello -> ' splitAtString:'->' withoutSeparators:true - 'hello > error' splitAtString:'->' withoutSeparators:true - " - - "Created: 25.11.1995 / 11:04:18 / cg" ! ! !CharacterArray methodsFor:'testing'! @@ -7266,11 +7296,11 @@ !CharacterArray class methodsFor:'documentation'! version - ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.562 2014-12-19 17:19:40 cg Exp $' + ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.563 2014-12-22 00:19:04 cg Exp $' ! version_CVS - ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.562 2014-12-19 17:19:40 cg Exp $' + ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.563 2014-12-22 00:19:04 cg Exp $' ! !