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 OHOS_IDL_STRING_H 17 #define OHOS_IDL_STRING_H 18 19 #include <cstddef> 20 21 namespace OHOS { 22 namespace Idl { 23 class String { 24 public: String()25 String() {} 26 27 String(const char* string); 28 29 String(const char* string, size_t length); 30 31 String(const String& other); 32 33 String(String&& other); 34 35 ~String(); 36 string()37 const char* string() const 38 { 39 return string_; 40 } 41 42 operator const char*() const 43 { 44 return string_; 45 } 46 IsNull()47 bool IsNull() const 48 { 49 return string_ == nullptr; 50 } 51 IsEmpty()52 bool IsEmpty() const 53 { 54 return string_ == nullptr || string_[0] == '\0'; 55 } 56 57 int GetLength() const; 58 59 char operator[](int index) const; 60 61 bool Equals(const char* string) const; 62 63 bool Equals(const String& other) const; 64 65 int GetHashCode() const; 66 67 int IndexOf(char c, int fromIndex = 0) const; 68 69 int IndexOf(const char* string, int fromIndex = 0) const; 70 71 int IndexOf(const String& other, int fromIndex = 0) const; 72 73 int LastIndexOf(char c, int fromIndex = 0) const; 74 75 int LastIndexOf(const char* string, int fromIndex = 0) const; 76 77 int LastIndexOf(const String& other, int fromIndex = 0) const; 78 79 bool StartsWith(const char* string) const; 80 81 bool StartsWith(const String& other) const; 82 83 bool EndsWith(const char* string) const; 84 85 bool EndsWith(const String& other) const; 86 87 String ToLowerCase() const; 88 89 String ToUpperCase() const; 90 91 String Substring(int begin) const; 92 93 String Substring(int begin, int end) const; 94 95 String Replace(char oldChar, char newChar) const; 96 97 String Replace(const char* target, const char* replacement) const; 98 99 String Replace(const String& target, const String& replacement) const; 100 101 String& operator=(const char* string); 102 103 String& operator=(const String& other); 104 105 String& operator=(String&& other); 106 107 String operator+=(const char* string) const; 108 109 String operator+=(const String& other) const; 110 111 static String Format(const char* format, ...); 112 113 static const char* TAG; 114 static constexpr int MAX_SIZE = 262144; // 2^18 115 116 private: 117 String(int size); 118 119 int LastIndexOfInternal(const char* string, int fromIndex) const; 120 121 char* string_ = nullptr; 122 }; 123 124 inline String operator+(const String& string1, const char* string2) 125 { 126 return string1 += string2; 127 } 128 129 struct StringHashFunc { operatorStringHashFunc130 int operator()(const String& key) const 131 { 132 return key.GetHashCode(); 133 } 134 }; 135 136 struct StringEqualFunc { operatorStringEqualFunc137 bool operator()(const String& lhs, const String& rhs) const 138 { 139 return lhs.Equals(rhs); 140 } 141 }; 142 } 143 } 144 #endif // OHOS_IDL_STRING_H 145