From 0ec26858a5158029288b0f5417dd1d35eb664bb6 Mon Sep 17 00:00:00 2001 From: "Peter W. Draper" <p.w.draper@durham.ac.uk> Date: Mon, 18 Jun 2018 15:46:27 +0100 Subject: [PATCH] Move general string trimming functions into tools --- src/parser.c | 38 +------------------------------------- src/tools.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/tools.h | 4 ++++ 3 files changed, 50 insertions(+), 37 deletions(-) diff --git a/src/parser.c b/src/parser.c index ac416bcaec..25eca24b85 100644 --- a/src/parser.c +++ b/src/parser.c @@ -34,6 +34,7 @@ #include "common_io.h" #include "error.h" #include "restart.h" +#include "tools.h" #define PARSER_COMMENT_STRING "#" #define PARSER_COMMENT_CHAR '#' @@ -57,43 +58,6 @@ static void find_duplicate_section(const struct swift_params *params, const char *section_name); static int lineNumber = 0; -/** - * @brief trim leading white space from a string. - * - * @param s the string. - * @result the result. - */ -static char *trim_leading(char *s) { - if (s == NULL || strlen(s) < 2) return s; - while (isspace(*s)) s++; - return s; -} - -/** - * @brief trim trailing white space from a string. - * - * @param s the string. - * @result the result. - */ -static char *trim_trailing(char *s) { - if (s == NULL || strlen(s) < 2) return s; - char *end = s + strlen(s) - 1; - while (isspace(*end)) end--; - *(end + 1) = '\0'; - return s; -} - -/** - * @brief trim leading and trailing white space from a string. - * - * @param s the string. - * @result the result. - */ -static char *trim_both(char *s) { - if (s == NULL || strlen(s) < 2) return s; - return trim_trailing(trim_leading(s)); -} - /** * @brief parse a YAML list of strings returning a set of pointers to * the strings. diff --git a/src/tools.c b/src/tools.c index 8b8b7fdf37..3899685b96 100644 --- a/src/tools.c +++ b/src/tools.c @@ -23,6 +23,7 @@ #include "../config.h" /* Some standard headers. */ +#include <ctype.h> #include <math.h> #include <stddef.h> #include <stdio.h> @@ -728,3 +729,47 @@ long get_maxrss() { getrusage(RUSAGE_SELF, &usage); return usage.ru_maxrss; } + +/** + * @brief trim leading white space from a string. + * + * Returns pointer to first character. + * + * @param s the string. + * @result the result. + */ +char *trim_leading(char *s) { + if (s == NULL || strlen(s) < 2) return s; + while (isspace(*s)) s++; + return s; +} + +/** + * @brief trim trailing white space from a string. + * + * Modifies the string by adding a NULL to the end. + * + * @param s the string. + * @result the result. + */ +char *trim_trailing(char *s) { + if (s == NULL || strlen(s) < 2) return s; + char *end = s + strlen(s) - 1; + while (isspace(*end)) end--; + *(end + 1) = '\0'; + return s; +} + +/** + * @brief trim leading and trailing white space from a string. + * + * Can modify the string by adding a NULL to the end. + * + * @param s the string. + * @result the result. + */ +char *trim_both(char *s) { + if (s == NULL || strlen(s) < 2) return s; + return trim_trailing(trim_leading(s)); +} + diff --git a/src/tools.h b/src/tools.h index a54510000d..25d0246791 100644 --- a/src/tools.h +++ b/src/tools.h @@ -54,4 +54,8 @@ int compare_particles(struct part a, struct part b, double threshold); long get_maxrss(void); +char *trim_leading(char *s); +char *trim_trailing(char *s); +char *trim_both(char *s); + #endif /* SWIFT_TOOL_H */ -- GitLab