提交 6be9f719 编写于 作者: D dongdaxiang

make string_helper dependency work

test=develop
上级 e95cafd9
cc_library(fs SRCS fs.cc DEPS glog boost) cc_library(fs SRCS fs.cc DEPS string_helper glog boost)
cc_library(shell SRCS shell.cc DEPS glog) cc_library(shell SRCS shell.cc DEPS string_helper glog)
cc_library(stringpiece SRCS piece.cc) cc_library(stringpiece SRCS piece.cc)
cc_library(pretty_log SRCS pretty_log.cc) cc_library(pretty_log SRCS pretty_log.cc)
cc_library(string_helper SRCS string_helper.cc DEPS boost)
cc_test(stringpiece_test SRCS piece_test.cc DEPS stringpiece glog gflags) cc_test(stringpiece_test SRCS piece_test.cc DEPS stringpiece glog gflags)
cc_test(stringprintf_test SRCS printf_test.cc DEPS glog gflags) cc_test(stringprintf_test SRCS printf_test.cc DEPS glog gflags)
cc_test(to_string_test SRCS to_string_test.cc) cc_test(to_string_test SRCS to_string_test.cc)
......
// 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"
#include <ctype.h>
#include <stdio.h>
#include <cstring>
#include <string>
#include <vector>
#include "boost/lexical_cast.hpp"
#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);
}
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;
}
// 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) {
int32_t ret = ::getdelim(&_buffer, &_buf_size, delim, f);
if (ret >= 0) {
if (ret >= 1 && _buffer[ret - 1] == delim) {
_buffer[--ret] = 0;
}
_length = (size_t)ret;
return _buffer;
} else {
_length = 0;
CHECK(feof(f));
return NULL;
}
}
} // end namespace string
} // end namespace paddle
...@@ -26,29 +26,13 @@ ...@@ -26,29 +26,13 @@
namespace paddle { namespace paddle {
namespace string { namespace string {
inline size_t count_spaces(const char* s) { inline size_t count_spaces(const char* s);
size_t count = 0;
while (*s != 0 && isspace(*s++)) { inline size_t count_nonspaces(const char* s);
count++;
}
return count;
}
inline size_t count_nonspaces(const char* s) {
size_t count = 0;
while (*s != 0 && !isspace(*s++)) {
count++;
}
return count;
}
template <class... ARGS> template <class... ARGS>
void format_string_append(std::string& str, const char* fmt, // NOLINT void format_string_append(std::string& str, const char* fmt, // NOLINT
ARGS&&... args) { // use VA_ARGS may be better ? ARGS&&... args) {
int len = snprintf(NULL, 0, fmt, args...); int len = snprintf(NULL, 0, fmt, args...);
CHECK_GE(len, 0); CHECK_GE(len, 0);
size_t oldlen = str.length(); size_t oldlen = str.length();
...@@ -76,35 +60,9 @@ std::string format_string(const std::string& fmt, ARGS&&... args) { ...@@ -76,35 +60,9 @@ std::string format_string(const std::string& fmt, ARGS&&... args) {
} }
// remove leading and tailing spaces // remove leading and tailing spaces
inline std::string trim_spaces(const std::string& str) { std::string trim_spaces(const std::string& str);
const char* p = str.c_str();
while (*p != 0 && isspace(*p)) { int str_to_float(const char* str, float* v);
p++;
}
size_t len = strlen(p);
while (len > 0 && isspace(p[len - 1])) {
len--;
}
return std::string(p, len);
}
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;
}
// split string by delim // split string by delim
template <class T = std::string> template <class T = std::string>
...@@ -117,7 +75,6 @@ std::vector<T> split_string(const std::string& str, const std::string& delim) { ...@@ -117,7 +75,6 @@ std::vector<T> split_string(const std::string& str, const std::string& delim) {
if (str.empty()) { if (str.empty()) {
return res_list; return res_list;
} }
while ((pos = str.find(delim, pre_pos)) != std::string::npos) { while ((pos = str.find(delim, pre_pos)) != std::string::npos) {
tmp_str.assign(str, pre_pos, pos - pre_pos); tmp_str.assign(str, pre_pos, pos - pre_pos);
res_list.push_back(tmp_str); res_list.push_back(tmp_str);
...@@ -128,30 +85,6 @@ std::vector<T> split_string(const std::string& str, const std::string& delim) { ...@@ -128,30 +85,6 @@ std::vector<T> split_string(const std::string& str, const std::string& delim) {
res_list.push_back(tmp_str); res_list.push_back(tmp_str);
} }
return res_list; return res_list;
/*
size_t num = 1;
const char* p;
for (p = str.c_str(); *p != 0; p++) {
if (*p == delim) {
num++;
}
}
std::vector<T> list(num);
const char* last = str.c_str();
num = 0;
for (p = str.c_str(); *p != 0; p++) {
if (*p == delim) {
list[num++] = boost::lexical_cast<T>(last, p - last);
last = p + 1;
}
}
list[num] = boost::lexical_cast<T>(last, p - last);
return list;
*/
} }
// split string by spaces. Leading and tailing spaces are ignored. Consecutive // split string by spaces. Leading and tailing spaces are ignored. Consecutive
...@@ -183,7 +116,6 @@ std::vector<T> split_string(const std::string& str) { ...@@ -183,7 +116,6 @@ std::vector<T> split_string(const std::string& str) {
p++; p++;
} }
} }
return list; return list;
} }
...@@ -204,6 +136,7 @@ std::string join_strings(const std::vector<T>& strs, char delim) { ...@@ -204,6 +136,7 @@ std::string join_strings(const std::vector<T>& strs, char delim) {
// A helper class for reading lines from file. A line buffer is maintained. It // 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. // doesn't need to know the maximum possible length of a line.
class LineFileReader { class LineFileReader {
public: public:
LineFileReader() {} LineFileReader() {}
...@@ -211,22 +144,7 @@ class LineFileReader { ...@@ -211,22 +144,7 @@ class LineFileReader {
LineFileReader(const LineFileReader&) = delete; LineFileReader(const LineFileReader&) = delete;
~LineFileReader() { ::free(_buffer); } ~LineFileReader() { ::free(_buffer); }
char* getline(FILE* f) { return this->getdelim(f, '\n'); } char* getline(FILE* f) { return this->getdelim(f, '\n'); }
char* getdelim(FILE* f, char delim) { char* getdelim(FILE* f, char delim);
int32_t ret = ::getdelim(&_buffer, &_buf_size, delim, f);
if (ret >= 0) {
if (ret >= 1 && _buffer[ret - 1] == delim) {
_buffer[--ret] = 0;
}
_length = (size_t)ret;
return _buffer;
} else {
_length = 0;
CHECK(feof(f));
return NULL;
}
}
char* get() { return _buffer; } char* get() { return _buffer; }
size_t length() { return _length; } size_t length() { return _length; }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册