stdlib: Try to coerce VS2019 to not replace some loops with memset() calls. Fixes (?) Bugzilla #4759.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c
index fdce135..65cf85e 100644
--- a/src/stdlib/SDL_string.c
+++ b/src/stdlib/SDL_string.c
@@ -1387,15 +1387,18 @@ SDL_PrintString(char *text, size_t maxlen, SDL_FormatInfo *info, const char *str
sz = SDL_strlen(string);
if (info && info->width > 0 && (size_t)info->width > sz) {
- char fill = info->pad_zeroes ? '0' : ' ';
+ const char fill = info->pad_zeroes ? '0' : ' ';
size_t width = info->width - sz;
+ size_t filllen;
+
if (info->precision >= 0 && (size_t)info->precision < sz)
width += sz - (size_t)info->precision;
- while (width-- > 0 && maxlen > 0) {
- *text++ = fill;
- ++length;
- --maxlen;
- }
+
+ filllen = SDL_min(width, maxlen);
+ SDL_memset(text, fill, filllen);
+ text += filllen;
+ length += filllen;
+ maxlen -= filllen;
}
slen = SDL_strlcpy(text, string, maxlen);
@@ -1580,7 +1583,7 @@ SDL_PrintFloat(char *text, size_t maxlen, SDL_FormatInfo *info, double arg)
width = info->width - (int)(text - textstart);
if (width > 0) {
- char fill = info->pad_zeroes ? '0' : ' ';
+ const char fill = info->pad_zeroes ? '0' : ' ';
char *end = text+left-1;
len = (text - textstart);
for (len = (text - textstart); len--; ) {
@@ -1596,10 +1599,10 @@ SDL_PrintFloat(char *text, size_t maxlen, SDL_FormatInfo *info, double arg)
text += len;
left -= len;
}
- while (len--) {
- if (textstart+len < end) {
- textstart[len] = fill;
- }
+
+ if (end != textstart) {
+ const size_t filllen = SDL_min(len, ((size_t) (end - textstart)) - 1);
+ SDL_memset(textstart, fill, fillen);
}
}