@@ -39,32 +39,6 @@ function Base.length(buf::Buffer)
39
39
return length (buf. data)
40
40
end
41
41
42
- function Base. endof (buf:: Buffer )
43
- return buffersize (buf)
44
- end
45
-
46
- function Base. getindex (buf:: Buffer , i:: Integer )
47
- @boundscheck checkbounds (buf, i)
48
- @inbounds return buf. data[i+ buf. bufferpos- 1 ]
49
- end
50
-
51
- function Base. checkbounds (buf:: Buffer , i:: Integer )
52
- if ! (1 ≤ i ≤ endof (buf))
53
- throw (BoundsError (buf, i))
54
- end
55
- end
56
-
57
- function Base. getindex (buf:: Buffer , r:: UnitRange{<:Integer} )
58
- @boundscheck checkbounds (buf, r)
59
- @inbounds return buf. data[r+ buf. bufferpos- 1 ]
60
- end
61
-
62
- function Base. checkbounds (buf:: Buffer , r:: UnitRange{<:Integer} )
63
- if ! isempty (r) && ! (1 ≤ first (r) && last (r) ≤ endof (buf))
64
- throw (BoundsError (buf, r))
65
- end
66
- end
67
-
68
42
function bufferptr (buf:: Buffer )
69
43
return pointer (buf. data, buf. bufferpos)
70
44
end
@@ -77,42 +51,6 @@ function buffermem(buf::Buffer)
77
51
return Memory (bufferptr (buf), buffersize (buf))
78
52
end
79
53
80
- # Notify that `n` bytes are consumed from `buf`.
81
- function consumed! (buf:: Buffer , n:: Integer )
82
- buf. bufferpos += n
83
- return buf
84
- end
85
-
86
- # Notify that `n` bytes are supplied to `buf`.
87
- function supplied! (buf:: Buffer , n:: Integer )
88
- buf. marginpos += n
89
- return buf
90
- end
91
-
92
- function consumed2! (buf:: Buffer , n:: Integer )
93
- buf. bufferpos += n
94
- buf. total += n
95
- return buf
96
- end
97
-
98
- function supplied2! (buf:: Buffer , n:: Integer )
99
- buf. marginpos += n
100
- buf. total += n
101
- return buf
102
- end
103
-
104
- function readbyte! (buf:: Buffer )
105
- b = buf. data[buf. bufferpos]
106
- consumed! (buf, 1 )
107
- return b
108
- end
109
-
110
- function writebyte! (buf:: Buffer , b:: UInt8 )
111
- buf. data[buf. marginpos] = b
112
- supplied! (buf, 1 )
113
- return 1
114
- end
115
-
116
54
function marginptr (buf:: Buffer )
117
55
return pointer (buf. data, buf. marginpos)
118
56
end
@@ -149,6 +87,44 @@ function reset!(buf::Buffer)
149
87
return buf. bufferpos
150
88
end
151
89
90
+ # Notify that `n` bytes are consumed from `buf`.
91
+ function consumed! (buf:: Buffer , n:: Integer )
92
+ buf. bufferpos += n
93
+ return buf
94
+ end
95
+
96
+ # Notify that `n` bytes are supplied to `buf`.
97
+ function supplied! (buf:: Buffer , n:: Integer )
98
+ buf. marginpos += n
99
+ return buf
100
+ end
101
+
102
+ function consumed2! (buf:: Buffer , n:: Integer )
103
+ buf. bufferpos += n
104
+ buf. total += n
105
+ return buf
106
+ end
107
+
108
+ function supplied2! (buf:: Buffer , n:: Integer )
109
+ buf. marginpos += n
110
+ buf. total += n
111
+ return buf
112
+ end
113
+
114
+ # Discard buffered data and initialize positions.
115
+ function initbuffer! (buf:: Buffer )
116
+ buf. markpos = 0
117
+ buf. bufferpos = buf. marginpos = 1
118
+ buf. total = 0
119
+ return buf
120
+ end
121
+
122
+ # Remove all buffered data.
123
+ function emptybuffer! (buf:: Buffer )
124
+ buf. marginpos = buf. bufferpos
125
+ return buf
126
+ end
127
+
152
128
# Make margin with ≥`minsize` and return the size of it.
153
129
function makemargin! (buf:: Buffer , minsize:: Integer )
154
130
@assert minsize ≥ 0
@@ -165,11 +141,12 @@ function makemargin!(buf::Buffer, minsize::Integer)
165
141
datasize = buf. marginpos - buf. markpos
166
142
end
167
143
copy! (buf. data, 1 , buf. data, datapos, datasize)
144
+ shift = datapos - 1
168
145
if buf. markpos > 0
169
- buf. markpos -= datapos - 1
146
+ buf. markpos -= shift
170
147
end
171
- buf. bufferpos -= datapos - 1
172
- buf. marginpos -= datapos - 1
148
+ buf. bufferpos -= shift
149
+ buf. marginpos -= shift
173
150
end
174
151
if marginsize (buf) < minsize
175
152
# expand data buffer
@@ -179,10 +156,18 @@ function makemargin!(buf::Buffer, minsize::Integer)
179
156
return marginsize (buf)
180
157
end
181
158
182
- # Remove all buffered data.
183
- function emptybuffer! (buf:: Buffer )
184
- buf. marginpos = buf. bufferpos
185
- return buf
159
+ # Read a byte.
160
+ function readbyte! (buf:: Buffer )
161
+ b = buf. data[buf. bufferpos]
162
+ consumed! (buf, 1 )
163
+ return b
164
+ end
165
+
166
+ # Write a byte.
167
+ function writebyte! (buf:: Buffer , b:: UInt8 )
168
+ buf. data[buf. marginpos] = b
169
+ supplied! (buf, 1 )
170
+ return 1
186
171
end
187
172
188
173
# Skip `n` bytes in the buffer.
@@ -192,14 +177,6 @@ function skipbuffer!(buf::Buffer, n::Integer)
192
177
return buf
193
178
end
194
179
195
- # Discard buffered data and initialize positions.
196
- function initbuffer! (buf:: Buffer )
197
- buf. markpos = 0
198
- buf. bufferpos = buf. marginpos = 1
199
- buf. total = 0
200
- return buf
201
- end
202
-
203
180
# Take the ownership of the marked data.
204
181
function takemarked! (buf:: Buffer )
205
182
@assert buf. markpos > 0
0 commit comments