diff --git a/td/telegram/files/FileGenerateManager.cpp b/td/telegram/files/FileGenerateManager.cpp index 7ebac8ff1..27f14be1f 100644 --- a/td/telegram/files/FileGenerateManager.cpp +++ b/td/telegram/files/FileGenerateManager.cpp @@ -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('#'); diff --git a/tdutils/td/utils/HttpUrl.cpp b/tdutils/td/utils/HttpUrl.cpp index ea0e171f3..5bfb9207c 100644 --- a/tdutils/td/utils/HttpUrl.cpp +++ b/tdutils/td/utils/HttpUrl.cpp @@ -46,8 +46,7 @@ Result 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") { diff --git a/tdutils/td/utils/JsonBuilder.cpp b/tdutils/td/utils/JsonBuilder.cpp index d58b65b6f..1847f056b 100644 --- a/tdutils/td/utils/JsonBuilder.cpp +++ b/tdutils/td/utils/JsonBuilder.cpp @@ -343,17 +343,17 @@ Result 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"); diff --git a/tdutils/td/utils/Parser.h b/tdutils/td/utils/Parser.h index 015bede53..a6cd01549 100644 --- a/tdutils/td/utils/Parser.h +++ b/tdutils/td/utils/Parser.h @@ -139,6 +139,14 @@ class ParserImpl { return false; } + bool try_skip(Slice prefix) { + if (prefix.size() > static_cast(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(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_);