aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2024-11-22 15:46:56 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2024-11-22 15:46:56 -0500
commit4bb95cb0d95138ac768c80b30728bef8f691f1e8 (patch)
treeb1e15a0f12e2a3fe4923354e2711cacd07d0ba9e
parentb3facf587175307ef32547d0264bf096d2fcd283 (diff)
downloadstasis-4bb95cb0d95138ac768c80b30728bef8f691f1e8.tar.gz
Add basic unindent function
-rw-r--r--include/str.h2
-rw-r--r--src/lib/core/str.c25
2 files changed, 27 insertions, 0 deletions
diff --git a/include/str.h b/include/str.h
index 7254225..bb96db0 100644
--- a/include/str.h
+++ b/include/str.h
@@ -308,4 +308,6 @@ char *tolower_s(char *s);
*/
char *to_short_version(const char *s);
+void unindent(char *s);
+
#endif //STASIS_STR_H
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