1800b99b8Sopenharmony_ci/* 2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License. 5800b99b8Sopenharmony_ci * You may obtain a copy of the License at 6800b99b8Sopenharmony_ci * 7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8800b99b8Sopenharmony_ci * 9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and 13800b99b8Sopenharmony_ci * limitations under the License. 14800b99b8Sopenharmony_ci */ 15800b99b8Sopenharmony_ci 16800b99b8Sopenharmony_ci#ifndef STRING_UTIL_H 17800b99b8Sopenharmony_ci#define STRING_UTIL_H 18800b99b8Sopenharmony_ci 19800b99b8Sopenharmony_ci#include <cstdint> 20800b99b8Sopenharmony_ci#include <cstdio> 21800b99b8Sopenharmony_ci#include <stdlib.h> 22800b99b8Sopenharmony_ci#include <string> 23800b99b8Sopenharmony_ci#include <string.h> 24800b99b8Sopenharmony_ci#include <securec.h> 25800b99b8Sopenharmony_ci 26800b99b8Sopenharmony_cinamespace OHOS { 27800b99b8Sopenharmony_cinamespace HiviewDFX { 28800b99b8Sopenharmony_ci#ifndef PATH_MAX 29800b99b8Sopenharmony_ci#define PATH_MAX 1024 30800b99b8Sopenharmony_ci#endif 31800b99b8Sopenharmony_ci 32800b99b8Sopenharmony_ciinline bool RealPath(const std::string& path, std::string& realPath) 33800b99b8Sopenharmony_ci{ 34800b99b8Sopenharmony_ci#if is_ohos 35800b99b8Sopenharmony_ci realPath.reserve(PATH_MAX); 36800b99b8Sopenharmony_ci realPath.resize(PATH_MAX - 1); 37800b99b8Sopenharmony_ci if (realpath(path.c_str(), &(realPath[0])) == nullptr) { 38800b99b8Sopenharmony_ci return false; 39800b99b8Sopenharmony_ci } 40800b99b8Sopenharmony_ci#else 41800b99b8Sopenharmony_ci realPath = path; 42800b99b8Sopenharmony_ci#endif 43800b99b8Sopenharmony_ci return true; 44800b99b8Sopenharmony_ci} 45800b99b8Sopenharmony_ci 46800b99b8Sopenharmony_ciinline bool StartsWith(const std::string& s, const std::string& prefix) 47800b99b8Sopenharmony_ci{ 48800b99b8Sopenharmony_ci return s.substr(0, prefix.size()) == prefix; 49800b99b8Sopenharmony_ci} 50800b99b8Sopenharmony_ci 51800b99b8Sopenharmony_ciinline bool StartsWith(const std::string& s, char prefix) 52800b99b8Sopenharmony_ci{ 53800b99b8Sopenharmony_ci return !s.empty() && s.front() == prefix; 54800b99b8Sopenharmony_ci} 55800b99b8Sopenharmony_ci 56800b99b8Sopenharmony_ciinline bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix) 57800b99b8Sopenharmony_ci{ 58800b99b8Sopenharmony_ci return s.size() >= prefix.size() && strncasecmp(s.data(), prefix.data(), prefix.size()) == 0; 59800b99b8Sopenharmony_ci} 60800b99b8Sopenharmony_ci 61800b99b8Sopenharmony_ciinline bool EndsWith(const std::string& s, const std::string& suffix) 62800b99b8Sopenharmony_ci{ 63800b99b8Sopenharmony_ci return s.size() >= suffix.size() && 64800b99b8Sopenharmony_ci s.substr(s.size() - suffix.size(), suffix.size()) == suffix; 65800b99b8Sopenharmony_ci} 66800b99b8Sopenharmony_ci 67800b99b8Sopenharmony_ciinline bool EndsWith(const std::string& s, char suffix) 68800b99b8Sopenharmony_ci{ 69800b99b8Sopenharmony_ci return !s.empty() && s.back() == suffix; 70800b99b8Sopenharmony_ci} 71800b99b8Sopenharmony_ci 72800b99b8Sopenharmony_ciinline bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix) 73800b99b8Sopenharmony_ci{ 74800b99b8Sopenharmony_ci return s.size() >= suffix.size() && 75800b99b8Sopenharmony_ci strncasecmp(s.data() + (s.size() - suffix.size()), suffix.data(), suffix.size()) == 0; 76800b99b8Sopenharmony_ci} 77800b99b8Sopenharmony_ci 78800b99b8Sopenharmony_ciinline void Trim(std::string& str) 79800b99b8Sopenharmony_ci{ 80800b99b8Sopenharmony_ci std::string blanks("\f\v\r\t\n "); 81800b99b8Sopenharmony_ci str.erase(0, str.find_first_not_of(blanks)); 82800b99b8Sopenharmony_ci str.erase(str.find_last_not_of(blanks) + sizeof(char)); 83800b99b8Sopenharmony_ci} 84800b99b8Sopenharmony_ci} // namespace HiviewDFX 85800b99b8Sopenharmony_ci} // namespace OHOS 86800b99b8Sopenharmony_ci#endif 87