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 const size_t MAX_STRING_SIZE = 256;
25 } // namespace
26
27 const char DEFAULT_STRING[] = "error";
28 const std::wstring DEFAULT_WSTRING = L"error";
29 const std::u16string DEFAULT_USTRING = u"error";
30
FormatString(const char * fmt,...)31 const std::string FormatString(const char* fmt, ...)
32 {
33 va_list args;
34 va_start(args, fmt);
35 char name[MAX_STRING_SIZE] = { 0 };
36 if (vsnprintf_s(name, sizeof(name), sizeof(name) - 1, fmt, args) < 0) {
37 va_end(args);
38 return "";
39 }
40 va_end(args);
41 return name;
42 }
43
IsAscii(const std::string & str)44 bool IsAscii(const std::string& str)
45 {
46 for (const auto& c : str) {
47 if (!isascii(c)) {
48 return false;
49 }
50 }
51 return true;
52 }
53
54 } // namespace OHOS::Ace::StringUtils