• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "string_help.h"
16 #include <cstdio>
17 #include <memory.h>
18 #define UNUSED(expr)             \
19     do {                         \
20         static_cast<void>(expr); \
21     } while (0)
22 
23 #if !is_mingw
memcpy_s(void * dest,uint32_t destSize,const void * src,size_t srcSize)24 int32_t memcpy_s(void* dest, uint32_t destSize, const void* src, size_t srcSize)
25 {
26     if (srcSize > destSize || src == nullptr || dest == nullptr) {
27         return -1;
28     } else {
29         if (!memcpy(dest, src, srcSize)) {
30             printf("memcpy fail\n");
31             return -1;
32         }
33     }
34     return 0;
35 }
36 
sscanf_s(const char * buffer,const char * format,...)37 int32_t sscanf_s(const char* buffer, const char* format, ...)
38 {
39     va_list ap;
40     __builtin_va_start(ap, format);
41     int32_t ret = scanf(buffer, format, ap);
42     __builtin_va_end(ap);
43     return ret;
44 }
45 
strncpy_s(char * strDest,size_t destMax,const char * strSrc,size_t count)46 int32_t strncpy_s(char* strDest, size_t destMax, const char* strSrc, size_t count)
47 {
48     (void)strncpy(strDest, strSrc, destMax);
49     return destMax;
50 }
51 #endif
52 
memset_s(void * dest,size_t destSize,int32_t ch,size_t n)53 void* memset_s(void* dest, size_t destSize, int32_t ch, size_t n)
54 {
55     UNUSED(destSize);
56     UNUSED(ch);
57     return memset(dest, 0, n);
58 }
59 
snprintf_s(char * strDest,size_t destMax,size_t count,const char * format,...)60 int32_t snprintf_s(char* strDest, size_t destMax, size_t count, const char* format, ...)
61 {
62     UNUSED(count);
63     int32_t ret;
64     va_list ap;
65     __builtin_va_start(ap, format);
66     ret = vsnprintf(strDest, destMax, format, ap);
67     __builtin_va_end(ap);
68     return ret;
69 }
70 
sprintf_s(char * strDest,size_t destMax,const char * format,...)71 int32_t sprintf_s(char* strDest, size_t destMax, const char* format, ...)
72 {
73     va_list ap;
74     __builtin_va_start(ap, format);
75     int32_t ret = vsnprintf(strDest, destMax, format, ap);
76     __builtin_va_end(ap);
77     return ret;
78 }
79 
GetDemangleSymbolIndex(const char * mangled)80 const char* GetDemangleSymbolIndex(const char* mangled)
81 {
82     int status = 0;
83     auto demangle = abi::__cxa_demangle(mangled, nullptr, nullptr, &status);
84     if (status) { // status != 0 failed
85         return mangled;
86     } else {
87         return demangle;
88     }
89 }
90 
GetProcessorNumFromString(char * str)91 int GetProcessorNumFromString(char* str)
92 {
93     int processorNum = 0;
94     int lastNum = -1;
95     char* s = str;
96     while (*s != '\0') {
97         if (isdigit(*s)) {
98             int currentNum = strtol(s, &s, 10);
99             if (lastNum == -1) {
100                 processorNum++;
101             } else {
102                 processorNum += currentNum - lastNum;
103             }
104             lastNum = currentNum;
105         } else {
106             if (*s == ',') {
107                 lastNum = -1;
108             }
109             s++;
110         }
111     }
112     return processorNum;
113 }
114 
StartWith(const std::string & str,const std::string & res)115 bool StartWith(const std::string& str, const std::string& res)
116 {
117     if (res.size() > str.size()) {
118         return false;
119     }
120     return str.compare(0, res.length(), res) == 0;
121 }
122 
EndWith(const std::string & str,const std::string & res)123 bool EndWith(const std::string& str, const std::string& res)
124 {
125     if (res.size() > str.size()) {
126         return false;
127     }
128     return str.compare(str.size() - res.size(), res.size(), res) == 0;
129 }
130 
SplitStringToVec(const std::string & str,const std::string & pat)131 std::vector<std::string> SplitStringToVec(const std::string& str, const std::string& pat)
132 {
133     std::vector<std::string> result;
134     size_t curPos = 0;
135     size_t strSize = str.size();
136     size_t patSize = pat.size();
137     while (curPos < strSize) {
138         auto patPos = str.find(pat, curPos);
139         if (patPos == std::string::npos) {
140             break;
141         }
142         result.emplace_back(str.substr(curPos, patPos - curPos));
143         curPos = patPos + patSize;
144     }
145     if (curPos < strSize) {
146         result.emplace_back(str.substr(curPos));
147     }
148 
149     return result;
150 }
151