ByteArray.st
changeset 18596 d6abf6a67de4
parent 18584 ee039e582133
child 18608 7d521f25267c
child 18611 fca966d5e383
equal deleted inserted replaced
18595:aa02a050b351 18596:d6abf6a67de4
       
     1 "{ Encoding: utf8 }"
       
     2 
     1 "
     3 "
     2  COPYRIGHT (c) 1989 by Claus Gittinger
     4  COPYRIGHT (c) 1989 by Claus Gittinger
     3 	      All Rights Reserved
     5 	      All Rights Reserved
     4 
     6 
     5  This software is furnished under a license and may be used
     7  This software is furnished under a license and may be used
   163     ^ self basicNew:anInteger
   165     ^ self basicNew:anInteger
   164 ! !
   166 ! !
   165 
   167 
   166 
   168 
   167 
   169 
   168 
       
   169 
       
   170 !ByteArray class methodsFor:'queries'!
   170 !ByteArray class methodsFor:'queries'!
   171 
   171 
   172 elementByteSize
   172 elementByteSize
   173     "for bit-like containers, return the number of bytes stored per element.
   173     "for bit-like containers, return the number of bytes stored per element.
   174      Here, 1 is returned"
   174      Here, 1 is returned"
   184 
   184 
   185     ^ self == ByteArray
   185     ^ self == ByteArray
   186 
   186 
   187     "Modified: 23.4.1996 / 15:56:25 / cg"
   187     "Modified: 23.4.1996 / 15:56:25 / cg"
   188 ! !
   188 ! !
   189 
       
   190 
   189 
   191 
   190 
   192 !ByteArray methodsFor:'Compatibility-Squeak'!
   191 !ByteArray methodsFor:'Compatibility-Squeak'!
   193 
   192 
   194 bitXor:aByteArray
   193 bitXor:aByteArray
  1159 !
  1158 !
  1160 
  1159 
  1161 asString
  1160 asString
  1162     "speed up string conversions"
  1161     "speed up string conversions"
  1163 
  1162 
  1164     |size|
  1163     |cls size|
  1165 
  1164 
  1166     self class == ByteArray ifTrue:[
  1165     cls := self class.
  1167 	size := self size.
  1166     (cls == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[
  1168 	^ (String uninitializedNew:size) replaceBytesFrom:1 to:size with:self startingAt:1.
  1167         size := self size.
       
  1168         ^ (String uninitializedNew:size) replaceBytesFrom:1 to:size with:self startingAt:1.
  1169     ].
  1169     ].
  1170     ^ super asString
  1170     ^ super asString
  1171 
  1171 
  1172     "
  1172     "
  1173       #[60 61 62 63] asString
  1173       #[16r41 16r42 16r43] asString
  1174     "
  1174     "
  1175 !
  1175 !
  1176 
  1176 
  1177 beImmutable
  1177 beImmutable
  1178     "make myself write-protected"
  1178     "make myself write-protected"
  1200     ].
  1200     ].
  1201     ^ super literalArrayEncoding
  1201     ^ super literalArrayEncoding
  1202 
  1202 
  1203     "
  1203     "
  1204      #[1 2 3] literalArrayEncoding
  1204      #[1 2 3] literalArrayEncoding
       
  1205     "
       
  1206 !
       
  1207 
       
  1208 utf8Decoded
       
  1209     "Interpreting myself as an UTF-8 representation, decode and return the decoded string."
       
  1210 
       
  1211     |cls in out|
       
  1212 
       
  1213     cls := self class.
       
  1214     (cls == ByteArray or:[cls == ImmutableByteArray]) ifTrue:[
       
  1215         self containsNon7BitAscii ifFalse:[
       
  1216             "speed up common case"
       
  1217             ^ self asSingleByteString.
       
  1218         ].
       
  1219         out := CharacterWriteStream on:(String uninitializedNew:self size).
       
  1220         in := self readStream.
       
  1221         [in atEnd] whileFalse:[
       
  1222             out nextPut:(Character utf8DecodeFrom:in).
       
  1223         ].
       
  1224         ^ out contents
       
  1225     ].
       
  1226     ^ super utf8Decoded
       
  1227 
       
  1228     "
       
  1229      #[16r41 16r42] utf8Decoded
       
  1230      #[16rC8 16rA0] utf8Decoded
       
  1231      (Character value:16r220) utf8Encoded
       
  1232      (Character value:16r220) utf8Encoded utf8Decoded
       
  1233 
       
  1234      (Character value:16r800) utf8Encoded
       
  1235      (Character value:16r220) utf8Encoded utf8Decoded
       
  1236     "
       
  1237 
       
  1238     "test:
       
  1239 
       
  1240       |utf8Encoding original readBack|
       
  1241 
       
  1242       1 to:16rFFFF do:[:ascii |
       
  1243         original := (Character value:ascii) asString.
       
  1244         utf8Encoding := original utf8Encoded.
       
  1245         readBack := utf8Encoding utf8Decoded.
       
  1246         readBack = original ifFalse:[
       
  1247             self halt
       
  1248         ]
       
  1249       ]
  1205     "
  1250     "
  1206 ! !
  1251 ! !
  1207 
  1252 
  1208 !ByteArray methodsFor:'copying'!
  1253 !ByteArray methodsFor:'copying'!
  1209 
  1254 
  2609 displayOn:aGCOrStream
  2654 displayOn:aGCOrStream
  2610     "return a printed representation of the receiver for displaying"
  2655     "return a printed representation of the receiver for displaying"
  2611 
  2656 
  2612     "/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
  2657     "/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
  2613     "/ ST/X (and some old ST80's) mean: draw-yourself on a GC.
  2658     "/ ST/X (and some old ST80's) mean: draw-yourself on a GC.
  2614     (self class == ByteArray and:[aGCOrStream isStream]) ifTrue:[
  2659     |cls|
       
  2660 
       
  2661     cls := self class.
       
  2662     ((cls == ByteArray or:[cls == ImmutableByteArray]) and:[aGCOrStream isStream]) ifTrue:[
  2615         self storeOn:aGCOrStream.
  2663         self storeOn:aGCOrStream.
  2616         ^ self
  2664         ^ self
  2617     ].
  2665     ].
  2618     ^ super displayOn:aGCOrStream
  2666     ^ super displayOn:aGCOrStream
  2619 
  2667 
  2806 containsNon7BitAscii
  2854 containsNon7BitAscii
  2807     "return true, if any byte in the receiver has the 7th bit on.
  2855     "return true, if any byte in the receiver has the 7th bit on.
  2808      This my look as a too specific operation to be put here,
  2856      This my look as a too specific operation to be put here,
  2809      put it is very helpful for UTF8 string reading (Java class reader),
  2857      put it is very helpful for UTF8 string reading (Java class reader),
  2810      to quickly determine, if UTF8 decoding is needed or not.
  2858      to quickly determine, if UTF8 decoding is needed or not.
  2811      As most strings in a class fiel are in fact only containing 7bit ascii,
  2859      As most strings in a class file are in fact only containing 7bit ascii,
  2812      this should speedup class file reading considerably"
  2860      this should speedup class file reading considerably"
  2813 
  2861 
  2814 %{  /* NOCONTEXT */
  2862 %{  /* NOCONTEXT */
  2815     REGISTER unsigned char *cp;
  2863     REGISTER unsigned char *cp;
  2816     REGISTER unsigned char *endP;
  2864     REGISTER unsigned char *endP;
  3103     ^ self class == ByteArray
  3151     ^ self class == ByteArray
  3104 
  3152 
  3105     "Modified: 22.4.1996 / 12:55:30 / cg"
  3153     "Modified: 22.4.1996 / 12:55:30 / cg"
  3106 ! !
  3154 ! !
  3107 
  3155 
  3108 
       
  3109 !ByteArray class methodsFor:'documentation'!
  3156 !ByteArray class methodsFor:'documentation'!
  3110 
  3157 
  3111 version
  3158 version
  3112     ^ '$Header$'
  3159     ^ '$Header$'
  3113 !
  3160 !