string_util.h 4.7 KB
Newer Older
L
lujiale 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
/**
 * Copyright 2019-2020 Huawei Technologies Co., Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef INC_FRAMEWORK_COMMON_STRING_UTIL_H_
#define INC_FRAMEWORK_COMMON_STRING_UTIL_H_

#include <securec.h>

#include <algorithm>
#include <functional>
#include <sstream>
#include <string>
#include <vector>

namespace ge {
class StringUtils {
 public:
  static std::string &Ltrim(std::string &s) {
#if __cplusplus >= 201103L
    (void)s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c) { return !std::isspace(c); }));
#else
    (void)s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
#endif
    return s;
  }

  static std::string &Rtrim(std::string &s) {
#if __cplusplus >= 201103L
    (void)s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int c) { return !std::isspace(c); }));
#else
    (void)s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
#endif
    return s;
  }

  ///
  ///  @ingroup domi_common
  ///  @brief delete spaces at the beginning and end of a string
  ///  @param [in] string to be trimmed
  ///  @return string after trim
  ///
  static std::string &Trim(std::string &s) { return Ltrim(Rtrim(s)); }

  ///
  ///  @ingroup domi_common
  ///  @brief string splitting
  ///  @param [in] str string to be trimmed
  ///  @param [in] delim  separator
  ///  @return string array after segmentation
  ///
  static std::vector<std::string> Split(const std::string &str, char delim) {
    std::vector<std::string> elems;

    if (str.empty()) {
      elems.emplace_back("");
      return elems;
    }

    std::stringstream ss(str);
    std::string item;

    while (getline(ss, item, delim)) {
      elems.push_back(item);
    }

    auto str_size = str.size();
    if (str_size > 0 && str[str_size - 1] == delim) {
      elems.emplace_back("");
    }

    return elems;
  }
  ///
  ///  @ingroup domi_common
  ///  @brief obtain the file name
  ///  @param [in] s path name
  ///  @return file name
  ///
  static std::string GetFileName(std::string &s) {
    if (s.empty()) {
      return "";
    }
    std::vector<std::string> files = StringUtils::Split(s, '/');

    return files.empty() ? "" : files[files.size() - 1];
  }
  ///
  ///  @ingroup domi_common
  ///  @brief full replacement
  ///  @link
  ///  @param [in] str str string to be replaced
  ///  @param [in] old_value  old Characters Before Replacement
  ///  @param [in] new_value  new Characters Before Replacement
  ///  @return string after replacement
  ///
  static std::string ReplaceAll(std::string str, const std::string &old_value, const std::string &new_value) {
    std::string::size_type cur_pos = 0;
    std::string::size_type old_length = old_value.length();
    std::string::size_type new_length = new_value.length();
    // cycle replace
    for (; cur_pos != std::string::npos; cur_pos += new_length) {
      if ((cur_pos = str.find(old_value, cur_pos)) != std::string::npos) {
        (void)str.replace(cur_pos, old_length, new_value);
      } else {
        break;
      }
    }
    return str;
  }

  ///
  ///  @ingroup domi_common
  ///  @brief checks whether a character string starts with a character string (prefix)
  ///  @link
  ///  @param [in] str string to be compared
  ///  @param [in] str_x prefix
  ///  @return if the value is a prefix, true is returned. Otherwise, false is returned
  ///
  static bool StartWith(const std::string &str, const std::string str_x) {
    return ((str.size() >= str_x.size()) && (str.compare(0, str_x.size(), str_x) == 0));
  }

  ///
  ///  @ingroup domi_common
  ///  @brief format string
  ///  @link
  ///  @param [in] format specifies the character string format
  ///  @param [in] ... format Filling Content
  ///  @return formatted string
  ///
  static std::string FormatString(const char *format, ...) {
    const uint32_t MAX_BUFFER_LEN = 1024;  // the stack memory plint check result must be less than 1024
    va_list args;
    va_start(args, format);
    char buffer[MAX_BUFFER_LEN] = {0};
    int32_t ret = vsnprintf_s(buffer, MAX_BUFFER_LEN, MAX_BUFFER_LEN - 1, format, args);
    va_end(args);
    return ret > 0 ? buffer : "";
  }
};
}  // namespace ge

#endif  // INC_FRAMEWORK_COMMON_STRING_UTIL_H_