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