diff options
author | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-11-22 15:46:56 -0500 |
---|---|---|
committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2024-12-03 19:07:22 -0500 |
commit | 9173fe4a472500b3f87780d116ff7a54d4729c78 (patch) | |
tree | abec557976f8a68560c0c1fe7e9dafaa2d24e56a /src | |
parent | 93ad037af095d490e836a16b92e23f637f1f61ab (diff) | |
download | stasis-9173fe4a472500b3f87780d116ff7a54d4729c78.tar.gz |
Add basic unindent function
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/core/str.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/lib/core/str.c b/src/lib/core/str.c index a7dbab1..45fb60a 100644 --- a/src/lib/core/str.c +++ b/src/lib/core/str.c @@ -649,3 +649,28 @@ char *to_short_version(const char *s) { strchrdel(result, "."); return result; } + +void unindent(char *s) { + char *pos = NULL; + size_t leading_spaces; + + // Set position to beginning of string + pos = s; + + while (pos != NULL) { + const size_t len = strlen(s); + for (leading_spaces = 0; isspace(pos[leading_spaces]); leading_spaces++) {} + + // For each new line strip an indent + if (leading_spaces >= 4 && len >= 4) { + leading_spaces = 4; // remove first level of indentation + memmove(pos, pos + leading_spaces, len - leading_spaces); + pos[len - leading_spaces] = '\0'; + } + + pos = strchr(pos, '\n'); + if (pos && strlen(pos)) { + pos++; + } + } +}
\ No newline at end of file |