From 76ac8dc3ca172a3d63e1769865cd1d1ea4181c5d Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Tue, 5 Sep 2023 11:09:35 +0800 Subject: [PATCH] Add `xy_str_strip()` --- xy.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/xy.h b/xy.h index 2736615..8497408 100644 --- a/xy.h +++ b/xy.h @@ -7,6 +7,8 @@ * xy: * * y = f(x) +* +* Corss-Platform C utilities in Ruby flavor * -------------------------------------------------------------*/ #ifndef XY_H @@ -348,4 +350,28 @@ xy_str_delete_suffix (const char* str, const char* suffix) return new; } + +char* +xy_str_strip (const char* str) +{ + const char* lf = "\n"; + const char* crlf = "\r\n"; + + char* new = xy_strdup(str); + + while (xy_str_start_with(new, lf)) { + new = xy_str_delete_prefix(new, lf); + } + while (xy_str_start_with(new, crlf)) { + new = xy_str_delete_prefix(new, crlf); + } + while (xy_str_end_with(new, lf)) { + new = xy_str_delete_suffix(new, lf); + } + while (xy_str_end_with(new, crlf)) { + new = xy_str_delete_suffix(new, crlf); + } + return new; +} + #endif