• 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 = 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, argsCopy;
90 
91     va_start(args, format);
92     va_copy(argsCopy, args);
93 
94     char buf[LINE_MAX_SIZE] = {0};
95     int len = vsnprintf_s(buf, LINE_MAX_SIZE, LINE_MAX_SIZE - 1, format, args);
96     if (len <= 0) {
97         va_end(args);
98         va_end(argsCopy);
99         return *this;
100     }
101 
102     if (position_ + len >= capacity_) {
103         if (!Grow(len)) {
104             va_end(args);
105             va_end(argsCopy);
106             return *this;
107         }
108     }
109 
110     if (vsnprintf_s(buffer_ + position_, len + 1, len, format, argsCopy) < 0) {
111         va_end(args);
112         va_end(argsCopy);
113         return *this;
114     }
115     position_ += len;
116     va_end(args);
117     va_end(argsCopy);
118 
119     return *this;
120 }
121 
Grow(size_t size)122 bool StringBuilder::Grow(size_t size)
123 {
124     if (capacity_ > String::MAX_SIZE) {
125         Logger::E(TAG, "The StringBuilder is full.");
126         return false;
127     }
128     // 256->the default capacity.
129     size_t newSize = (capacity_ == 0 ? 256 : capacity_ * 2);
130     if (newSize < capacity_ + size) {
131         newSize = capacity_ + size;
132     }
133     if (newSize > String::MAX_SIZE) {
134         newSize = String::MAX_SIZE;
135     }
136     if (newSize <= capacity_) {
137         return false;
138     }
139 
140     char* newBuffer = reinterpret_cast<char*>(calloc(newSize, 1));
141     if (newBuffer == nullptr) {
142         Logger::E(TAG, "Fail to malloc %lu bytes memory.", newSize);
143         return false;
144     }
145 
146     if (buffer_ != nullptr) {
147         errno_t ret = memcpy_s(newBuffer, newSize, buffer_, capacity_);
148         if (ret != EOK) {
149             free(newBuffer);
150             newBuffer = nullptr;
151             return false;
152         }
153         free(buffer_);
154     }
155     buffer_ = newBuffer;
156     capacity_ = newSize;
157     return true;
158 }
159 
ToString() const160 String StringBuilder::ToString() const
161 {
162     return String(buffer_, position_);
163 }
164 }
165 }
166