From 69c7062f15a677544a9e65f8c034f021217ac1e3 Mon Sep 17 00:00:00 2001 From: Michael Schupikov Date: Mon, 9 Jan 2017 16:25:29 +0100 Subject: [PATCH] stringop: Add is_empty() for checking for empty strings --- common/stringop.c | 18 ++++++++++++++++++ include/stringop.h | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/common/stringop.c b/common/stringop.c index ad128e52d..c8e10f8c3 100644 --- a/common/stringop.c +++ b/common/stringop.c @@ -293,6 +293,24 @@ static bool has_whitespace(const char *str) { return false; } + +/* + * Returns true if the string contains only whitespace and false otherwise. + */ +bool is_empty(const char *str) { + bool ret = true; + if (str) { + while (*str != '\0') { + if (!isspace(*(const unsigned char*)str)) { + ret = false; + break; + } + str++; + } + } + return ret; +} + /** * Add quotes around any argv with whitespaces. */ diff --git a/include/stringop.h b/include/stringop.h index 2555babfb..7be3d0a9c 100644 --- a/include/stringop.h +++ b/include/stringop.h @@ -1,5 +1,7 @@ #ifndef _SWAY_STRINGOP_H #define _SWAY_STRINGOP_H + +#include #include "list.h" #if !HAVE_DECL_SETENV @@ -14,6 +16,9 @@ void strip_whitespace(char *str); char *strip_comments(char *str); void strip_quotes(char *str); +// Returns true if the string contains only whitespace. +bool is_empty(const char *str); + // strcmp that also handles null pointers. int lenient_strcmp(char *a, char *b);