stdlib: Try to coerce VS2019 to not replace some loops with memset() calls.
authorRyan C. Gordon <icculus@icculus.org>
Thu, 26 Sep 2019 12:55:05 -0400
changeset 130943def438a1540
parent 13093 599ed6e984d2
child 13095 47cbedebfbd1
stdlib: Try to coerce VS2019 to not replace some loops with memset() calls.

Fixes (?) Bugzilla #4759.
src/stdlib/SDL_string.c
     1.1 --- a/src/stdlib/SDL_string.c	Wed Sep 25 15:40:27 2019 -0400
     1.2 +++ b/src/stdlib/SDL_string.c	Thu Sep 26 12:55:05 2019 -0400
     1.3 @@ -1387,15 +1387,18 @@
     1.4  
     1.5      sz = SDL_strlen(string);
     1.6      if (info && info->width > 0 && (size_t)info->width > sz) {
     1.7 -        char fill = info->pad_zeroes ? '0' : ' ';
     1.8 +        const char fill = info->pad_zeroes ? '0' : ' ';
     1.9          size_t width = info->width - sz;
    1.10 +        size_t filllen;
    1.11 +
    1.12          if (info->precision >= 0 && (size_t)info->precision < sz)
    1.13              width += sz - (size_t)info->precision;
    1.14 -        while (width-- > 0 && maxlen > 0) {
    1.15 -            *text++ = fill;
    1.16 -            ++length;
    1.17 -            --maxlen;
    1.18 -        }
    1.19 +
    1.20 +        filllen = SDL_min(width, maxlen);
    1.21 +        SDL_memset(text, fill, filllen);
    1.22 +        text += filllen;
    1.23 +        length += filllen;
    1.24 +        maxlen -= filllen;
    1.25      }
    1.26  
    1.27      slen = SDL_strlcpy(text, string, maxlen);
    1.28 @@ -1580,7 +1583,7 @@
    1.29  
    1.30      width = info->width - (int)(text - textstart);
    1.31      if (width > 0) {
    1.32 -        char fill = info->pad_zeroes ? '0' : ' ';
    1.33 +        const char fill = info->pad_zeroes ? '0' : ' ';
    1.34          char *end = text+left-1;
    1.35          len = (text - textstart);
    1.36          for (len = (text - textstart); len--; ) {
    1.37 @@ -1596,10 +1599,10 @@
    1.38              text += len;
    1.39              left -= len;
    1.40          }
    1.41 -        while (len--) {
    1.42 -            if (textstart+len < end) {
    1.43 -                textstart[len] = fill;
    1.44 -            }
    1.45 +
    1.46 +        if (end != textstart) {
    1.47 +            const size_t filllen = SDL_min(len, ((size_t) (end - textstart)) - 1);
    1.48 +            SDL_memset(textstart, fill, fillen);
    1.49          }
    1.50      }
    1.51