]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
clean up u8_StringLengthNoColors
authordivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Sat, 4 Sep 2010 15:32:12 +0000 (15:32 +0000)
committerdivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Sat, 4 Sep 2010 15:32:12 +0000 (15:32 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@10440 d7cf8633-e32d-0410-b094-e92efae38249

utf8lib.c

index 3a2fabe79da7fb6dd4542dad468a1fa9512180f4..0461672d796688b3bee06f2ab17a2bc058a9067d 100644 (file)
--- a/utf8lib.c
+++ b/utf8lib.c
@@ -682,7 +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 bits = 0;
+       size_t st, ln;
 
        if (!utf8_enable.integer)
                return COM_StringLengthNoColors(_s, size_s, valid);
@@ -691,7 +691,7 @@ u8_COM_StringLengthNoColors(const char *_s, size_t size_s, qboolean *valid)
 
        for(;;)
        {
-               switch((s == end) ? 0 : *s)
+               switch((s >= end) ? 0 : *s)
                {
                        case 0:
                                if(valid)
@@ -728,23 +728,45 @@ u8_COM_StringLengthNoColors(const char *_s, size_t size_s, qboolean *valid)
                                                ++len; // the character
                                                break;
                                }
-                               break;
+                               continue;
                        default:
-                               ++len;
                                break;
                }
 
-               // start of a wide character
-               bits = utf8_lengths[*s];
-               if (bits >= 2)
+               // ascii char, skip u8_analyze
+               if (*s < 0x80)
+               {
+                       ++len;
+                       ++s;
+                       continue;
+               }
+
+               // invalid, skip u8_analyze
+               if (*s < 0xC2)
                {
-                       for (++s; bits >= 1 && *s >= 0x80 && *s < 0xC0; ++s, --bits);
+                       ++s;
                        continue;
                }
-               // part of a wide character or invalid character, we ignore that one
-               if (bits == 0)
-                       --len;
-               ++s;
+
+               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(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
 }