summaryrefslogtreecommitdiffstats
path: root/src/util/trim.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:24:48 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:24:48 +0000
commitcca66b9ec4e494c1d919bff0f71a820d8afab1fa (patch)
tree146f39ded1c938019e1ed42d30923c2ac9e86789 /src/util/trim.h
parentInitial commit. (diff)
downloadinkscape-cca66b9ec4e494c1d919bff0f71a820d8afab1fa.tar.xz
inkscape-cca66b9ec4e494c1d919bff0f71a820d8afab1fa.zip
Adding upstream version 1.2.2.upstream/1.2.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--src/util/trim.h60
1 files changed, 60 insertions, 0 deletions
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 <rs@rs-math.net>
+ *
+ * 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 <glibmm/ustring.h>
+#include <glibmm/regex.h>
+
+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 :