From 63973a52e03a29e57984a0af938f5a59584e12f2 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 5 Mar 2017 19:01:44 +0100 Subject: [PATCH] Simplify word_wrap --- libretro-common/string/stdstring.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/libretro-common/string/stdstring.c b/libretro-common/string/stdstring.c index 7cd05e8b14..8aa4cff2e7 100644 --- a/libretro-common/string/stdstring.c +++ b/libretro-common/string/stdstring.c @@ -168,10 +168,11 @@ char *string_trim_whitespace(char *const s) char* word_wrap(char* buffer, char* string, int line_width) { unsigned i = 0; - int k, counter; while(i < strlen(string)) { + int counter; + /* copy string until the end of the line is reached */ for (counter = 1; counter <= line_width; counter++) { @@ -190,6 +191,7 @@ char* word_wrap(char* buffer, char* string, int line_width) counter = 1; i++; } + /* check for whitespace */ if (string[i] == ' ') { @@ -198,16 +200,18 @@ char* word_wrap(char* buffer, char* string, int line_width) } else { + int k; + /* check for nearest whitespace back in string */ for (k = i; k > 0; k--) { - if (string[k] == ' ') - { - buffer[k] = '\n'; - /* set string index back to character after this one */ - i = k + 1; - break; - } + if (string[k] != ' ') + continue; + + buffer[k] = '\n'; + /* set string index back to character after this one */ + i = k + 1; + break; } } }