ReadStream.st
author Claus Gittinger <cg@exept.de>
Fri, 08 Jul 2005 19:15:03 +0200
changeset 8913 b9498d27a554
parent 8614 88421fbd4ab6
child 8914 acac69ff65b0
permissions -rw-r--r--
64bit; mkSmallInteger
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     1
"
5
67342904af11 *** empty log message ***
claus
parents: 3
diff changeset
     2
 COPYRIGHT (c) 1988 by Claus Gittinger
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
     3
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
     4
a27a279701f8 Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
a27a279701f8 Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
a27a279701f8 Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
a27a279701f8 Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
a27a279701f8 Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
a27a279701f8 Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
a27a279701f8 Initial revision
claus
parents:
diff changeset
    11
"
a27a279701f8 Initial revision
claus
parents:
diff changeset
    12
5810
3de0efaab65c checkin from browser
frank
parents: 5310
diff changeset
    13
"{ Package: 'stx:libbasic' }"
3de0efaab65c checkin from browser
frank
parents: 5310
diff changeset
    14
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    15
PositionableStream subclass:#ReadStream
970
3b59c9b38dbb ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents: 829
diff changeset
    16
	instanceVariableNames:''
3b59c9b38dbb ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents: 829
diff changeset
    17
	classVariableNames:''
3b59c9b38dbb ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents: 829
diff changeset
    18
	poolDictionaries:''
3b59c9b38dbb ST80 compatibility: generate signal at end of stream when instance variable
Stefan Vogel <sv@exept.de>
parents: 829
diff changeset
    19
	category:'Streams'
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    20
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
    21
2162
7e1601f633b6 ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents: 1688
diff changeset
    22
!ReadStream class methodsFor:'documentation'!
88
81dacba7a63a *** empty log message ***
claus
parents: 59
diff changeset
    23
81dacba7a63a *** empty log message ***
claus
parents: 59
diff changeset
    24
copyright
81dacba7a63a *** empty log message ***
claus
parents: 59
diff changeset
    25
"
81dacba7a63a *** empty log message ***
claus
parents: 59
diff changeset
    26
 COPYRIGHT (c) 1988 by Claus Gittinger
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
    27
	      All Rights Reserved
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    28
88
81dacba7a63a *** empty log message ***
claus
parents: 59
diff changeset
    29
 This software is furnished under a license and may be used
81dacba7a63a *** empty log message ***
claus
parents: 59
diff changeset
    30
 only in accordance with the terms of that license and with the
81dacba7a63a *** empty log message ***
claus
parents: 59
diff changeset
    31
 inclusion of the above copyright notice.   This software may not
81dacba7a63a *** empty log message ***
claus
parents: 59
diff changeset
    32
 be provided or otherwise made available to, or used by, any
81dacba7a63a *** empty log message ***
claus
parents: 59
diff changeset
    33
 other person.  No title to or ownership of the software is
81dacba7a63a *** empty log message ***
claus
parents: 59
diff changeset
    34
 hereby transferred.
81dacba7a63a *** empty log message ***
claus
parents: 59
diff changeset
    35
"
81dacba7a63a *** empty log message ***
claus
parents: 59
diff changeset
    36
!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    37
88
81dacba7a63a *** empty log message ***
claus
parents: 59
diff changeset
    38
documentation
81dacba7a63a *** empty log message ***
claus
parents: 59
diff changeset
    39
"
81dacba7a63a *** empty log message ***
claus
parents: 59
diff changeset
    40
    ReadStream defines protocol for reading streamwise over collections. 
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1138
diff changeset
    41
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1138
diff changeset
    42
    [author:]
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
    43
	Claus Gittinger
1295
83f594f05c52 documentation
Claus Gittinger <cg@exept.de>
parents: 1138
diff changeset
    44
88
81dacba7a63a *** empty log message ***
claus
parents: 59
diff changeset
    45
"
81dacba7a63a *** empty log message ***
claus
parents: 59
diff changeset
    46
! !
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
    47
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
    48
!ReadStream methodsFor:'converting'!
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
    49
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
    50
readStream
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
    51
    "return a readStream from the receiver. Since this is already
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
    52
     a readStream, return self."
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
    53
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
    54
    ^ self
8140
7cc7ac5f29ca Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents: 8098
diff changeset
    55
!
7cc7ac5f29ca Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents: 8098
diff changeset
    56
7cc7ac5f29ca Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents: 8098
diff changeset
    57
readStreamOrNil
7cc7ac5f29ca Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents: 8098
diff changeset
    58
    "return a readStream from the receiver. Since this is already
7cc7ac5f29ca Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents: 8098
diff changeset
    59
     a readStream, return self.
7cc7ac5f29ca Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents: 8098
diff changeset
    60
7cc7ac5f29ca Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents: 8098
diff changeset
    61
     This method has been defined for protocol copmatibility with Filename"
7cc7ac5f29ca Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents: 8098
diff changeset
    62
7cc7ac5f29ca Define #xxxStreamOrNil methods.
Stefan Vogel <sv@exept.de>
parents: 8098
diff changeset
    63
    ^ self
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
    64
! !
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
    65
1400
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    66
!ReadStream methodsFor:'emphasis'!
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    67
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    68
emphasis
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    69
    "return the emphasis of the current (i.e. next returned by #next)
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    70
     element. Streams on a string will return nil for all elements.
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    71
     Streams on collections which nothing at all about emphasises, 
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    72
     will report an error."
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    73
7114
acc13967229e position fixes
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
    74
    ^ collection emphasisAt:(position - ZeroPosition + 1).
1400
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    75
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    76
    "
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    77
     |t s|
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    78
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    79
     t := 'hello world' asText
7114
acc13967229e position fixes
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
    80
                emphasizeFrom:1 to:5 with:#bold;
acc13967229e position fixes
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
    81
                emphasizeFrom:7 to:11 with:#italic.
1400
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    82
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    83
     s := t readStream.
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    84
     [s atEnd] whileFalse:[
7114
acc13967229e position fixes
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
    85
        Transcript show:(s emphasis); show:' '.
acc13967229e position fixes
Claus Gittinger <cg@exept.de>
parents: 7051
diff changeset
    86
        Transcript show:''''; show:(s next); showCR:''''.
1400
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    87
     ].
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    88
    "
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    89
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    90
    "Modified: 15.5.1996 / 17:30:33 / cg"
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    91
! !
0ee12d945849 checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1295
diff changeset
    92
345
claus
parents: 329
diff changeset
    93
!ReadStream methodsFor:'queries'!
claus
parents: 329
diff changeset
    94
5310
b943d0eae029 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4826
diff changeset
    95
copyFrom:beginning to:end
b943d0eae029 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4826
diff changeset
    96
    ^ collection copyFrom:beginning to:end
b943d0eae029 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4826
diff changeset
    97
!
b943d0eae029 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 4826
diff changeset
    98
345
claus
parents: 329
diff changeset
    99
isReadable
2997
5254aeab421d comments
Claus Gittinger <cg@exept.de>
parents: 2446
diff changeset
   100
    "return true, if reading is supported by the recevier.
5254aeab421d comments
Claus Gittinger <cg@exept.de>
parents: 2446
diff changeset
   101
     Here, true is always returned."
5254aeab421d comments
Claus Gittinger <cg@exept.de>
parents: 2446
diff changeset
   102
345
claus
parents: 329
diff changeset
   103
    ^ true
2997
5254aeab421d comments
Claus Gittinger <cg@exept.de>
parents: 2446
diff changeset
   104
5254aeab421d comments
Claus Gittinger <cg@exept.de>
parents: 2446
diff changeset
   105
    "Modified: 4.10.1997 / 17:59:21 / cg"
1665
928e9a308cea added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents: 1488
diff changeset
   106
!
928e9a308cea added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents: 1488
diff changeset
   107
6315
11b585e578f5 Define #isWritable
Stefan Vogel <sv@exept.de>
parents: 6011
diff changeset
   108
isWritable
11b585e578f5 Define #isWritable
Stefan Vogel <sv@exept.de>
parents: 6011
diff changeset
   109
    "return true, if writing is supported by the recevier.
11b585e578f5 Define #isWritable
Stefan Vogel <sv@exept.de>
parents: 6011
diff changeset
   110
     This has to be redefined in concrete subclasses."
11b585e578f5 Define #isWritable
Stefan Vogel <sv@exept.de>
parents: 6011
diff changeset
   111
11b585e578f5 Define #isWritable
Stefan Vogel <sv@exept.de>
parents: 6011
diff changeset
   112
    ^ false
11b585e578f5 Define #isWritable
Stefan Vogel <sv@exept.de>
parents: 6011
diff changeset
   113
!
11b585e578f5 Define #isWritable
Stefan Vogel <sv@exept.de>
parents: 6011
diff changeset
   114
1665
928e9a308cea added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents: 1488
diff changeset
   115
size
2997
5254aeab421d comments
Claus Gittinger <cg@exept.de>
parents: 2446
diff changeset
   116
    "return the number of elements in the streamed collection."
5254aeab421d comments
Claus Gittinger <cg@exept.de>
parents: 2446
diff changeset
   117
1665
928e9a308cea added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents: 1488
diff changeset
   118
    ^ collection size
928e9a308cea added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents: 1488
diff changeset
   119
928e9a308cea added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents: 1488
diff changeset
   120
    "Created: 13.9.1996 / 18:14:35 / cg"
2997
5254aeab421d comments
Claus Gittinger <cg@exept.de>
parents: 2446
diff changeset
   121
    "Modified: 4.10.1997 / 17:59:45 / cg"
345
claus
parents: 329
diff changeset
   122
! !
claus
parents: 329
diff changeset
   123
59
4a86aad06603 *** empty log message ***
claus
parents: 13
diff changeset
   124
!ReadStream methodsFor:'reading'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   125
a27a279701f8 Initial revision
claus
parents:
diff changeset
   126
next
a27a279701f8 Initial revision
claus
parents:
diff changeset
   127
    "return the next element; advance read pointer.
2
claus
parents: 1
diff changeset
   128
     return nil, if there is no next element.
345
claus
parents: 329
diff changeset
   129
     - tuned for a bit more speed on String/ByteArray/Array-Streams"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   130
a27a279701f8 Initial revision
claus
parents:
diff changeset
   131
    |ret|
a27a279701f8 Initial revision
claus
parents:
diff changeset
   132
a27a279701f8 Initial revision
claus
parents:
diff changeset
   133
%{  /* NOCONTEXT */
a27a279701f8 Initial revision
claus
parents:
diff changeset
   134
a27a279701f8 Initial revision
claus
parents:
diff changeset
   135
    REGISTER int pos;
a27a279701f8 Initial revision
claus
parents:
diff changeset
   136
    unsigned ch;
252
   137
    OBJ coll, p, l;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   138
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   139
    coll = __INST(collection);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   140
    p = __INST(position);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   141
    l = __INST(readLimit);
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   142
252
   143
    if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) {
   144
8098
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   145
        pos = __intVal(p);
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   146
        /* make 1-based */
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   147
        pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   148
        if (pos > 0 && pos <= __intVal(l)) {
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   149
            OBJ cls, ret;
59
4a86aad06603 *** empty log message ***
claus
parents: 13
diff changeset
   150
8098
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   151
            cls = __qClass(coll);
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   152
            if (cls == @global(String)) {
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   153
                if (pos <= __stringSize(coll)) {
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   154
                    ch = __stringVal(coll)[pos-1];
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   155
                    ret = __MKCHARACTER(ch);
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   156
                    __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   157
                    RETURN ( ret );
8098
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   158
                }
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   159
            } else if (cls == @global(ByteArray)) {
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   160
                if (pos <= __byteArraySize(coll)) {
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   161
                    ch = __ByteArrayInstPtr(coll)->ba_element[pos-1];
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   162
                    ret = __mkSmallInteger(ch);
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   163
                    __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   164
                    RETURN ( ret );
8098
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   165
                }
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   166
            } else if (cls == @global(Unicode16String)) {
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   167
                if (pos <= __unicode16StringSize(coll)) {
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   168
                    ch = __Unicode16StringInstPtr(coll)->s_element[pos-1];
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   169
                    ret = __MKUCHARACTER(ch);
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   170
                    __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   171
                    RETURN ( ret );
8098
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   172
                }
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   173
            } else if (cls == @global(Array)) {
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   174
                if (pos <= __arraySize(coll)) {
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   175
                    ret = __ArrayInstPtr(coll)->a_element[pos-1];
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   176
                    __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   177
                    RETURN ( ret );
8098
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   178
                }
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   179
            }
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   180
        }
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   181
    }
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   182
%}.
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   183
    ((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ self pastEndRead].
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   184
    ret := collection at:(position + 1 - ZeroPosition).
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   185
    position := position + 1.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   186
    ^ ret
a27a279701f8 Initial revision
claus
parents:
diff changeset
   187
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   188
252
   189
nextAlphaNumericWord
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   190
    "read the next word (i.e. up to non letter-or-digit).
a27a279701f8 Initial revision
claus
parents:
diff changeset
   191
     return a string containing those characters.
3112
ccf7121ddae8 nextAlphaNumericWord changed
Claus Gittinger <cg@exept.de>
parents: 2997
diff changeset
   192
     Skips any non-alphanumeric chars first.
59
4a86aad06603 *** empty log message ***
claus
parents: 13
diff changeset
   193
     - tuned for speed on String-Streams for faster scanning"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   194
%{
a27a279701f8 Initial revision
claus
parents:
diff changeset
   195
    /* speedup, if collection is a string */
a27a279701f8 Initial revision
claus
parents:
diff changeset
   196
a27a279701f8 Initial revision
claus
parents:
diff changeset
   197
    int pos, limit, sz;
a27a279701f8 Initial revision
claus
parents:
diff changeset
   198
    int len;
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   199
    char buffer[256];
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   200
    REGISTER unsigned char *cp;
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   201
    REGISTER unsigned ch;
252
   202
    OBJ coll, p, l;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   203
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   204
    coll = __INST(collection);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   205
    p = __INST(position);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   206
    l = __INST(readLimit);
252
   207
    
   208
    if (__isString(coll) && __bothSmallInteger(p, l)) {
   209
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   210
	pos = __intVal(p);
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   211
	/* make 1-based */
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   212
	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   213
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   214
	limit = __intVal(l);
329
claus
parents: 252
diff changeset
   215
	sz = __qSize(coll) - OHDR_SIZE;
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   216
	if (sz < limit)
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   217
	    limit = sz; 
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   218
	cp = __stringVal(coll) + pos - 1;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   219
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   220
	for (;;) {
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   221
	    if (pos > limit) break;
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   222
	    ch = *cp;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   223
3112
ccf7121ddae8 nextAlphaNumericWord changed
Claus Gittinger <cg@exept.de>
parents: 2997
diff changeset
   224
	    if (((ch >= 'a') && (ch <= 'z')) ||
ccf7121ddae8 nextAlphaNumericWord changed
Claus Gittinger <cg@exept.de>
parents: 2997
diff changeset
   225
		((ch >= 'A') && (ch <= 'Z')) ||
ccf7121ddae8 nextAlphaNumericWord changed
Claus Gittinger <cg@exept.de>
parents: 2997
diff changeset
   226
		((ch >= '0') && (ch <= '9')))
ccf7121ddae8 nextAlphaNumericWord changed
Claus Gittinger <cg@exept.de>
parents: 2997
diff changeset
   227
		break;
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   228
	    cp++;
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   229
	    pos++;
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   230
	}
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   231
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   232
	len = 0;
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   233
	for (;;) {
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   234
	    if (pos > limit) break;
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   235
	    ch = *cp & 0xFF;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   236
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   237
	    if (! (((ch >= 'a') && (ch <= 'z')) ||
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   238
		   ((ch >= 'A') && (ch <= 'Z')) ||
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   239
		   ((ch >= '0') && (ch <= '9'))))
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   240
		break;
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   241
	    buffer[len++] = ch;
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   242
	    if (len >= (sizeof(buffer)-1)) {
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   243
		/* emergency */
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   244
		break;
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   245
	    }
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   246
	    pos++;
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   247
	    cp++;
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   248
	}
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   249
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   250
	/* make ZeroPosition-based */
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   251
	pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   252
	__INST(position) = __mkSmallInteger(pos);
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   253
	buffer[len] = '\0';
1688
8a42db1eea60 removed all COMMA_CON / CON_COMMA uses
Claus Gittinger <cg@exept.de>
parents: 1665
diff changeset
   254
	RETURN ( (len != 0) ? __MKSTRING_L(buffer, len) : nil );
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   255
    }
a27a279701f8 Initial revision
claus
parents:
diff changeset
   256
%}
a27a279701f8 Initial revision
claus
parents:
diff changeset
   257
.
252
   258
    ^ super nextAlphaNumericWord
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   259
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   260
1665
928e9a308cea added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents: 1488
diff changeset
   261
nextByte
4826
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   262
    "return the next element; advance read pointer.
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   263
     return nil, if there is no next element.
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   264
     - tuned for a bit more speed on String/ByteArray/Array-Streams"
1665
928e9a308cea added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents: 1488
diff changeset
   265
928e9a308cea added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents: 1488
diff changeset
   266
    |ret|
928e9a308cea added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents: 1488
diff changeset
   267
4826
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   268
%{  /* NOCONTEXT */
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   269
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   270
    REGISTER int pos;
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   271
    unsigned ch;
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   272
    OBJ coll, p, l;
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   273
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   274
    coll = __INST(collection);
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   275
    p = __INST(position);
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   276
    l = __INST(readLimit);
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   277
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   278
    if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) {
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   279
8098
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   280
        pos = __intVal(p);
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   281
        /* make 1-based */
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   282
        pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   283
        if (pos > 0 && pos <= __intVal(l)) {
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   284
            OBJ cls, ret;
4826
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   285
8098
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   286
            cls = __qClass(coll);
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   287
            if (cls == @global(String)) {
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   288
                if (pos <= __stringSize(coll)) {
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   289
                    ch = __stringVal(coll)[pos-1];
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   290
                    ret = __mkSmallInteger(ch);
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   291
                    __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   292
                    RETURN ( ret );
8098
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   293
                }
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   294
            } else if (cls == @global(ByteArray)) {
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   295
                if (pos <= __byteArraySize(coll)) {
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   296
                    ch = __ByteArrayInstPtr(coll)->ba_element[pos-1];
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   297
                    ret = __mkSmallInteger(ch);
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   298
                    __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   299
                    RETURN ( ret );
8098
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   300
                }
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   301
            } else if (cls == @global(Array)) {
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   302
                if (pos <= __arraySize(coll)) {
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   303
                    ret = __ArrayInstPtr(coll)->a_element[pos-1];
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   304
                    __INST(position) = __mkSmallInteger(__intVal(__INST(position)) + 1);
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   305
                    RETURN ( ret );
8098
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   306
                }
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   307
            }
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   308
        }
4826
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   309
    }
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   310
%}.
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   311
    ((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ self pastEndRead].
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   312
    ret := collection at:(position + 1 -ZeroPosition).
4826
ff30434f0feb #nextByte does return an integer, even when operating on a
Claus Gittinger <cg@exept.de>
parents: 4522
diff changeset
   313
    position := position + 1.
8098
8d1d5cd8529b nextByte should (at least) return integer when reading from a string-stream
Claus Gittinger <cg@exept.de>
parents: 7483
diff changeset
   314
    ^ ret asInteger
1665
928e9a308cea added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents: 1488
diff changeset
   315
!
928e9a308cea added ExternalStream compatibility protocol
Claus Gittinger <cg@exept.de>
parents: 1488
diff changeset
   316
8530
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   317
nextBytes:numBytes into:aCollection startingAt:initialIndex
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   318
    "return the next numBytes from the stream. If the end is
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   319
     reached before, only that many bytes are copyied into the
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   320
     collection.
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   321
     Returns the number of bytes that have been actually read.
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   322
     The receiver must support reading of binary bytes.
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   323
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   324
     Notice: this method is provided here for protocol completeness
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   325
             with externalStreams - it is normally not used with other
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   326
             streams."
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   327
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   328
    (initialIndex + numBytes - 1) <= aCollection size ifTrue:[
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   329
        (position + numBytes - 1) <= readLimit ifTrue:[
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   330
            collection class isBytes ifTrue:[
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   331
                aCollection class isBytes ifTrue:[ 
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   332
                    aCollection 
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   333
                        replaceBytesFrom:initialIndex to:(initialIndex + numBytes - 1) 
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   334
                        with:collection startingAt:position+1.
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   335
                    position := position + numBytes.
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   336
                    ^ numBytes
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   337
                ].
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   338
            ].
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   339
        ].
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   340
    ].
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   341
    ^ super nextBytes:numBytes into:aCollection startingAt:initialIndex
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   342
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   343
    "
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   344
     |s n buffer|
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   345
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   346
     buffer := ByteArray new:10.
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   347
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   348
     s := ReadStream on:#[1 2 3 4 5 6 7 8 9].
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   349
     s next:3.
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   350
     n := s nextBytes:9 into:buffer startingAt:1.
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   351
     Transcript showCR:('n = %1; buffer = <%2>' bindWith:n with:buffer)
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   352
    "
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   353
!
edcadeea17a1 tuned reading long strings & byteArrays from byteArray streams.
Claus Gittinger <cg@exept.de>
parents: 8140
diff changeset
   354
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   355
nextDecimalInteger
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   356
    "read the next integer in radix 10. dont skip whitespace.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   357
     - tuned for speed on String-Streams for faster scanning"
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   358
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   359
    |value nextOne|
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   360
%{
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   361
    int pos, limit, sz;
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   362
    REGISTER unsigned char *cp;
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   363
    REGISTER unsigned ch;
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   364
    int val = 0;
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   365
    OBJ coll, p, l;
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   366
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   367
    coll = __INST(collection);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   368
    p = __INST(position);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   369
    l = __INST(readLimit);
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   370
    
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   371
    if (__isString(coll) && __bothSmallInteger(p, l)) {
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   372
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   373
	pos = __intVal(p);
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   374
	/* make 1-based */
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   375
	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   376
	limit = __intVal(l);
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   377
	sz = __qSize(coll) - OHDR_SIZE;
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   378
	if (sz < limit)
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   379
	    limit = sz; 
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   380
	cp = __stringVal(coll) + pos - 1;
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   381
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   382
	for (;;) {
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   383
	    if (pos > limit) break;
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   384
	    ch = *cp;
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   385
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   386
	    if ((ch < '0') || (ch > '9')) break;
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   387
	    val = val * 10 + (ch - '0');
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   388
	    pos++;
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   389
	    if (val > (_MAX_INT / 10)) goto oops;
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   390
	    cp++;
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   391
	}
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   392
	/* make ZeroPosition-based */
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   393
	pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   394
	__INST(position) = __mkSmallInteger(pos);
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   395
	RETURN (__mkSmallInteger(val));
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   396
    }
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   397
oops:
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   398
    value = __mkSmallInteger(val);
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   399
%}
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   400
.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   401
    "fall-back for non-string streams - we have to continue where
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   402
     above primitive left off, in case of a large integer ...
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   403
     (instead of doing a super nextDecimalInteger)"
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   404
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   405
    nextOne := self peek.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   406
    [nextOne notNil and:[nextOne isDigitRadix:10]] whileTrue:[
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   407
	value = (value * 10) + nextOne digitValue.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   408
	nextOne := self nextPeek
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   409
    ].
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   410
    ^ value
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   411
!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   412
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   413
nextPeek
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   414
    "advance read pointer return the peek element.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   415
     this is equivalent to (self next; peek).
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   416
     - tuned for speed on String-Streams for faster scanning"
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   417
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   418
%{  /* NOCONTEXT */
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   419
    OBJ coll, l, p;
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   420
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   421
    coll = __INST(collection);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   422
    p = __INST(position);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   423
    l = __INST(readLimit);
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   424
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   425
    if (__isString(coll) && __bothSmallInteger(p, l)) {
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   426
        REGISTER int pos;
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   427
        unsigned ch;
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   428
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   429
        pos = __intVal(p);
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   430
        /* make 1-based */
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   431
        pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   432
        if ((pos > 0) && (pos < __intVal(l)) && (pos < __stringSize(coll))) {
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   433
            pos = pos + 1;
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   434
            ch = __stringVal(coll)[pos-1];
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   435
            /* make ZeroPosition-based */
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   436
            pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   437
            __INST(position) = __mkSmallInteger(pos);
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   438
            RETURN ( __MKCHARACTER(ch) );
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   439
        }
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   440
    }
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   441
%}.
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   442
    ((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ self pastEndRead].
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   443
    position := position + 1.
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   444
    ((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ self pastEndRead].
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   445
    ^ collection at:(position + 1 - ZeroPosition)
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   446
!
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   447
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   448
nextSymbol
a27a279701f8 Initial revision
claus
parents:
diff changeset
   449
    "read the next selector-symbol (i.e. up to non letter-or-digit).
a27a279701f8 Initial revision
claus
parents:
diff changeset
   450
     return a string containing those characters.
59
4a86aad06603 *** empty log message ***
claus
parents: 13
diff changeset
   451
     - tuned for speed on String-Streams for faster scanning"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   452
%{
a27a279701f8 Initial revision
claus
parents:
diff changeset
   453
    int pos, limit, sz;
a27a279701f8 Initial revision
claus
parents:
diff changeset
   454
    int len;
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   455
    char buffer[256];
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   456
    REGISTER unsigned char *cp;
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   457
    REGISTER unsigned ch;
252
   458
    OBJ coll, p, l;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   459
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   460
    coll = __INST(collection);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   461
    p = __INST(position);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   462
    l = __INST(readLimit);
252
   463
   464
    if (__isString(coll) && __bothSmallInteger(p, l)) {
   465
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   466
	pos = __intVal(p);
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   467
	/* make 1-based */
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   468
	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   469
	limit = __intVal(l);
329
claus
parents: 252
diff changeset
   470
	sz = __qSize(coll) - OHDR_SIZE;
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   471
	if (sz < limit)
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   472
	    limit = sz; 
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   473
	cp = __stringVal(coll) + pos - 1;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   474
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   475
	len = 0;
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   476
	for (;;) {
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   477
	    if (pos > limit) break;
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   478
	    ch = *cp;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   479
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   480
	    if (! (((ch >= 'a') && (ch <= 'z')) ||
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   481
		   ((ch >= 'A') && (ch <= 'Z')) ||
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   482
		   ((ch >= '0') && (ch <= '9')) ||
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   483
		   (ch == ':')))
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   484
		break;
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   485
	    buffer[len++] = ch;
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   486
	    if (len >= (sizeof(buffer)-1)) {
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   487
		/* emergency */
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   488
		break;
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   489
	    }
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   490
	    pos++;
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   491
	    cp++;
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   492
	}
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   493
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   494
	/* make ZeroPosition-based */
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   495
	pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   496
	__INST(position) = __mkSmallInteger(pos);
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   497
	buffer[len] = '\0';
1688
8a42db1eea60 removed all COMMA_CON / CON_COMMA uses
Claus Gittinger <cg@exept.de>
parents: 1665
diff changeset
   498
	RETURN ( (len != 0) ? __MKSTRING_L(buffer, len) : nil );
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   499
    }
a27a279701f8 Initial revision
claus
parents:
diff changeset
   500
%}
a27a279701f8 Initial revision
claus
parents:
diff changeset
   501
.
a27a279701f8 Initial revision
claus
parents:
diff changeset
   502
    ^ super nextSymbol
a27a279701f8 Initial revision
claus
parents:
diff changeset
   503
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   504
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   505
peek
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   506
    "return the next element; do NOT advance read pointer.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   507
     return nil, if there is no next element.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   508
     - tuned for a bit more speed on String/ByteArray/Array-Streams"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   509
a27a279701f8 Initial revision
claus
parents:
diff changeset
   510
%{  /* NOCONTEXT */
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   511
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   512
    REGISTER int pos;
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   513
    unsigned ch;
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   514
    OBJ coll;
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   515
    OBJ cls, p, l;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   516
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   517
    coll = __INST(collection);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   518
    p = __INST(position);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   519
    l = __INST(readLimit);
252
   520
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   521
    if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) {
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   522
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   523
        pos = __intVal(p);
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   524
        /* make 1-based */
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   525
        pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   526
        if (pos <= __intVal(l) && pos > 0) {
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   527
            cls = __qClass(coll);
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   528
            if (cls == @global(String)) {
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   529
                if (pos <= __stringSize(coll)) {
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   530
                    ch = __stringVal(coll)[pos-1];
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   531
                    RETURN ( __MKCHARACTER(ch) );
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   532
                }
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   533
            } else if (cls == @global(ByteArray)) {
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   534
                if (pos <= __byteArraySize(coll)) {
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   535
                    ch = __ByteArrayInstPtr(coll)->ba_element[pos-1];
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   536
                    RETURN ( __mkSmallInteger(ch) );
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   537
                }
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   538
            } else if (cls == @global(Array)) {
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   539
                if (pos <= __arraySize(coll)) {
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   540
                    RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]);
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   541
                }
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   542
            }
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   543
        }
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   544
    }
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   545
%}.
8614
88421fbd4ab6 pastEnd -> pastEndRead
Claus Gittinger <cg@exept.de>
parents: 8530
diff changeset
   546
    ((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ self pastEndRead].
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   547
    ^ collection at:(position + 1 - ZeroPosition)
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   548
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   549
3316
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   550
peekOrNil
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   551
    "return the next element; do NOT advance read pointer.
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   552
     return nil, if there is no next element.
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   553
     This is much like #peek -
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   554
     However, unlike #peek, this does not raise an atEnd-query signal - even
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   555
     if handled. Instead, nil is returned immediately."
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   556
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   557
%{  /* NOCONTEXT */
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   558
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   559
    REGISTER int pos;
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   560
    unsigned ch;
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   561
    OBJ coll;
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   562
    OBJ cls, p, l;
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   563
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   564
    coll = __INST(collection);
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   565
    p = __INST(position);
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   566
    l = __INST(readLimit);
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   567
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   568
    if (__isNonNilObject(coll) && __bothSmallInteger(p, l)) {
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   569
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   570
	pos = __intVal(p);
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   571
	/* make 1-based */
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   572
	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   573
	if (pos <= __intVal(l) && pos > 0) {
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   574
	    cls = __qClass(coll);
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   575
	    if (cls == @global(String)) {
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   576
		if (pos <= __stringSize(coll)) {
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   577
		    ch = __stringVal(coll)[pos-1];
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   578
		    RETURN ( __MKCHARACTER(ch) );
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   579
		}
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   580
		RETURN ( nil );
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   581
	    } else if (cls == @global(ByteArray)) {
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   582
		if (pos <= __byteArraySize(coll)) {
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   583
		    ch = __ByteArrayInstPtr(coll)->ba_element[pos-1];
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   584
		    RETURN ( __mkSmallInteger(ch) );
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   585
		}
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   586
		RETURN ( nil );
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   587
	    } else if (cls == @global(Array)) {
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   588
		if (pos <= __arraySize(coll)) {
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   589
		    RETURN ( __ArrayInstPtr(coll)->a_element[pos-1]);
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   590
		}
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   591
		RETURN ( nil );
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   592
	    }
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   593
	}
3316
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   594
    }
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   595
%}.
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   596
    ((position + 1 - ZeroPosition) > readLimit) ifTrue:[^ nil].
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   597
    ^ collection at:(position + 1 - ZeroPosition)
3316
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   598
!
ba0a4305f900 added #peekOrNil
Claus Gittinger <cg@exept.de>
parents: 3112
diff changeset
   599
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   600
skipSeparators
a27a279701f8 Initial revision
claus
parents:
diff changeset
   601
    "skip all whitespace; next will return next non-white-space element.
59
4a86aad06603 *** empty log message ***
claus
parents: 13
diff changeset
   602
     Return the peeked character or nil, if the end-of-stream was reached.
2
claus
parents: 1
diff changeset
   603
     - reimplemented for speed on String-Streams for faster scanning"
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   604
a27a279701f8 Initial revision
claus
parents:
diff changeset
   605
%{  /* NOCONTEXT */
252
   606
    OBJ coll, p, l;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   607
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   608
    coll = __INST(collection);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   609
    p = __INST(position);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   610
    l = __INST(readLimit);
252
   611
   612
    if (__isString(coll) && __bothSmallInteger(p, l)) {
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   613
	REGISTER unsigned char *chars;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   614
	REGISTER unsigned ch;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   615
	REGISTER int pos;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   616
	int limit;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   617
	int sz;
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   618
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   619
	pos = __intVal(p);
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   620
	/* make 1-based */
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   621
	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   622
	if (pos <= 0) {
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   623
	    RETURN ( nil );
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   624
	}
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   625
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   626
	limit = __intVal(l);
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   627
	sz = __qSize(coll) - OHDR_SIZE;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   628
	if (limit > sz) {
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   629
	    limit = sz;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   630
	}
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   631
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   632
	chars = (unsigned char *)(__stringVal(coll) + pos - 1);
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   633
	while (pos <= limit) {
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   634
	    pos++;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   635
	    ch = *chars++;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   636
	    if ((ch > 0x20)
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   637
	     || ((ch != ' ')
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   638
		 && (ch != '\t')
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   639
		 && (ch != '\r')
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   640
		 && (ch != '\n')
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   641
		 && (ch != '\f')
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   642
		 && (ch != 0x0B))) {
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   643
		/* make ZeroPosition-based */
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   644
		pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   645
		__INST(position) = __mkSmallInteger(pos-1);
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   646
		RETURN ( __MKCHARACTER(ch) );
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   647
	    }
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   648
	}
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   649
	/* make ZeroPosition-based */
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   650
	pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   651
	__INST(position) = __mkSmallInteger(pos);
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   652
	RETURN ( nil );
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   653
    }
1406
fd7eed0e648f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   654
%}.
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   655
    ^ super skipSeparators
1406
fd7eed0e648f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   656
fd7eed0e648f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   657
    "
fd7eed0e648f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   658
     |s|
fd7eed0e648f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   659
fd7eed0e648f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   660
     s := '     hello     world    ' readStream.
fd7eed0e648f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   661
     s skipSeparators.
fd7eed0e648f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   662
     s next.
fd7eed0e648f checkin from browser
Claus Gittinger <cg@exept.de>
parents: 1400
diff changeset
   663
    "
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   664
!
a27a279701f8 Initial revision
claus
parents:
diff changeset
   665
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   666
skipSeparatorsExceptCR
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   667
    "skip all whitespace except newlines;
202
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   668
     next will return next non-white-space element.
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   669
     - reimplemented for speed on String-Streams for faster scanning"
40ca7cc6fb9c *** empty log message ***
claus
parents: 93
diff changeset
   670
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   671
%{  /* NOCONTEXT */
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   672
252
   673
    OBJ coll, p, l;
   674
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   675
    coll = __INST(collection);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   676
    p = __INST(position);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   677
    l = __INST(readLimit);
252
   678
   679
    if (__isString(coll) && __bothSmallInteger(p, l)) {
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   680
	REGISTER unsigned char *chars;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   681
	REGISTER unsigned ch;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   682
	REGISTER int pos;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   683
	int limit;
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   684
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   685
	pos = __intVal(p);
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   686
	/* make 1-based */
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   687
	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   688
	if (pos <= 0) {
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   689
	    RETURN ( nil );
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   690
	}
2162
7e1601f633b6 ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents: 1688
diff changeset
   691
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   692
	limit = __intVal(l);
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   693
	if (limit > (__qSize(coll) - OHDR_SIZE))
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   694
	    limit = __qSize(coll) - OHDR_SIZE;
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   695
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   696
	chars = (unsigned char *)(__stringVal(coll) + pos - 1);
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   697
	while (pos <= limit) {
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   698
	    ch = *chars++;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   699
	    if (((int)ch > 0x20)
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   700
	     || (
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   701
		 (ch != ' ') 
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   702
		 && (ch != '\t')
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   703
		 && (ch != '\f')
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   704
		 && (ch != '\b')
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   705
		 && (ch != 0x0B))) {
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   706
		/* make ZeroPosition-based */
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   707
		pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   708
		__INST(position) = __mkSmallInteger(pos);
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   709
		RETURN ( __MKCHARACTER(ch) );
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   710
	    }
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   711
	    pos++;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   712
	}
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   713
	/* make ZeroPosition-based */
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   714
	pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   715
	__INST(position) = __mkSmallInteger(pos);
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   716
	RETURN ( nil );
13
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   717
    }
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   718
%}
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   719
.
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   720
    ^ super skipSeparatorsExceptCR
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   721
!
62303f84ff5f *** empty log message ***
claus
parents: 10
diff changeset
   722
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   723
skipThrough:anObject
6011
3b91d7834ca5 fixed: skipThrough dod not position atEnd if the element was not found.
Claus Gittinger <cg@exept.de>
parents: 5810
diff changeset
   724
    "skip all objects up-to and including anObject.
3b91d7834ca5 fixed: skipThrough dod not position atEnd if the element was not found.
Claus Gittinger <cg@exept.de>
parents: 5810
diff changeset
   725
     Return the receiver if skip was successful, 
3b91d7834ca5 fixed: skipThrough dod not position atEnd if the element was not found.
Claus Gittinger <cg@exept.de>
parents: 5810
diff changeset
   726
     otherwise (i.e. if not found) return nil and leave the stream positioned at the end.
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   727
     On success, the next read operation will return the element after anObject.
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   728
     - reimplemented for speed on String-Streams for faster scanning"
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   729
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   730
%{  /* NOCONTEXT */
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   731
    OBJ coll, p, l;
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   732
1133
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   733
    coll = __INST(collection);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   734
    p = __INST(position);
961f2b095c22 underline cleanup
Claus Gittinger <cg@exept.de>
parents: 970
diff changeset
   735
    l = __INST(readLimit);
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   736
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   737
    if (__isString(coll)
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   738
     && __isCharacter(anObject)
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   739
     && __bothSmallInteger(p, l)) {
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   740
	REGISTER unsigned char *chars;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   741
	REGISTER int pos, limit;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   742
	unsigned ch;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   743
	int sz;
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   744
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   745
	pos = __intVal(p);
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   746
	/* make 1-based */
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   747
	pos = pos + 1 - __intVal( @global(PositionableStream:ZeroPosition));
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   748
	if (pos <= 0) {
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   749
	    RETURN ( nil );
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   750
	}
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   751
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   752
	limit = __intVal(l);
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   753
	sz = __stringSize(coll);
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   754
	if (limit > sz) limit = sz;
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   755
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   756
	chars = (unsigned char *)(__stringVal(coll) + pos - 1);
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   757
	ch = __intVal(_characterVal(anObject)) & 0xFF;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   758
	while (pos < limit) {
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   759
	    if (*chars == ch) {
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   760
		ch = *++chars;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   761
		pos++;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   762
		/* make ZeroPosition-based */
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   763
		pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   764
		__INST(position) = __mkSmallInteger(pos);
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   765
		RETURN ( self );
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   766
	    }
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   767
	    chars++;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   768
	    pos++;
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   769
	}
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   770
	/* make ZeroPosition-based */
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   771
	pos = pos - 1 + __intVal( @global(PositionableStream:ZeroPosition));
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   772
	__INST(position) = __mkSmallInteger(pos+1);
7051
984d8271d06b preps for 0-based stream positions
Claus Gittinger <cg@exept.de>
parents: 6814
diff changeset
   773
	RETURN ( nil );
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   774
    }
6011
3b91d7834ca5 fixed: skipThrough dod not position atEnd if the element was not found.
Claus Gittinger <cg@exept.de>
parents: 5810
diff changeset
   775
%}.
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   776
    ^ super skipThrough:anObject
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   777
! !
a27a279701f8 Initial revision
claus
parents:
diff changeset
   778
59
4a86aad06603 *** empty log message ***
claus
parents: 13
diff changeset
   779
!ReadStream methodsFor:'writing'!
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   780
a27a279701f8 Initial revision
claus
parents:
diff changeset
   781
nextPut:anElement
10
claus
parents: 5
diff changeset
   782
    "catch write access to readstreams - report an error"
claus
parents: 5
diff changeset
   783
claus
parents: 5
diff changeset
   784
    self shouldNotImplement
1
a27a279701f8 Initial revision
claus
parents:
diff changeset
   785
! !
611
80bb0f1a7bab checkin from browser
Claus Gittinger <cg@exept.de>
parents: 546
diff changeset
   786
2162
7e1601f633b6 ff is a separator too (in #skipSeparators*)
Claus Gittinger <cg@exept.de>
parents: 1688
diff changeset
   787
!ReadStream class methodsFor:'documentation'!
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 611
diff changeset
   788
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 611
diff changeset
   789
version
8913
b9498d27a554 64bit; mkSmallInteger
Claus Gittinger <cg@exept.de>
parents: 8614
diff changeset
   790
    ^ '$Header: /cvs/stx/stx/libbasic/ReadStream.st,v 1.53 2005-07-08 17:15:02 cg Exp $'
701
a309e3ef7faf checkin from browser
Claus Gittinger <cg@exept.de>
parents: 611
diff changeset
   791
! !