1 #include "svg/strip-trailing-zeros.h"
2 #include <glib/gmessages.h>
3 using std::string;
5 string
6 strip_trailing_zeros(string str)
7 {
8 string::size_type p_ix = str.find('.');
9 if (p_ix != string::npos) {
10 string::size_type e_ix = str.find('e', p_ix);
11 /* N.B. In some contexts (e.g. CSS) it is an error for a number to contain `e'. fixme:
12 * Default to avoiding `e', e.g. using sprintf(str, "%17f", d). Add a new function that
13 * allows use of `e' and use that function only where the spec allows it.
14 */
15 string::size_type nz_ix = str.find_last_not_of('0', (e_ix == string::npos
16 ? e_ix
17 : e_ix - 1));
18 if (nz_ix == string::npos || nz_ix < p_ix || nz_ix >= e_ix) {
19 g_error("have `.' but couldn't find non-0");
20 } else {
21 str.erase(str.begin() + (nz_ix == p_ix
22 ? p_ix
23 : nz_ix + 1),
24 (e_ix == string::npos
25 ? str.end()
26 : str.begin() + e_ix));
27 }
28 }
29 return str;
30 }
33 /*
34 Local Variables:
35 mode:c++
36 c-file-style:"stroustrup"
37 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
38 indent-tabs-mode:nil
39 fill-column:99
40 End:
41 */
42 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :