string_helper.cc 2.6 KB
Newer Older
D
dongdaxiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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.

#include "paddle/fluid/string/string_helper.h"
W
wanghuancoder 已提交
16

D
dongdaxiang 已提交
17 18 19 20
#include <ctype.h>
#include <stdio.h>
#include <cstring>
#include <string>
W
wanghuancoder 已提交
21

D
dongdaxiang 已提交
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
#include "glog/logging.h"

namespace paddle {
namespace string {

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

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

// remove leading and tailing spaces
std::string trim_spaces(const std::string& str) {
  const char* p = str.c_str();

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

  size_t len = strlen(p);

  while (len > 0 && isspace(p[len - 1])) {
    len--;
  }

  return std::string(p, len);
}

64 65 66 67 68 69 70 71 72 73 74 75 76
std::string erase_spaces(const std::string& str) {
  std::string result;
  result.reserve(str.size());
  const char* p = str.c_str();
  while (*p != 0) {
    if (!isspace(*p)) {
      result.append(p, 1);
    }
    ++p;
  }
  return result;
}

D
dongdaxiang 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90
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;
}

91 92 93 94 95
bool ends_with(std::string const& input, std::string const& test) {
  if (test.size() > input.size()) return false;
  return std::equal(test.rbegin(), test.rend(), input.rbegin());
}

D
dongdaxiang 已提交
96 97 98 99
// 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.
char* LineFileReader::getdelim(FILE* f, char delim) {
100
#ifndef _WIN32
D
dongdaxiang 已提交
101 102 103 104 105 106 107
  int32_t ret = ::getdelim(&_buffer, &_buf_size, delim, f);

  if (ret >= 0) {
    if (ret >= 1 && _buffer[ret - 1] == delim) {
      _buffer[--ret] = 0;
    }

108
    _length = static_cast<size_t>(ret);
D
dongdaxiang 已提交
109 110 111 112 113 114
    return _buffer;
  } else {
    _length = 0;
    CHECK(feof(f));
    return NULL;
  }
D
dongdaxiang 已提交
115 116 117
#else
  return NULL;
#endif
D
dongdaxiang 已提交
118 119 120 121
}

}  // end namespace string
}  // end namespace paddle