]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - utf8lib.c
detect whether dpsoftrast is being built with SDL
[xonotic/darkplaces.git] / utf8lib.c
index 16a94a7ae7fefe2fe108543fb69c52f39e68a780..bb775b7fd91b749a4593382f52691135c58f4250 100644 (file)
--- a/utf8lib.c
+++ b/utf8lib.c
@@ -62,9 +62,8 @@ Uchar utf8_range[5] = {
 static qboolean u8_analyze(const char *_s, size_t *_start, size_t *_len, Uchar *_ch, size_t _maxlen)
 {
        const unsigned char *s = (const unsigned char*)_s;
-       //unsigned char bt;//, bc;
-       size_t i;
-       size_t bits, j;
+       size_t i, j;
+       size_t bits = 0;
        Uchar ch;
 
        i = 0;
@@ -586,7 +585,7 @@ int u8_fromchar(Uchar w, char *to, size_t maxlen)
                to[3] = 0x80 | (w & 0x3F); w >>= 6;
                to[2] = 0x80 | (w & 0x3F); w >>= 6;
                to[1] = 0x80 | (w & 0x3F); w >>= 6;
-               to[0] = 0xE0 | w;
+               to[0] = 0xF0 | w;
                return 4;
        }
        return 0;
@@ -683,6 +682,7 @@ u8_COM_StringLengthNoColors(const char *_s, size_t size_s, qboolean *valid)
        const unsigned char *s = (const unsigned char*)_s;
        const unsigned char *end;
        size_t len = 0;
+       size_t st, ln;
 
        if (!utf8_enable.integer)
                return COM_StringLengthNoColors(_s, size_s, valid);
@@ -728,22 +728,46 @@ u8_COM_StringLengthNoColors(const char *_s, size_t size_s, qboolean *valid)
                                                ++len; // the character
                                                break;
                                }
-                               break;
+                               ++s;
+                               continue;
                        default:
-                               ++len;
                                break;
                }
 
-               // start of a wide character
-               if (*s & 0xC0)
+               // ascii char, skip u8_analyze
+               if (*s < 0x80)
                {
-                       for (++s; *s >= 0x80 && *s <= 0xC0; ++s);
+                       ++len;
+                       ++s;
                        continue;
                }
-               // part of a wide character, we ignore that one
-               if (*s <= 0xBF)
-                       --len;
-               ++s;
+
+               // invalid, skip u8_analyze
+               if (*s < 0xC2)
+               {
+                       ++s;
+                       continue;
+               }
+
+               if (!u8_analyze((const char*)s, &st, &ln, NULL, U8_ANALYZE_INFINITY))
+               {
+                       // we CAN end up here, if an invalid char is between this one and the end of the string
+                       if(valid)
+                               *valid = TRUE;
+                       return len;
+               }
+
+               if(end && s + st + ln > end)
+               {
+                       // string length exceeded by new character
+                       if(valid)
+                               *valid = TRUE;
+                       return len;
+               }
+
+               // valid character, skip after it
+               s += st + ln;
+               ++len;
        }
        // never get here
 }