summaryrefslogtreecommitdiff
path: root/src/string.cpp
diff options
context:
space:
mode:
authorrubidium42 <rubidium@openttd.org>2021-04-23 07:46:50 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-04-24 08:02:54 +0200
commit5202869f0fec5512211988b35b1ee4b29a33686d (patch)
treeec00d6173df3abb5fd0d29d6cb5d60108179a01a /src/string.cpp
parentb14f4121170cb75aba785efc9e73a364c232301b (diff)
downloadopenttd-5202869f0fec5512211988b35b1ee4b29a33686d.tar.xz
Add: String functionality to trim spaces from C-style strings
Diffstat (limited to 'src/string.cpp')
-rw-r--r--src/string.cpp60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/string.cpp b/src/string.cpp
index adc757b87..dfd01450e 100644
--- a/src/string.cpp
+++ b/src/string.cpp
@@ -302,6 +302,66 @@ bool StrValid(const char *str, const char *last)
return *str == '\0';
}
+/**
+ * Trim the spaces from the begin of given string in place, i.e. the string buffer
+ * that is passed will be modified whenever spaces exist in the given string.
+ * When there are spaces at the begin, the whole string is moved forward.
+ * @param str The string to perform the in place left trimming on.
+ */
+static void StrLeftTrimInPlace(char *str)
+{
+ if (StrEmpty(str)) return;
+
+ char *first_non_space = str;
+ while (*first_non_space == ' ') first_non_space++;
+
+ if (first_non_space == str) return;
+
+ /* The source will reach '\0' first, but set the '\0' on the destination afterwards. */
+ char *dst = str;
+ for (char *src = first_non_space; *src != '\0'; dst++, src++) *dst = *src;
+ *dst = '\0';
+}
+
+/**
+ * Trim the spaces from the end of given string in place, i.e. the string buffer
+ * that is passed will be modified whenever spaces exist in the given string.
+ * When there are spaces at the end, the '\0' will be moved forward.
+ * @param str The string to perform the in place left trimming on.
+ */
+static void StrRightTrimInPlace(char *str)
+{
+ if (StrEmpty(str)) return;
+
+ char *end = str;
+ while (*end != '\0') end++;
+
+ char *last_non_space = end - 1;
+ while (last_non_space >= str && *last_non_space == ' ') last_non_space--;
+
+ /* The last non space points to the last character of the string that is not
+ * a space. For a string with only spaces or an empty string this would be
+ * the position before the begin of the string. The previous search ensures
+ * that this location before the string is not read.
+ * In any case, the character after the last non space character will be
+ * either a space or the existing termination, so it can be set to '\0'.
+ */
+ last_non_space[1] = '\0';
+}
+
+/**
+ * Trim the spaces from given string in place, i.e. the string buffer that
+ * is passed will be modified whenever spaces exist in the given string.
+ * When there are spaces at the begin, the whole string is moved forward
+ * and when there are spaces at the back the '\0' termination is moved.
+ * @param str The string to perform the in place trimming on.
+ */
+void StrTrimInPlace(char *str)
+{
+ StrLeftTrimInPlace(str);
+ StrRightTrimInPlace(str);
+}
+
/** Scans the string for colour codes and strips them */
void str_strip_colours(char *str)
{