Simplify parser.start_with usage.
GitOrigin-RevId: 465e51bba514259ebe15976d2612e1a46754a841
This commit is contained in:
parent
23534d218f
commit
ceb49d0143
@ -376,7 +376,7 @@ static Status check_mtime(std::string &conversion, CSlice original_path) {
|
|||||||
return Status::OK();
|
return Status::OK();
|
||||||
}
|
}
|
||||||
ConstParser parser(conversion);
|
ConstParser parser(conversion);
|
||||||
if (!parser.skip_start_with("#mtime#")) {
|
if (!parser.try_skip("#mtime#")) {
|
||||||
return Status::OK();
|
return Status::OK();
|
||||||
}
|
}
|
||||||
auto mtime_str = parser.read_till('#');
|
auto mtime_str = parser.read_till('#');
|
||||||
|
@ -46,8 +46,7 @@ Result<HttpUrl> parse_url(Slice url, HttpUrl::Protocol default_protocol) {
|
|||||||
string protocol_str = to_lower(parser.read_till_nofail(":/?#@[]"));
|
string protocol_str = to_lower(parser.read_till_nofail(":/?#@[]"));
|
||||||
|
|
||||||
HttpUrl::Protocol protocol;
|
HttpUrl::Protocol protocol;
|
||||||
if (parser.start_with("://")) {
|
if (parser.try_skip("://")) {
|
||||||
parser.advance(3);
|
|
||||||
if (protocol_str == "http") {
|
if (protocol_str == "http") {
|
||||||
protocol = HttpUrl::Protocol::Http;
|
protocol = HttpUrl::Protocol::Http;
|
||||||
} else if (protocol_str == "https") {
|
} else if (protocol_str == "https") {
|
||||||
|
@ -343,17 +343,17 @@ Result<JsonValue> do_json_decode(Parser &parser, int32 max_depth) {
|
|||||||
parser.skip_whitespaces();
|
parser.skip_whitespaces();
|
||||||
switch (parser.peek_char()) {
|
switch (parser.peek_char()) {
|
||||||
case 'f':
|
case 'f':
|
||||||
if (parser.skip_start_with("false")) {
|
if (parser.try_skip("false")) {
|
||||||
return JsonValue::create_boolean(false);
|
return JsonValue::create_boolean(false);
|
||||||
}
|
}
|
||||||
return Status::Error("Token starts with 'f' -- false expected");
|
return Status::Error("Token starts with 'f' -- false expected");
|
||||||
case 't':
|
case 't':
|
||||||
if (parser.skip_start_with("true")) {
|
if (parser.try_skip("true")) {
|
||||||
return JsonValue::create_boolean(true);
|
return JsonValue::create_boolean(true);
|
||||||
}
|
}
|
||||||
return Status::Error("Token starts with 't' -- true expected");
|
return Status::Error("Token starts with 't' -- true expected");
|
||||||
case 'n':
|
case 'n':
|
||||||
if (parser.skip_start_with("null")) {
|
if (parser.try_skip("null")) {
|
||||||
return JsonValue();
|
return JsonValue();
|
||||||
}
|
}
|
||||||
return Status::Error("Token starts with 'n' -- null expected");
|
return Status::Error("Token starts with 'n' -- null expected");
|
||||||
@ -463,17 +463,17 @@ Status do_json_skip(Parser &parser, int32 max_depth) {
|
|||||||
parser.skip_whitespaces();
|
parser.skip_whitespaces();
|
||||||
switch (parser.peek_char()) {
|
switch (parser.peek_char()) {
|
||||||
case 'f':
|
case 'f':
|
||||||
if (parser.skip_start_with("false")) {
|
if (parser.try_skip("false")) {
|
||||||
return Status::OK();
|
return Status::OK();
|
||||||
}
|
}
|
||||||
return Status::Error("Starts with 'f' -- false expected");
|
return Status::Error("Starts with 'f' -- false expected");
|
||||||
case 't':
|
case 't':
|
||||||
if (parser.skip_start_with("true")) {
|
if (parser.try_skip("true")) {
|
||||||
return Status::OK();
|
return Status::OK();
|
||||||
}
|
}
|
||||||
return Status::Error("Starts with 't' -- true expected");
|
return Status::Error("Starts with 't' -- true expected");
|
||||||
case 'n':
|
case 'n':
|
||||||
if (parser.skip_start_with("null")) {
|
if (parser.try_skip("null")) {
|
||||||
return Status::OK();
|
return Status::OK();
|
||||||
}
|
}
|
||||||
return Status::Error("Starts with 'n' -- null expected");
|
return Status::Error("Starts with 'n' -- null expected");
|
||||||
|
@ -139,6 +139,14 @@ class ParserImpl {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool try_skip(Slice prefix) {
|
||||||
|
if (prefix.size() > static_cast<size_t>(end_ - ptr_) || prefix != Slice(ptr_, prefix.size())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
advance(prefix.size());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void skip_till_not(Slice str) {
|
void skip_till_not(Slice str) {
|
||||||
while (ptr_ != end_) {
|
while (ptr_ != end_) {
|
||||||
if (std::memchr(str.data(), *ptr_, str.size()) == nullptr) {
|
if (std::memchr(str.data(), *ptr_, str.size()) == nullptr) {
|
||||||
@ -163,21 +171,6 @@ class ParserImpl {
|
|||||||
return status_;
|
return status_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool start_with(Slice prefix) const {
|
|
||||||
if (prefix.size() > static_cast<size_t>(end_ - ptr_)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return prefix == Slice(ptr_, prefix.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
bool skip_start_with(Slice prefix) {
|
|
||||||
if (start_with(prefix)) {
|
|
||||||
advance(prefix.size());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void advance(size_t diff) {
|
void advance(size_t diff) {
|
||||||
ptr_ += diff;
|
ptr_ += diff;
|
||||||
CHECK(ptr_ <= end_);
|
CHECK(ptr_ <= end_);
|
||||||
|
Loading…
Reference in New Issue
Block a user