1 /* 2 * Copyright (c) 2024 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 OHOS_IDL_STRING_HELPER_H 17 #define OHOS_IDL_STRING_HELPER_H 18 19 #include <cstring> 20 #include <string> 21 #include <vector> 22 23 namespace OHOS { 24 namespace Idl { 25 class StringHelper { 26 public: 27 static std::vector<std::string> Split(std::string sources, const std::string &limit); 28 29 static bool StartWith(const std::string &value, char prefix); 30 31 static bool StartWith(const std::string &value, const std::string &prefix); 32 33 static bool EndWith(const std::string &value, char suffix); 34 35 static bool EndWith(const std::string &value, const std::string &suffix); 36 37 static std::string Replace(const std::string &value, char oldChar, char newChar); 38 39 static std::string Replace(const std::string &value, const std::string &oldstr, const std::string &newstr); 40 41 static std::string Replace( 42 const std::string &value, size_t position, const std::string &substr, const std::string &newstr); 43 44 static std::string Replace(const std::string &value, size_t position, size_t len, const std::string &newStr); 45 46 static std::string SubStr(const std::string &value, size_t start, size_t end = std::string::npos); 47 48 static std::string StrToLower(const std::string &value); 49 50 static std::string StrToUpper(const std::string &value); 51 52 static std::string FirstToUpper(const std::string &value); 53 54 static std::string Format(const char *format, ...); 55 56 static int GetHashCode(const std::string &key); 57 58 static constexpr size_t lineMaxSize = 2048; // 2KB 59 static constexpr size_t maxSize = 524288; // 512KB 60 }; 61 62 struct StringHashFunc { operatorStringHashFunc63 int operator()(const std::string& key) const 64 { 65 return StringHelper::GetHashCode(key); 66 } 67 }; 68 69 struct StringEqualFunc { operatorStringEqualFunc70 bool operator()(const std::string& lhs, const std::string& rhs) const 71 { 72 return lhs == rhs; 73 } 74 }; 75 } // namespace Idl 76 } // namespace OHOS 77 78 #endif // OHOS_IDL_STRING_HELPER_H