added contract methods
authorclaus
Fri, 28 Oct 1994 02:15:39 +0100
changeset 163 9868f2750ba5
parent 162 79c831f473a9
child 164 9bad8fe706e9
added contract methods
CharArray.st
CharacterArray.st
--- a/CharArray.st	Sun Oct 23 17:12:24 1994 +0100
+++ b/CharArray.st	Fri Oct 28 02:15:39 1994 +0100
@@ -21,7 +21,7 @@
 COPYRIGHT (c) 1994 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic/Attic/CharArray.st,v 1.8 1994-10-10 00:19:39 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/CharArray.st,v 1.9 1994-10-28 01:15:39 claus Exp $
 '!
 
 !AbstractString class methodsFor:'documentation'!
@@ -42,7 +42,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libbasic/Attic/CharArray.st,v 1.8 1994-10-10 00:19:39 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/CharArray.st,v 1.9 1994-10-28 01:15:39 claus Exp $
 "
 !
 
@@ -329,6 +329,73 @@
      'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:(Array with:$: with:Character space) 
      'h1e2l3l4o' asCollectionOfSubstringsSeparatedByAny:($1 to: $9) 
     "
+!
+
+contractTo:maxLen
+    "if the receivers size is less or equal to maxLen, return it.
+     Otherwise, return a copy of the receiver, where some characters 
+     in the middle have been replaced by '...' for a total string length
+     of maxLen. Can be used to abbreviate long entries in tables."
+
+    |sz halfSize|
+
+    (sz := self size) > maxLen ifTrue:[
+	halfSize := maxLen // 2.
+	^ self copyReplaceFrom:halfSize - 1
+			    to:sz - maxLen + halfSize + 1
+			    with:'...'
+    ]
+
+    "
+     '12345678901234' contractTo:15          
+     '123456789012345' contractTo:15          
+     '1234567890123456' contractTo:15         
+     'aShortString' contractTo:15 
+     'aVeryLongNameForAStringThatShouldBeShortened' contractTo:15 
+    "
+!
+
+contractAtEndTo:maxLen
+    "if the receivers size is less or equal to maxLen, return it.
+     Otherwise, return a copy of the receiver, where some characters 
+     at the end have been replaced by '...' for a total string length
+     of maxLen. Can be used to abbreviate long entries in tables."
+
+    |sz|
+
+    (sz := self size) > maxLen ifTrue:[
+	^ self copyReplaceFrom:maxLen - 3
+			    with:'...'
+    ]
+
+    "
+     '12345678901234' contractAtEndTo:15          
+     '123456789012345' contractAtEndTo:15          
+     '1234567890123456' contractAtEndTo:15          
+     'aShortString' contractAtEndTo:15          
+     'aVeryLongNameForAStringThatShouldBeShortened' contractAtEndTo:15 
+    "
+!
+
+contractAtBeginningTo:maxLen
+    "if the receivers size is less or equal to maxLen, return it.
+     Otherwise, return a copy of the receiver, where some characters 
+     at the beginning have been replaced by '...' for a total string length
+     of maxLen. Can be used to abbreviate long entries in tables."
+
+    |sz|
+
+    (sz := self size) > maxLen ifTrue:[
+	^ '...' , (self copyFrom:(sz - (maxLen - 4))) 
+    ]
+
+    "
+     '12345678901234' contractAtBeginningTo:15          
+     '123456789012345' contractAtBeginningTo:15          
+     '1234567890123456' contractAtBeginningTo:15          
+     'aShortString' contractAtBeginningTo:15          
+     'aVeryLongNameForAStringThatShouldBeShortened' contractAtBeginningTo:15
+    "
 ! !
 
 !AbstractString methodsFor:'ST/V compatibility'!
@@ -1155,6 +1222,20 @@
 
 !AbstractString methodsFor:'filling and replacing'!
 
+withCRs
+    "return a new string consisting of receivers characters
+     with all \-characters replaced by cr-characters."
+
+    ^ self copy replaceAll:$\ by:(Character cr)
+!
+
+withoutCRs
+    "return a new collection consisting of receivers elements
+     with all cr-characters replaced by \-characters."
+
+    ^ self copy replaceAll:(Character cr) by:$\
+!
+
 withoutSpaces
     "return a copy of myself without leading and trailing spaces.
      Notice: this does NOT remove tabs, newline or any other whitespace.
--- a/CharacterArray.st	Sun Oct 23 17:12:24 1994 +0100
+++ b/CharacterArray.st	Fri Oct 28 02:15:39 1994 +0100
@@ -21,7 +21,7 @@
 COPYRIGHT (c) 1994 by Claus Gittinger
 	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.8 1994-10-10 00:19:39 claus Exp $
+$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.9 1994-10-28 01:15:39 claus Exp $
 '!
 
 !AbstractString class methodsFor:'documentation'!
@@ -42,7 +42,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.8 1994-10-10 00:19:39 claus Exp $
+$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.9 1994-10-28 01:15:39 claus Exp $
 "
 !
 
@@ -329,6 +329,73 @@
      'hello:world:isnt:this nice' asCollectionOfSubstringsSeparatedByAny:(Array with:$: with:Character space) 
      'h1e2l3l4o' asCollectionOfSubstringsSeparatedByAny:($1 to: $9) 
     "
+!
+
+contractTo:maxLen
+    "if the receivers size is less or equal to maxLen, return it.
+     Otherwise, return a copy of the receiver, where some characters 
+     in the middle have been replaced by '...' for a total string length
+     of maxLen. Can be used to abbreviate long entries in tables."
+
+    |sz halfSize|
+
+    (sz := self size) > maxLen ifTrue:[
+	halfSize := maxLen // 2.
+	^ self copyReplaceFrom:halfSize - 1
+			    to:sz - maxLen + halfSize + 1
+			    with:'...'
+    ]
+
+    "
+     '12345678901234' contractTo:15          
+     '123456789012345' contractTo:15          
+     '1234567890123456' contractTo:15         
+     'aShortString' contractTo:15 
+     'aVeryLongNameForAStringThatShouldBeShortened' contractTo:15 
+    "
+!
+
+contractAtEndTo:maxLen
+    "if the receivers size is less or equal to maxLen, return it.
+     Otherwise, return a copy of the receiver, where some characters 
+     at the end have been replaced by '...' for a total string length
+     of maxLen. Can be used to abbreviate long entries in tables."
+
+    |sz|
+
+    (sz := self size) > maxLen ifTrue:[
+	^ self copyReplaceFrom:maxLen - 3
+			    with:'...'
+    ]
+
+    "
+     '12345678901234' contractAtEndTo:15          
+     '123456789012345' contractAtEndTo:15          
+     '1234567890123456' contractAtEndTo:15          
+     'aShortString' contractAtEndTo:15          
+     'aVeryLongNameForAStringThatShouldBeShortened' contractAtEndTo:15 
+    "
+!
+
+contractAtBeginningTo:maxLen
+    "if the receivers size is less or equal to maxLen, return it.
+     Otherwise, return a copy of the receiver, where some characters 
+     at the beginning have been replaced by '...' for a total string length
+     of maxLen. Can be used to abbreviate long entries in tables."
+
+    |sz|
+
+    (sz := self size) > maxLen ifTrue:[
+	^ '...' , (self copyFrom:(sz - (maxLen - 4))) 
+    ]
+
+    "
+     '12345678901234' contractAtBeginningTo:15          
+     '123456789012345' contractAtBeginningTo:15          
+     '1234567890123456' contractAtBeginningTo:15          
+     'aShortString' contractAtBeginningTo:15          
+     'aVeryLongNameForAStringThatShouldBeShortened' contractAtBeginningTo:15
+    "
 ! !
 
 !AbstractString methodsFor:'ST/V compatibility'!
@@ -1155,6 +1222,20 @@
 
 !AbstractString methodsFor:'filling and replacing'!
 
+withCRs
+    "return a new string consisting of receivers characters
+     with all \-characters replaced by cr-characters."
+
+    ^ self copy replaceAll:$\ by:(Character cr)
+!
+
+withoutCRs
+    "return a new collection consisting of receivers elements
+     with all cr-characters replaced by \-characters."
+
+    ^ self copy replaceAll:(Character cr) by:$\
+!
+
 withoutSpaces
     "return a copy of myself without leading and trailing spaces.
      Notice: this does NOT remove tabs, newline or any other whitespace.