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 #include "core/pipeline/base/constants.h"
18
19 #ifndef WINDOWS_PLATFORM
20 #include "securec.h"
21 #endif
22
23 namespace OHOS::Ace::StringUtils {
24 namespace {
25 const size_t MAX_STRING_SIZE = 256;
26 } // namespace
27 const double RADIANS_VALUE = 2 * ACE_PI; // one turn means 2*pi rad
28 const char DEFAULT_STRING[] = "error";
29 const std::wstring DEFAULT_WSTRING = L"error";
30 const std::u16string DEFAULT_USTRING = u"error";
31 const std::u32string DEFAULT_U32STRING = 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
IsAscii(const std::string & str)46 bool IsAscii(const std::string& str)
47 {
48 for (const auto& c : str) {
49 if (!isascii(c)) {
50 return false;
51 }
52 }
53 return true;
54 }
55
56 } // namespace OHOS::Ace::StringUtils