• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "util/string_builder.h"
17 
18 #include "securec.h"
19 
20 #include "util/logger.h"
21 
22 namespace OHOS {
23 namespace Idl {
24 const char* StringBuilder::TAG = "StringBuilder";
25 constexpr int LINE_MAX_SIZE = 1024;
26 
~StringBuilder()27 StringBuilder::~StringBuilder()
28 {
29     if (buffer_ != nullptr) {
30         free(buffer_);
31     }
32 }
33 
Append(char c)34 StringBuilder& StringBuilder::Append(char c)
35 {
36     if (position_ + 1 >= capacity_) {
37         if (!Grow(1)) {
38             return *this;
39         }
40     }
41 
42     buffer_[position_] = c;
43     position_ += 1;
44     return *this;
45 }
46 
Append(const char * string)47 StringBuilder& StringBuilder::Append(const char* string)
48 {
49     if (string == nullptr || string[0] == '\0') {
50         return *this;
51     }
52 
53     size_t len = strlen(string);
54     if (position_ + len >= capacity_) {
55         if (!Grow(len)) {
56             return *this;
57         }
58     }
59 
60     if (memcpy_s(buffer_ + position_, capacity_ - position_, string, len) != EOK) {
61         return *this;
62     }
63     position_ += len;
64     return *this;
65 }
66 
Append(const String & string)67 StringBuilder& StringBuilder::Append(const String& string)
68 {
69     if (string.IsEmpty()) {
70         return *this;
71     }
72 
73     size_t len = static_cast<size_t>(string.GetLength());
74     if (position_ + len >= capacity_) {
75         if (!Grow(len)) {
76             return *this;
77         }
78     }
79 
80     if (memcpy_s(buffer_ + position_, capacity_ - position_, string.string(), len) != EOK) {
81         return *this;
82     }
83     position_ += len;
84     return *this;
85 }
86 
AppendFormat(const char * format,...)87 StringBuilder& StringBuilder::AppendFormat(const char* format, ...)
88 {
89     va_list args;
90     va_list argsCopy;
91 
92     va_start(args, format);
93     va_copy(argsCopy, args);
94 
95     char buf[LINE_MAX_SIZE] = {0};
96     int len = vsnprintf_s(buf, LINE_MAX_SIZE, LINE_MAX_SIZE - 1, format, args);
97     if (len <= 0) {
98         va_end(args);
99         va_end(argsCopy);
100         return *this;
101     }
102 
103     size_t positionTmp = position_ + static_cast<size_t>(len);
104     if (positionTmp >= capacity_) {
105         if (!Grow(len)) {
106             va_end(args);
107             va_end(argsCopy);
108             return *this;
109         }
110     }
111 
112     if (vsnprintf_s(buffer_ + position_, len + 1, len, format, argsCopy) < 0) {
113         va_end(args);
114         va_end(argsCopy);
115         return *this;
116     }
117     position_ += len;
118     va_end(args);
119     va_end(argsCopy);
120 
121     return *this;
122 }
123 
Grow(size_t size)124 bool StringBuilder::Grow(size_t size)
125 {
126     if (capacity_ > String::MAX_SIZE) {
127         Logger::E(TAG, "The StringBuilder is full.");
128         return false;
129     }
130     // 256->the default capacity.
131     size_t newSize = (capacity_ == 0 ? 256 : capacity_ * 2);
132     if (newSize < capacity_ + size) {
133         newSize = capacity_ + size;
134     }
135     if (newSize > String::MAX_SIZE) {
136         newSize = String::MAX_SIZE;
137     }
138     if (newSize <= capacity_) {
139         return false;
140     }
141 
142     char* newBuffer = reinterpret_cast<char*>(calloc(newSize, 1));
143     if (newBuffer == nullptr) {
144         Logger::E(TAG, "Fail to malloc %lu bytes memory.", newSize);
145         return false;
146     }
147 
148     if (buffer_ != nullptr) {
149         errno_t ret = memcpy_s(newBuffer, newSize, buffer_, capacity_);
150         if (ret != EOK) {
151             free(newBuffer);
152             newBuffer = nullptr;
153             return false;
154         }
155         free(buffer_);
156     }
157     buffer_ = newBuffer;
158     capacity_ = newSize;
159     return true;
160 }
161 
ToString() const162 String StringBuilder::ToString() const
163 {
164     return String(buffer_, position_);
165 }
166 }
167 }
168