Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
6cb7cb36
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看板
提交
6cb7cb36
编写于
6月 28, 2017
作者:
Y
Yi Wang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add paddle/string/printf and tests
上级
ea1d3acf
变更
4
展开全部
隐藏空白更改
内联
并排
Showing
4 changed file
with
1025 addition
and
0 deletion
+1025
-0
paddle/string/CMakeLists.txt
paddle/string/CMakeLists.txt
+2
-0
paddle/string/printf.h
paddle/string/printf.h
+105
-0
paddle/string/printf_test.cc
paddle/string/printf_test.cc
+16
-0
paddle/string/tinyformat/tinyformat.h
paddle/string/tinyformat/tinyformat.h
+902
-0
未找到文件。
paddle/string/CMakeLists.txt
浏览文件 @
6cb7cb36
cc_library
(
stringpiece SRCS piece.cc
)
cc_test
(
stringpiece_test SRCS piece_test.cc DEPS stringpiece glog gflags
)
cc_test
(
stringprintf_test SRCS printf_test.cc DEPS glog gflags
)
paddle/string/printf.h
0 → 100644
浏览文件 @
6cb7cb36
/*
Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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.
*/
// Compared with std::stringstream, there are primary purpose of
// string::Printf:
//
// 1. Type-safe printing, with why and how explained in
// http://www.drdobbs.com/stringprintf-a-typesafe-printf-family-fo/184401999.
// Implementation includes
//
// https://github.com/c42f/tinyformat
// boost::format
// std::stringstream
//
// std::stringstream is not convenient enough in many cases. For example:
//
// std::cout << std::setprecision(2) << std::fixed << 1.23456 << "\n";
//
// boost::format is the most convenient one. We can have
//
// std::cout << format("%2% %1%") % 36 % 77;
//
// or
//
// format fmter("%2% %1%");
// fmter % 36; fmter % 77;
// std::cout << fmter.c_str();
//
// But the overloading of % might be overkilling and it would be
// more efficient if it can write to std::cout directly.
//
// tinyformat has an interface compatible with the C-printf style,
// and it can writes to a stream or returns a std::string:
//
// std::cout << tfm::printf(
// "%s, %s %d, %.2d:%.2d\n",
// weekday, month, day, hour, min);
//
// or
//
// tfm::format(std::cout,
// "%s, %s %d, %.2d:%.2d\n",
// weekday, month, day, hour, min);
//
// 2. High-performance -- most printed strings are not too long and
// doens't need dynamic memory allocation. Many StringPrintf
// implementations doesn't enforce type-safe, but are
// high-performance, including
//
// https://developers.google.com/optimization/reference/base/stringprintf/
// https://github.com/adobe/chromium/blob/master/base/stringprintf.h
// https://github.com/google/protobuf/blob/master/src/google/protobuf/stubs/stringprintf.h
//
// According to
// https://github.com/c42f/tinyformat#compile-time-and-code-bloat,
// boost::format runs too slow and results in large executable binary
// files. So here we port tinyformat.
#pragma once
#include <iostream>
#include <sstream>
#include "paddle/string/tinyformat/tinyformat.h" // https://github.com/c42f/tinyformat
namespace
paddle
{
namespace
string
{
template
<
typename
...
Args
>
void
Fprintf
(
std
::
ostream
&
out
,
const
char
*
fmt
,
const
Args
&
...
args
)
{
tinyformat
::
vformat
(
out
,
fmt
,
makeFormatList
(
args
...));
}
template
<
typename
...
Args
>
std
::
string
Sprintf
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
std
::
ostringstream
oss
;
tinyformat
::
format
(
oss
,
fmt
,
args
...);
return
oss
.
str
();
}
template
<
typename
...
Args
>
void
printf
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
tinyformat
::
format
(
std
::
cout
,
fmt
,
args
...);
}
template
<
typename
...
Args
>
void
printfln
(
const
char
*
fmt
,
const
Args
&
...
args
)
{
tinyformat
::
format
(
std
::
cout
,
fmt
,
args
...);
std
::
cout
<<
'\n'
;
}
}
// namespace string
}
// namespace paddle
paddle/string/printf_test.cc
0 → 100644
浏览文件 @
6cb7cb36
#include "paddle/string/printf.h"
#include <string>
#include "gtest/gtest.h"
TEST
(
StringPrintf
,
StringPrintf
)
{
std
::
string
weekday
=
"Wednesday"
;
const
char
*
month
=
"July"
;
size_t
day
=
27
;
long
hour
=
14
;
int
min
=
44
;
EXPECT_EQ
(
std
::
string
(
"Wednesday, July 27, 14:44"
),
paddle
::
string
::
Sprintf
(
"%s, %s %d, %.2d:%.2d"
,
weekday
,
month
,
day
,
hour
,
min
));
}
paddle/string/tinyformat/tinyformat.h
0 → 100644
浏览文件 @
6cb7cb36
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录