1
0
mirror of https://github.com/moparisthebest/spdylay synced 2024-12-21 23:28:52 -05:00

Add spdylay_buffer_reader_count to replace bunch of 1 byte read

This commit is contained in:
Tatsuhiro Tsujikawa 2013-01-22 00:12:15 +09:00
parent 29bec93eb9
commit 0649a7ea61
3 changed files with 33 additions and 6 deletions

View File

@ -201,6 +201,31 @@ void spdylay_buffer_reader_data(spdylay_buffer_reader *reader,
}
}
int spdylay_buffer_reader_count(spdylay_buffer_reader *reader,
size_t len, uint8_t c)
{
int res = 0;
while(len) {
size_t remlen, readlen, i;
uint8_t *p;
remlen = reader->buffer->capacity - reader->offset;
readlen = spdylay_min(remlen, len);
p = reader->current->data + reader->offset;
for(i = 0; i < readlen; ++i) {
if(p[i] == c) {
++res;
}
}
len -= readlen;
reader->offset += readlen;
if(reader->buffer->capacity == reader->offset) {
reader->current = reader->current->next;
reader->offset = 0;
}
}
return res;
}
void spdylay_buffer_reader_advance(spdylay_buffer_reader *reader,
size_t amount)
{

View File

@ -154,6 +154,13 @@ uint32_t spdylay_buffer_reader_uint32(spdylay_buffer_reader *reader);
void spdylay_buffer_reader_data(spdylay_buffer_reader *reader,
uint8_t *out, size_t len);
/**
* Reads |len| bytes and count the occurrence of |c| there and return
* it. This function will advance the current position by |len|.
*/
int spdylay_buffer_reader_count(spdylay_buffer_reader *reader,
size_t len, uint8_t c);
/*
* Advances the current position by |amount|.
*/

View File

@ -146,12 +146,7 @@ int spdylay_frame_count_unpack_nv_space(size_t *nvlen_ptr, size_t *buflen_ptr,
spdylay_buffer_reader_advance(&reader, len);
}
}
for(j = off, off -= len; off != j; ++off) {
uint8_t b = spdylay_buffer_reader_uint8(&reader);
if(b == '\0') {
++nvlen;
}
}
nvlen += spdylay_buffer_reader_count(&reader, len, '\0');
++nvlen;
}
if(inlen == off) {