Add xy_str_strip()

This commit is contained in:
Aoran Zeng 2023-09-05 11:09:35 +08:00
parent 84a9e7338f
commit 76ac8dc3ca

26
xy.h
View File

@ -7,6 +7,8 @@
* xy: * xy:
* *
* y = f(x) * y = f(x)
*
* Corss-Platform C utilities in Ruby flavor
* -------------------------------------------------------------*/ * -------------------------------------------------------------*/
#ifndef XY_H #ifndef XY_H
@ -348,4 +350,28 @@ xy_str_delete_suffix (const char* str, const char* suffix)
return new; 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 #endif