From 3b5b41bf54b2a6c95dab8cdc1516541aa819007f Mon Sep 17 00:00:00 2001 From: Lorenzo Cogotti Date: Fri, 15 Oct 2021 12:13:01 +0200 Subject: [PATCH] [lonetix/strlib] Add convenience function to reverse strings --- lonetix/include/df/strlib.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lonetix/include/df/strlib.h b/lonetix/include/df/strlib.h index 32ceb45..a7e4b8e 100644 --- a/lonetix/include/df/strlib.h +++ b/lonetix/include/df/strlib.h @@ -195,4 +195,25 @@ INLINE size_t Df_strpadr(char *s, char c, size_t n) return i; } +/** + * \brief Reverse string in place. + * + * \return String length. + */ +INLINE size_t Df_strrev(char *s) +{ + EXTERNC size_t strlen(const char *); + + size_t n = strlen(s); + char *e = s + n - 1; + while (e > s) { + char c = *s; + + *s++ = *e; + *e-- = c; + } + + return n; +} + #endif