1 /*
2 * Copyright (c) 2021 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 "base/utils/string_utils.h"
17
18 #ifndef WINDOWS_PLATFORM
19 #include "securec.h"
20 #endif
21
22 namespace OHOS::Ace::StringUtils {
23 namespace {
24
25 const size_t MAX_STRING_SIZE = 256;
26
27 }
28
29 const char DEFAULT_STRING[] = "error";
30 const std::wstring DEFAULT_WSTRING = L"error";
31 const std::u16string DEFAULT_USTRING = u"error";
32
FormatString(const char * fmt,...)33 const std::string FormatString(const char* fmt, ...)
34 {
35 va_list args;
36 va_start(args, fmt);
37 char name[MAX_STRING_SIZE] = { 0 };
38 if (vsnprintf_s(name, sizeof(name), sizeof(name) - 1, fmt, args) < 0) {
39 va_end(args);
40 return "";
41 }
42 va_end(args);
43 return name;
44 }
45
46 } // namespace OHOS::Ace::StringUtils