diff --git a/src/parser.c b/src/parser.c index ac416bcaec14509749918beaba06bd16e51f1a3c..25eca24b858430d8e8ff83a107de78cf13a8d114 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 8b8b7fdf37d91547f328a6b49d69b3c5a491aed1..3899685b966971eeb52ffc56d4cb154fda205cd7 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 a54510000d3c2843e8d60047752b19a46bd502d9..25d024679174eabbe89908c0254651e4bbc69e15 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 */