Skip to content
Snippets Groups Projects
Commit 0ec26858 authored by Peter W. Draper's avatar Peter W. Draper
Browse files

Move general string trimming functions into tools

parent 5a826948
Branches
Tags
1 merge request!564Yaml arrays
......@@ -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.
......
......@@ -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));
}
......@@ -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 */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment