Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sspans/afbfb93878e068cfe5cdaa78b0378634 to your computer and use it in GitHub Desktop.
Save sspans/afbfb93878e068cfe5cdaa78b0378634 to your computer and use it in GitHub Desktop.
From 3b5bfecc666e8f09f3b6629f0f3f89e1b8ed619a Mon Sep 17 00:00:00 2001
From: Christian Hesse <mail@eworm.de>
Date: Fri, 29 Nov 2013 12:09:53 +0100
Subject: [PATCH 1/1] add support for trimming whitespaces from strings
---
src/core/settings.c | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/src/core/settings.c b/src/core/settings.c
index 03ef084..aa127f3 100644
--- a/src/core/settings.c
+++ b/src/core/settings.c
@@ -1666,6 +1666,40 @@
.format = format_string_setting,
};
+/**
+ * Format trimmed string setting value
+ *
+ * @v type Setting type
+ * @v raw Raw setting value
+ * @v raw_len Length of raw setting value
+ * @v buf Buffer to contain formatted value
+ * @v len Length of buffer
+ * @ret len Length of formatted value, or negative error
+ */
+static int format_string_trim_setting ( struct setting_type *type __unused,
+ const void *raw, size_t raw_len, char *buf,
+ size_t len ) {
+
+ char * end;
+
+ /* call plain string format first */
+ raw_len = format_string_setting (type, raw, raw_len, buf, len);
+
+ /* then cut all trailing spaces */
+ end = buf + raw_len;
+ while (end > buf && (*end == ' ' || *end == 0))
+ *end-- = 0;
+
+ return end - buf;
+}
+
+/** A trimmed string setting type */
+const struct setting_type setting_type_string_trim __setting_type = {
+ .name = "trim",
+ .parse = parse_string_setting,
+ .format = format_string_trim_setting,
+};
+
/** A URI-encoded string setting type
*
* This setting type is obsolete; the name ":uristring" is retained to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment