Lukhnos: Optimized KeyValueBlobReader.
This commit is contained in:
parent
a713cb324c
commit
8b9e5ca57e
|
@ -10,13 +10,13 @@
|
|||
|
||||
namespace vChewing {
|
||||
|
||||
KeyValueBlobReader::State KeyValueBlobReader::Next(KeyValue* out) {
|
||||
KeyValueBlobReader::State KeyValueBlobReader::Next(KeyValue* out)
|
||||
{
|
||||
static auto new_line = [](char c) { return c == '\n' || c == '\r'; };
|
||||
static auto blank = [](char c) { return c == ' ' || c == '\t'; };
|
||||
static auto blank_or_newline = [](char c) { return blank(c) || new_line(c); };
|
||||
static auto content_char = [](char c) {
|
||||
return !blank(c) && !new_line(c);
|
||||
};
|
||||
static auto blank_or_newline
|
||||
= [](char c) { return blank(c) || new_line(c); };
|
||||
static auto content_char = [](char c) { return !blank(c) && !new_line(c); };
|
||||
|
||||
if (state_ == State::ERROR) {
|
||||
return state_;
|
||||
|
@ -80,21 +80,21 @@ KeyValueBlobReader::State KeyValueBlobReader::Next(KeyValue* out) {
|
|||
SkipUntil(new_line);
|
||||
|
||||
if (out != nullptr) {
|
||||
*out = KeyValue{
|
||||
std::string_view{key_begin, key_length},
|
||||
std::string_view{value_begin, value_length}};
|
||||
*out = KeyValue { std::string_view { key_begin, key_length },
|
||||
std::string_view { value_begin, value_length } };
|
||||
}
|
||||
state_ = State::HAS_PAIR;
|
||||
return state_;
|
||||
|
||||
error:
|
||||
state_ = State::ERROR;
|
||||
return State::ERROR;
|
||||
return state_;
|
||||
}
|
||||
|
||||
KeyValueBlobReader::State KeyValueBlobReader::SkipUntilNot(
|
||||
const std::function<bool(char)>& f) {
|
||||
while (current_ != end_ &&* current_) {
|
||||
const std::function<bool(char)>& f)
|
||||
{
|
||||
while (current_ != end_ && *current_) {
|
||||
if (!f(*current_)) {
|
||||
return State::CAN_CONTINUE;
|
||||
}
|
||||
|
@ -105,8 +105,9 @@ KeyValueBlobReader::State KeyValueBlobReader::SkipUntilNot(
|
|||
}
|
||||
|
||||
KeyValueBlobReader::State KeyValueBlobReader::SkipUntil(
|
||||
const std::function<bool(char)>& f) {
|
||||
while (current_ != end_ &&* current_) {
|
||||
const std::function<bool(char)>& f)
|
||||
{
|
||||
while (current_ != end_ && *current_) {
|
||||
if (f(*current_)) {
|
||||
return State::CAN_CONTINUE;
|
||||
}
|
||||
|
@ -116,8 +117,9 @@ KeyValueBlobReader::State KeyValueBlobReader::SkipUntil(
|
|||
return State::END;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os,
|
||||
const KeyValueBlobReader::KeyValue& kv) {
|
||||
std::ostream& operator<<(
|
||||
std::ostream& os, const KeyValueBlobReader::KeyValue& kv)
|
||||
{
|
||||
os << "(key: " << kv.key << ", value: " << kv.value << ")";
|
||||
return os;
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
namespace vChewing {
|
||||
|
||||
class KeyValueBlobReader {
|
||||
public:
|
||||
public:
|
||||
enum class State : int {
|
||||
// There are no more key-value pairs in this blob.
|
||||
END = 0,
|
||||
|
@ -44,11 +44,19 @@ class KeyValueBlobReader {
|
|||
};
|
||||
|
||||
struct KeyValue {
|
||||
constexpr KeyValue() : key(""), value("") {}
|
||||
constexpr KeyValue()
|
||||
: key("")
|
||||
, value("")
|
||||
{
|
||||
}
|
||||
constexpr KeyValue(std::string_view k, std::string_view v)
|
||||
: key(k), value(v) {}
|
||||
: key(k)
|
||||
, value(v)
|
||||
{
|
||||
}
|
||||
|
||||
bool operator==(const KeyValue& another) const {
|
||||
bool operator==(const KeyValue& another) const
|
||||
{
|
||||
return key == another.key && value == another.value;
|
||||
}
|
||||
|
||||
|
@ -57,13 +65,17 @@ class KeyValueBlobReader {
|
|||
};
|
||||
|
||||
KeyValueBlobReader(const char* blob, size_t size)
|
||||
: current_(blob), end_(blob + size) {}
|
||||
: current_(blob)
|
||||
, end_(blob + size)
|
||||
{
|
||||
}
|
||||
|
||||
// Parse the next key-value pair and return the state of the reader. If `out`
|
||||
// is passed, out will be set to the produced key-value pair if there is one.
|
||||
// Parse the next key-value pair and return the state of the reader. If
|
||||
// `out` is passed, out will be set to the produced key-value pair if there
|
||||
// is one.
|
||||
State Next(KeyValue* out = nullptr);
|
||||
|
||||
private:
|
||||
private:
|
||||
State SkipUntil(const std::function<bool(char)>& f);
|
||||
State SkipUntilNot(const std::function<bool(char)>& f);
|
||||
|
||||
|
|
Loading…
Reference in New Issue