string_helper.h 4.2 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  int len = snprintf(NULL, 0, fmt, args...);
  CHECK_GE(len, 0);
  size_t oldlen = str.length();
  str.resize(oldlen + len + 1);
  CHECK(snprintf(&str[oldlen], (size_t)len + 1, fmt, args...) == len);
  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 已提交
64
std::string trim_spaces(const std::string& str);
D
dongdaxiang 已提交
65

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

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

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

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

130 131
  int i = 0;
  for (auto& elem : strs) {
D
dongdaxiang 已提交
132 133 134 135
    if (i > 0) {
      str += delim;
    }

136 137
    str += boost::lexical_cast<std::string>(elem);
    ++i;
D
dongdaxiang 已提交
138 139 140 141 142 143 144
  }

  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 已提交
145

D
dongdaxiang 已提交
146 147 148 149 150 151 152
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 已提交
153
  char* getdelim(FILE* f, char delim);
D
dongdaxiang 已提交
154 155 156 157 158 159 160 161
  char* get() { return _buffer; }
  size_t length() { return _length; }

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