SequenceableCollection.st
changeset 4677 90a037f88b0e
parent 4459 a0466230c74b
child 4678 f2265a5b06c1
equal deleted inserted replaced
4676:5d469e2da40a 4677:90a037f88b0e
   987 
   987 
   988     "Modified: / 17.6.1998 / 12:31:19 / cg"
   988     "Modified: / 17.6.1998 / 12:31:19 / cg"
   989 !
   989 !
   990 
   990 
   991 asStringWith:sepCharacter from:firstLine to:lastLine compressTabs:compressTabs final:endCharacter withEmphasis:withEmphasis
   991 asStringWith:sepCharacter from:firstLine to:lastLine compressTabs:compressTabs final:endCharacter withEmphasis:withEmphasis
   992     "return part of myself as a string or text with embedded sepCharacters.
   992     "return part of myself as a string or text with embedded sepCharacters
       
   993      and followup endCharacter.
   993      My elements must be strings or nil; nil entries and empty strings are
   994      My elements must be strings or nil; nil entries and empty strings are
   994      taken as empty lines.
   995      taken as empty lines.
       
   996      sepCharacter and endCharacter may be nil, a character or a string.
   995      If the argument compressTabs is true, leading spaces are converted
   997      If the argument compressTabs is true, leading spaces are converted
   996      to tab-characters (8col tabs). The last line is followed by a final
   998      to tab-characters (8col tabs). The last line is followed by a final
   997      character (if non-nil).
   999      character (if non-nil).
   998      The withEmphais argument controls if the returned string should preserve
  1000      The withEmphais argument controls if the returned string should preserve
   999      any emphasis. If false, a plain string is returned.
  1001      any emphasis. If false, a plain string is returned.
  1003 
  1005 
  1004     |idx1        "{ Class:SmallInteger }"
  1006     |idx1        "{ Class:SmallInteger }"
  1005      idx2        "{ Class:SmallInteger }"
  1007      idx2        "{ Class:SmallInteger }"
  1006      totalLength "{ Class:SmallInteger }"
  1008      totalLength "{ Class:SmallInteger }"
  1007      pos         "{ Class:SmallInteger }"
  1009      pos         "{ Class:SmallInteger }"
  1008      newString lineString spaces idx nTabs sepCnt
  1010      sepCnt      "{ Class:SmallInteger }"
       
  1011      newString lineString spaces idx nTabs 
  1009      any16Bit stringClass needEmphasis newRuns c
  1012      any16Bit stringClass needEmphasis newRuns c
  1010      thisLen anyTab|
  1013      thisLen anyTab|
  1011 
  1014 
  1012     "
  1015     "
  1013      first accumulate the size of the string, to avoid
  1016      first accumulate the size of the string, to avoid
  1017     "
  1020     "
  1018     any16Bit := needEmphasis := false.
  1021     any16Bit := needEmphasis := false.
  1019     stringClass := String.
  1022     stringClass := String.
  1020 
  1023 
  1021     totalLength := 0.
  1024     totalLength := 0.
  1022     sepCnt := sepCharacter notNil ifTrue:[1] ifFalse:[0].
  1025     sepCharacter isNil ifTrue:[
       
  1026         sepCnt := 0
       
  1027     ] ifFalse:[
       
  1028         sepCharacter isCharacter ifTrue:[
       
  1029             sepCnt := 1
       
  1030         ] ifFalse:[
       
  1031             sepCnt := sepCharacter size
       
  1032         ]
       
  1033     ].
  1023 
  1034 
  1024     idx1 := firstLine.
  1035     idx1 := firstLine.
  1025     idx2 := lastLine.
  1036     idx2 := lastLine.
  1026     idx1 to:idx2 do:[:lineIndex |
  1037     idx1 to:idx2 do:[:lineIndex |
  1027         lineString := self at:lineIndex.
  1038         lineString := self at:lineIndex.
  1028 
  1039 
  1029         lineString isNil ifTrue:[
  1040         lineString notNil ifTrue:[
  1030             totalLength := totalLength + sepCnt
       
  1031         ] ifFalse: [
       
  1032             withEmphasis ifTrue:[
  1041             withEmphasis ifTrue:[
  1033                 lineString hasChangeOfEmphasis ifTrue:[
  1042                 lineString hasChangeOfEmphasis ifTrue:[
  1034                     needEmphasis := true
  1043                     needEmphasis := true
  1035                 ].
  1044                 ].
  1036                 (lineString bitsPerCharacter == 16) ifTrue:[
  1045                 (lineString bitsPerCharacter == 16) ifTrue:[
  1037                     any16Bit := true.
  1046                     any16Bit := true.
  1038                     stringClass := lineString class
  1047                     stringClass := lineString class
  1039                 ].
  1048                 ].
  1040             ].
  1049             ].
  1041             totalLength := totalLength + lineString size + sepCnt
  1050             totalLength := totalLength + lineString size
  1042         ].
  1051         ].
  1043     ].
  1052         totalLength := totalLength + sepCnt
  1044     endCharacter isNil ifTrue:[
  1053     ].
  1045         totalLength := totalLength - 1.
  1054     totalLength := totalLength - sepCnt.
  1046         totalLength < 0 ifTrue:[^ ''].
  1055 
  1047     ].
  1056     endCharacter notNil ifTrue:[
       
  1057         endCharacter isCharacter ifTrue:[
       
  1058             totalLength := totalLength + 1
       
  1059         ] ifFalse:[
       
  1060             totalLength := totalLength + endCharacter size
       
  1061         ].
       
  1062     ].
       
  1063     totalLength <= 0 ifTrue:[^ ''].
       
  1064 
  1048     spaces := '        '.
  1065     spaces := '        '.
  1049     newString := stringClass new:totalLength.
  1066     newString := stringClass new:totalLength.
  1050 
  1067 
  1051     needEmphasis ifTrue:[
  1068     needEmphasis ifTrue:[
  1052         newRuns := RunArray new.
  1069         newRuns := RunArray new.
  1123         ] ifFalse:[
  1140         ] ifFalse:[
  1124             c := endCharacter
  1141             c := endCharacter
  1125         ].
  1142         ].
  1126 
  1143 
  1127         c notNil ifTrue:[
  1144         c notNil ifTrue:[
  1128             newString at:pos put:c.
  1145             c isCharacter ifTrue:[
  1129             newRuns notNil ifTrue:[
  1146                 newString at:pos put:c.
  1130                 newRuns add:nil.
  1147                 newRuns notNil ifTrue:[
  1131             ].
  1148                     newRuns add:nil.
  1132             pos := pos + 1
  1149                 ].
       
  1150                 pos := pos + 1
       
  1151             ] ifFalse:[
       
  1152                 newString replaceFrom:pos with:c.
       
  1153                 newRuns notNil ifTrue:[
       
  1154                     newRuns add:nil withOccurrences:c size
       
  1155                 ].
       
  1156                 pos := pos + c size
       
  1157             ]
  1133         ].
  1158         ].
  1134     ].
  1159     ].
  1135 
  1160 
  1136     "
  1161     "
  1137      in case of tab compression, the result has to be
  1162      in case of tab compression, the result has to be
  1162           asStringWith:$: 
  1187           asStringWith:$: 
  1163           from:1 to:4 
  1188           from:1 to:4 
  1164           compressTabs:false 
  1189           compressTabs:false 
  1165           final:$: 
  1190           final:$: 
  1166 
  1191 
  1167      concatenating all elements:
  1192      concatenating elements (nil sepChars and nil endChars):
  1168 
  1193 
  1169        #('foo' 'bar' 'baz') 
  1194        #('foo' 'bar' 'baz') 
  1170           asStringWith:nil 
  1195           asStringWith:nil 
  1171           from:1 to:3 
  1196           from:1 to:3 
  1172           compressTabs:false 
  1197           compressTabs:false 
  1173           final:nil 
  1198           final:nil  
  1174 
  1199 
  1175      creating a string from a collection of lines:
  1200      creating a string from a collection of lines:
  1176 
  1201 
  1177        #('foo' 'bar' 'baz') 
  1202        #('foo' 'bar' 'baz') 
  1178           asStringWith:(Character cr) 
  1203           asStringWith:(Character cr) 
  1190        ) 
  1215        ) 
  1191           asStringWith:(Character cr) 
  1216           asStringWith:(Character cr) 
  1192           from:1 to:4 
  1217           from:1 to:4 
  1193           compressTabs:false 
  1218           compressTabs:false 
  1194           final:(Character cr) 
  1219           final:(Character cr) 
       
  1220 
       
  1221      can also use strings as separating characters:
       
  1222 
       
  1223        #('foo' 'bar' 'baz') 
       
  1224           asStringWith:'__' 
       
  1225           from:1 to:3 
       
  1226           compressTabs:false 
       
  1227           final:nil       
       
  1228 
       
  1229      and as final characters:
       
  1230 
       
  1231        #('foo' 'bar' 'baz') 
       
  1232           asStringWith:'__' 
       
  1233           from:1 to:3 
       
  1234           compressTabs:false 
       
  1235           final:'***'       
       
  1236 
  1195     "
  1237     "
  1196 
  1238 
  1197     "Created: / 17.6.1998 / 12:30:32 / cg"
  1239     "Created: / 17.6.1998 / 12:30:32 / cg"
  1198     "Modified: / 17.6.1998 / 12:31:59 / cg"
  1240     "Modified: / 17.6.1998 / 12:31:59 / cg"
  1199 !
  1241 !
  4316 ! !
  4358 ! !
  4317 
  4359 
  4318 !SequenceableCollection class methodsFor:'documentation'!
  4360 !SequenceableCollection class methodsFor:'documentation'!
  4319 
  4361 
  4320 version
  4362 version
  4321     ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.114 1999-07-26 14:30:58 cg Exp $'
  4363     ^ '$Header: /cvs/stx/stx/libbasic/SequenceableCollection.st,v 1.115 1999-09-04 11:17:18 cg Exp $'
  4322 ! !
  4364 ! !
  4323 SequenceableCollection initialize!
  4365 SequenceableCollection initialize!