• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef COMMON_COMPONENTS_BASE_C_STRING_H
16 #define COMMON_COMPONENTS_BASE_C_STRING_H
17 
18 #include <cstdint>
19 #include <vector>
20 #include <cstdlib>
21 
22 #include "common_interfaces/base/common.h"
23 #include "securec.h"
24 
25 namespace common {
26 class CString {
27 public:
28     CString();
29     CString(const char* initStr);
30     explicit CString(char c);
31     explicit CString(int64_t number);
32     explicit CString(int32_t number);
33     explicit CString(uint64_t number);
34     explicit CString(uint32_t number);
35     CString(size_t number, char ch);
36     CString(const CString& other);
37     CString& operator=(const CString& other);
38     ~CString();
39 
40     void EnsureSpace(size_t addLen);
41 
42     CString& Append(const char* addStr, size_t addLen = 0);
43     CString& Append(const CString& addStr, size_t addLen = 0);
44 
45     template<typename T>
46     CString& operator+=(const T addStr)
47     {
48         return Append(addStr);
49     }
50 
51     CString Combine(const char* addStr) const;
52     CString Combine(const CString& addStr) const;
53 
54     template<typename T>
55     CString operator+(const T addStr) const
56     {
57         return Combine(addStr);
58     }
59 
60     size_t Length() const;
61 
62     const char* Str() const noexcept;
63 
64     char* GetStr() const noexcept;
65 
IsEmpty()66     bool IsEmpty() const { return length_ == 0; }
67 
68     CString& Truncate(size_t index);
69     CString& Insert(size_t index, const char* addStr);
70 
71     int Find(const char* subStr, size_t begin = 0) const;
72     int Find(const char subStr, size_t begin = 0) const;
73     int RFind(const char* subStr) const;
74 
75     CString SubStr(size_t index, size_t len) const;
76     CString SubStr(size_t index) const;
77 
EndsWith(const CString & suffix)78     bool EndsWith(const CString& suffix) const
79     {
80         return Length() >= suffix.Length() && SubStr(Length() - suffix.Length()) == suffix;
81     }
82 
83     CString RemoveBlankSpace() const;
84 
85     void Replace(size_t pos, CString cStr);
86 
87     void ReplaceAll(CString replacement, CString target);
88 
89     bool operator==(const CString& other) const
90     {
91         DCHECK_CC(other.str_ != nullptr);
92         return strcmp(str_, other.str_) == 0;
93     }
94 
95     bool operator!=(const CString& other) const { return !(strcmp(str_, other.str_) == 0); }
96 
97     bool operator<(const CString& other) const { return strcmp(str_, other.str_) < 0; }
98 
99     const char& operator[](size_t index) const;
100 
101     char& operator[](size_t index);
102 
103     // Split a string into string tokens according to the separator, such as blank space
104     static std::vector<CString> Split(CString& source, char separator = ' ');
105 
106     static CString Strip(CString& source, char separator = ' ');
107     static char* BaseName(const CString& path);
108 
ToLowerCase()109     CString& ToLowerCase()
110     {
111         for (size_t i = 0; i < length_; ++i) {
112             if (str_[i] >= 'A' && str_[i] <= 'Z') {
113                 str_[i] += ('a' - 'A');
114             }
115         }
116         return *this;
117     }
118 
119     // helpers for logging one line with no more than 256 characters
120     static CString FormatString(const char* format, ...);
121 
122     static size_t ParseSizeFromEnv(const CString& env);
123     static size_t ParseTimeFromEnv(const CString& env);
124     static int ParseNumFromEnv(const CString& env);
125     static size_t ParsePosNumFromEnv(const CString& env);
126     static double ParsePosDecFromEnv(const CString& env);
127     static bool ParseFlagFromEnv(const CString& s);
128     static bool IsPosNumber(const CString& s);
129     static bool IsPosDecimal(const CString& s);
130     static bool IsNumber(const CString& s);
131     static constexpr size_t C_STRING_MIN_SIZE = 16;
132 
133 private:
134     char* str_ = nullptr;
135     size_t capacity_ = C_STRING_MIN_SIZE;
136     size_t length_ = 0;
137 };
138 } // namespace common
139 #endif // COMMON_COMPONENTS_BASE_C_STRING_H
140