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