mirror of
https://github.com/hrydgard/ppsspp.git
synced 2024-11-23 05:19:56 +00:00
Buffer: Optimize scanning for the next crlf
This commit is contained in:
parent
129c64c6b8
commit
305453b52d
@ -95,12 +95,12 @@ int Buffer::SkipLineCRLF() {
|
|||||||
|
|
||||||
// This relies on having buffered data!
|
// This relies on having buffered data!
|
||||||
int Buffer::OffsetToAfterNextCRLF() {
|
int Buffer::OffsetToAfterNextCRLF() {
|
||||||
for (int i = 0; i < (int)data_.size() - 1; i++) {
|
int offset = data_.next_crlf_offset();
|
||||||
if (data_.peek(i) == '\r' && data_.peek(i + 1) == '\n') {
|
if (offset >= 0) {
|
||||||
return i + 2;
|
return offset + 2;
|
||||||
}
|
} else {
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buffer::Printf(const char *fmt, ...) {
|
void Buffer::Printf(const char *fmt, ...) {
|
||||||
|
@ -186,6 +186,27 @@ public:
|
|||||||
head_ = b;
|
head_ = b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If return value is negative, one wasn't found.
|
||||||
|
int next_crlf_offset() {
|
||||||
|
int offset = 0;
|
||||||
|
Block *b = head_;
|
||||||
|
do {
|
||||||
|
int remain = b->tail - b->head;
|
||||||
|
for (int i = 0; i < remain; i++) {
|
||||||
|
if (b->data[b->head + i] == '\r') {
|
||||||
|
// Use peek to avoid handling edge cases.
|
||||||
|
if (peek(offset + i + 1) == '\n') {
|
||||||
|
return offset + i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
offset += remain;
|
||||||
|
b = b->next;
|
||||||
|
} while (b);
|
||||||
|
// Ran out of data.
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Block {
|
struct Block {
|
||||||
~Block() {
|
~Block() {
|
||||||
|
@ -15,6 +15,10 @@
|
|||||||
#define MSG_NOSIGNAL 0x00
|
#define MSG_NOSIGNAL 0x00
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if _MSC_VER
|
||||||
|
#pragma warning(disable:4267)
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "Common/File/FileDescriptor.h"
|
#include "Common/File/FileDescriptor.h"
|
||||||
#include "Common/Log.h"
|
#include "Common/Log.h"
|
||||||
#include "Common/Net/NetBuffer.h"
|
#include "Common/Net/NetBuffer.h"
|
||||||
|
@ -1076,6 +1076,10 @@ bool TestCharQueue() {
|
|||||||
queue.pop_front_bulk(dest, 4);
|
queue.pop_front_bulk(dest, 4);
|
||||||
EXPECT_EQ_MEM(dest, "opqr", 4);
|
EXPECT_EQ_MEM(dest, "opqr", 4);
|
||||||
EXPECT_TRUE(queue.empty());
|
EXPECT_TRUE(queue.empty());
|
||||||
|
queue.push_back("asdf");
|
||||||
|
EXPECT_EQ_INT(queue.next_crlf_offset(), -1);
|
||||||
|
queue.push_back("\r\r\n");
|
||||||
|
EXPECT_EQ_INT(queue.next_crlf_offset(), 5);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user