Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
ea1d3acf
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
ea1d3acf
编写于
6月 28, 2017
作者:
Y
Yi Wang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rename string/stringpiece* into string/piece
上级
6215f47c
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
225 addition
and
83 deletion
+225
-83
paddle/CMakeLists.txt
paddle/CMakeLists.txt
+1
-1
paddle/string/CMakeLists.txt
paddle/string/CMakeLists.txt
+2
-2
paddle/string/piece.cc
paddle/string/piece.cc
+138
-0
paddle/string/piece.h
paddle/string/piece.h
+33
-31
paddle/string/piece_test.cc
paddle/string/piece_test.cc
+51
-49
未找到文件。
paddle/CMakeLists.txt
浏览文件 @
ea1d3acf
...
@@ -9,7 +9,7 @@ add_subdirectory(pserver)
...
@@ -9,7 +9,7 @@ add_subdirectory(pserver)
add_subdirectory
(
trainer
)
add_subdirectory
(
trainer
)
add_subdirectory
(
scripts
)
add_subdirectory
(
scripts
)
add_subdirectory
(
optimizer
)
add_subdirectory
(
optimizer
)
add_subdirectory
(
string
s
)
add_subdirectory
(
string
)
if
(
Boost_FOUND
)
if
(
Boost_FOUND
)
add_subdirectory
(
memory
)
add_subdirectory
(
memory
)
...
...
paddle/string/CMakeLists.txt
浏览文件 @
ea1d3acf
cc_library
(
stringpiece SRCS
string
piece.cc
)
cc_library
(
stringpiece SRCS piece.cc
)
cc_test
(
stringpiece_test SRCS
string
piece_test.cc DEPS stringpiece glog gflags
)
cc_test
(
stringpiece_test SRCS piece_test.cc DEPS stringpiece glog gflags
)
paddle/string/
string
piece.cc
→
paddle/string/piece.cc
浏览文件 @
ea1d3acf
...
@@ -14,7 +14,7 @@
...
@@ -14,7 +14,7 @@
limitations under the License.
limitations under the License.
*/
*/
#include "paddle/string
s/string
piece.h"
#include "paddle/string
/
piece.h"
#include <string.h>
#include <string.h>
...
@@ -23,29 +23,25 @@
...
@@ -23,29 +23,25 @@
#include <stdexcept>
#include <stdexcept>
namespace
paddle
{
namespace
paddle
{
namespace
string
{
StringPiece
::
String
Piece
()
:
data_
(
NULL
),
size_
(
0
)
{}
Piece
::
Piece
()
:
data_
(
NULL
),
size_
(
0
)
{}
StringPiece
::
String
Piece
(
const
char
*
d
,
size_t
n
)
:
data_
(
d
),
size_
(
n
)
{
Piece
::
Piece
(
const
char
*
d
,
size_t
n
)
:
data_
(
d
),
size_
(
n
)
{
if
(
d
==
NULL
&&
n
!=
0
)
if
(
d
==
NULL
&&
n
!=
0
)
throw
std
::
invalid_argument
(
throw
std
::
invalid_argument
(
"Piece requires len to be 0 for NULL data"
);
"StringPiece requires len to be 0 for NULL data"
);
}
}
StringPiece
::
StringPiece
(
const
char
*
s
)
:
data_
(
s
)
{
Piece
::
Piece
(
const
char
*
s
)
:
data_
(
s
)
{
size_
=
(
s
==
NULL
)
?
0
:
strlen
(
s
);
}
size_
=
(
s
==
NULL
)
?
0
:
strlen
(
s
);
}
StringPiece
::
StringPiece
(
const
std
::
string
&
s
)
Piece
::
Piece
(
const
std
::
string
&
s
)
:
data_
(
s
.
data
()),
size_
(
s
.
size
())
{}
:
data_
(
s
.
data
()),
size_
(
s
.
size
())
{}
char
StringPiece
::
operator
[](
size_t
n
)
const
{
char
Piece
::
operator
[](
size_t
n
)
const
{
if
(
n
>=
len
())
if
(
n
>=
len
())
throw
std
::
invalid_argument
(
"index out of Piece length"
);
throw
std
::
invalid_argument
(
"index out of StringPiece length"
);
return
data_
[
n
];
return
data_
[
n
];
}
}
int
Compare
(
StringPiece
a
,
String
Piece
b
)
{
int
Compare
(
Piece
a
,
Piece
b
)
{
const
size_t
min_len
=
(
a
.
len
()
<
b
.
len
())
?
a
.
len
()
:
b
.
len
();
const
size_t
min_len
=
(
a
.
len
()
<
b
.
len
())
?
a
.
len
()
:
b
.
len
();
int
r
=
memcmp
(
a
.
data
(),
b
.
data
(),
min_len
);
int
r
=
memcmp
(
a
.
data
(),
b
.
data
(),
min_len
);
if
(
r
==
0
)
{
if
(
r
==
0
)
{
...
@@ -57,85 +53,86 @@ int Compare(StringPiece a, StringPiece b) {
...
@@ -57,85 +53,86 @@ int Compare(StringPiece a, StringPiece b) {
return
r
;
return
r
;
}
}
bool
operator
==
(
StringPiece
x
,
String
Piece
y
)
{
bool
operator
==
(
Piece
x
,
Piece
y
)
{
return
((
x
.
len
()
==
y
.
len
())
&&
return
((
x
.
len
()
==
y
.
len
())
&&
(
x
.
data
()
==
y
.
data
()
||
memcmp
(
x
.
data
(),
y
.
data
(),
x
.
len
())
==
0
));
(
x
.
data
()
==
y
.
data
()
||
memcmp
(
x
.
data
(),
y
.
data
(),
x
.
len
())
==
0
));
}
}
bool
operator
!=
(
StringPiece
x
,
String
Piece
y
)
{
return
!
(
x
==
y
);
}
bool
operator
!=
(
Piece
x
,
Piece
y
)
{
return
!
(
x
==
y
);
}
bool
operator
<
(
StringPiece
x
,
String
Piece
y
)
{
return
Compare
(
x
,
y
)
<
0
;
}
bool
operator
<
(
Piece
x
,
Piece
y
)
{
return
Compare
(
x
,
y
)
<
0
;
}
bool
operator
>
(
StringPiece
x
,
String
Piece
y
)
{
return
Compare
(
x
,
y
)
>
0
;
}
bool
operator
>
(
Piece
x
,
Piece
y
)
{
return
Compare
(
x
,
y
)
>
0
;
}
bool
operator
<=
(
StringPiece
x
,
String
Piece
y
)
{
return
Compare
(
x
,
y
)
<=
0
;
}
bool
operator
<=
(
Piece
x
,
Piece
y
)
{
return
Compare
(
x
,
y
)
<=
0
;
}
bool
operator
>=
(
StringPiece
x
,
String
Piece
y
)
{
return
Compare
(
x
,
y
)
>=
0
;
}
bool
operator
>=
(
Piece
x
,
Piece
y
)
{
return
Compare
(
x
,
y
)
>=
0
;
}
bool
HasPrefix
(
StringPiece
s
,
String
Piece
x
)
{
bool
HasPrefix
(
Piece
s
,
Piece
x
)
{
return
((
s
.
len
()
>=
x
.
len
())
&&
(
memcmp
(
s
.
data
(),
x
.
data
(),
x
.
len
())
==
0
));
return
((
s
.
len
()
>=
x
.
len
())
&&
(
memcmp
(
s
.
data
(),
x
.
data
(),
x
.
len
())
==
0
));
}
}
bool
HasSuffix
(
StringPiece
s
,
String
Piece
x
)
{
bool
HasSuffix
(
Piece
s
,
Piece
x
)
{
return
((
s
.
len
()
>=
x
.
len
())
&&
return
((
s
.
len
()
>=
x
.
len
())
&&
(
memcmp
(
s
.
data
()
+
(
s
.
len
()
-
x
.
len
()),
x
.
data
(),
x
.
len
())
==
0
));
(
memcmp
(
s
.
data
()
+
(
s
.
len
()
-
x
.
len
()),
x
.
data
(),
x
.
len
())
==
0
));
}
}
StringPiece
SkipPrefix
(
String
Piece
s
,
size_t
n
)
{
Piece
SkipPrefix
(
Piece
s
,
size_t
n
)
{
if
(
n
>
s
.
len
())
if
(
n
>
s
.
len
())
throw
std
::
invalid_argument
(
"Skip distance larger than
String
Piece length"
);
throw
std
::
invalid_argument
(
"Skip distance larger than Piece length"
);
return
String
Piece
(
s
.
data
()
+
n
,
s
.
len
()
-
n
);
return
Piece
(
s
.
data
()
+
n
,
s
.
len
()
-
n
);
}
}
StringPiece
SkipSuffix
(
String
Piece
s
,
size_t
n
)
{
Piece
SkipSuffix
(
Piece
s
,
size_t
n
)
{
if
(
n
>
s
.
len
())
if
(
n
>
s
.
len
())
throw
std
::
invalid_argument
(
"Skip distance larger than
String
Piece length"
);
throw
std
::
invalid_argument
(
"Skip distance larger than Piece length"
);
return
String
Piece
(
s
.
data
(),
s
.
len
()
-
n
);
return
Piece
(
s
.
data
(),
s
.
len
()
-
n
);
}
}
StringPiece
TrimPrefix
(
StringPiece
s
,
String
Piece
x
)
{
Piece
TrimPrefix
(
Piece
s
,
Piece
x
)
{
return
HasPrefix
(
s
,
x
)
?
SkipPrefix
(
s
,
x
.
len
())
:
s
;
return
HasPrefix
(
s
,
x
)
?
SkipPrefix
(
s
,
x
.
len
())
:
s
;
}
}
StringPiece
TrimSuffix
(
StringPiece
s
,
String
Piece
x
)
{
Piece
TrimSuffix
(
Piece
s
,
Piece
x
)
{
return
HasSuffix
(
s
,
x
)
?
SkipSuffix
(
s
,
x
.
len
())
:
s
;
return
HasSuffix
(
s
,
x
)
?
SkipSuffix
(
s
,
x
.
len
())
:
s
;
}
}
bool
Contains
(
StringPiece
s
,
String
Piece
sub
)
{
bool
Contains
(
Piece
s
,
Piece
sub
)
{
return
std
::
search
(
s
.
begin
(),
s
.
end
(),
sub
.
begin
(),
sub
.
end
())
!=
s
.
end
();
return
std
::
search
(
s
.
begin
(),
s
.
end
(),
sub
.
begin
(),
sub
.
end
())
!=
s
.
end
();
}
}
size_t
Index
(
StringPiece
s
,
String
Piece
sub
)
{
size_t
Index
(
Piece
s
,
Piece
sub
)
{
auto
e
=
std
::
search
(
s
.
begin
(),
s
.
end
(),
sub
.
begin
(),
sub
.
end
());
auto
e
=
std
::
search
(
s
.
begin
(),
s
.
end
(),
sub
.
begin
(),
sub
.
end
());
return
e
!=
s
.
end
()
?
e
-
s
.
data
()
:
String
Piece
::
npos
;
return
e
!=
s
.
end
()
?
e
-
s
.
data
()
:
Piece
::
npos
;
}
}
size_t
Find
(
String
Piece
s
,
char
c
,
size_t
pos
)
{
size_t
Find
(
Piece
s
,
char
c
,
size_t
pos
)
{
if
(
pos
>=
s
.
len
())
{
if
(
pos
>=
s
.
len
())
{
return
String
Piece
::
npos
;
return
Piece
::
npos
;
}
}
const
char
*
result
=
const
char
*
result
=
reinterpret_cast
<
const
char
*>
(
memchr
(
s
.
data
()
+
pos
,
c
,
s
.
len
()
-
pos
));
reinterpret_cast
<
const
char
*>
(
memchr
(
s
.
data
()
+
pos
,
c
,
s
.
len
()
-
pos
));
return
result
!=
nullptr
?
result
-
s
.
data
()
:
String
Piece
::
npos
;
return
result
!=
nullptr
?
result
-
s
.
data
()
:
Piece
::
npos
;
}
}
size_t
RFind
(
String
Piece
s
,
char
c
,
size_t
pos
)
{
size_t
RFind
(
Piece
s
,
char
c
,
size_t
pos
)
{
if
(
s
.
len
()
==
0
)
return
String
Piece
::
npos
;
if
(
s
.
len
()
==
0
)
return
Piece
::
npos
;
for
(
const
char
*
p
=
s
.
data
()
+
std
::
min
(
pos
,
s
.
len
()
-
1
);
p
>=
s
.
data
();
for
(
const
char
*
p
=
s
.
data
()
+
std
::
min
(
pos
,
s
.
len
()
-
1
);
p
>=
s
.
data
();
p
--
)
{
p
--
)
{
if
(
*
p
==
c
)
{
if
(
*
p
==
c
)
{
return
p
-
s
.
data
();
return
p
-
s
.
data
();
}
}
}
}
return
String
Piece
::
npos
;
return
Piece
::
npos
;
}
}
StringPiece
SubStr
(
String
Piece
s
,
size_t
pos
,
size_t
n
)
{
Piece
SubStr
(
Piece
s
,
size_t
pos
,
size_t
n
)
{
if
(
pos
>
s
.
len
())
pos
=
s
.
len
();
if
(
pos
>
s
.
len
())
pos
=
s
.
len
();
if
(
n
>
s
.
len
()
-
pos
)
n
=
s
.
len
()
-
pos
;
if
(
n
>
s
.
len
()
-
pos
)
n
=
s
.
len
()
-
pos
;
return
String
Piece
(
s
.
data
()
+
pos
,
n
);
return
Piece
(
s
.
data
()
+
pos
,
n
);
}
}
std
::
ostream
&
operator
<<
(
std
::
ostream
&
o
,
String
Piece
piece
)
{
std
::
ostream
&
operator
<<
(
std
::
ostream
&
o
,
Piece
piece
)
{
return
o
<<
piece
.
ToString
();
return
o
<<
piece
.
ToString
();
}
}
}
// namespace string
}
// namespace paddle
}
// namespace paddle
paddle/string/
string
piece.h
→
paddle/string/piece.h
浏览文件 @
ea1d3acf
...
@@ -20,33 +20,34 @@
...
@@ -20,33 +20,34 @@
#include <string>
#include <string>
namespace
paddle
{
namespace
paddle
{
namespace
string
{
//
String
Piece points into a std::string object but doesn't own the
// Piece points into a std::string object but doesn't own the
// string. It is for efficient access to strings. Like Go's string
// string. It is for efficient access to strings. Like Go's string
// type. Not that
String
Piece doesn't mutate the underlying string,
// type. Not that Piece doesn't mutate the underlying string,
// so it is thread-safe given that the underlying string doesn't
// so it is thread-safe given that the underlying string doesn't
// change. Because
String
Piece contains a little data members, and
// change. Because Piece contains a little data members, and
// its syntax is simple as it doesn't own/manage the string, it is
// its syntax is simple as it doesn't own/manage the string, it is
// cheap to construct
String
Pieces and pass them around.
// cheap to construct Pieces and pass them around.
class
String
Piece
{
class
Piece
{
public:
public:
static
const
size_t
npos
=
static_cast
<
size_t
>
(
-
1
);
static
const
size_t
npos
=
static_cast
<
size_t
>
(
-
1
);
// We provide non-explicit singleton constructors so users can
// We provide non-explicit singleton constructors so users can
// pass in a "const char*" or a "string" wherever a "
String
Piece"
// pass in a "const char*" or a "string" wherever a "Piece"
// is expected. These contructors ensure that if data_ is NULL,
// is expected. These contructors ensure that if data_ is NULL,
// size_ is 0.
// size_ is 0.
String
Piece
();
Piece
();
String
Piece
(
const
char
*
d
,
size_t
n
);
Piece
(
const
char
*
d
,
size_t
n
);
String
Piece
(
const
char
*
d
);
Piece
(
const
char
*
d
);
String
Piece
(
const
std
::
string
&
s
);
Piece
(
const
std
::
string
&
s
);
const
char
*
data
()
const
{
return
data_
;
}
const
char
*
data
()
const
{
return
data_
;
}
size_t
len
()
const
{
return
size_
;
}
size_t
len
()
const
{
return
size_
;
}
char
operator
[](
size_t
n
)
const
;
char
operator
[](
size_t
n
)
const
;
//
String
Piece doesn't own the string, so both iterator and const
// Piece doesn't own the string, so both iterator and const
// iterator are const char* indeed.
// iterator are const char* indeed.
typedef
const
char
*
const_iterator
;
typedef
const
char
*
const_iterator
;
typedef
const
char
*
iterator
;
typedef
const
char
*
iterator
;
...
@@ -63,43 +64,44 @@ private:
...
@@ -63,43 +64,44 @@ private:
// Intentionally copyable
// Intentionally copyable
};
};
int
Compare
(
StringPiece
a
,
String
Piece
b
);
int
Compare
(
Piece
a
,
Piece
b
);
bool
operator
==
(
StringPiece
x
,
String
Piece
y
);
bool
operator
==
(
Piece
x
,
Piece
y
);
bool
operator
!=
(
StringPiece
x
,
String
Piece
y
);
bool
operator
!=
(
Piece
x
,
Piece
y
);
bool
operator
<
(
StringPiece
x
,
String
Piece
y
);
bool
operator
<
(
Piece
x
,
Piece
y
);
bool
operator
>
(
StringPiece
x
,
String
Piece
y
);
bool
operator
>
(
Piece
x
,
Piece
y
);
bool
operator
<=
(
StringPiece
x
,
String
Piece
y
);
bool
operator
<=
(
Piece
x
,
Piece
y
);
bool
operator
>=
(
StringPiece
x
,
String
Piece
y
);
bool
operator
>=
(
Piece
x
,
Piece
y
);
bool
HasPrefix
(
StringPiece
s
,
String
Piece
prefix
);
bool
HasPrefix
(
Piece
s
,
Piece
prefix
);
bool
HasSuffix
(
StringPiece
s
,
String
Piece
suffix
);
bool
HasSuffix
(
Piece
s
,
Piece
suffix
);
StringPiece
SkipPrefix
(
String
Piece
s
,
size_t
n
);
Piece
SkipPrefix
(
Piece
s
,
size_t
n
);
StringPiece
SkipSuffix
(
String
Piece
s
,
size_t
n
);
Piece
SkipSuffix
(
Piece
s
,
size_t
n
);
// Skip the prefix (or suffix) if it matches with the string.
// Skip the prefix (or suffix) if it matches with the string.
StringPiece
TrimPrefix
(
StringPiece
s
,
String
Piece
prefix
);
Piece
TrimPrefix
(
Piece
s
,
Piece
prefix
);
StringPiece
TrimSuffix
(
StringPiece
s
,
String
Piece
suffix
);
Piece
TrimSuffix
(
Piece
s
,
Piece
suffix
);
// Returns if s contains sub. Any s except for empty s contains an
// Returns if s contains sub. Any s except for empty s contains an
// empty sub.
// empty sub.
bool
Contains
(
StringPiece
s
,
String
Piece
sub
);
bool
Contains
(
Piece
s
,
Piece
sub
);
// Return the first occurrence of sub in s, or npos. If both s and
// Return the first occurrence of sub in s, or npos. If both s and
// sub is empty, it returns npos; otherwise, if only sub is empty, it
// sub is empty, it returns npos; otherwise, if only sub is empty, it
// returns 0.
// returns 0.
size_t
Index
(
StringPiece
s
,
String
Piece
sub
);
size_t
Index
(
Piece
s
,
Piece
sub
);
// Return the first occurrence of c in s[pos:end], or npos.
// Return the first occurrence of c in s[pos:end], or npos.
size_t
Find
(
String
Piece
s
,
char
c
,
size_t
pos
);
size_t
Find
(
Piece
s
,
char
c
,
size_t
pos
);
// Search range is [0..pos] inclusive. If pos == npos, search everything.
// Search range is [0..pos] inclusive. If pos == npos, search everything.
size_t
RFind
(
String
Piece
s
,
char
c
,
size_t
pos
);
size_t
RFind
(
Piece
s
,
char
c
,
size_t
pos
);
StringPiece
SubStr
(
String
Piece
s
,
size_t
pos
,
size_t
n
);
Piece
SubStr
(
Piece
s
,
size_t
pos
,
size_t
n
);
// allow
String
Piece to be logged
// allow Piece to be logged
std
::
ostream
&
operator
<<
(
std
::
ostream
&
o
,
String
Piece
piece
);
std
::
ostream
&
operator
<<
(
std
::
ostream
&
o
,
Piece
piece
);
}
// namespace string
}
// namespace paddle
}
// namespace paddle
paddle/string/
string
piece_test.cc
→
paddle/string/piece_test.cc
浏览文件 @
ea1d3acf
...
@@ -14,7 +14,7 @@
...
@@ -14,7 +14,7 @@
limitations under the License.
limitations under the License.
*/
*/
#include "paddle/string
s/string
piece.h"
#include "paddle/string
/
piece.h"
#include <sstream>
#include <sstream>
...
@@ -22,42 +22,44 @@
...
@@ -22,42 +22,44 @@
TEST
(
StringPiece
,
Construct
)
{
TEST
(
StringPiece
,
Construct
)
{
{
{
paddle
::
String
Piece
s
;
paddle
::
string
::
Piece
s
;
EXPECT_EQ
(
NULL
,
s
.
data
());
EXPECT_EQ
(
NULL
,
s
.
data
());
EXPECT_EQ
(
0U
,
s
.
len
());
EXPECT_EQ
(
0U
,
s
.
len
());
}
}
{
EXPECT_THROW
(
paddle
::
StringPiece
s
(
NULL
,
10000U
),
std
::
invalid_argument
);
}
{
{
paddle
::
StringPiece
s
(
NULL
);
EXPECT_THROW
(
paddle
::
string
::
Piece
s
(
NULL
,
10000U
),
std
::
invalid_argument
);
}
{
paddle
::
string
::
Piece
s
(
NULL
);
EXPECT_EQ
(
0U
,
s
.
len
());
EXPECT_EQ
(
0U
,
s
.
len
());
}
}
{
{
std
::
string
a
;
std
::
string
a
;
EXPECT_EQ
(
0U
,
a
.
size
());
EXPECT_EQ
(
0U
,
a
.
size
());
paddle
::
String
Piece
s
(
a
);
paddle
::
string
::
Piece
s
(
a
);
EXPECT_EQ
(
0U
,
s
.
len
());
EXPECT_EQ
(
0U
,
s
.
len
());
}
}
}
}
TEST
(
StringPiece
,
CopyAndAssign
)
{
TEST
(
StringPiece
,
CopyAndAssign
)
{
paddle
::
String
Piece
empty
;
paddle
::
string
::
Piece
empty
;
EXPECT_EQ
(
0U
,
empty
.
len
());
EXPECT_EQ
(
0U
,
empty
.
len
());
paddle
::
String
Piece
a
(
"hello"
);
paddle
::
string
::
Piece
a
(
"hello"
);
paddle
::
String
Piece
b
=
a
;
paddle
::
string
::
Piece
b
=
a
;
EXPECT_EQ
(
b
.
len
(),
strlen
(
"hello"
));
EXPECT_EQ
(
b
.
len
(),
strlen
(
"hello"
));
EXPECT_EQ
(
a
,
b
);
EXPECT_EQ
(
a
,
b
);
std
::
string
storage
(
"hello"
);
std
::
string
storage
(
"hello"
);
paddle
::
String
Piece
c
(
storage
);
paddle
::
string
::
Piece
c
(
storage
);
EXPECT_EQ
(
a
,
c
);
EXPECT_EQ
(
a
,
c
);
EXPECT_NE
(
a
.
data
(),
c
.
data
());
EXPECT_NE
(
a
.
data
(),
c
.
data
());
}
}
TEST
(
StringPiece
,
Compare
)
{
TEST
(
StringPiece
,
Compare
)
{
{
{
paddle
::
String
Piece
a
(
"hello"
);
paddle
::
string
::
Piece
a
(
"hello"
);
paddle
::
String
Piece
b
(
"world"
);
paddle
::
string
::
Piece
b
(
"world"
);
EXPECT_TRUE
(
a
!=
b
);
EXPECT_TRUE
(
a
!=
b
);
EXPECT_FALSE
(
a
==
b
);
EXPECT_FALSE
(
a
==
b
);
EXPECT_TRUE
(
a
<
b
);
EXPECT_TRUE
(
a
<
b
);
...
@@ -68,7 +70,7 @@ TEST(StringPiece, Compare) {
...
@@ -68,7 +70,7 @@ TEST(StringPiece, Compare) {
EXPECT_GT
(
Compare
(
b
,
a
),
0
);
EXPECT_GT
(
Compare
(
b
,
a
),
0
);
}
}
{
{
paddle
::
String
Piece
a
,
b
;
paddle
::
string
::
Piece
a
,
b
;
EXPECT_TRUE
(
a
==
b
);
EXPECT_TRUE
(
a
==
b
);
EXPECT_FALSE
(
a
!=
b
);
EXPECT_FALSE
(
a
!=
b
);
EXPECT_FALSE
(
a
<
b
);
EXPECT_FALSE
(
a
<
b
);
...
@@ -82,31 +84,31 @@ TEST(StringPiece, Compare) {
...
@@ -82,31 +84,31 @@ TEST(StringPiece, Compare) {
TEST
(
StringPiece
,
ToString
)
{
TEST
(
StringPiece
,
ToString
)
{
{
{
paddle
::
String
Piece
s
;
paddle
::
string
::
Piece
s
;
EXPECT_EQ
(
std
::
string
(
""
),
s
.
ToString
());
EXPECT_EQ
(
std
::
string
(
""
),
s
.
ToString
());
}
}
{
{
paddle
::
String
Piece
s
(
NULL
);
paddle
::
string
::
Piece
s
(
NULL
);
EXPECT_EQ
(
std
::
string
(
""
),
s
.
ToString
());
EXPECT_EQ
(
std
::
string
(
""
),
s
.
ToString
());
}
}
{
{
paddle
::
String
Piece
s
(
"hello"
);
paddle
::
string
::
Piece
s
(
"hello"
);
EXPECT_EQ
(
std
::
string
(
"hello"
),
s
.
ToString
());
EXPECT_EQ
(
std
::
string
(
"hello"
),
s
.
ToString
());
}
}
}
}
TEST
(
StringPiece
,
HasPrefixSuffix
)
{
TEST
(
StringPiece
,
HasPrefixSuffix
)
{
using
paddle
::
HasPrefix
;
using
paddle
::
string
::
HasPrefix
;
using
paddle
::
HasSuffix
;
using
paddle
::
string
::
HasSuffix
;
{
{
paddle
::
String
Piece
s
;
paddle
::
string
::
Piece
s
;
EXPECT_FALSE
(
HasPrefix
(
s
,
"something"
));
EXPECT_FALSE
(
HasPrefix
(
s
,
"something"
));
EXPECT_TRUE
(
HasPrefix
(
s
,
""
));
EXPECT_TRUE
(
HasPrefix
(
s
,
""
));
EXPECT_FALSE
(
HasSuffix
(
s
,
"something"
));
EXPECT_FALSE
(
HasSuffix
(
s
,
"something"
));
EXPECT_TRUE
(
HasSuffix
(
s
,
""
));
EXPECT_TRUE
(
HasSuffix
(
s
,
""
));
}
}
{
{
paddle
::
String
Piece
s
(
"app"
);
paddle
::
string
::
Piece
s
(
"app"
);
EXPECT_TRUE
(
HasPrefix
(
s
,
""
));
EXPECT_TRUE
(
HasPrefix
(
s
,
""
));
EXPECT_TRUE
(
HasPrefix
(
s
,
"a"
));
EXPECT_TRUE
(
HasPrefix
(
s
,
"a"
));
EXPECT_TRUE
(
HasPrefix
(
s
,
"ap"
));
EXPECT_TRUE
(
HasPrefix
(
s
,
"ap"
));
...
@@ -120,10 +122,10 @@ TEST(StringPiece, HasPrefixSuffix) {
...
@@ -120,10 +122,10 @@ TEST(StringPiece, HasPrefixSuffix) {
}
}
TEST
(
StringPiece
,
SkipPrefixSuffix
)
{
TEST
(
StringPiece
,
SkipPrefixSuffix
)
{
using
paddle
::
SkipPrefix
;
using
paddle
::
string
::
SkipPrefix
;
using
paddle
::
SkipSuffix
;
using
paddle
::
string
::
SkipSuffix
;
{
{
paddle
::
String
Piece
s
;
paddle
::
string
::
Piece
s
;
EXPECT_EQ
(
""
,
SkipPrefix
(
s
,
0
));
EXPECT_EQ
(
""
,
SkipPrefix
(
s
,
0
));
EXPECT_THROW
(
SkipPrefix
(
s
,
1
),
std
::
invalid_argument
);
EXPECT_THROW
(
SkipPrefix
(
s
,
1
),
std
::
invalid_argument
);
...
@@ -131,7 +133,7 @@ TEST(StringPiece, SkipPrefixSuffix) {
...
@@ -131,7 +133,7 @@ TEST(StringPiece, SkipPrefixSuffix) {
EXPECT_THROW
(
SkipSuffix
(
s
,
1
),
std
::
invalid_argument
);
EXPECT_THROW
(
SkipSuffix
(
s
,
1
),
std
::
invalid_argument
);
}
}
{
{
paddle
::
String
Piece
s
(
"app"
);
paddle
::
string
::
Piece
s
(
"app"
);
EXPECT_EQ
(
"app"
,
SkipPrefix
(
s
,
0
));
EXPECT_EQ
(
"app"
,
SkipPrefix
(
s
,
0
));
EXPECT_EQ
(
"pp"
,
SkipPrefix
(
s
,
1
));
EXPECT_EQ
(
"pp"
,
SkipPrefix
(
s
,
1
));
EXPECT_EQ
(
"p"
,
SkipPrefix
(
s
,
2
));
EXPECT_EQ
(
"p"
,
SkipPrefix
(
s
,
2
));
...
@@ -147,10 +149,10 @@ TEST(StringPiece, SkipPrefixSuffix) {
...
@@ -147,10 +149,10 @@ TEST(StringPiece, SkipPrefixSuffix) {
}
}
TEST
(
StringPiece
,
TrimPrefixSuffix
)
{
TEST
(
StringPiece
,
TrimPrefixSuffix
)
{
using
paddle
::
TrimPrefix
;
using
paddle
::
string
::
TrimPrefix
;
using
paddle
::
TrimSuffix
;
using
paddle
::
string
::
TrimSuffix
;
{
{
paddle
::
String
Piece
s
;
paddle
::
string
::
Piece
s
;
EXPECT_EQ
(
""
,
TrimPrefix
(
s
,
""
));
EXPECT_EQ
(
""
,
TrimPrefix
(
s
,
""
));
EXPECT_EQ
(
""
,
TrimPrefix
(
s
,
"something"
));
EXPECT_EQ
(
""
,
TrimPrefix
(
s
,
"something"
));
...
@@ -158,7 +160,7 @@ TEST(StringPiece, TrimPrefixSuffix) {
...
@@ -158,7 +160,7 @@ TEST(StringPiece, TrimPrefixSuffix) {
EXPECT_EQ
(
""
,
TrimSuffix
(
s
,
"something"
));
EXPECT_EQ
(
""
,
TrimSuffix
(
s
,
"something"
));
}
}
{
{
paddle
::
String
Piece
s
(
"app"
);
paddle
::
string
::
Piece
s
(
"app"
);
EXPECT_EQ
(
"app"
,
TrimPrefix
(
s
,
""
));
EXPECT_EQ
(
"app"
,
TrimPrefix
(
s
,
""
));
EXPECT_EQ
(
"pp"
,
TrimPrefix
(
s
,
"a"
));
EXPECT_EQ
(
"pp"
,
TrimPrefix
(
s
,
"a"
));
EXPECT_EQ
(
"p"
,
TrimPrefix
(
s
,
"ap"
));
EXPECT_EQ
(
"p"
,
TrimPrefix
(
s
,
"ap"
));
...
@@ -174,14 +176,14 @@ TEST(StringPiece, TrimPrefixSuffix) {
...
@@ -174,14 +176,14 @@ TEST(StringPiece, TrimPrefixSuffix) {
}
}
TEST
(
StringPiece
,
Contains
)
{
TEST
(
StringPiece
,
Contains
)
{
using
paddle
::
Contains
;
using
paddle
::
string
::
Contains
;
{
{
paddle
::
String
Piece
s
;
paddle
::
string
::
Piece
s
;
EXPECT_FALSE
(
Contains
(
s
,
""
));
EXPECT_FALSE
(
Contains
(
s
,
""
));
EXPECT_FALSE
(
Contains
(
s
,
"something"
));
EXPECT_FALSE
(
Contains
(
s
,
"something"
));
}
}
{
{
paddle
::
String
Piece
s
(
"app"
);
paddle
::
string
::
Piece
s
(
"app"
);
EXPECT_TRUE
(
Contains
(
s
,
""
));
EXPECT_TRUE
(
Contains
(
s
,
""
));
EXPECT_TRUE
(
Contains
(
s
,
"a"
));
EXPECT_TRUE
(
Contains
(
s
,
"a"
));
EXPECT_TRUE
(
Contains
(
s
,
"p"
));
EXPECT_TRUE
(
Contains
(
s
,
"p"
));
...
@@ -193,15 +195,15 @@ TEST(StringPiece, Contains) {
...
@@ -193,15 +195,15 @@ TEST(StringPiece, Contains) {
}
}
TEST
(
StringPiece
,
Index
)
{
TEST
(
StringPiece
,
Index
)
{
using
paddle
::
Index
;
using
paddle
::
string
::
Index
;
auto
npos
=
paddle
::
String
Piece
::
npos
;
auto
npos
=
paddle
::
string
::
Piece
::
npos
;
{
{
paddle
::
String
Piece
s
;
paddle
::
string
::
Piece
s
;
EXPECT_EQ
(
npos
,
Index
(
s
,
""
));
EXPECT_EQ
(
npos
,
Index
(
s
,
""
));
EXPECT_EQ
(
npos
,
Index
(
s
,
"something"
));
EXPECT_EQ
(
npos
,
Index
(
s
,
"something"
));
}
}
{
{
paddle
::
String
Piece
s
(
"app"
);
paddle
::
string
::
Piece
s
(
"app"
);
EXPECT_EQ
(
0U
,
Index
(
s
,
""
));
EXPECT_EQ
(
0U
,
Index
(
s
,
""
));
EXPECT_EQ
(
0U
,
Index
(
s
,
"a"
));
EXPECT_EQ
(
0U
,
Index
(
s
,
"a"
));
EXPECT_EQ
(
1U
,
Index
(
s
,
"p"
));
EXPECT_EQ
(
1U
,
Index
(
s
,
"p"
));
...
@@ -213,14 +215,14 @@ TEST(StringPiece, Index) {
...
@@ -213,14 +215,14 @@ TEST(StringPiece, Index) {
}
}
TEST
(
StringPiece
,
Find
)
{
TEST
(
StringPiece
,
Find
)
{
using
paddle
::
Find
;
using
paddle
::
string
::
Find
;
auto
npos
=
paddle
::
String
Piece
::
npos
;
auto
npos
=
paddle
::
string
::
Piece
::
npos
;
{
{
paddle
::
String
Piece
s
;
paddle
::
string
::
Piece
s
;
EXPECT_EQ
(
npos
,
Find
(
s
,
'a'
,
0U
));
EXPECT_EQ
(
npos
,
Find
(
s
,
'a'
,
0U
));
}
}
{
{
paddle
::
String
Piece
s
(
"app"
);
paddle
::
string
::
Piece
s
(
"app"
);
EXPECT_EQ
(
0U
,
Find
(
s
,
'a'
,
0U
));
EXPECT_EQ
(
0U
,
Find
(
s
,
'a'
,
0U
));
EXPECT_EQ
(
1U
,
Find
(
s
,
'p'
,
0U
));
EXPECT_EQ
(
1U
,
Find
(
s
,
'p'
,
0U
));
EXPECT_EQ
(
1U
,
Find
(
s
,
'p'
,
1U
));
EXPECT_EQ
(
1U
,
Find
(
s
,
'p'
,
1U
));
...
@@ -230,14 +232,14 @@ TEST(StringPiece, Find) {
...
@@ -230,14 +232,14 @@ TEST(StringPiece, Find) {
}
}
TEST
(
StringPiece
,
RFind
)
{
TEST
(
StringPiece
,
RFind
)
{
using
paddle
::
RFind
;
using
paddle
::
string
::
RFind
;
auto
npos
=
paddle
::
String
Piece
::
npos
;
auto
npos
=
paddle
::
string
::
Piece
::
npos
;
{
{
paddle
::
String
Piece
s
;
paddle
::
string
::
Piece
s
;
EXPECT_EQ
(
npos
,
RFind
(
s
,
'a'
,
0U
));
EXPECT_EQ
(
npos
,
RFind
(
s
,
'a'
,
0U
));
}
}
{
{
paddle
::
String
Piece
s
(
"app"
);
paddle
::
string
::
Piece
s
(
"app"
);
EXPECT_EQ
(
2U
,
RFind
(
s
,
'p'
,
2U
));
EXPECT_EQ
(
2U
,
RFind
(
s
,
'p'
,
2U
));
EXPECT_EQ
(
0U
,
RFind
(
s
,
'a'
,
2U
));
EXPECT_EQ
(
0U
,
RFind
(
s
,
'a'
,
2U
));
EXPECT_EQ
(
1U
,
RFind
(
s
,
'p'
,
1U
));
EXPECT_EQ
(
1U
,
RFind
(
s
,
'p'
,
1U
));
...
@@ -247,15 +249,15 @@ TEST(StringPiece, RFind) {
...
@@ -247,15 +249,15 @@ TEST(StringPiece, RFind) {
}
}
TEST
(
StringPiece
,
SubStr
)
{
TEST
(
StringPiece
,
SubStr
)
{
using
paddle
::
SubStr
;
using
paddle
::
string
::
SubStr
;
{
{
paddle
::
String
Piece
s
;
paddle
::
string
::
Piece
s
;
EXPECT_EQ
(
""
,
SubStr
(
s
,
0
,
0
));
EXPECT_EQ
(
""
,
SubStr
(
s
,
0
,
0
));
EXPECT_EQ
(
""
,
SubStr
(
s
,
0
,
1
));
EXPECT_EQ
(
""
,
SubStr
(
s
,
0
,
1
));
EXPECT_EQ
(
""
,
SubStr
(
s
,
1
,
0
));
EXPECT_EQ
(
""
,
SubStr
(
s
,
1
,
0
));
}
}
{
{
paddle
::
String
Piece
s
(
"app"
);
paddle
::
string
::
Piece
s
(
"app"
);
EXPECT_EQ
(
""
,
SubStr
(
s
,
0
,
0
));
EXPECT_EQ
(
""
,
SubStr
(
s
,
0
,
0
));
EXPECT_EQ
(
""
,
SubStr
(
s
,
1
,
0
));
EXPECT_EQ
(
""
,
SubStr
(
s
,
1
,
0
));
EXPECT_EQ
(
""
,
SubStr
(
s
,
2
,
0
));
EXPECT_EQ
(
""
,
SubStr
(
s
,
2
,
0
));
...
@@ -279,15 +281,15 @@ TEST(StringPiece, SubStr) {
...
@@ -279,15 +281,15 @@ TEST(StringPiece, SubStr) {
}
}
TEST
(
StringPiece
,
StreamOutput
)
{
TEST
(
StringPiece
,
StreamOutput
)
{
using
paddle
::
String
Piece
;
using
paddle
::
string
::
Piece
;
std
::
stringstream
o
;
std
::
stringstream
o
;
o
<<
String
Piece
();
o
<<
paddle
::
string
::
Piece
();
EXPECT_EQ
(
""
,
o
.
str
());
EXPECT_EQ
(
""
,
o
.
str
());
o
<<
String
Piece
(
"hello"
);
o
<<
paddle
::
string
::
Piece
(
"hello"
);
EXPECT_EQ
(
"hello"
,
o
.
str
());
EXPECT_EQ
(
"hello"
,
o
.
str
());
o
<<
String
Piece
();
o
<<
paddle
::
string
::
Piece
();
EXPECT_EQ
(
"hello"
,
o
.
str
());
EXPECT_EQ
(
"hello"
,
o
.
str
());
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录