Conv
folly/Conv.h
folly/Conv.h
is a one-stop-shop for converting values across types. Its main features are simplicity of the API (only the names to
and toAppend
must be memorized), speed (folly is significantly faster, sometimes by an order of magnitude, than comparable APIs), and correctness.
Synopsis
All examples below are assume to have included folly/Conv.h
and issued using namespace folly;
You will need:
// To format as text and append to a string, use toAppend.
fbstring str;
toAppend(2.5, &str);
CHECK_EQ(str, "2.5"); // Multiple arguments are okay, too. Just put the pointer to string at the end.
toAppend(" is ", , " point ", , &str);
CHECK_EQ(str, "2.5 is 2 point 5"); // You don't need to use fbstring (although it's much faster for conversions and in general).
std::string stdStr;
toAppend("Pi is about ", 22.0 / , &stdStr);
// In general, just use to<TargetType>(sourceValue). It returns its result by value.
stdStr = to<std::string>("Variadic ", "arguments also accepted."); // to<fbstring> is 2.5x faster than to<std::string> for typical workloads.
str = to<fbstring>("Variadic ", "arguments also accepted.");
Integral-to-integral conversion
Using to<Target>(value)
to convert one integral type to another will behave as follows:
- If the target type can accommodate all possible values of the source value, the value is implicitly converted. No further action is taken. Example:
short x;
unsigned short y;
...
auto a = to<int>(x); // zero overhead conversion
auto b = to<int>(y); // zero overhead conversion
- Otherwise,
to
inserts bounds checks and throwsstd::range_error
if the target type cannot accommodate the source value. Example:
short x;
unsigned short y;
long z;
...
x = ;
auto a = to<unsigned short>(x); // fine
x = -;
a = to<unsigned short>(x); // THROWS
z = ;
auto b = to<int>(z); // fine
z += ;
b = to<int>(z); // THROWS
auto b = to<unsigned int>(z); // fine
Anything-to-string conversion
As mentioned, there are two primitives for converting anything to string: to
and toAppend
. They support the same set of source types, literally by definition (to
is implemented in terms of toAppend
for all types). The call toAppend(value, &str)
formats and appends value
to str
whereas to<StringType>(value)
formats value
as a StringType
and returns the result by value. Currently, the supported StringType
s are std::string
and fbstring
Both toAppend
and to
with a string type as a target support variadic arguments. Each argument is converted in turn. FortoAppend
the last argument in a variadic list must be the address of a supported string type (no need to specify the string type as a template argument).
Integral-to-string conversion
Nothing special here - integrals are converted to strings in decimal format, with a '-' prefix for negative values. Example:
auto a = to<fbstring>();
assert(a == "");
a = to<fbstring>(-);
assert(a == "-456");
The conversion implementation is aggressively optimized. It converts two digits at a time assisted by fixed-size tables. Converting a long
to an fbstring
is 3.6x faster than using boost::lexical_cast
and 2.5x faster than using sprintf
even though the latter is used in conjunction with a stack-allocated constant-size buffer.
Note that converting integral types to fbstring
has a particular advantage compared to converting to std::string
No integral type (<= 64 bits) has more than 20 decimal digits including sign. Since fbstring
employs the small string optimization for up to 23 characters, converting an integral to fbstring
is guaranteed to not allocate memory, resulting in significant speed and memory locality gains. Benchmarks reveal a 2x gain on a typical workload.
char
to string conversion
Although char
is technically an integral type, most of the time you want the string representation of 'a'
to be "a"
, not 96
That's why folly/Conv.h
handles char
as a special case that does the expected thing. Note that signed char
and unsigned char
are still considered integral types.
Floating point to string conversion
folly/Conv.h
uses V8's double conversion routines. They are accurate and fast; on typical workloads, to<fbstring>(doubleValue)
is 1.9x faster than sprintf
and 5.5x faster than boost::lexical_cast
(It is also 1.3x faster than to<std::string>(doubleValue)
const char*
to string conversion
For completeness, folly/Conv.h
supports const char*
including i.e. string literals. The "conversion" consists, of course, of the string itself. Example:
auto s = to<fbstring>("Hello, world");
assert(s == "Hello, world");
Anything from string conversion (i.e. parsing)
folly/Conv.h
includes three kinds of parsing routines:
to<Type>(const char* begin, const char* end)
rigidly converts the range [begin, end) toType
These routines have drastic restrictions (e.g. allow no leading or trailing whitespace) and are intended as an efficient back-end for more tolerant routines.to<Type>(stringy)
convertsstringy
toType
Valuestringy
may be of typeconst char*
,StringPiece
,std::string
, orfbstring
(Technically, the requirement is thatstringy
implicitly converts to aStringPiece
to<Type>(&stringPiece)
parses with progress information: givenstringPiece
of typeStringPiece
it parses as much as possible from it as typeType
and altersstringPiece
to remove the munched characters. This is easiest clarified by an example:
fbstring s = " 1234 angels on a pin";
StringPiece pc(s);
auto x = to<int>(&pc);
assert(x == );
assert(pc == " angels on a pin";
Note how the routine ate the leading space but not the trailing one.
Parsing integral types
Parsing integral types is unremarkable - decimal format is expected, optional '+'
or '-'
sign for signed types, but no optional '+'
is allowed for unsigned types. The one remarkable element is speed - parsing typical long
values is 6x faster than sscanf
. folly/Conv.h
uses aggressive loop unrolling and table-assisted SIMD-style code arrangement that avoids integral division (slow) and data dependencies across operations (ILP-unfriendly). Example:
fbstring str = " 12345 ";
assert(to<int>(str) == );
str = " 12345six seven eight";
StringPiece pc(str);
assert(to<int>(&pc) == );
assert(str == "six seven eight");
Parsing floating-point types
folly/Conv.h
uses, again, V8's double-conversion routines as back-end. The speed is 3x faster than sscanf
and 1.7x faster than in-home routines such as parse<double>
But the more important detail is accuracy - even if you do code a routine that works faster than to<double>
chances are it is incorrect and will fail in a variety of corner cases. Using to<double>
is strongly recommended.
Note that if the string "NaN" (with any capitalization) is passed to to<double>
then NaN
is returned, which can be tested for as follows:
fbstring str = "nan"; // "NaN", "NAN", etc.
double d = to<double>(str);
if (std::isnan(d)) {
// string was a valid representation of the double value NaN
}
Note that passing "-NaN" (with any capitalization) to to<double>
also returns NaN
.
Note that if the strings "inf" or "infinity" (with any capitalization) are passed to to<double>
then infinity
is returned, which can be tested for as follows:
fbstring str = "inf"; // "Inf", "INF", "infinity", "Infinity", etc.
double d = to<double>(str);
if (std::isinf(d)) {
// string was a valid representation of one of the double values +Infinity
// or -Infinity
}
Note that passing "-inf" or "-infinity" (with any capitalization) to to<double>
returns -infinity
rather than +infinity
. The sign of the infinity
can be tested for as follows:
fbstring str = "-inf"; // or "inf", "-Infinity", "+Infinity", etc.
double d = to<double>(str);
if (d == std::numeric_limits<double>::infinity()) {
// string was a valid representation of the double value +Infinity
} else if (d == -std::numeric_limits<double>::infinity()) {
// string was a valid representation of the double value -Infinity
}
Note that if an unparseable string is passed to to<double>
then an exception is thrown, rather than NaN
being returned. This can be tested for as follows:
fbstring str = "not-a-double"; // Or "1.1.1", "", "$500.00", etc.
double d;
try {
d = to<double>(str);
} catch (const std::range_error &) {
// string could not be parsed
}
Note that the empty string (""
) is an unparseable value, and will cause to<double>
to throw an exception.
Non-throwing interfaces
tryTo<T>
is the non-throwing variant of to<T>
. It returns an Expected<T, ConversionCode>
. You can think of Expected
as like an Optional<T>
, but if the conversion failed, Expected
stores an error code instead of a T
.
tryTo<T>
has similar performance as to<T>
when the conversion is successful. On the error path, you can expect tryTo<T>
to be roughly three orders of magnitude faster than the throwing to<T>
and to completely avoid any lock contention arising from stack unwinding.
Here is how to use non-throwing conversions:
auto t1 = tryTo<int>(str);
if (t1.hasValue()) {
use(t1.value());
}
Expected
has a composability feature to make the above pattern simpler.
tryTo<int>(str).then([](int i) { use(i); });
Conv的更多相关文章
- matlab中的卷积——filter,conv之间的区别
%Matlab提供了计算线性卷积和两个多项式相乘的函数conv,语法格式w=conv(u,v),其中u和v分别是有限长度序列向量,w是u和v的卷积结果序列向量. %如果向量u和v的长度分别为N和M,则 ...
- mysql的conv的用法
这次的ctf比赛用到这个函数,这里记录一下 题目禁了ascii , ord 那就使用conv 这个函数是用来将字符转换进制的,例如将a转成ASCII码(换个说法就是将16进制的a换成10进制) 那就直 ...
- (原)caffe中的conv
转载请注明出处: https://www.cnblogs.com/darkknightzh/p/10486686.html conv总体调用流程如下图所示: 说明:带o的为输出,如Wo代表输出宽度:带 ...
- 深度学习卷积网络中反卷积/转置卷积的理解 transposed conv/deconv
搞明白了卷积网络中所谓deconv到底是个什么东西后,不写下来怕又忘记,根据参考资料,加上我自己的理解,记录在这篇博客里. 先来规范表达 为了方便理解,本文出现的举例情况都是2D矩阵卷积,卷积输入和核 ...
- [转载] Conv Nets: A Modular Perspective
原文地址:http://colah.github.io/posts/2014-07-Conv-Nets-Modular/ Conv Nets: A Modular Perspective Posted ...
- MATLAB卷积运算(conv、conv2、convn)解释
1 conv(向量卷积运算) 所谓两个向量卷积,说白了就是多项式乘法.比如:p=[1 2 3],q=[1 1]是两个向量,p和q的卷积如下:把p的元素作为一个多项式的系数,多项式按升幂(或降幂)排列, ...
- boost-字符编码转换:使用conv
Windows下的字符集转换可以使用WideCharToMultiByte/ MultiByteToWideChar,Linux下字符集转换可以使用iconv()函数,下面为使用boost的conv来 ...
- tensorflow 之常见模块conv,bn...实现
使用tensorflow时,会发现tf.nn,tf.layers, tf.contrib模块有很多功能是重复的,尤其是卷积操作,在使用的时候,我们可以根据需要现在不同的模块.但有些时候可以一起混用. ...
- MySQL中特有的函数CONV函数
CONV函数:用于对数字进行转换,比如将十进制的数字转化成二进制,参数格式convert(N,frombse,tobase) 将数字N从frombase进制转化成tobase进制,并且以字符串的格式返 ...
随机推荐
- 【转】IntelliJ IDEA的光芒会盖过Eclipse吗
作为一个资深的Eclipse用户,我想对IntelliJ IDEA做一个更为严谨的审视.JetBrains的工作人员非常的友善,并为Podcastpedia.org和Codingpedia.org这两 ...
- 利用JavaScript将页面截图生成图片传给后台的插件:html2canvas
利用JavaScript将页面截图生成图片传给后台的插件:html2canvas 一.总结 一句话总结: 10 <script type="text/javascript"& ...
- 资源管理器总是生成 avi,mpeg的预览图
感觉非常讨厌. 图片可以直接显示,但是视频的预览图感觉很不舒服. 查了一下 , 用ShellExView 禁用一下 MF XXXX Property Handler 就可以了. 官网最下面可以下载: ...
- python2和python3的区别——持续更新
1.在 cookbook 上看到的,python3支持 *运算符 来接收迭代变量,如: a, *b = [, , , ] python2是不支持的! 2.在 cookbook 上看到的,python3 ...
- myeclipse出现src作为报名一部分src.com.*
打开历史悠久的项目,所有类报错,发现src变成了报名的一部分. 右击src->Build Path->Use as Source Folder即可. WebRoot中同理.
- UVA-10410 Tree Reconstruction (树重建)
题目大意:给出对一棵树的BFS遍历序列和DFS遍历序列,求出每一个节点的子节点. 题目分析:在BFS的序列中,子节点的下标一定比父节点的下标至少大1(根节点与第一个子节点除外),即pos[fa]+1& ...
- Sub-process /usr/bin/dpkg returned an error code (1)解决方法
在ubuntu下使用apt-get install 安装资源的时候,总是会遇到Sub-process /usr/bin/dpkg returned an error code (1) 错了, 跟安装软 ...
- 苹果iPhone 5C和5S发布后,消费者如何选择?
9月11日凌晨苹果新品发布会,笔者的朋友圈居然没有看直播的,真果粉太少了.笔者来阐述一些容易忽略的东西. iPhone5C和5S与5有什么不一样? 新品iPhone 5S 外观与iPhone5 相似度 ...
- 201621123006 《Java程序设计》第6周学习总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰 ...
- FOR UPDATE
1. for update的使用场景 `如果遇到存在高并发并且对于数据的准确性很有要求的场景,是需要了解和使用for update的. 比如涉及到金钱.库存等.一般这些操作都是很长一串并且是开启 ...