Navigation Menu

Skip to content

Commit

Permalink
Remove cast conversion (char **) to (const char **)
Browse files Browse the repository at this point in the history
because it can end up doing or hiding mistakes.

The conversion was added in previous commit.
A detailled reason to remove this conversion, which is not obvious at first,
can be found here: http://c-faq.com/ansi/constmismatch.html

And simplify UTF8_getch()
  • Loading branch information
1bsyl committed Mar 26, 2019
1 parent 0b24720 commit d69f094
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions SDL_ttf.c
Expand Up @@ -2216,17 +2216,19 @@ static void UCS2_to_UTF8(const Uint16 *src, Uint8 *dst)
*dst = '\0';
}

/* Gets a unicode value from a UTF-8 encoded string and advance the string */
/* Gets a unicode value from a UTF-8 encoded string
* Ouputs increment to advance the string */
#define UNKNOWN_UNICODE 0xFFFD
static Uint32 UTF8_getch(const char **src, size_t *srclen)
static Uint32 UTF8_getch(const char *src, size_t srclen, int *inc)
{
const Uint8 *p = *(const Uint8 **)src;
const Uint8 *p = (const Uint8 *)src;
size_t left = 0;
size_t save_srclen = srclen;
SDL_bool overlong = SDL_FALSE;
SDL_bool underflow = SDL_FALSE;
Uint32 ch = UNKNOWN_UNICODE;

if (*srclen == 0) {
if (srclen == 0) {
return UNKNOWN_UNICODE;
}
if (p[0] >= 0xFC) {
Expand Down Expand Up @@ -2274,18 +2276,16 @@ static Uint32 UTF8_getch(const char **src, size_t *srclen)
ch = (Uint32) p[0];
}
}
++*src;
--*srclen;
while (left > 0 && *srclen > 0) {
--srclen;
while (left > 0 && srclen > 0) {
++p;
if ((p[0] & 0xC0) != 0x80) {
ch = UNKNOWN_UNICODE;
break;
}
ch <<= 6;
ch |= (p[0] & 0x3F);
++*src;
--*srclen;
--srclen;
--left;
}
if (left > 0) {
Expand All @@ -2303,6 +2303,9 @@ static Uint32 UTF8_getch(const char **src, size_t *srclen)
(ch == 0xFFFE || ch == 0xFFFF) || ch > 0x10FFFF) {
ch = UNKNOWN_UNICODE;
}

*inc = save_srclen - srclen;

return ch;
}

Expand Down Expand Up @@ -2444,8 +2447,11 @@ static int TTF_Size_Internal(TTF_Font *font,
/* Load each character and sum it's bounding box */
textlen = SDL_strlen(text);
while (textlen > 0) {
Uint32 c = UTF8_getch(&text, &textlen);
int inc = 0;
Uint32 c = UTF8_getch(text, textlen, &inc);
FT_UInt idx = get_char_index(font, c);
text += inc;
textlen -= inc;

if (c == UNICODE_BOM_NATIVE || c == UNICODE_BOM_SWAPPED) {
continue;
Expand Down Expand Up @@ -2834,7 +2840,11 @@ static SDL_Surface* TTF_Render_Wrapped_Internal(TTF_Font *font, const char *text
}

while (textlen > 0) {
Uint32 c = UTF8_getch(&text_cpy, &textlen);
int inc = 0;
Uint32 c = UTF8_getch(text_cpy, textlen, &inc);
text_cpy += inc;
textlen -= inc;

if (c == UNICODE_BOM_NATIVE || c == UNICODE_BOM_SWAPPED) {
continue;
}
Expand Down

0 comments on commit d69f094

Please sign in to comment.