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 |