|
Boost Users : |
From: Martin (adrianm_at_[hidden])
Date: 2005-06-03 06:21:41
I have a library which allows you to do all kind of conversions like
to_string<string>(1234);
to_string<wstring>(1234, &hex);
to_string<fixed_string<10> >(10.25, make_tuple(&fixed, setprecision(1), setw
(10)));
to_string<string>(1234, locale::classic());
to_string_classic<wstring>(1234); // same as above but with non-stream
specializations for int (planning to add double)
from_string<int>(str);
from_string<double>(str, -5); // return -5 on error
from_string<double>(str, &hex);
from_string<int>(str, &noskipws, std::locale("German"), 0);
from_string_classic<int>(str);
If you have many conversions you can increase preformance by reusing the
stream.
basic_tofrom_string<string> conv(make_tuple(setprecision(2), &fixed),
locale::classic());
conv.to_string(1234, &hex);
conv.to_char(1.234, setw(10)); // avoid copying result to std::string
conv.from_string<double>(str, 0.0);
All conversion routines uses a home-made streambuffer with optimizations to
avoid copying and memory allocations (e.g. small string optimization & buffer
reuse).
Main benefit over lexical_cast is that you can control formatting & locale but
performance is also much better due to less memory alllocations.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net