1 /* 2 * Copyright (C) 2023 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 ANDROID_SERVERS_CAMERA_STRINGUTILS_H 18 #define ANDROID_SERVERS_CAMERA_STRINGUTILS_H 19 20 #include <memory> 21 #include <optional> 22 #include <string> 23 24 #include <fmt/printf.h> 25 #include <utils/String8.h> 26 #include <utils/String16.h> 27 28 namespace android { toString8(const std::string & str)29 inline String8 toString8(const std::string &str) { 30 return String8(str.c_str()); 31 } 32 toString8(const String16 & str)33 inline String8 toString8(const String16 &str) { 34 return String8(str); 35 } 36 toString8(const char * str)37 inline String8 toString8(const char *str) { 38 return String8(str); 39 } 40 toString16(const std::string & str)41 inline String16 toString16(const std::string &str) { 42 return String16(str.c_str()); 43 } 44 toString16(const String8 & str)45 inline String16 toString16(const String8 &str) { 46 return String16(str); 47 } 48 toString16(const char * str)49 inline String16 toString16(const char *str) { 50 return String16(str); 51 } 52 toString16(std::optional<std::string> str)53 inline std::optional<String16> toString16(std::optional<std::string> str) { 54 if (str.has_value()) { 55 return std::optional<String16>(toString16(str.value())); 56 } 57 58 return std::nullopt; 59 } 60 toStdString(const String8 & str)61 inline std::string toStdString(const String8 &str) { 62 return std::string(str.c_str()); 63 } 64 toStdString(const String16 & str)65 inline std::string toStdString(const String16 &str) { 66 String8 str8(str); 67 return std::string(str8.c_str()); 68 } 69 70 /** 71 * Convert a non-null-terminated UTF16 string to a UTF8 string (i.e. in jni functions) 72 * len is the number of characters. 73 */ toStdString(const char16_t * str,size_t len)74 inline std::string toStdString(const char16_t *str, size_t len) { 75 String16 str16(str, len); 76 String8 str8(str16); 77 return std::string(str8.c_str()); 78 } 79 } // namespace android 80 81 #endif // ANDROID_SERVERS_CAMERA_STRINGUTILS_H 82