CharacterArray.st
changeset 19233 5ef1a009d69d
parent 19171 b6405bcb8e75
child 19236 e6403ba50de1
child 19420 e2578414e873
equal deleted inserted replaced
19232:47ab0b47e9c9 19233:5ef1a009d69d
   317     ^ EncodingFailedSignal
   317     ^ EncodingFailedSignal
   318 
   318 
   319     "Modified: 28.6.1997 / 20:09:35 / cg"
   319     "Modified: 28.6.1997 / 20:09:35 / cg"
   320     "Created: 3.8.1997 / 18:16:40 / cg"
   320     "Created: 3.8.1997 / 18:16:40 / cg"
   321 ! !
   321 ! !
       
   322 
   322 
   323 
   323 !CharacterArray class methodsFor:'cleanup'!
   324 !CharacterArray class methodsFor:'cleanup'!
   324 
   325 
   325 lowSpaceCleanup
   326 lowSpaceCleanup
   326     "cleanup in low-memory situations"
   327     "cleanup in low-memory situations"
  2759 asByteArray
  2760 asByteArray
  2760     "depending on the size of the characters in the receiver,
  2761     "depending on the size of the characters in the receiver,
  2761      return a byteArray containing single-, double- or even 4-bytewise values.
  2762      return a byteArray containing single-, double- or even 4-bytewise values.
  2762      The size of the returned byteArray will be the strings size multiplied by the
  2763      The size of the returned byteArray will be the strings size multiplied by the
  2763      size required for the largest character.
  2764      size required for the largest character.
       
  2765      Attention: The bytes are in native byte order.
  2764      Caveat: better use utf8Encoded, to get reproducable results"
  2766      Caveat: better use utf8Encoded, to get reproducable results"
  2765 
  2767 
  2766     |bytes sz bytesPerCharacter idx str|
  2768     |bytes sz bytesPerCharacter idx str|
  2767 
  2769 
  2768     str := self string.
  2770     str := self string.
  2769     str ~~ self ifTrue:[
  2771     str ~~ self ifTrue:[
  2770 	"/ for text and other wrappers
  2772         "/ for text and other wrappers
  2771 	^ str asByteArray
  2773         ^ str asByteArray
  2772     ].
  2774     ].
  2773 
  2775 
  2774     "/ for real strings, a fallback
  2776     "/ for real strings, a fallback
  2775     sz := self size.
  2777     sz := self size.
  2776     bytesPerCharacter := self bitsPerCharacter // 8.
  2778     bytesPerCharacter := self bitsPerCharacter // 8.
  2777     bytes := ByteArray new:(sz * bytesPerCharacter).
  2779     bytes := ByteArray new:(sz * bytesPerCharacter).
  2778     idx := 1.
  2780     idx := 1.
  2779     self do:[:char |
  2781     self do:[:char |
  2780 	|code|
  2782         |code|
  2781 
  2783 
  2782 	code := char codePoint.
  2784         code := char codePoint.
  2783 	bytesPerCharacter == 2 ifTrue:[
  2785         bytesPerCharacter == 2 ifTrue:[
  2784 	    bytes unsignedShortAt:idx put:code
  2786             bytes unsignedShortAt:idx put:code
  2785 	] ifFalse:[
  2787         ] ifFalse:[
  2786 	    bytesPerCharacter == 4 ifTrue:[
  2788             bytesPerCharacter == 4 ifTrue:[
  2787 		bytes unsignedLongAt:idx put:code
  2789                 bytes unsignedLongAt:idx put:code
  2788 	    ] ifFalse:[
  2790             ] ifFalse:[
  2789 		bytes at:idx put:code
  2791                 bytes at:idx put:code
  2790 	    ].
  2792             ].
  2791 	].
  2793         ].
  2792 	idx := idx + bytesPerCharacter.
  2794         idx := idx + bytesPerCharacter.
  2793     ].
  2795     ].
  2794     ^ bytes
  2796     ^ bytes
  2795 
  2797 
  2796     "Created: / 27-07-2011 / 00:56:17 / cg"
  2798     "Created: / 27-07-2011 / 00:56:17 / cg"
       
  2799 !
       
  2800 
       
  2801 asByteArrayMSB:msb
       
  2802     "depending on the size of the characters in the receiver,
       
  2803      return a byteArray containing single-, double- or even 4-bytewise values.
       
  2804      The size of the returned byteArray will be the strings size multiplied by the
       
  2805      size required for the largest character.
       
  2806      Caveat: better use utf8Encoded, to get reproducable results"
       
  2807 
       
  2808     |ba|
       
  2809 
       
  2810     ba := self asByteArray. "/ native order
       
  2811     UninterpretedBytes isBigEndian ~~ msb ifTrue:[
       
  2812         ba swapBytes
       
  2813     ].
       
  2814     ^ ba
  2797 !
  2815 !
  2798 
  2816 
  2799 asCollectionOfLines
  2817 asCollectionOfLines
  2800     "return a collection containing the lines (separated by cr)
  2818     "return a collection containing the lines (separated by cr)
  2801      of the receiver. If multiple cr's occur in a row, the result will
  2819      of the receiver. If multiple cr's occur in a row, the result will
  4431      String streamContents:[:w|
  4449      String streamContents:[:w|
  4432          'abcdeäöüß' asUnicode32String utf8EncodedOn:w
  4450          'abcdeäöüß' asUnicode32String utf8EncodedOn:w
  4433      ].
  4451      ].
  4434     "
  4452     "
  4435 ! !
  4453 ! !
       
  4454 
       
  4455 
  4436 
  4456 
  4437 
  4457 
  4438 !CharacterArray methodsFor:'matching - glob expressions'!
  4458 !CharacterArray methodsFor:'matching - glob expressions'!
  4439 
  4459 
  4440 compoundMatch:aString
  4460 compoundMatch:aString
  7608     "dispatch for visitor pattern; send #visitString:with: to aVisitor"
  7628     "dispatch for visitor pattern; send #visitString:with: to aVisitor"
  7609 
  7629 
  7610     ^ aVisitor visitString:self with:aParameter
  7630     ^ aVisitor visitString:self with:aParameter
  7611 ! !
  7631 ! !
  7612 
  7632 
       
  7633 
  7613 !CharacterArray class methodsFor:'documentation'!
  7634 !CharacterArray class methodsFor:'documentation'!
  7614 
  7635 
  7615 version
  7636 version
  7616     ^ '$Header$'
  7637     ^ '$Header$'
  7617 !
  7638 !