Thread Tools Display Modes
09-18-15, 02:49 PM   #1
Miiru
A Flamescale Wyrmkin
 
Miiru's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2008
Posts: 138
string.sub acting weird!

I created a char named Drunkensteín (notice the í).

print (string.sub(UnitName("player"), 1, 12)) returns Drunkensteí

print (string.sub(UnitName("player"), 1, 11)) returns Drunkenste?

print (string.sub(UnitName("player"), 1, 10)) returns Drunkenste

Why? :S (No addons enabled, no font swapped)
__________________
◘◘ Author of MiirGui Texture Pack - [Core] [Blue] [Grey] ◘◘

Last edited by Miiru : 09-18-15 at 02:52 PM.
  Reply With Quote
09-18-15, 05:58 PM   #2
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 822
http://wowprogramming.com/snippets/U...re_stringsub_7
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
09-18-15, 07:28 PM   #3
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
Explaining the situation:

Text from the game is encoded in UTF-8, which is backwards compatible with standard ASCII text. Special characters not supported in standard ASCII are encoded with multiple bytes residing in the binary code space above 127. string.sub() works directly on the bytes within the string, not the characters. This means that a special Unicode chars will show errors if you cut off part of the encoding.

In your example, í takes up 2 bytes in the string. The middle line cuts off the last byte representing this character and produces the error shown.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 09-18-15 at 07:32 PM.
  Reply With Quote
09-19-15, 12:47 AM   #4
Miiru
A Flamescale Wyrmkin
 
Miiru's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2008
Posts: 138
Thank you very much for that link and the explanation! I was already guessing that the problem was related to some encoding stuff.
__________________
◘◘ Author of MiirGui Texture Pack - [Core] [Blue] [Grey] ◘◘
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » string.sub acting weird!


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off