string_helper.h 4.5 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 27
#include "boost/lexical_cast.hpp"
#include "glog/logging.h"

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

D
dongdaxiang 已提交
30
inline size_t count_spaces(const char* s);
D
dongdaxiang 已提交
31

D
dongdaxiang 已提交
32
inline size_t count_nonspaces(const char* s);
D
dongdaxiang 已提交
33 34 35

template <class... ARGS>
void format_string_append(std::string& str, const char* fmt,  // NOLINT
D
dongdaxiang 已提交
36
                          ARGS&&... args) {
D
dongdaxiang 已提交
37 38 39 40
  int len = snprintf(NULL, 0, fmt, args...);
  CHECK_GE(len, 0);
  size_t oldlen = str.length();
  str.resize(oldlen + len + 1);
Z
Zeng Jinle 已提交
41 42
  CHECK(snprintf(&str[oldlen], (size_t)len + 1, fmt, args...) ==  // NOLINT
        len);
D
dongdaxiang 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  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...);
  return std::move(str);
}

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 已提交
65
std::string trim_spaces(const std::string& str);
D
dongdaxiang 已提交
66

67 68 69
// erase all spaces in str
std::string erase_spaces(const std::string& str);

D
dongdaxiang 已提交
70
int str_to_float(const char* str, float* v);
D
dongdaxiang 已提交
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

// 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;
}

127 128
template <class Container>
std::string join_strings(const Container& strs, char delim) {
D
dongdaxiang 已提交
129 130
  std::string str;

Z
Zeng Jinle 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  size_t i = 0;
  for (auto& elem : strs) {
    if (i > 0) {
      str += delim;
    }

    str += boost::lexical_cast<std::string>(elem);
    ++i;
  }

  return str;
}

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

  size_t i = 0;
149
  for (auto& elem : strs) {
D
dongdaxiang 已提交
150 151 152 153
    if (i > 0) {
      str += delim;
    }

154 155
    str += boost::lexical_cast<std::string>(elem);
    ++i;
D
dongdaxiang 已提交
156 157 158 159 160 161 162
  }

  return str;
}

// 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 已提交
163

D
dongdaxiang 已提交
164 165 166 167 168 169 170
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 已提交
171
  char* getdelim(FILE* f, char delim);
D
dongdaxiang 已提交
172 173 174 175 176 177 178 179
  char* get() { return _buffer; }
  size_t length() { return _length; }

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