From cca66b9ec4e494c1d919bff0f71a820d8afab1fa Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 20:24:48 +0200 Subject: Adding upstream version 1.2.2. Signed-off-by: Daniel Baumann --- src/util/trim.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/util/trim.h (limited to 'src/util/trim.h') diff --git a/src/util/trim.h b/src/util/trim.h new file mode 100644 index 0000000..c8c9fd5 --- /dev/null +++ b/src/util/trim.h @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/** + * @file + * Inkscape::Util::trim - trim whitespace and other characters + * + * Authors: + * Rafael Siejakowski + * + * Copyright (C) 2022 the Authors. + * + * Released under GNU GPL v2+, read the file 'COPYING' for more information. + */ + +#ifndef SEEN_TRIM_H +#define SEEN_TRIM_H + +#include +#include + +namespace Inkscape { +namespace Util { + +/** + * @brief + * Modifies a string in place, removing leading and trailing whitespace characters. + * Optionally, it can remove other characters or ranges in addition to whitespace. + * + * @param input - a reference to a Glib::ustring which will be modified in place. + * @param also_remove - optional range of characters to remove in addition to whitespace. + * NOTE: these characters are inserted into a regex range (square brackets) and + * therefore may need to be regex-escaped. It is the responsibility of + * the user to pass a string that will work correctly in a regex range. + */ +inline void trim(Glib::ustring &input, Glib::ustring const &also_remove = "") +{ + auto const regex = Glib::Regex::create(Glib::ustring("^[\\s") + also_remove + "]*(.+?)[\\s" + + also_remove + "]*$"); + Glib::MatchInfo match_info; + regex->match(input, match_info); + if (!match_info.matches()) { + input.clear(); + return; + } + input = match_info.fetch(1); +} + +} // namespace Util +} // namespace Inkscape + +#endif // SEEN_TRIM_H +/* + 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 : -- cgit v1.2.3