2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class Parser {
|
|
|
|
public:
|
|
|
|
explicit Parser(MutableSlice data) : ptr_(data.begin()), end_(data.end()), status_() {
|
|
|
|
}
|
|
|
|
Parser(Parser &&other) : ptr_(other.ptr_), end_(other.end_), status_(std::move(other.status_)) {
|
|
|
|
other.clear();
|
|
|
|
}
|
|
|
|
Parser &operator=(Parser &&other) {
|
|
|
|
if (&other == this) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
ptr_ = other.ptr_;
|
|
|
|
end_ = other.end_;
|
|
|
|
status_ = std::move(other.status_);
|
|
|
|
other.clear();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Parser(const Parser &) = delete;
|
|
|
|
Parser &operator=(const Parser &) = delete;
|
|
|
|
~Parser() = default;
|
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return ptr_ == end_;
|
|
|
|
}
|
|
|
|
void clear() {
|
|
|
|
ptr_ = nullptr;
|
|
|
|
end_ = ptr_;
|
2018-01-31 13:43:36 +01:00
|
|
|
status_ = Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MutableSlice read_till_nofail(char c) {
|
|
|
|
if (status_.is_error()) {
|
|
|
|
return MutableSlice();
|
|
|
|
}
|
2018-12-19 22:44:15 +01:00
|
|
|
char *till = static_cast<char *>(std::memchr(ptr_, c, end_ - ptr_));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (till == nullptr) {
|
|
|
|
till = end_;
|
|
|
|
}
|
|
|
|
MutableSlice result(ptr_, till);
|
|
|
|
ptr_ = till;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
MutableSlice read_till_nofail(Slice str) {
|
|
|
|
if (status_.is_error()) {
|
|
|
|
return MutableSlice();
|
|
|
|
}
|
|
|
|
char *best_till = end_;
|
|
|
|
for (auto c : str) {
|
2018-12-19 22:44:15 +01:00
|
|
|
char *till = static_cast<char *>(std::memchr(ptr_, c, end_ - ptr_));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (till != nullptr && till < best_till) {
|
|
|
|
best_till = till;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MutableSlice result(ptr_, best_till);
|
|
|
|
ptr_ = best_till;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class F>
|
|
|
|
MutableSlice read_while(const F &f) {
|
|
|
|
auto save_ptr = ptr_;
|
|
|
|
while (ptr_ != end_ && f(*ptr_)) {
|
|
|
|
ptr_++;
|
|
|
|
}
|
|
|
|
return MutableSlice(save_ptr, ptr_);
|
|
|
|
}
|
|
|
|
MutableSlice read_all() {
|
|
|
|
auto save_ptr = ptr_;
|
|
|
|
ptr_ = end_;
|
|
|
|
return MutableSlice(save_ptr, ptr_);
|
|
|
|
}
|
|
|
|
|
|
|
|
MutableSlice read_till(char c) {
|
|
|
|
if (status_.is_error()) {
|
|
|
|
return MutableSlice();
|
|
|
|
}
|
|
|
|
MutableSlice res = read_till_nofail(c);
|
|
|
|
if (ptr_ == end_ || ptr_[0] != c) {
|
|
|
|
status_ = Status::Error(PSLICE() << "Read till " << tag("char", c) << " failed");
|
|
|
|
return MutableSlice();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
char peek_char() {
|
|
|
|
if (ptr_ == end_) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return *ptr_;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *ptr() {
|
|
|
|
return ptr_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void skip_nofail(char c) {
|
|
|
|
if (ptr_ != end_ && ptr_[0] == c) {
|
|
|
|
ptr_++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void skip(char c) {
|
|
|
|
if (status_.is_error()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ptr_ == end_ || ptr_[0] != c) {
|
|
|
|
status_ = Status::Error(PSLICE() << "Skip " << tag("char", c) << " failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ptr_++;
|
|
|
|
}
|
|
|
|
bool try_skip(char c) {
|
|
|
|
if (ptr_ != end_ && ptr_[0] == c) {
|
|
|
|
ptr_++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void skip_till_not(Slice str) {
|
|
|
|
while (ptr_ != end_) {
|
|
|
|
if (std::memchr(str.data(), *ptr_, str.size()) == nullptr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ptr_++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void skip_whitespaces() {
|
|
|
|
skip_till_not(" \t\r\n");
|
|
|
|
}
|
2018-12-19 15:48:39 +01:00
|
|
|
MutableSlice read_word() {
|
|
|
|
skip_whitespaces();
|
|
|
|
return read_till_nofail(" \t\r\n");
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
MutableSlice data() const {
|
|
|
|
return MutableSlice(ptr_, end_);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status &status() {
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
|
2019-07-27 00:42:18 +02:00
|
|
|
bool start_with(Slice prefix) const {
|
|
|
|
if (prefix.size() > static_cast<size_t>(end_ - ptr_)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-07-27 00:42:18 +02:00
|
|
|
return prefix == Slice(ptr_, prefix.size());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
char *ptr_;
|
|
|
|
char *end_;
|
|
|
|
Status status_;
|
|
|
|
};
|
2019-08-01 02:40:28 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|