ByteArray.st
changeset 3210 6c4099f5c318
parent 3208 2d71538b9fd5
child 3234 4cc58bc6a8ed
equal deleted inserted replaced
3209:eff7ad7f0825 3210:6c4099f5c318
   385     }
   385     }
   386 %}.
   386 %}.
   387     ^ super basicAt:index put:value
   387     ^ super basicAt:index put:value
   388 !
   388 !
   389 
   389 
   390 doubleAt:index
       
   391     "return the 8-bytes starting at index as a Float.
       
   392      Notice, that (currently) ST/X Floats are what Doubles are in ST-80.
       
   393      Notice also, that the bytes are expected to be in this machines
       
   394      float representation - if the bytearray originated from another
       
   395      machine, some conversion is usually needed."
       
   396 
       
   397     |newFloat|
       
   398 
       
   399     newFloat := Float basicNew.
       
   400     1 to:8 do:[:destIndex|
       
   401 	newFloat basicAt:destIndex put:(self at:index - 1 + destIndex)
       
   402     ].
       
   403     ^ newFloat.
       
   404 !
       
   405 
       
   406 doubleAt:index put:aFloat
       
   407     "store the value of the argument, aFloat into the receiver
       
   408      starting at index.
       
   409      Notice, that (currently) ST/X Floats are what Doubles are in ST-80.
       
   410      Notice also, that the bytes are expected to be in this machines
       
   411      float representation - if the bytearray originated from another
       
   412      machine, some conversion is usually needed."
       
   413 
       
   414     |flt|
       
   415 
       
   416     flt := aFloat asFloat.
       
   417     1 to:8 do:[:srcIndex|
       
   418         self at:index - 1 + srcIndex put:(flt basicAt:srcIndex)
       
   419     ].
       
   420     ^ aFloat
       
   421 !
       
   422 
       
   423 doubleWordAt:index
   390 doubleWordAt:index
   424     "return the 4-bytes starting at index as an (unsigned) Integer.
   391     "return the 4-bytes starting at index as an (unsigned) Integer.
   425      The value is retrieved in the machines natural byte order.
   392      The value is retrieved in the machines natural byte order.
   426      Q: should it return a signed value ? (see ByteArray>>signedDoubleWordAt:)"
   393      Q: should it return a signed value ? (see ByteArray>>signedDoubleWordAt:)"
   427 
   394 
   635      b doubleWordAt:5 put:16r04030201 MSB:false.
   602      b doubleWordAt:5 put:16r04030201 MSB:false.
   636      b inspect
   603      b inspect
   637     "
   604     "
   638 !
   605 !
   639 
   606 
   640 floatAt:index
       
   641     "return the 4-bytes starting at index as a Float.
       
   642      Notice, that (currently) ST/X Floats are what Doubles are in ST-80;
       
   643      therefore this method reads a 4-byte float from the byteArray and returns
       
   644      a float object which keeps an 8-byte double internally.
       
   645      Notice also, that the bytes are expected to be in this machines
       
   646      float representation and order - if the bytearray originated from another
       
   647      machine, some conversion is usually needed."
       
   648 
       
   649     |newFloat|
       
   650 
       
   651     newFloat := ShortFloat basicNew.
       
   652     1 to:4 do:[:destIndex|
       
   653         newFloat basicAt:destIndex put:(self at:index - 1 + destIndex)
       
   654     ].
       
   655     ^ newFloat.
       
   656 !
       
   657 
       
   658 floatAt:index put:aFloat
       
   659     "store the 4 bytes of value of the argument, aFloat into the receiver
       
   660      starting at index.
       
   661      Notice, that (currently) ST/X Floats are what Doubles are in ST-80.
       
   662      Notice also, that the bytes are expected to be in this machines
       
   663      float representation - if the bytearray originated from another
       
   664      machine, some conversion is usually needed."
       
   665 
       
   666     |sflt|
       
   667 
       
   668     sflt := aFloat asShortFloat.
       
   669     1 to:4 do:[:srcIndex|
       
   670         self at:index - 1 + srcIndex put:(sflt basicAt:srcIndex)
       
   671     ].
       
   672     ^ aFloat
       
   673 !
       
   674 
       
   675 ieeDoubleAt:index
       
   676     "retrieve the 8 bytes starting at index as a float.
       
   677      The 8 bytes are assumed to be in IEE floating point single precision
       
   678      number format."
       
   679 
       
   680     "
       
   681      currently, we assume that the machines native number format is already
       
   682      IEE format - we need some more code here whenever ST/X is ported
       
   683      to an IBM 370 or old VAX etc.
       
   684      To date, all supported systems use IEE float numbers, so there should be
       
   685      no problem.
       
   686     "
       
   687     ^ self doubleAt:index
       
   688 !
       
   689 
       
   690 ieeDoubleAt:index put:aFloat
       
   691     "store the value of the argument, aFloat into the receiver
       
   692      starting at index. Storage is in IEE floating point double precision format.
       
   693      (i.e. 8 bytes are stored)."
       
   694 
       
   695     "
       
   696      currently, we assume that the machines native number format is already
       
   697      IEE format - we need some more code here whenever ST/X is ported
       
   698      to an IBM 370 or old VAX etc.
       
   699      To date, all supported systems use IEE float numbers, so there should be
       
   700      no problem.
       
   701     "
       
   702     ^ self doubleAt:index put:aFloat
       
   703 !
       
   704 
       
   705 ieeFloatAt:index
       
   706     "retrieve the 4 bytes starting at index as a float.
       
   707      The 4 bytes are assumed to be in IEE floating point single precision
       
   708      number format."
       
   709 
       
   710     "
       
   711      currently, we assume that the machines native number format is already
       
   712      IEE format - we need some more code here whenever ST/X is ported
       
   713      to an IBM 370 or old VAX etc.
       
   714      To date, all supported systems use IEE float numbers, so there should be
       
   715      no problem.
       
   716     "
       
   717     ^ self floatAt:index
       
   718 !
       
   719 
       
   720 ieeFloatAt:index put:aFloat
       
   721     "store the value of the argument, aFloat into the receiver
       
   722      starting at index. Storage is in IEE floating point single precision format.
       
   723      (i.e. 4 bytes are stored). Since ST/X floats are really doubles, the low-
       
   724      order 4 bytes of the precision is lost."
       
   725 
       
   726     "
       
   727      currently, we assume that the machines native number format is already
       
   728      IEE format - we need some more code here whenever ST/X is ported
       
   729      to an IBM 370 or old VAX etc.
       
   730      To date, all supported systems use IEE float numbers, so there should be
       
   731      no problem.
       
   732     "
       
   733     ^ self floatAt:index put:aFloat
       
   734 !
       
   735 
       
   736 quadWordAt:index MSB:msb
       
   737     "return the 8-bytes starting at index as an (unsigned) Integer.
       
   738      Depending on msb, the value is retrieved MSB or LSB-first."
       
   739 
       
   740     |l 
       
   741      bIdx  "{ Class: SmallInteger }"
       
   742      delta "{ Class: SmallInteger }"|
       
   743 
       
   744     l := LargeInteger basicNew numberOfDigits:8.
       
   745     msb ifTrue:[
       
   746 	bIdx := index + 7.
       
   747 	delta := -1
       
   748     ] ifFalse:[
       
   749 	bIdx := index.
       
   750 	delta := 1
       
   751     ].
       
   752     1 to:8 do:[:i |
       
   753 	l digitAt:i put:(self basicAt:bIdx).
       
   754 	bIdx := bIdx + delta
       
   755     ].
       
   756     ^ l compressed
       
   757 
       
   758     "
       
   759      |b|
       
   760 
       
   761      b := ByteArray withAll:#(1 2 3 4 5 6 7 8).
       
   762      (b quadWordAt:1 MSB:false) printStringRadix:16  
       
   763     "
       
   764 
       
   765     "Modified: 5.11.1996 / 14:06:21 / cg"
       
   766 !
       
   767 
       
   768 quadWordAt:index put:anInteger MSB:msb
       
   769     "set the 8-bytes starting at index from the (unsigned) Integer value.
       
   770      The value must be in the range 0 to 16rFFFFFFFFFFFFFFFF.
       
   771      Depending on msb, the value is stored MSB-first or LSB-first."
       
   772 
       
   773     |bIdx  "{ Class: SmallInteger }"
       
   774      delta "{ Class: SmallInteger }"|
       
   775 
       
   776     msb ifTrue:[
       
   777 	bIdx := index + 7.
       
   778 	delta := -1
       
   779     ] ifFalse:[
       
   780 	bIdx := index.
       
   781 	delta := 1
       
   782     ].
       
   783     1 to:8 do:[:i |
       
   784 	self basicAt:bIdx put:(anInteger digitAt:i).
       
   785 	bIdx := bIdx + delta.
       
   786     ].
       
   787     ^ anInteger
       
   788 
       
   789     "
       
   790      |b|
       
   791      b := ByteArray new:8.
       
   792      b quadWordAtIndex:1 put:16r0807060504030201 MSB:false.
       
   793      b inspect
       
   794     "
       
   795 !
       
   796 
       
   797 signedByteAt:index
       
   798     "return the byte at index as a signed 8 bit value.
       
   799      This may be worth a primitive."
       
   800 
       
   801     ^ (self at:index) signExtendedByteValue
       
   802 
       
   803 "/    |b "{ Class: SmallInteger }"|
       
   804 "/
       
   805 "/    b := self at:index.
       
   806 "/    (b > 16r7F) ifTrue:[
       
   807 "/        ^ b - 16r100
       
   808 "/    ].
       
   809 "/    ^ b
       
   810 
       
   811     "
       
   812      |b|
       
   813      b := ByteArray new:2.
       
   814      b at:1 put:16rFF.
       
   815      b at:2 put:16r7F.
       
   816      b signedByteAt:1  
       
   817     "
       
   818 
       
   819     "Modified: 1.7.1996 / 21:13:53 / cg"
       
   820 !
       
   821 
       
   822 signedByteAt:index put:aSignedByteValue
       
   823     "return the byte at index as a signed 8 bit value.
       
   824      Return the signedByteValue argument.
       
   825      This may be worth a primitive."
       
   826 
       
   827     |b "{ Class: SmallInteger }"|
       
   828 
       
   829     aSignedByteValue >= 0 ifTrue:[
       
   830 	b := aSignedByteValue
       
   831     ] ifFalse:[
       
   832 	b := 16r100 + aSignedByteValue
       
   833     ].
       
   834     self at:index put:b.
       
   835     ^ aSignedByteValue
       
   836 
       
   837     "
       
   838      |b|
       
   839      b := ByteArray new:2.
       
   840      b signedByteAt:1 put:-1.
       
   841      b at:1   
       
   842     "
       
   843 
       
   844     "Modified: 1.7.1996 / 21:12:37 / cg"
       
   845 !
       
   846 
       
   847 signedDoubleWordAt:index
       
   848     "return the 4-bytes starting at index as a signed Integer.
       
   849      The value is retrieved in the machines natural byte order.
       
   850      This may be worth a primitive."
       
   851 
       
   852     |w|
       
   853 
       
   854     w := self doubleWordAt:index.
       
   855     (w > (16r7FFFFFFF)) ifTrue:[
       
   856 	^ w - (16r100000000)
       
   857     ].
       
   858     ^ w
       
   859 
       
   860 "
       
   861     w := self doubleWordAt:index.
       
   862     (w > 16r7FFFFFFF) ifTrue:[
       
   863 	^ w - 16r100000000
       
   864     ].
       
   865     ^ w
       
   866 "
       
   867 
       
   868     "
       
   869      |b|
       
   870      b := ByteArray new:4.
       
   871      b doubleWordAt:1 put:16rFFFFFFFF.
       
   872      (b signedDoubleWordAt:1)    
       
   873     "
       
   874 
       
   875     "Modified: 1.7.1996 / 21:11:28 / cg"
       
   876 !
       
   877 
       
   878 signedDoubleWordAt:index MSB:msb
       
   879     "return the 4-bytes starting at index as a signed Integer.
       
   880      Depending on msb, the value is retrieved MSB-first or LSB-first.
       
   881      This may be worth a primitive."
       
   882 
       
   883     |w|
       
   884 
       
   885     w := self doubleWordAt:index MSB:msb.
       
   886     (w > (16r7FFFFFFF)) ifTrue:[
       
   887 	^ w - (16r100000000)
       
   888     ].
       
   889     ^ w
       
   890 "
       
   891     w := self doubleWordAt:index.
       
   892     (w > 16r7FFFFFFF) ifTrue:[
       
   893 	^ w - 16r100000000
       
   894     ].
       
   895     ^ w
       
   896 "
       
   897 
       
   898     "
       
   899      |b|
       
   900      b := ByteArray new:4.
       
   901      b doubleWordAt:1 put:16rFFFFFFFF.
       
   902      (b signedDoubleWordAt:1)    
       
   903     "
       
   904 
       
   905     "Modified: 1.7.1996 / 21:11:33 / cg"
       
   906 !
       
   907 
       
   908 signedDoubleWordAt:index put:value
       
   909     "set the 4-bytes starting at index from the signed Integer value.
       
   910      The value is stored in the machines natural byte order.
       
   911      This may be worth a primitive."
       
   912 
       
   913     |v|
       
   914 
       
   915     value >= 0 ifTrue:[
       
   916 	v := value
       
   917     ] ifFalse:[
       
   918 	v := value + 16r100000000
       
   919     ].
       
   920     self doubleWordAt:index put:v.
       
   921     ^ value
       
   922 
       
   923     "
       
   924      |b|
       
   925      b := ByteArray new:4.
       
   926      b signedDoubleWordAt:1 put:-1.
       
   927      (b doubleWordAt:1) printStringRadix:16   
       
   928     "
       
   929 
       
   930     "Modified: 1.7.1996 / 21:11:39 / cg"
       
   931 !
       
   932 
       
   933 signedDoubleWordAt:index put:value MSB:msb
       
   934     "set the 4-bytes starting at index from the signed Integer value.
       
   935      Depending on msb, the value is stored MSB-first or LSB-first.
       
   936      This may be worth a primitive."
       
   937 
       
   938     |v|
       
   939 
       
   940     value >= 0 ifTrue:[
       
   941 	v := value
       
   942     ] ifFalse:[
       
   943 	v := value + 16r100000000
       
   944     ].
       
   945     self doubleWordAt:index put:v MSB:msb.
       
   946     ^ value
       
   947 
       
   948     "
       
   949      |b|
       
   950      b := ByteArray new:4.
       
   951      b signedDoubleWordAt:1 put:-1.
       
   952      (b doubleWordAt:1) printStringRadix:16   
       
   953     "
       
   954 
       
   955     "Modified: 1.7.1996 / 21:11:46 / cg"
       
   956 !
       
   957 
       
   958 signedWordAt:index
       
   959     "return the 2-bytes starting at index as a signed Integer.
       
   960      The value is retrieved in the machines natural byte order.
       
   961      This may be worth a primitive."
       
   962 
       
   963     ^ (self wordAt:index) signExtendedShortValue
       
   964 
       
   965 "/    |w "{ Class: SmallInteger }"|
       
   966 "/
       
   967 "/    w := self wordAt:index.
       
   968 "/    (w > 16r7FFF) ifTrue:[
       
   969 "/        ^ w - 16r10000
       
   970 "/    ].
       
   971 "/    ^ w
       
   972 
       
   973     "
       
   974      |b|
       
   975      b := ByteArray new:2.
       
   976      b wordAt:1 put:16rFFFF.
       
   977      b signedWordAt:1  
       
   978     "
       
   979 
       
   980     "Modified: 1.7.1996 / 21:14:38 / cg"
       
   981 !
       
   982 
       
   983 signedWordAt:index MSB:msb
       
   984     "return the 2-bytes starting at index as a signed Integer.
       
   985      The value is retrieved MSB-first if the msb-arg is true,
       
   986      LSB-first otherwise.
       
   987      This may be worth a primitive."
       
   988 
       
   989     ^ (self wordAt:index MSB:msb) signExtendedShortValue
       
   990 
       
   991 "/    |w "{ Class: SmallInteger }"|
       
   992 "/
       
   993 "/    w := self wordAt:index MSB:msb.
       
   994 "/    (w > 16r7FFF) ifTrue:[
       
   995 "/        ^ w - 16r10000
       
   996 "/    ].
       
   997 "/    ^ w
       
   998 
       
   999     "
       
  1000      |b|
       
  1001      b := ByteArray new:2.
       
  1002      b wordAt:1 put:16r0080.
       
  1003      b signedWordAt:1 MSB:true.  
       
  1004      b signedWordAt:1 MSB:false.  
       
  1005     "
       
  1006 
       
  1007     "Modified: 1.7.1996 / 21:15:57 / cg"
       
  1008 !
       
  1009 
       
  1010 signedWordAt:index put:value
       
  1011     "set the 2-bytes starting at index from the signed Integer value.
       
  1012      The stored value must be in the range -32768 .. +32676.
       
  1013      The value is stored in the machines natural byteorder.
       
  1014      This may be worth a primitive."
       
  1015 
       
  1016     |v|
       
  1017 
       
  1018     value >= 0 ifTrue:[
       
  1019 	v := value
       
  1020     ] ifFalse:[
       
  1021 	v := 16r10000 + value
       
  1022     ].
       
  1023     self wordAt:index put:v.
       
  1024     ^ value
       
  1025 
       
  1026     "
       
  1027      |b|
       
  1028      b := ByteArray new:6.
       
  1029      b signedWordAt:1 put:-1.
       
  1030      b signedWordAt:3 put:-2.
       
  1031      b signedWordAt:5 put:0.
       
  1032      b inspect
       
  1033     "
       
  1034 
       
  1035     "Modified: 1.7.1996 / 21:12:07 / cg"
       
  1036 !
       
  1037 
       
  1038 signedWordAt:index put:value MSB:msb
       
  1039     "set the 2-bytes starting at index from the signed Integer value.
       
  1040      The stored value must be in the range -32768 .. +32676.
       
  1041      The value is stored MSB-first, if the msb-arg is true;
       
  1042      LSB-first otherwise.
       
  1043      This may be worth a primitive."
       
  1044 
       
  1045     |v|
       
  1046 
       
  1047     value >= 0 ifTrue:[
       
  1048 	v := value
       
  1049     ] ifFalse:[
       
  1050 	v := 16r10000 + value
       
  1051     ].
       
  1052     self wordAt:index put:v MSB:msb.
       
  1053     ^ value
       
  1054 
       
  1055     "
       
  1056      |b|
       
  1057      b := ByteArray new:4.
       
  1058      b signedWordAt:1 put:-1.
       
  1059      b signedWordAt:3 put:-2.
       
  1060      b inspect
       
  1061     "
       
  1062 
       
  1063     "Modified: 1.7.1996 / 21:12:13 / cg"
       
  1064 !
       
  1065 
       
  1066 stringAt:index size:count
       
  1067     "extract a string, given initial index and number of characters (bytes)"
       
  1068 
       
  1069     ^ (self copyFrom:index to:(index + count - 1)) asString
       
  1070 
       
  1071     "Modified: 9.9.1996 / 15:28:08 / cg"
       
  1072     "Created: 9.9.1996 / 15:28:48 / cg"
       
  1073 !
       
  1074 
       
  1075 wordAt:index
   607 wordAt:index
  1076     "return the 2-bytes starting at index as an (unsigned) Integer.
   608     "return the 2-bytes starting at index as an (unsigned) Integer.
  1077      The value is retrieved in the machines natural byte order
   609      The value is retrieved in the machines natural byte order
  1078      Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)"
   610      Question: should it be retrieve signed values ? (see ByteArray>>signedWordAt:)"
  1079 
   611 
  1243      b wordAt:3 put:16r0304 MSB:false.
   775      b wordAt:3 put:16r0304 MSB:false.
  1244      b wordAt:5 put:16r0102 MSB:true.
   776      b wordAt:5 put:16r0102 MSB:true.
  1245      b wordAt:7 put:16r0304 MSB:true.
   777      b wordAt:7 put:16r0304 MSB:true.
  1246      b inspect  
   778      b inspect  
  1247     "
   779     "
  1248 !
       
  1249 
       
  1250 zeroByteStringAt:index maximumSize:count
       
  1251     "extract a zeroByte-delimited string, given initial index and
       
  1252      maximum number of characters (bytes)"
       
  1253 
       
  1254     |bytes idx|
       
  1255 
       
  1256     bytes := self copyFrom:index to:(index + count - 1).
       
  1257     idx := bytes indexOf:0.
       
  1258     idx ~~ 0 ifTrue:[ bytes := bytes copyTo:idx-1 ].
       
  1259     ^ bytes asString
       
  1260 
       
  1261     "Created: 9.9.1996 / 15:28:34 / cg"
       
  1262 ! !
   780 ! !
  1263 
   781 
  1264 !ByteArray methodsFor:'binary storage'!
   782 !ByteArray methodsFor:'binary storage'!
  1265 
   783 
  1266 storeBinaryDefinitionOn:stream manager:manager
   784 storeBinaryDefinitionOn:stream manager:manager
  2375 ! !
  1893 ! !
  2376 
  1894 
  2377 !ByteArray class methodsFor:'documentation'!
  1895 !ByteArray class methodsFor:'documentation'!
  2378 
  1896 
  2379 version
  1897 version
  2380     ^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.93 1998-01-21 16:08:55 cg Exp $'
  1898     ^ '$Header: /cvs/stx/stx/libbasic/ByteArray.st,v 1.94 1998-01-21 16:39:03 cg Exp $'
  2381 ! !
  1899 ! !