CharacterArray.st
changeset 68 59faa75185ba
parent 64 af7aeb79b25e
child 77 6c38ca59927f
--- a/CharacterArray.st	Wed Mar 16 10:40:51 1994 +0100
+++ b/CharacterArray.st	Wed Mar 30 11:38:21 1994 +0200
@@ -22,15 +22,15 @@
 COPYRIGHT (c) 1994 by Claus Gittinger
              All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.1 1994-02-25 13:07:32 claus Exp $
+$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.2 1994-03-30 09:28:09 claus Exp $
 '!
 
 !AbstractString class methodsFor:'documentation'!
 
 documentation
 "
-AbstractString is a superclass for all kinds of Strings (i.e.
-singleByte Strings, TwoByteStrings and whatever comes in the future.
+    AbstractString is a superclass for all kinds of Strings (i.e.
+    (singleByte-)Strings, TwoByteStrings and whatever comes in the future.
 "
 ! !
 
@@ -51,7 +51,9 @@
 fromString:aString
     "return a copy of the argument, aString"
 
-    ^ aString copyFrom:1 to:(aString size)
+    ^ (self basicNew:(aString size)) replaceFrom:1 with:aString
+
+    "TwoByteString fromString:'hello'"
 ! !
 
 !AbstractString methodsFor:'converting'!
@@ -90,8 +92,23 @@
     ^ self
 !
 
+asTwoByteString
+    "return the receiver converted to a two-byte string"
+
+    ^ TwoByteString fromString:self
+!
+
+asSingleByteString
+    "return the receiver converted to a 'normal' string"
+
+    ^ String fromString:self
+!
+
 asText
-    "return a Text-object (collection of lines) from myself"
+    "return a Text-object (collection of lines) from myself.
+     BIG warning: Text is totally misnamed here 
+         - ST/X's Text has nothing to do with PP's Text.
+         Therefore it will ne removed/renamed soon."
 
     ^ Text from:self
 !
@@ -116,7 +133,10 @@
     "return a collection containing the words (separated by whitespace) 
      of the receiver"
 
-    |words start stop mySize ch|
+    |words ch
+     start  "{ Class:SmallInteger }" 
+     stop   "{ Class:SmallInteger }" 
+     mySize "{ Class:SmallInteger }"|
 
     words := OrderedCollection new.
     start := 1.
@@ -138,6 +158,45 @@
     ^ words
 
     "'hello world isnt this nice' asCollectionOfWords'"
+!
+
+asCollectionOfLines
+    "return a collection containing the lines (separated by cr) 
+     of the receiver."
+
+    |lines myClass
+     numberOfLines "{ Class:SmallInteger }"
+     startIndex    "{ Class:SmallInteger }"
+     stopIndex     "{ Class:SmallInteger }" |
+
+    "count first, to avoid regrowing"
+
+    numberOfLines := (self occurrencesOf:Character cr) + 1.
+    lines := OrderedCollection new:numberOfLines.
+    myClass := self species.
+
+    startIndex := 1.
+    1 to:numberOfLines do:[:lineNr |
+        stopIndex := self indexOf:(Character cr) startingAt:startIndex.
+        stopIndex == 0 ifTrue:[
+            stopIndex := self size
+        ] ifFalse: [
+            stopIndex := stopIndex - 1.
+        ].
+
+        (stopIndex < startIndex) ifTrue: [
+            lines add:(myClass new:0)
+        ] ifFalse: [
+            lines add:(self copyFrom:startIndex to:stopIndex)
+        ].
+        startIndex := stopIndex + 2
+    ].
+    ^ lines
+
+    "
+     '1 one\2 two\3 three\4 four\5 five' withCRs asCollectionOfLines
+     '1 one\2 two\3 three\4 four\5 five' withCRs asCollectionOfWords
+    "
 ! !
 
 !AbstractString methodsFor:'printing & storing'!
@@ -286,8 +345,16 @@
     ^ self includesAny:'*#['
 !
 
+indexOfSeparator
+    "return the index of the first whitespace character"
+
+    ^ self indexOfSeparatorStartingAt:1
+
+    "'hello world' indexOfSeparator"
+!
+
 indexOfSeparatorStartingAt:start
-    "return the index of the next separator character"
+    "return the index of the next whitespace character"
 
     start to:self size do:[:index |
         (self at:index) isSeparator ifTrue:[^ index]
@@ -673,6 +740,47 @@
     " 'Smalltalk' spellAgainst: 'Smalltolk' "
 ! !
 
+!AbstractString methodsFor:'padded copying'!
+
+paddedTo:newSize with:padCharacter
+    "return a new string consisting of the receivers characters,
+     plus pad characters up to length"
+
+    |s len|
+
+    len := self size.
+    len < newSize ifTrue:[
+        s := self species new:newSize withAll:padCharacter.
+        s replaceFrom:1 to:len with:self.
+        ^ s
+    ]
+
+    "'123' printStringPaddedTo:10 with:$."
+    "'123' printStringPaddedTo:10 with:$*"
+    "(Float pi class name , ' ') printStringPaddedTo:15 with:$."
+!
+
+leftPaddedTo:size with:padCharacter
+    "return a new string of length size, which contains the receiver
+     right-adjusted (i.e. padded on the left).
+     characters on the left are filled with padCharacter.
+     If the receivers size is larger than the legth argument, it
+     is returned unchanged."
+
+    |len s|
+
+    len := self size.
+    (len < size) ifTrue:[
+        s := self species new:size withAll:padCharacter.
+        s replaceFrom:(size - len + 1) with:self.
+        ^ s
+    ]
+
+    "'123' leftPaddedTo:10 with:$."
+    "'1' leftPaddedTo:10 with:$."
+    "'hello' leftPaddedTo:10 with:$."
+! !
+
 !AbstractString methodsFor:'copying'!
 
 concatenate:string1 and:string2