File: ByteArray.st

package info (click to toggle)
gnu-smalltalk 3.2.4-2.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 32,688 kB
  • ctags: 14,104
  • sloc: ansic: 87,424; sh: 22,729; asm: 8,465; perl: 4,513; cpp: 3,548; xml: 1,669; awk: 1,581; yacc: 1,357; makefile: 1,237; lisp: 855; lex: 843; sed: 258; objc: 124
file content (638 lines) | stat: -rw-r--r-- 17,192 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
"======================================================================
|
|   ByteArray Method Definitions
|
|
 ======================================================================"

"======================================================================
|
| Copyright 1988,92,94,95,99,2000,2001,2002,2006,2008,2009
| Free Software Foundation, Inc.
| Written by Steve Byrne.
|
| This file is part of the GNU Smalltalk class library.
|
| The GNU Smalltalk class library is free software; you can redistribute it
| and/or modify it under the terms of the GNU Lesser General Public License
| as published by the Free Software Foundation; either version 2.1, or (at
| your option) any later version.
| 
| The GNU Smalltalk class library is distributed in the hope that it will be
| useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
| General Public License for more details.
| 
| You should have received a copy of the GNU Lesser General Public License
| along with the GNU Smalltalk class library; see the file COPYING.LIB.
| If not, write to the Free Software Foundation, 59 Temple Place - Suite
| 330, Boston, MA 02110-1301, USA.  
|
 ======================================================================"



ArrayedCollection subclass: ByteArray [
    
    <shape: #byte>
    <import: CSymbols>
    <category: 'Collections-Sequenceable'>
    <comment: 'My instances are similar to strings in that they are both represented as
a sequence of bytes, but my individual elements are integers, where as
a String''s elements are characters.'>

    ByteArray class >> fromCData: aCObject size: anInteger [
	"Answer a ByteArray containing anInteger bytes starting at the location pointed
	 to by aCObject"

	<category: 'instance creation'>
	<primitive: VMpr_ByteArray_fromCData_size>
	^SystemExceptions.WrongClass signalOn: anInteger mustBe: SmallInteger
    ]

    asString [
	"Answer a String whose character's ASCII codes are the receiver's contents"

	<category: 'converting'>
	| string size |
	size := self size.
	string := String new: size.
	string 
	    replaceFrom: 1
	    to: size
	    withByteArray: self
	    startingAt: 1.
	^string
    ]

    asUnicodeString [
	"Answer a UnicodeString whose character's codes are the receiver's contents.
	 This is not implemented unless you load the I18N package."

	<category: 'converting'>
	self shouldNotImplement
    ]

    isLiteralObject [
	"Answer whether the receiver is expressible as a Smalltalk literal."

	<category: 'storing'>
	^self isReadOnly not
    ]

    storeLiteralOn: aStream [
	"Put a Smalltalk literal evaluating to the receiver on aStream."

	<category: 'storing'>
	self class == ByteArray ifFalse: [ ^super storeLiteralOn: aStream ].
	aStream nextPut: $#.
	aStream nextPut: $[.
	self do: 
		[:elt | 
		aStream
		    print: elt;
		    space].
	aStream nextPut: $]
    ]

    storeOn: aStream [
	"Put Smalltalk code evaluating to the receiver on aStream."

	<category: 'storing'>
	self class == ByteArray ifFalse: [ ^super storeOn: aStream ].
	self storeLiteralOn: aStream.
	self isReadOnly ifFalse: [aStream nextPutAll: ' copy']
    ]

    at: anIndex ifAbsent: aBlock [
	"Answer the index-th indexed instance variable of the receiver"

	<category: 'built ins'>
	<primitive: VMpr_Object_basicAt>
	^self checkIndexableBounds: anIndex ifAbsent: aBlock
    ]

    objectAt: index [
	"Access the Smalltalk object (OOP) at the given index in the receiver.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 9
	    at: index
    ]

    charAt: index [
	"Access the C char at the given index in the receiver. The value is
	 returned as a Smalltalk Character.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 0
	    at: index
    ]

    unsignedCharAt: index [
	"Access the C unsigned char at the given index in the receiver.
	 The value is returned as a Smalltalk Character.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 1
	    at: index
    ]

    ucharAt: index [
	"Access the C unsigned char at the given index in the receiver.
	 The value is returned as a Smalltalk Character.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 1
	    at: index
    ]

    shortAt: index [
	"Access the C short int at the given index in the receiver.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 2
	    at: index
    ]

    unsignedShortAt: index [
	"Access the C unsigned short int at the given index in the receiver.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 3
	    at: index
    ]

    ushortAt: index [
	"Access the C unsigned short int at the given index in the receiver.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 3
	    at: index
    ]

    longAt: index [
	"Access the C long int at the given index in the receiver.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 4
	    at: index
    ]

    unsignedLongAt: index [
	"Access the C unsigned long int at the given index in the receiver.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 5
	    at: index
    ]

    ulongAt: index [
	"Access the C unsigned long int at the given index in the receiver.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 5
	    at: index
    ]

    intAt: index [
	"Access the C int at the given index in the receiver.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 10
	    at: index
    ]

    unsignedIntAt: index [
	"Access the C unsigned int at the given index in the receiver.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 11
	    at: index
    ]

    uintAt: index [
	"Access the C unsigned int at the given index in the receiver.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 11
	    at: index
    ]

    floatAt: index [
	"Access the C float at the given index in the receiver.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 6
	    at: index
    ]

    doubleAt: index [
	"Access the C double at the given index in the receiver.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 7
	    at: index
    ]

    longDoubleAt: index [
	"Access the C long double at the given index in the receiver.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 12
	    at: index
    ]

    stringAt: index [
	"Access the string pointed by the C `char *' at the given index in the
	 receiver. Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 8
	    at: index
    ]

    objectAt: index put: value [
	"Store a pointer (OOP) to the Smalltalk object identified by `value',
	 at the given index in the receiver.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 9
	    at: index
	    put: value
    ]

    charAt: index put: value [
	"Store as a C char the Smalltalk Character or Integer object
	 identified by `value', at the given index in the receiver, using
	 sizeof(char) bytes - i.e. 1 byte.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 0
	    at: index
	    put: value
    ]

    unsignedCharAt: index put: value [
	"Store as a C char the Smalltalk Character or Integer object
	 identified by `value', at the given index in the receiver, using
	 sizeof(char) bytes - i.e. 1 byte.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 1
	    at: index
	    put: value
    ]

    ucharAt: index put: value [
	"Store as a C char the Smalltalk Character or Integer object
	 identified by `value', at the given index in the receiver, using
	 sizeof(char) bytes - i.e. 1 byte.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 1
	    at: index
	    put: value
    ]

    shortAt: index put: value [
	"Store the Smalltalk Integer object identified by `value', at the
	 given index in the receiver, using sizeof(short) bytes.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 2
	    at: index
	    put: value
    ]

    unsignedShortAt: index put: value [
	"Store the Smalltalk Integer object identified by `value', at the
	 given index in the receiver, using sizeof(short) bytes.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 3
	    at: index
	    put: value
    ]

    ushortAt: index put: value [
	"Store the Smalltalk Integer object identified by `value', at the
	 given index in the receiver, using sizeof(short) bytes.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 3
	    at: index
	    put: value
    ]

    longAt: index put: value [
	"Store the Smalltalk Integer object identified by `value', at the
	 given index in the receiver, using sizeof(long) bytes.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 4
	    at: index
	    put: value
    ]

    unsignedLongAt: index put: value [
	"Store the Smalltalk Integer object identified by `value', at the
	 given index in the receiver, using sizeof(long) bytes.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 5
	    at: index
	    put: value
    ]

    ulongAt: index put: value [
	"Store the Smalltalk Integer object identified by `value', at the
	 given index in the receiver, using sizeof(long) bytes.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 5
	    at: index
	    put: value
    ]

    intAt: index put: value [
	"Store the Smalltalk Integer object identified by `value', at the
	 given index in the receiver, using sizeof(int) bytes.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 10
	    at: index
	    put: value
    ]

    unsignedIntAt: index put: value [
	"Store the Smalltalk Integer object identified by `value', at the
	 given index in the receiver, using sizeof(int) bytes.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 11
	    at: index
	    put: value
    ]

    uintAt: index put: value [
	"Store the Smalltalk Integer object identified by `value', at the
	 given index in the receiver, using sizeof(int) bytes.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 11
	    at: index
	    put: value
    ]

    floatAt: index put: value [
	"Store the Smalltalk Float object identified by `value', at the
	 given index in the receiver, writing it like a C float.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 6
	    at: index
	    put: value
    ]

    doubleAt: index put: value [
	"Store the Smalltalk Float object identified by `value', at the
	 given index in the receiver, writing it like a C double.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 7
	    at: index
	    put: value
    ]

    longDoubleAt: index put: value [
	"Store the Smalltalk Float object identified by `value', at the
	 given index in the receiver, writing it like a C double.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 12
	    at: index
	    put: value
    ]

    stringAt: index put: value [
	"Store the Smalltalk String object identified by `value', at the
	 given index in the receiver, writing it like a *FRESHLY
	 ALLOCATED* C string. It is the caller's responsibility to free
	 it if necessary.
	 Indices are 1-based just like for other Smalltalk access."

	<category: 'more advanced accessing'>
	^self 
	    type: 8
	    at: index
	    put: value
    ]

    growSize [
	"Answer the amount by which a ByteArray will grow if necessary.
	 Note - explicit growing of a Collection is a private thing you
	 should not employ"

	<category: 'private'>
	^self size
    ]

    castTo: type [
	"Give access to the receiver as a value with the given CType."

	<category: 'CObject'>
	^(CObject new storage: self) castTo: type
    ]

    type: type at: index [
	"Private - Access in the receiver a value with the given type
         at the given 1-based index."

	<category: 'private'>
	^(CObject new storage: self) at: index - 1 type: type
    ]

    type: type at: index put: value [
	"Private - Write to the receiver a value with the given type
         at the given 1-based index."

	<category: 'private'>
	self isReadOnly ifTrue: [^SystemExceptions.ReadOnlyObject signal].
	^(CObject new storage: self) at: index - 1 put: value type: type
    ]

    byteAt: index [
	"Answer the index-th indexed instance variable of the receiver"

	<category: 'built ins'>
	<primitive: VMpr_Object_basicAt>
	self checkIndexableBounds: index
    ]

    byteAt: index put: value [
	"Store the `value' byte in the index-th indexed instance variable
	 of the receiver"

	<category: 'built ins'>
	<primitive: VMpr_Object_basicAtPut>
	self checkIndexableBounds: index put: value
    ]

    = aCollection [
	"Answer whether the receiver's items match those in aCollection"

	<category: 'basic'>
	<primitive: VMpr_ArrayedCollection_equal>
	^false
    ]

    hash [
	"Answer an hash value for the receiver"

	<category: 'built ins'>
	<primitive: VMpr_String_hash>
	^0
    ]

    indexOf: anElement startingAt: anIndex ifAbsent: exceptionBlock [
	"Answer the first index > anIndex which contains anElement.
	 Invoke exceptionBlock and answer its result if no item is found"

	<category: 'basic'>
	<primitive: VMpr_ArrayedCollection_indexOfStartingAt>
	"If anIndex is just past the end of the collection, don't raise
	 an error (this is the most generic solution that avoids that
	 #indexOf: fails when the collection is empty."
	^(anIndex < 1 or: [anIndex > (self size + 1)])
	    ifTrue: [self checkIndexableBounds: anIndex]
	    ifFalse: [exceptionBlock value]
    ]

    indexOf: anElement startingAt: anIndex [
	"Answer the first index > anIndex which contains anElement.
	 Invoke exceptionBlock and answer its result if no item is found"

	<category: 'basic'>
	<primitive: VMpr_ArrayedCollection_indexOfStartingAt>
	"If anIndex is just past the end of the collection, don't raise
	 an error (this is the most generic solution that avoids that
	 #indexOf: fails when the collection is empty."
	^(anIndex < 1 or: [anIndex > (self size + 1)])
	    ifTrue: [self checkIndexableBounds: anIndex]
	    ifFalse: [0]
    ]

    replaceFrom: start to: stop withString: aString startingAt: replaceStart [
	"Replace the characters from start to stop with the
	 ASCII codes contained in aString (which, actually, can be
	 any variable byte class), starting at the replaceStart
	 location of aString"

	<category: 'built ins'>
	<primitive: VMpr_ArrayedCollection_replaceFromToWithStartingAt>
	^super 
	    replaceFrom: start
	    to: stop
	    with: aString
	    startingAt: replaceStart
    ]

    replaceFrom: start to: stop with: aByteArray startingAt: replaceStart [
	"Replace the characters from start to stop with the
	 bytes contained in aByteArray (which, actually, can be
	 any variable byte class), starting at the replaceStart
	 location of aByteArray"

	<category: 'built ins'>
	<primitive: VMpr_ArrayedCollection_replaceFromToWithStartingAt>
	^super 
	    replaceFrom: start
	    to: stop
	    with: aByteArray
	    startingAt: replaceStart
    ]

    asCData [
	"Allocate memory with malloc for a copy of the receiver, and return
         a pointer to it as a CByte."

	<category: 'CObject'>
	^self asCData: CByteType
    ]

    asCData: aCType [
	"Allocate memory with malloc for a copy of the receiver, and return
         it converted to a CObject with the given type"

	<category: 'built ins'>
	<primitive: VMpr_ByteArray_asCData>
	^self primitiveFailed
    ]
]