Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
6be9f719
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
6be9f719
编写于
3月 25, 2019
作者:
D
dongdaxiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
make string_helper dependency work
test=develop
上级
e95cafd9
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
109 addition
and
91 deletion
+109
-91
paddle/fluid/framework/io/CMakeLists.txt
paddle/fluid/framework/io/CMakeLists.txt
+2
-2
paddle/fluid/string/CMakeLists.txt
paddle/fluid/string/CMakeLists.txt
+1
-0
paddle/fluid/string/string_helper.cc
paddle/fluid/string/string_helper.cc
+99
-0
paddle/fluid/string/string_helper.h
paddle/fluid/string/string_helper.h
+7
-89
未找到文件。
paddle/fluid/framework/io/CMakeLists.txt
浏览文件 @
6be9f719
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
)
paddle/fluid/string/CMakeLists.txt
浏览文件 @
6be9f719
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
)
...
...
paddle/fluid/string/string_helper.cc
0 → 100644
浏览文件 @
6be9f719
// 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
paddle/fluid/string/string_helper.h
浏览文件 @
6be9f719
...
...
@@ -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
<
class
...
ARGS
>
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
<
class
T
=
std
::
string
>
...
...
@@ -117,7 +75,6 @@ std::vector<T> 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<T> 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<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
...
...
@@ -183,7 +116,6 @@ std::vector<T> split_string(const std::string& str) {
p
++
;
}
}
return
list
;
}
...
...
@@ -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
// 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
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录