diff --git a/paddle/fluid/framework/io/CMakeLists.txt b/paddle/fluid/framework/io/CMakeLists.txt index bc43f569b7483c0e5a5d3c6dbad67a63c002f222..2baef77b9ce32ce616e7781b971665d3d885066c 100644 --- a/paddle/fluid/framework/io/CMakeLists.txt +++ b/paddle/fluid/framework/io/CMakeLists.txt @@ -1,2 +1,2 @@ -cc_library(fs SRCS fs.cc DEPS glog boost) -cc_library(shell SRCS shell.cc DEPS glog) +cc_library(fs SRCS fs.cc DEPS string_helper glog boost) +cc_library(shell SRCS shell.cc DEPS string_helper glog) diff --git a/paddle/fluid/string/CMakeLists.txt b/paddle/fluid/string/CMakeLists.txt index 169a925d12328e7d1df744635445b5674c19b125..49a8fb82dbf67357c1c3f2658538789af51b7cdc 100644 --- a/paddle/fluid/string/CMakeLists.txt +++ b/paddle/fluid/string/CMakeLists.txt @@ -1,5 +1,6 @@ cc_library(stringpiece SRCS piece.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(stringprintf_test SRCS printf_test.cc DEPS glog gflags) cc_test(to_string_test SRCS to_string_test.cc) diff --git a/paddle/fluid/string/string_helper.cc b/paddle/fluid/string/string_helper.cc new file mode 100644 index 0000000000000000000000000000000000000000..d5ae5b1e33f8652af02b196bcc4c577bd1952bd7 --- /dev/null +++ b/paddle/fluid/string/string_helper.cc @@ -0,0 +1,99 @@ +// 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 +#include +#include +#include +#include +#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 diff --git a/paddle/fluid/string/string_helper.h b/paddle/fluid/string/string_helper.h index c3b99a979757e7d981706867b799bca6396401a5..bec11b39f765676d25824764cd122dddfa7eb0a5 100644 --- a/paddle/fluid/string/string_helper.h +++ b/paddle/fluid/string/string_helper.h @@ -26,29 +26,13 @@ namespace paddle { namespace string { -inline size_t count_spaces(const char* s) { - size_t count = 0; +inline size_t count_spaces(const char* s); - 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; -} +inline size_t count_nonspaces(const char* s); template 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...); CHECK_GE(len, 0); size_t oldlen = str.length(); @@ -76,35 +60,9 @@ std::string format_string(const std::string& fmt, ARGS&&... args) { } // remove leading and tailing spaces -inline std::string trim_spaces(const std::string& str) { - const char* p = str.c_str(); +std::string trim_spaces(const std::string& 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; -} +int str_to_float(const char* str, float* v); // split string by delim template @@ -117,7 +75,6 @@ std::vector split_string(const std::string& str, const std::string& delim) { 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); @@ -128,30 +85,6 @@ std::vector split_string(const std::string& str, const std::string& delim) { res_list.push_back(tmp_str); } return res_list; - /* - size_t num = 1; - const char* p; - - for (p = str.c_str(); *p != 0; p++) { - if (*p == delim) { - num++; - } - } - - std::vector 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(last, p - last); - last = p + 1; - } - } - - list[num] = boost::lexical_cast(last, p - last); - return list; - */ } // split string by spaces. Leading and tailing spaces are ignored. Consecutive @@ -183,7 +116,6 @@ std::vector split_string(const std::string& str) { p++; } } - return list; } @@ -204,6 +136,7 @@ std::string join_strings(const std::vector& strs, char delim) { // 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. + class LineFileReader { public: LineFileReader() {} @@ -211,22 +144,7 @@ class LineFileReader { LineFileReader(const LineFileReader&) = delete; ~LineFileReader() { ::free(_buffer); } char* getline(FILE* f) { return this->getdelim(f, '\n'); } - 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* getdelim(FILE* f, char delim); char* get() { return _buffer; } size_t length() { return _length; }