1 #pragma once 2 3 /* 4 * Copyright (C) 2018 The Android Open Source Project 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 #include <string> 20 21 #include <android-base/logging.h> 22 23 struct Range { RangeRange24 explicit Range(std::string data) : data_(std::move(data)) {} 25 26 Range(const Range& copy) = delete; 27 Range& operator=(const Range& copy) = delete; 28 29 Range(Range&& move) = default; 30 Range& operator=(Range&& move) = default; 31 emptyRange32 bool empty() const { 33 return size() == 0; 34 } 35 sizeRange36 size_t size() const { 37 return data_.size() - begin_offset_ - end_offset_; 38 }; 39 drop_frontRange40 void drop_front(size_t n) { 41 CHECK_GE(size(), n); 42 begin_offset_ += n; 43 } 44 drop_endRange45 void drop_end(size_t n) { 46 CHECK_GE(size(), n); 47 end_offset_ += n; 48 } 49 dataRange50 char* data() { 51 return &data_[0] + begin_offset_; 52 } 53 beginRange54 std::string::iterator begin() { 55 return data_.begin() + begin_offset_; 56 } 57 endRange58 std::string::iterator end() { 59 return data_.end() - end_offset_; 60 } 61 62 std::string data_; 63 size_t begin_offset_ = 0; 64 size_t end_offset_ = 0; 65 }; 66