2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
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 {
|
|
|
|
|
2019-08-05 11:42:42 +02:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <class SliceT>
|
|
|
|
class ParserImpl {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2019-08-05 11:42:42 +02:00
|
|
|
explicit ParserImpl(SliceT data) : ptr_(data.begin()), end_(data.end()), status_() {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-08-05 11:42:42 +02:00
|
|
|
ParserImpl(ParserImpl &&other) : ptr_(other.ptr_), end_(other.end_), status_(std::move(other.status_)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
other.clear();
|
|
|
|
}
|
2019-08-05 11:42:42 +02:00
|
|
|
ParserImpl &operator=(ParserImpl &&other) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (&other == this) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
ptr_ = other.ptr_;
|
|
|
|
end_ = other.end_;
|
|
|
|
status_ = std::move(other.status_);
|
|
|
|
other.clear();
|
|
|
|
return *this;
|
|
|
|
}
|
2019-08-05 11:42:42 +02:00
|
|
|
ParserImpl(const ParserImpl &) = delete;
|
|
|
|
ParserImpl &operator=(const ParserImpl &) = delete;
|
|
|
|
~ParserImpl() = default;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-05 11:42:42 +02:00
|
|
|
SliceT read_till_nofail(char c) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (status_.is_error()) {
|
2019-08-05 11:42:42 +02:00
|
|
|
return SliceT();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-08-05 11:42:42 +02:00
|
|
|
auto till = static_cast<decltype(ptr_)>(std::memchr(ptr_, c, end_ - ptr_));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (till == nullptr) {
|
|
|
|
till = end_;
|
|
|
|
}
|
2019-08-05 11:42:42 +02:00
|
|
|
SliceT result(ptr_, till);
|
2018-12-31 20:04:05 +01:00
|
|
|
ptr_ = till;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-08-05 11:42:42 +02:00
|
|
|
SliceT read_till_nofail(Slice str) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (status_.is_error()) {
|
2019-08-05 11:42:42 +02:00
|
|
|
return SliceT();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-08-05 11:42:42 +02:00
|
|
|
auto best_till = end_;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto c : str) {
|
2019-08-05 11:42:42 +02:00
|
|
|
auto till = static_cast<decltype(ptr_)>(std::memchr(ptr_, c, end_ - ptr_));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (till != nullptr && till < best_till) {
|
|
|
|
best_till = till;
|
|
|
|
}
|
|
|
|
}
|
2019-08-05 11:42:42 +02:00
|
|
|
SliceT result(ptr_, best_till);
|
2018-12-31 20:04:05 +01:00
|
|
|
ptr_ = best_till;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class F>
|
2019-08-05 11:42:42 +02:00
|
|
|
SliceT read_while(const F &f) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto save_ptr = ptr_;
|
|
|
|
while (ptr_ != end_ && f(*ptr_)) {
|
|
|
|
ptr_++;
|
|
|
|
}
|
2019-08-05 11:42:42 +02:00
|
|
|
return SliceT(save_ptr, ptr_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-08-05 11:42:42 +02:00
|
|
|
SliceT read_all() {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto save_ptr = ptr_;
|
|
|
|
ptr_ = end_;
|
2019-08-05 11:42:42 +02:00
|
|
|
return SliceT(save_ptr, ptr_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-08-05 11:42:42 +02:00
|
|
|
SliceT read_till(char c) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (status_.is_error()) {
|
2019-08-05 11:42:42 +02:00
|
|
|
return SliceT();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-08-05 11:42:42 +02:00
|
|
|
SliceT res = read_till_nofail(c);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (ptr_ == end_ || ptr_[0] != c) {
|
|
|
|
status_ = Status::Error(PSLICE() << "Read till " << tag("char", c) << " failed");
|
2019-08-05 11:42:42 +02:00
|
|
|
return SliceT();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
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");
|
|
|
|
}
|
2019-08-05 11:42:42 +02:00
|
|
|
SliceT read_word() {
|
2018-12-19 15:48:39 +01:00
|
|
|
skip_whitespaces();
|
|
|
|
return read_till_nofail(" \t\r\n");
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2019-08-05 11:42:42 +02:00
|
|
|
SliceT data() const {
|
|
|
|
return SliceT(ptr_, end_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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:
|
2019-08-05 11:42:42 +02:00
|
|
|
decltype(std::declval<SliceT>().begin()) ptr_;
|
|
|
|
decltype(std::declval<SliceT>().end()) end_;
|
2018-12-31 20:04:05 +01:00
|
|
|
Status status_;
|
|
|
|
};
|
2019-08-01 02:40:28 +02:00
|
|
|
|
2019-08-05 11:42:42 +02:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
using Parser = detail::ParserImpl<MutableSlice>;
|
|
|
|
using ConstParser = detail::ParserImpl<Slice>;
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|