1/* 2 * Copyright (c) 2023 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#ifndef STRING_UTIL_H 17#define STRING_UTIL_H 18 19#include <cstdint> 20#include <cstdio> 21#include <stdlib.h> 22#include <string> 23#include <string.h> 24#include <securec.h> 25 26namespace OHOS { 27namespace HiviewDFX { 28#ifndef PATH_MAX 29#define PATH_MAX 1024 30#endif 31 32inline bool RealPath(const std::string& path, std::string& realPath) 33{ 34#if is_ohos 35 realPath.reserve(PATH_MAX); 36 realPath.resize(PATH_MAX - 1); 37 if (realpath(path.c_str(), &(realPath[0])) == nullptr) { 38 return false; 39 } 40#else 41 realPath = path; 42#endif 43 return true; 44} 45 46inline bool StartsWith(const std::string& s, const std::string& prefix) 47{ 48 return s.substr(0, prefix.size()) == prefix; 49} 50 51inline bool StartsWith(const std::string& s, char prefix) 52{ 53 return !s.empty() && s.front() == prefix; 54} 55 56inline bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix) 57{ 58 return s.size() >= prefix.size() && strncasecmp(s.data(), prefix.data(), prefix.size()) == 0; 59} 60 61inline bool EndsWith(const std::string& s, const std::string& suffix) 62{ 63 return s.size() >= suffix.size() && 64 s.substr(s.size() - suffix.size(), suffix.size()) == suffix; 65} 66 67inline bool EndsWith(const std::string& s, char suffix) 68{ 69 return !s.empty() && s.back() == suffix; 70} 71 72inline bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix) 73{ 74 return s.size() >= suffix.size() && 75 strncasecmp(s.data() + (s.size() - suffix.size()), suffix.data(), suffix.size()) == 0; 76} 77 78inline void Trim(std::string& str) 79{ 80 std::string blanks("\f\v\r\t\n "); 81 str.erase(0, str.find_first_not_of(blanks)); 82 str.erase(str.find_last_not_of(blanks) + sizeof(char)); 83} 84} // namespace HiviewDFX 85} // namespace OHOS 86#endif 87