1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef STRING_UTILS_H 17 #define STRING_UTILS_H 18 19 #include <initializer_list> 20 #include <sstream> 21 #include <string> 22 #include <vector> 23 24 #include "bytes.h" 25 #include "distributed_object.h" 26 #include "logger.h" 27 #include "objectstore_errors.h" 28 29 namespace OHOS::ObjectStore { 30 class StringUtils final { 31 public: 32 StringUtils() = delete; 33 ~StringUtils() = delete; StrToBytes(const std::string & src)34 static std::vector<uint8_t> StrToBytes(const std::string &src) 35 { 36 std::vector<uint8_t> dst; 37 dst.resize(src.size()); 38 dst.assign(src.begin(), src.end()); 39 return dst; 40 } 41 BytesToStr(const std::vector<uint8_t> src)42 static std::string BytesToStr(const std::vector<uint8_t> src) 43 { 44 std::string result; 45 Bytes rstStr(src.begin(), src.end()); 46 result.assign(reinterpret_cast<char *>(rstStr.data()), rstStr.size()); 47 return result; 48 } BytesToStrWithType(Bytes input,std::string & str)49 static uint32_t BytesToStrWithType(Bytes input, std::string &str) 50 { 51 uint32_t len = input.end() - input.begin(); 52 if (len <= sizeof(Type)) { 53 LOG_ERROR("StringUtils:BytesToStrWithType get input len err."); 54 return ERR_DATA_LEN; 55 } 56 std::vector<uint8_t>::const_iterator first = input.begin() + sizeof(Type); 57 std::vector<uint8_t>::const_iterator end = input.end(); 58 Bytes rstStr(first, end); 59 str.assign(reinterpret_cast<char *>(rstStr.data()), rstStr.size()); 60 return SUCCESS; 61 } 62 }; 63 } // namespace OHOS::ObjectStore 64 #endif // STRING_UTILS_H 65