CharacterArray.st
changeset 15523 4323024f2f04
parent 15433 426f079817b4
child 15540 23f428755717
child 18075 bd252c0beac9
equal deleted inserted replaced
15522:04d0ab821dc8 15523:4323024f2f04
   815      by newString (i.e. slice in the newString in place of the oldChar).
   815      by newString (i.e. slice in the newString in place of the oldChar).
   816      This is an ST/V compatibility method."
   816      This is an ST/V compatibility method."
   817 
   817 
   818     |tmpStream|
   818     |tmpStream|
   819 
   819 
   820     tmpStream := WriteStream on:(self class new).
   820     tmpStream := self species writeStream.
   821     self do:[:element |
   821     self do:[:element |
   822 	element = oldChar ifTrue:[
   822         element = oldChar ifTrue:[
   823 	    tmpStream nextPutAll:newString
   823             tmpStream nextPutAll:newString
   824 	] ifFalse:[
   824         ] ifFalse:[
   825 	    tmpStream nextPut:element
   825             tmpStream nextPut:element
   826 	].
   826         ].
   827     ].
   827     ].
   828     ^ tmpStream contents
   828     ^ tmpStream contents
   829 
   829 
   830    "
   830    "
   831      '12345678901234567890' replChar:$0 withString:'foo'
   831      '12345678901234567890' replChar:$0 withString:'foo'
  3478     "return a copy of the receiver, with all sequences of subString replaced
  3478     "return a copy of the receiver, with all sequences of subString replaced
  3479      by newString (i.e. slice in the newString in place of the oldString)."
  3479      by newString (i.e. slice in the newString in place of the oldString)."
  3480 
  3480 
  3481     |tmpStream idx idx1|
  3481     |tmpStream idx idx1|
  3482 
  3482 
  3483     tmpStream := CharacterWriteStream on:(self class new).
  3483     tmpStream := self species writeStream.
  3484     idx := 1.
  3484     idx := 1.
  3485     [idx ~~ 0] whileTrue:[
  3485     [idx ~~ 0] whileTrue:[
  3486 	idx1 := idx.
  3486         idx1 := idx.
  3487 	idx := self indexOfSubCollection:subString startingAt:idx.
  3487         idx := self indexOfSubCollection:subString startingAt:idx.
  3488 	idx ~~ 0 ifTrue:[
  3488         idx ~~ 0 ifTrue:[
  3489 	    tmpStream nextPutAll:(self copyFrom:idx1 to:idx-1).
  3489             tmpStream nextPutAll:(self copyFrom:idx1 to:idx-1).
  3490 	    tmpStream nextPutAll:newString.
  3490             tmpStream nextPutAll:newString.
  3491 	    idx := idx + subString size
  3491             idx := idx + subString size
  3492 	]
  3492         ]
  3493     ].
  3493     ].
  3494     tmpStream nextPutAll:(self copyFrom:idx1).
  3494     tmpStream nextPutAll:(self copyFrom:idx1).
  3495     ^ tmpStream contents
  3495     ^ tmpStream contents
  3496 
  3496 
  3497    "
  3497    "
  3498      '12345678901234567890' copyReplaceString:'123' withString:'OneTwoThree'
  3498      '12345678901234567890' copyReplaceString:'123' withString:'OneTwoThree'
  3499      '12345678901234567890' copyReplaceString:'123' withString:'*'
  3499      '12345678901234567890' copyReplaceString:'123' withString:'*'
  3500      '12345678901234567890' copyReplaceString:'234' withString:'foo'
  3500      '12345678901234567890' copyReplaceString:'234' withString:'foo'
  3501 
  3501 
  3502      ('a string with spaces' copyReplaceAll:$  withAll:' foo ')
  3502      ('a string with spaces' copyReplaceAll:$  withAll:' foo ')
  3503 	copyReplaceString:'foo' withString:'bar'
  3503         copyReplaceString:'foo' withString:'bar'
  3504     "
  3504     "
  3505 
  3505 
  3506     "Modified: / 31-05-1999 / 12:33:59 / cg"
  3506     "Modified: / 31-05-1999 / 12:33:59 / cg"
  3507     "Created: / 12-05-2004 / 12:00:00 / cg"
  3507     "Created: / 12-05-2004 / 12:00:00 / cg"
  3508 !
  3508 !
  4577 
  4577 
  4578     |s n index|
  4578     |s n index|
  4579 
  4579 
  4580     n := self occurrencesOf:$'.
  4580     n := self occurrencesOf:$'.
  4581     n ~~ 0 ifTrue:[
  4581     n ~~ 0 ifTrue:[
  4582 	s := self class new:(n + 2 + self size).
  4582         s := self species new:(n + 2 + self size).
  4583 	s at:1 put:$'.
  4583         s at:1 put:$'.
  4584 	index := 2.
  4584         index := 2.
  4585 	self do:[:thisChar |
  4585         self do:[:thisChar |
  4586 	    (thisChar == $') ifTrue:[
  4586             (thisChar == $') ifTrue:[
  4587 		s at:index put:thisChar.
  4587                 s at:index put:thisChar.
  4588 		index := index + 1.
  4588                 index := index + 1.
  4589 	    ].
  4589             ].
  4590 	    s at:index put:thisChar.
  4590             s at:index put:thisChar.
  4591 	    index := index + 1.
  4591             index := index + 1.
  4592 	].
  4592         ].
  4593 	s at:index put:$'.
  4593         s at:index put:$'.
  4594 	^ s
  4594         ^ s
  4595     ].
  4595     ].
  4596 
  4596 
  4597     ^ '''' , self , ''''
  4597     ^ '''' , self , ''''
       
  4598 
       
  4599     "
       
  4600      '''immutable'' string' asImmutableString basicStoreString
       
  4601     "
       
  4602 
       
  4603     "Modified: / 14-07-2013 / 19:20:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
  4598 !
  4604 !
  4599 
  4605 
  4600 displayOn:aGCOrStream
  4606 displayOn:aGCOrStream
  4601     "Compatibility
  4607     "Compatibility
  4602      append a printed desription on some stream (Dolphin,  Squeak)
  4608      append a printed desription on some stream (Dolphin,  Squeak)
  4886     "return the underlying strings bitsPerCharacter
  4892     "return the underlying strings bitsPerCharacter
  4887      (i.e. is it a regular String or a TwoByteString)"
  4893      (i.e. is it a regular String or a TwoByteString)"
  4888 
  4894 
  4889     |string|
  4895     |string|
  4890 
  4896 
  4891     (string := self string) == self ifTrue:[^ self class].
  4897     string := self string.
       
  4898     string == self ifTrue:[^ self species].
  4892     ^ string stringSpecies
  4899     ^ string stringSpecies
  4893 
  4900 
  4894     "
  4901     "
  4895      'hello' stringSpecies
  4902      'hello' stringSpecies
  4896      'hello' asText allBold stringSpecies
  4903      'hello' asText allBold stringSpecies
  5394     "return a string consisting of the receivers characters
  5401     "return a string consisting of the receivers characters
  5395      where leading spaces are replaced by tabulator characters (assuming 8-col tabs).
  5402      where leading spaces are replaced by tabulator characters (assuming 8-col tabs).
  5396      Notice: if the receiver does not contain any tabs, it is returned unchanged;
  5403      Notice: if the receiver does not contain any tabs, it is returned unchanged;
  5397      otherwise a new string is returned.
  5404      otherwise a new string is returned.
  5398      Limitation: only the very first spaces are replaced
  5405      Limitation: only the very first spaces are replaced
  5399 		 (i.e. if the receiver contains newLine characters,
  5406                  (i.e. if the receiver contains newLine characters,
  5400 		  no tabs are inserted after those lineBreaks)"
  5407                   no tabs are inserted after those lineBreaks)"
  5401 
  5408 
  5402     |idx   "{ SmallInteger }"
  5409     |idx   "{ SmallInteger }"
  5403      nTabs "{ SmallInteger }"
  5410      nTabs "{ SmallInteger }"
  5404      newString|
  5411      newString|
  5405 
  5412 
  5406     idx := self findFirst:[:c | (c ~~ Character space)].
  5413     idx := self findFirst:[:c | (c ~~ Character space)].
  5407     nTabs := (idx-1) // 8.
  5414     nTabs := (idx-1) // 8.
  5408     nTabs <= 0 ifTrue:[^ self].
  5415     nTabs <= 0 ifTrue:[^ self].
  5409 
  5416 
  5410     "any tabs"
  5417     "any tabs"
  5411     newString := self class new:(self size - (nTabs * 7)).
  5418     newString := self species new:(self size - (nTabs * 7)).
  5412     newString atAll:(1 to:nTabs) put:(Character tab).
  5419     newString atAll:(1 to:nTabs) put:(Character tab).
  5413     newString replaceFrom:(nTabs + 1) with:self startingAt:(nTabs * 8 + 1).
  5420     newString replaceFrom:(nTabs + 1) with:self startingAt:(nTabs * 8 + 1).
  5414     ^ newString
  5421     ^ newString
  5415 
  5422 
  5416     "
  5423     "
  6415 ! !
  6422 ! !
  6416 
  6423 
  6417 !CharacterArray class methodsFor:'documentation'!
  6424 !CharacterArray class methodsFor:'documentation'!
  6418 
  6425 
  6419 version
  6426 version
  6420     ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.506 2013-06-26 07:59:54 stefan Exp $'
  6427     ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.507 2013-07-14 22:48:34 stefan Exp $'
  6421 !
  6428 !
  6422 
  6429 
  6423 version_CVS
  6430 version_CVS
  6424     ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.506 2013-06-26 07:59:54 stefan Exp $'
  6431     ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.507 2013-07-14 22:48:34 stefan Exp $'
  6425 ! !
  6432 ! !
  6426 
  6433 
  6427 
  6434 
  6428 CharacterArray initialize!
  6435 CharacterArray initialize!