string_helper.h 5.4 KB
Newer Older
D
dongdaxiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// 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.

#pragma once

#include <ctype.h>
#include <stdio.h>
#include <cstring>
#include <string>
X
xjqbest 已提交
21
#include <utility>
22
#include <vector>
W
wanghuancoder 已提交
23

D
dongdaxiang 已提交
24 25 26
#include "glog/logging.h"

namespace paddle {
27
namespace string {
D
dongdaxiang 已提交
28

Z
zhaocaibei123 已提交
29 30
inline size_t count_spaces(const char* s) {
  size_t count = 0;
D
dongdaxiang 已提交
31

Z
zhaocaibei123 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  while (*s != 0 && isspace(*s++)) {
    count++;
  }

  return count;
}

inline size_t count_nonspaces(const char* s) {
  size_t count = 0;

  while (*s != 0 && !isspace(*s++)) {
    count++;
  }

  return count;
}
D
dongdaxiang 已提交
48 49 50

template <class... ARGS>
void format_string_append(std::string& str, const char* fmt,  // NOLINT
D
dongdaxiang 已提交
51
                          ARGS&&... args) {
D
dongdaxiang 已提交
52 53 54 55
  int len = snprintf(NULL, 0, fmt, args...);
  CHECK_GE(len, 0);
  size_t oldlen = str.length();
  str.resize(oldlen + len + 1);
56

Z
Zeng Jinle 已提交
57 58
  CHECK(snprintf(&str[oldlen], (size_t)len + 1, fmt, args...) ==  // NOLINT
        len);
D
dongdaxiang 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71
  str.resize(oldlen + len);
}

template <class... ARGS>
void format_string_append(std::string& str, const std::string& fmt,  // NOLINT
                          ARGS&&... args) {
  format_string_append(str, fmt.c_str(), args...);
}

template <class... ARGS>
std::string format_string(const char* fmt, ARGS&&... args) {
  std::string str;
  format_string_append(str, fmt, args...);
T
Tomasz Socha 已提交
72
  return str;
D
dongdaxiang 已提交
73 74 75 76 77 78 79 80
}

template <class... ARGS>
std::string format_string(const std::string& fmt, ARGS&&... args) {
  return format_string(fmt.c_str(), args...);
}

// remove leading and tailing spaces
D
dongdaxiang 已提交
81
std::string trim_spaces(const std::string& str);
D
dongdaxiang 已提交
82

83 84 85
// erase all spaces in str
std::string erase_spaces(const std::string& str);

Z
zhaocaibei123 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98
inline int str_to_float(const char* str, float* v) {
  const char* head = str;
  char* cursor = NULL;
  int index = 0;
  while (*(head += count_spaces(head)) != 0) {
    v[index++] = std::strtof(head, &cursor);
    if (head == cursor) {
      break;
    }
    head = cursor;
  }
  return index;
}
D
dongdaxiang 已提交
99

100 101 102
// checks whether the test string is a suffix of the input string.
bool ends_with(std::string const& input, std::string const& test);

D
dongdaxiang 已提交
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 157
// split string by delim
template <class T = std::string>
std::vector<T> split_string(const std::string& str, const std::string& delim) {
  size_t pre_pos = 0;
  size_t pos = 0;
  std::string tmp_str;
  std::vector<T> res_list;
  res_list.clear();
  if (str.empty()) {
    return res_list;
  }
  while ((pos = str.find(delim, pre_pos)) != std::string::npos) {
    tmp_str.assign(str, pre_pos, pos - pre_pos);
    res_list.push_back(tmp_str);
    pre_pos = pos + 1;
  }
  tmp_str.assign(str, pre_pos, str.length() - pre_pos);
  if (!tmp_str.empty()) {
    res_list.push_back(tmp_str);
  }
  return res_list;
}

// split string by spaces. Leading and tailing spaces are ignored. Consecutive
// spaces are treated as one delim.
template <class T = std::string>
std::vector<T> split_string(const std::string& str) {
  std::vector<T> list;
  const char* p;
  int pre_pos = 0;
  int pos = 0;
  std::string tmp_str;
  if (str.empty()) {
    return list;
  }
  for (p = str.c_str(); *p != 0;) {
    if (!isspace(*p)) {
      pos = pre_pos;
      p++;

      while (*p != 0 && !isspace(*p)) {
        pos++;
        p++;
      }
      tmp_str.assign(str, pre_pos, pos - pre_pos + 1);
      list.push_back(tmp_str);
      pre_pos = pos + 1;
    } else {
      pre_pos++;
      p++;
    }
  }
  return list;
}

158 159
template <class Container>
std::string join_strings(const Container& strs, char delim) {
D
dongdaxiang 已提交
160 161
  std::string str;

Z
Zeng Jinle 已提交
162 163 164 165 166 167
  size_t i = 0;
  for (auto& elem : strs) {
    if (i > 0) {
      str += delim;
    }

168 169 170
    std::stringstream ss;
    ss << elem;
    str += ss.str();
Z
Zeng Jinle 已提交
171 172 173 174 175 176 177 178 179 180 181
    ++i;
  }

  return str;
}

template <class Container>
std::string join_strings(const Container& strs, const std::string& delim) {
  std::string str;

  size_t i = 0;
182
  for (auto& elem : strs) {
D
dongdaxiang 已提交
183 184 185 186
    if (i > 0) {
      str += delim;
    }

187 188 189
    std::stringstream ss;
    ss << elem;
    str += ss.str();
190
    ++i;
D
dongdaxiang 已提交
191 192 193 194 195
  }

  return str;
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
template <class Container, class DelimT, class ConvertFunc>
std::string join_strings(const Container& strs, DelimT&& delim,
                         ConvertFunc&& func) {
  std::stringstream ss;
  size_t i = 0;
  for (const auto& elem : strs) {
    if (i > 0) {
      ss << delim;
    }
    ss << func(elem);
    ++i;
  }

  return ss.str();
}

D
dongdaxiang 已提交
212 213
// A helper class for reading lines from file. A line buffer is maintained. It
// doesn't need to know the maximum possible length of a line.
D
dongdaxiang 已提交
214

D
dongdaxiang 已提交
215 216 217 218 219 220 221
class LineFileReader {
 public:
  LineFileReader() {}
  LineFileReader(LineFileReader&&) = delete;
  LineFileReader(const LineFileReader&) = delete;
  ~LineFileReader() { ::free(_buffer); }
  char* getline(FILE* f) { return this->getdelim(f, '\n'); }
D
dongdaxiang 已提交
222
  char* getdelim(FILE* f, char delim);
D
dongdaxiang 已提交
223 224 225 226 227 228 229 230
  char* get() { return _buffer; }
  size_t length() { return _length; }

 private:
  char* _buffer = NULL;
  size_t _buf_size = 0;
  size_t _length = 0;
};
231
}  // end namespace string
D
dongdaxiang 已提交
232
}  // end namespace paddle