diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:29:01 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:29:01 +0000 |
commit | 35a96bde514a8897f6f0fcc41c5833bf63df2e2a (patch) | |
tree | 657d15a03cc46bd099fc2c6546a7a4ad43815d9f /src/svg/strip-trailing-zeros.cpp | |
parent | Initial commit. (diff) | |
download | inkscape-35a96bde514a8897f6f0fcc41c5833bf63df2e2a.tar.xz inkscape-35a96bde514a8897f6f0fcc41c5833bf63df2e2a.zip |
Adding upstream version 1.0.2.upstream/1.0.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/svg/strip-trailing-zeros.cpp')
-rw-r--r-- | src/svg/strip-trailing-zeros.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/svg/strip-trailing-zeros.cpp b/src/svg/strip-trailing-zeros.cpp new file mode 100644 index 0000000..8abe4fa --- /dev/null +++ b/src/svg/strip-trailing-zeros.cpp @@ -0,0 +1,54 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** @file + * TODO: insert short description here + *//* + * Authors: see git history + * + * Copyright (C) 2018 Authors + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#include <cstring> +#include <string> +#include <glib.h> + +#include "svg/strip-trailing-zeros.h" + +std::string +strip_trailing_zeros(std::string str) +{ + std::string::size_type p_ix = str.find('.'); + if (p_ix != std::string::npos) { + std::string::size_type e_ix = str.find('e', p_ix); + /* N.B. In some contexts (e.g. CSS) it is an error for a number to contain `e'. fixme: + * Default to avoiding `e', e.g. using sprintf(str, "%17f", d). Add a new function that + * allows use of `e' and use that function only where the spec allows it. + */ + std::string::size_type nz_ix = str.find_last_not_of('0', (e_ix == std::string::npos + ? e_ix + : e_ix - 1)); + if (nz_ix == std::string::npos || nz_ix < p_ix || nz_ix >= e_ix) { + g_error("have `.' but couldn't find non-0"); + } else { + str.erase(str.begin() + (nz_ix == p_ix + ? p_ix + : nz_ix + 1), + (e_ix == std::string::npos + ? str.end() + : str.begin() + e_ix)); + } + } + return str; +} + + +/* + Local Variables: + mode:c++ + c-file-style:"stroustrup" + c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) + indent-tabs-mode:nil + fill-column:99 + End: +*/ +// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 : |