1 /* 2 * Copyright (C) 2015 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 ANDROIDFW_STRING_PIECE_H 18 #define ANDROIDFW_STRING_PIECE_H 19 20 #include <ostream> 21 #include <string> 22 #include <string_view> 23 24 #include "utils/Unicode.h" 25 26 namespace android { 27 28 template <class T> 29 using BasicStringPiece = std::basic_string_view<T>; 30 31 using StringPiece = BasicStringPiece<char>; 32 using StringPiece16 = BasicStringPiece<char16_t>; 33 34 } // namespace android 35 36 namespace std { 37 38 inline ::std::ostream& operator<<(::std::ostream& out, ::std::u16string_view str) { 39 ssize_t utf8_len = utf16_to_utf8_length(str.data(), str.size()); 40 if (utf8_len < 0) { 41 return out; // empty 42 } 43 44 std::string utf8; 45 utf8.resize(static_cast<size_t>(utf8_len)); 46 utf16_to_utf8(str.data(), str.size(), utf8.data(), utf8_len + 1); 47 return out << utf8; 48 } 49 50 inline ::std::ostream& operator<<(::std::ostream& out, const ::std::u16string& str) { 51 return out << std::u16string_view(str); 52 } 53 54 } // namespace std 55 56 #endif // ANDROIDFW_STRING_PIECE_H 57