]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
properly check the player name length as utf-8 octet count
authorRudolf Polzer <divVerent@xonotic.org>
Thu, 9 Dec 2010 11:14:41 +0000 (12:14 +0100)
committerRudolf Polzer <divVerent@xonotic.org>
Thu, 9 Dec 2010 11:14:41 +0000 (12:14 +0100)
qcsrc/common/util.qc
qcsrc/common/util.qh
qcsrc/menu/item/inputbox.c
qcsrc/menu/xonotic/dialog_multiplayer_playersetup.c

index 44f8751733aab62e96406a890da8cee451966806..83ea8e3e5827fedbc29f9f7534aec14f8865ca80 100644 (file)
@@ -1953,3 +1953,23 @@ float vercmp(string v1, string v2)
 
        return vercmp_recursive(v1, v2);
 }
 
        return vercmp_recursive(v1, v2);
 }
+
+float u8_strsize(string s)
+{
+       float l, i, c;
+       l = 0;
+       for(i = 0; ; ++i)
+       {
+               c = str2chr(s, i);
+               if(c <= 0)
+                       break;
+               ++l;
+               if(c >= 0x80)
+                       ++l;
+               if(c >= 0x800)
+                       ++l;
+               if(c >= 0x10000)
+                       ++l;
+       }
+       return l;
+}
index cfb955e3566cb44d576ab6b09963eed1fe79c1b2..79b93d3ede3d238d5e6d8ad4eaf4f787aa331ea0 100644 (file)
@@ -247,3 +247,5 @@ vector NearestPointOnBox(entity box, vector org);
 #endif
 
 float vercmp(string v1, string v2);
 #endif
 
 float vercmp(string v1, string v2);
+
+float u8_strsize(string s);
index 2d64590306139cc97f1b860ac56c5e7a72de836a..750bc83b890453eb917fcaaac3aa804a54142727 100644 (file)
@@ -25,7 +25,7 @@ CLASS(InputBox) EXTENDS(Label)
        ATTRIB(InputBox, forbiddenCharacters, string, "")
        ATTRIB(InputBox, color, vector, '1 1 1')
        ATTRIB(InputBox, colorF, vector, '1 1 1')
        ATTRIB(InputBox, forbiddenCharacters, string, "")
        ATTRIB(InputBox, color, vector, '1 1 1')
        ATTRIB(InputBox, colorF, vector, '1 1 1')
-       ATTRIB(InputBox, maxLength, float, 255)
+       ATTRIB(InputBox, maxLength, float, 255) // if negative, it counts bytes, not chars
 ENDCLASS(InputBox)
 void InputBox_Clear_Click(entity btn, entity me);
 #endif
 ENDCLASS(InputBox)
 void InputBox_Clear_Click(entity btn, entity me);
 #endif
@@ -79,8 +79,16 @@ void InputBox_enterText(entity me, string ch)
        for(i = 0; i < strlen(ch); ++i)
                if(strstrofs(me.forbiddenCharacters, substring(ch, i, 1), 0) > -1)
                        return;
        for(i = 0; i < strlen(ch); ++i)
                if(strstrofs(me.forbiddenCharacters, substring(ch, i, 1), 0) > -1)
                        return;
-       if(strlen(ch) + strlen(me.text) > me.maxLength)
-               return;
+       if(me.maxLength > 0)
+       {
+               if(strlen(ch) + strlen(me.text) > me.maxLength)
+                       return;
+       }
+       else if(me.maxLength < 0)
+       {
+               if(u8_strsize(ch) + u8_strsize(me.text) > -me.maxLength)
+                       return;
+       }
        me.setText(me, strcat(substring(me.text, 0, me.cursorPos), ch, substring(me.text, me.cursorPos, strlen(me.text) - me.cursorPos)));
        me.cursorPos += strlen(ch);
 }
        me.setText(me, strcat(substring(me.text, 0, me.cursorPos), ch, substring(me.text, me.cursorPos, strlen(me.text) - me.cursorPos)));
        me.cursorPos += strlen(ch);
 }
index 562c097ce8f228d6738fdf6c8bc0f8cd6a02c9f1..586896171e76df85676739208b209b6a113524b3 100644 (file)
@@ -46,7 +46,7 @@ void XonoticPlayerSettingsTab_fill(entity me)
        me.TR(me);
                me.TD(me, 1, 3.0, box = makeXonoticInputBox(1, "_cl_name"));
                        box.forbiddenCharacters = "\r\n\\\"$"; // don't care, isn't getting saved
        me.TR(me);
                me.TD(me, 1, 3.0, box = makeXonoticInputBox(1, "_cl_name"));
                        box.forbiddenCharacters = "\r\n\\\"$"; // don't care, isn't getting saved
-                       box.maxLength = 63;
+                       box.maxLength = -63; // negativ means encoded length in bytes
                        label.textEntity = box;
        me.TR(me);
                me.TD(me, 5, 1, e = makeXonoticColorpicker(box));
                        label.textEntity = box;
        me.TR(me);
                me.TD(me, 5, 1, e = makeXonoticColorpicker(box));