@@ -77,16 +77,27 @@ function buffermem(buf::Buffer)
77
77
return Memory (bufferptr (buf), buffersize (buf))
78
78
end
79
79
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
+
80
92
function readbyte! (buf:: Buffer )
81
93
b = buf. data[buf. bufferpos]
82
- buf. bufferpos += 1
94
+ consumed! ( buf, 1 )
83
95
return b
84
96
end
85
97
86
98
function writebyte! (buf:: Buffer , b:: UInt8 )
87
99
buf. data[buf. marginpos] = b
88
- buf. marginpos += 1
89
- buf. total += 1
100
+ supplied! (buf, 1 )
90
101
return 1
91
102
end
92
103
@@ -102,6 +113,10 @@ function marginmem(buf::Buffer)
102
113
return Memory (marginptr (buf), marginsize (buf))
103
114
end
104
115
116
+ function ismarked (buf:: Buffer )
117
+ return buf. markpos != 0
118
+ end
119
+
105
120
function mark! (buf:: Buffer )
106
121
return buf. markpos = buf. bufferpos
107
122
end
@@ -160,11 +175,12 @@ function emptybuffer!(buf::Buffer)
160
175
return buf
161
176
end
162
177
178
+ # Skip `n` bytes in the buffer.
163
179
function skipbuffer! (buf:: Buffer , n:: Integer )
164
180
if n > buffersize (buf)
165
181
throw (ArgumentError (" too large skip size" ))
166
182
end
167
- buf. bufferpos += n
183
+ consumed! ( buf, n)
168
184
return buf
169
185
end
170
186
@@ -176,12 +192,6 @@ function initbuffer!(buf::Buffer)
176
192
return buf
177
193
end
178
194
179
- # Copy marked data.
180
- function copymarked (buf:: Buffer )
181
- @assert buf. markpos > 0
182
- return buf. data[buf. markpos: buf. marginpos- 1 ]
183
- end
184
-
185
195
# Take the ownership of the marked data.
186
196
function takemarked! (buf:: Buffer )
187
197
@assert buf. markpos > 0
@@ -191,52 +201,43 @@ function takemarked!(buf::Buffer)
191
201
return resize! (buf. data, sz)
192
202
end
193
203
194
- # Insert data to the current buffer .
195
- function insertdata ! (buf:: Buffer , data:: Ptr{UInt8} , nbytes:: Integer )
204
+ # Copy data from `data` to `buf` .
205
+ function copydata ! (buf:: Buffer , data:: Ptr{UInt8} , nbytes:: Integer )
196
206
makemargin! (buf, nbytes)
197
- copy! (buf. data, buf. bufferpos + nbytes, buf. data, buf. bufferpos, buffersize (buf))
198
- unsafe_copy! (bufferptr (buf), data, nbytes)
199
- buf. marginpos += nbytes
207
+ unsafe_copy! (marginptr (buf), data, nbytes)
208
+ supplied! (buf, nbytes)
200
209
return buf
201
210
end
202
211
203
- # Read as much data as possbile from `input` to the margin of `output`.
204
- # This function will not block if `input` has buffered data.
205
- function readdata! (input:: IO , output:: Buffer )
206
- nread:: Int = 0
207
- navail = nb_available (input)
208
- if navail == 0 && marginsize (output) > 0 && ! eof (input)
209
- nread += writebyte! (output, read (input, UInt8))
210
- navail = nb_available (input)
211
- end
212
- n = min (navail, marginsize (output))
213
- Base. unsafe_read (input, marginptr (output), n)
214
- output. marginpos += n
215
- nread += n
216
- output. total += nread
217
- return nread
212
+ # Copy data from `buf` to `data`.
213
+ function copydata! (data:: Ptr{UInt8} , buf:: Buffer , nbytes:: Integer )
214
+ # NOTE: It's caller's responsibility to ensure that the buffer has at least
215
+ # nbytes.
216
+ @assert buffersize (buf) ≥ nbytes
217
+ unsafe_copy! (data, bufferptr (buf), nbytes)
218
+ consumed! (buf, nbytes)
219
+ return data
218
220
end
219
221
220
- # Read data from `buf` to `dst`.
221
- function readdata! (buf:: Buffer , dst:: Vector{UInt8} , dpos:: Integer , sz:: Integer )
222
- copy! (dst, dpos, buf. data, buf. bufferpos, sz)
223
- buf. bufferpos += sz
224
- return dst
225
- end
226
-
227
- # Write all data to `output` from the buffer of `input`.
228
- function writebuffer! (output:: IO , input:: Buffer )
229
- while buffersize (input) > 0
230
- input. bufferpos += Base. unsafe_write (output, bufferptr (input), buffersize (input))
231
- end
222
+ # Insert data to the current buffer.
223
+ function insertdata! (buf:: Buffer , data:: Ptr{UInt8} , nbytes:: Integer )
224
+ makemargin! (buf, nbytes)
225
+ copy! (buf. data, buf. bufferpos + nbytes, buf. data, buf. bufferpos, buffersize (buf))
226
+ unsafe_copy! (bufferptr (buf), data, nbytes)
227
+ supplied! (buf, nbytes)
228
+ return buf
232
229
end
233
230
234
231
# Find the first occurrence of a specific byte.
235
232
function findbyte (buf:: Buffer , byte:: UInt8 )
236
- ptr = ccall (:memchr , Ptr{Void}, (Ptr{Void}, Cint, Csize_t), pointer (buf. data, buf. bufferpos), byte, buffersize (buf))
237
- if ptr == C_NULL
238
- return 0
233
+ p = ccall (
234
+ :memchr ,
235
+ Ptr{UInt8},
236
+ (Ptr{UInt8}, Cint, Csize_t),
237
+ pointer (buf. data, buf. bufferpos), byte, buffersize (buf))
238
+ if p == C_NULL
239
+ return marginptr (buf)
239
240
else
240
- return Int (ptr - pointer (buf . data, buf . bufferpos)) + buf . bufferpos
241
+ return p
241
242
end
242
243
end
0 commit comments