1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINIKIN_U16STRING_PIECE_H 18 #define MINIKIN_U16STRING_PIECE_H 19 20 #include <vector> 21 22 #include "minikin/Range.h" 23 24 namespace minikin { 25 26 class U16StringPiece { 27 public: U16StringPiece()28 U16StringPiece() : mData(nullptr), mLength(0) {} U16StringPiece(const uint16_t * data,uint32_t length)29 U16StringPiece(const uint16_t* data, uint32_t length) : mData(data), mLength(length) {} U16StringPiece(const std::vector<uint16_t> & v)30 U16StringPiece(const std::vector<uint16_t>& v) // Intentionally not explicit. 31 : mData(v.data()), mLength(static_cast<uint32_t>(v.size())) {} 32 template <uint32_t length> U16StringPiece(uint16_t const (& data)[length])33 U16StringPiece(uint16_t const (&data)[length]) : mData(data), mLength(length) {} 34 35 U16StringPiece(const U16StringPiece&) = default; 36 U16StringPiece& operator=(const U16StringPiece&) = default; 37 data()38 inline const uint16_t* data() const { return mData; } size()39 inline uint32_t size() const { return mLength; } length()40 inline uint32_t length() const { return mLength; } 41 42 // Undefined behavior if pos is out of range. at(uint32_t pos)43 inline const uint16_t& at(uint32_t pos) const { return mData[pos]; } 44 inline const uint16_t& operator[](uint32_t pos) const { return mData[pos]; } 45 substr(const Range & range)46 inline U16StringPiece substr(const Range& range) const { 47 return U16StringPiece(mData + range.getStart(), range.getLength()); 48 } 49 hasChar(uint16_t c)50 inline bool hasChar(uint16_t c) const { 51 const uint16_t* end = mData + mLength; 52 return std::find(mData, end, c) != end; 53 } 54 55 private: 56 const uint16_t* mData; 57 uint32_t mLength; 58 }; 59 60 } // namespace minikin 61 62 #endif // MINIKIN_U16STRING_PIECE_H 63