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 "bytes.h" 20 #include "distributed_object.h" 21 #include "logger.h" 22 #include "objectstore_errors.h" 23 24 namespace OHOS::ObjectStore { 25 class StringUtils final { 26 public: 27 StringUtils() = delete; 28 ~StringUtils() = delete; StrToBytes(const std::string & src)29 static std::vector<uint8_t> StrToBytes(const std::string &src) 30 { 31 std::vector<uint8_t> dst; 32 dst.resize(src.size()); 33 dst.assign(src.begin(), src.end()); 34 return dst; 35 } 36 BytesToStr(const std::vector<uint8_t> src)37 static std::string BytesToStr(const std::vector<uint8_t> src) 38 { 39 std::string result; 40 Bytes rstStr(src.begin(), src.end()); 41 result.assign(reinterpret_cast<char *>(rstStr.data()), rstStr.size()); 42 return result; 43 } BytesToStrWithType(Bytes input,std::string & str)44 static uint32_t BytesToStrWithType(Bytes input, std::string &str) 45 { 46 uint32_t len = input.end() - input.begin(); 47 if (len <= sizeof(Type)) { 48 LOG_ERROR("StringUtils:BytesToStrWithType get input len err."); 49 return ERR_DATA_LEN; 50 } 51 std::vector<uint8_t>::const_iterator first = input.begin() + sizeof(Type); 52 std::vector<uint8_t>::const_iterator end = input.end(); 53 Bytes rstStr(first, end); 54 str.assign(reinterpret_cast<char *>(rstStr.data()), rstStr.size()); 55 return SUCCESS; 56 } 57 }; 58 } // namespace OHOS::ObjectStore 59 #endif // STRING_UTILS_H 60