Simplify parser.start_with usage.

GitOrigin-RevId: 465e51bba514259ebe15976d2612e1a46754a841
This commit is contained in:
levlam 2020-07-21 14:24:55 +03:00
parent 23534d218f
commit ceb49d0143
4 changed files with 16 additions and 24 deletions

View File

@ -376,7 +376,7 @@ static Status check_mtime(std::string &conversion, CSlice original_path) {
return Status::OK();
}
ConstParser parser(conversion);
if (!parser.skip_start_with("#mtime#")) {
if (!parser.try_skip("#mtime#")) {
return Status::OK();
}
auto mtime_str = parser.read_till('#');

View File

@ -46,8 +46,7 @@ Result<HttpUrl> parse_url(Slice url, HttpUrl::Protocol default_protocol) {
string protocol_str = to_lower(parser.read_till_nofail(":/?#@[]"));
HttpUrl::Protocol protocol;
if (parser.start_with("://")) {
parser.advance(3);
if (parser.try_skip("://")) {
if (protocol_str == "http") {
protocol = HttpUrl::Protocol::Http;
} else if (protocol_str == "https") {

View File

@ -343,17 +343,17 @@ Result<JsonValue> do_json_decode(Parser &parser, int32 max_depth) {
parser.skip_whitespaces();
switch (parser.peek_char()) {
case 'f':
if (parser.skip_start_with("false")) {
if (parser.try_skip("false")) {
return JsonValue::create_boolean(false);
}
return Status::Error("Token starts with 'f' -- false expected");
case 't':
if (parser.skip_start_with("true")) {
if (parser.try_skip("true")) {
return JsonValue::create_boolean(true);
}
return Status::Error("Token starts with 't' -- true expected");
case 'n':
if (parser.skip_start_with("null")) {
if (parser.try_skip("null")) {
return JsonValue();
}
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();
switch (parser.peek_char()) {
case 'f':
if (parser.skip_start_with("false")) {
if (parser.try_skip("false")) {
return Status::OK();
}
return Status::Error("Starts with 'f' -- false expected");
case 't':
if (parser.skip_start_with("true")) {
if (parser.try_skip("true")) {
return Status::OK();
}
return Status::Error("Starts with 't' -- true expected");
case 'n':
if (parser.skip_start_with("null")) {
if (parser.try_skip("null")) {
return Status::OK();
}
return Status::Error("Starts with 'n' -- null expected");

View File

@ -139,6 +139,14 @@ class ParserImpl {
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) {
while (ptr_ != end_) {
if (std::memchr(str.data(), *ptr_, str.size()) == nullptr) {
@ -163,21 +171,6 @@ class ParserImpl {
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) {
ptr_ += diff;
CHECK(ptr_ <= end_);