Thread Tools Display Modes
11-18-10, 11:56 AM   #1
Dawn
A Molten Giant
 
Dawn's Avatar
AddOn Author - Click to view addons
Join Date: May 2006
Posts: 918
string length issue

I'm using tags for my oUF layout that abbreviate long names.

Code:
	local oldName = UnitName(unit)
	local newName = (string.len(oldName) > 14) and string.gsub(oldName, "%s?(.[\128-\191]*)%S+%s", "%1. ") or oldName
	
	if not UnitIsPlayer(unit) then
		return utf8sub(newName, 14, false)
	else
		return oldName
	end

Everything works fine, but on level up the following error can occur. It only seems to happen when the unit name contains letters like "Æ", "Ø" and the like.

(Line 47 is highlighted red, above.)
Code:
Message: Interface\AddOns\oUF_Slim\Slim_Tags.lua:47: bad argument #1 to 'len' (string expected, got nil)
Time: 11/18/10 18:43:03
Count: 1
Stack: [C]: in function `len'
Interface\AddOns\oUF_Slim\Slim_Tags.lua:47: in function `func'
Interface\AddOns\oUF\elements\tags.lua:595: in function `UpdateTag'
Interface\AddOns\oUF\elements\tags.lua:375: in function <Interface\AddOns\oUF\elements\tags.lua:370>

Locals: (*temporary) = nil
(*temporary) = "string expected, got nil"
What I do not get is why it happens on level up. Normally, you can switch through units with no matter what characters and the name always abbreviates/updates correctly.

Any idea how to fix it or maybe a workaround?
__________________
Rock: "We're sub-standard DPS. Nerf Paper, Scissors are fine."
Paper: "OMG, WTF, Scissors!"
Scissors: "Rock is OP and Paper are QQers. We need PvP buffs."

"neeh the game wont be remembered as the game who made blizz the most money, it will be remembered as the game who had the most QQ'ers that just couldnt quit the game for some reason..."

  Reply With Quote
11-18-10, 12:32 PM   #2
yj589794
A Rage Talon Dragon Guard
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 314
hmmm, seems a bit odd.

What events is your tag registered to update from?

It may be that one of the events is being triggered without a parameter in the case that the player levels up.
  Reply With Quote
11-18-10, 12:41 PM   #3
Ailae
A Rage Talon Dragon Guard
 
Ailae's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 318
I suppose you could check that oldName isn't nil before trying to something with it. That'll probably stop the error, but doesn't really solve it. Can you "force" the event and add some printouts maybe?
__________________
Oh, the simulated horror!
  Reply With Quote
11-18-10, 03:21 PM   #4
Dawn
A Molten Giant
 
Dawn's Avatar
AddOn Author - Click to view addons
Join Date: May 2006
Posts: 918
Originally Posted by yj589794 View Post
What events is your tag registered to update from?
It's simply "UNIT_NAME_UPDATE"


Originally Posted by Ailae View Post
I suppose you could check that oldName isn't nil before trying to something with it. That'll probably stop the error, but doesn't really solve it. Can you "force" the event and add some printouts maybe?
How do I check if it isn't nil? What exactly do you mean with "force the event"? I mean switching targets = "UNIT_NAME_UPDATE", but like I said that works.


Btw, this is what I use for utf8sub, pretty straight forward, maybe except for the additional length check.

Code:
local utf8sub = function(string, i, dots)
	local bytes = string:len()
	if (bytes <= i) then
		return string
	else
		local len, pos = 0, 1
		while(pos <= bytes) do
			len = len + 1
			local c = string:byte(pos)
			if c > 240 then
				pos = pos + 4
			elseif c > 225 then
				pos = pos + 3
			elseif c > 192 then
				pos = pos + 2
			else
				pos = pos + 1
			end
			if (len == i) then break end
		end

		if (len == i and pos <= bytes) then
			return string:sub(1, pos - 1)..(dots and "..." or "")
		else
			return string
		end
	end
end

Edit:
"I do believe this is related to UTF8 and those special characters messing up the abbreviation."

I just tripple tested it and it doesn't matter if the player or player pet name contains special characters. It just happens.
__________________
Rock: "We're sub-standard DPS. Nerf Paper, Scissors are fine."
Paper: "OMG, WTF, Scissors!"
Scissors: "Rock is OP and Paper are QQers. We need PvP buffs."

"neeh the game wont be remembered as the game who made blizz the most money, it will be remembered as the game who had the most QQ'ers that just couldnt quit the game for some reason..."


Last edited by Dawn : 11-18-10 at 03:29 PM. Reason: tested and proofed wrong
  Reply With Quote
11-18-10, 04:31 PM   #5
Ailae
A Rage Talon Dragon Guard
 
Ailae's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2007
Posts: 318
To check if it's not nil you can just do
lua Code:
  1. if oldName then dostuff() end

What I meant by forcing the event was if you could tell oUF that the event for when a player levels happened when it actually didn't, since that's when the issue seems to crop up.
__________________
Oh, the simulated horror!
  Reply With Quote
11-18-10, 04:38 PM   #6
Xubera
A Cobalt Mageweaver
 
Xubera's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 207
to force the event, you just mimic it

so like if your eventhandler is called event handler, you just manually run the function... i.e

Code:
function eventHandler(frame, event, ...)
  --do Stuff
end

and then in the in-game chat box, just type

/run eventHandler(myFrame, "UNIT_NAME_UPDATE", arg1)

where arg1 is what normally gets passed with the event. also, to call the function it needs to be global, so you can take away the local, and when you fix the issue, just add local back in its place
__________________
Chat Consolidate is the solution to any out of control trade chat. Ignore lines, throttle chat, consolidate posts!Follow the link to find out how!

▲ ▲ WoWInterface wont let me triforce >.>
  Reply With Quote
11-18-10, 07:16 PM   #7
Dawn
A Molten Giant
 
Dawn's Avatar
AddOn Author - Click to view addons
Join Date: May 2006
Posts: 918
Figured it out. I was using

Code:
self:Tag(self.Name, '[afkdnd][difficulty][smartlevel]  [raidcolor][abbrevname]')
which seemed to confuse the string:len() check. And that's also where the damn level came from, which obviously had nothing to do with the utf8sub function or the abbreviation tag itself.

Worked like a charm again, after splitting it up into 2 separate font strings. D'OH!

...
__________________
Rock: "We're sub-standard DPS. Nerf Paper, Scissors are fine."
Paper: "OMG, WTF, Scissors!"
Scissors: "Rock is OP and Paper are QQers. We need PvP buffs."

"neeh the game wont be remembered as the game who made blizz the most money, it will be remembered as the game who had the most QQ'ers that just couldnt quit the game for some reason..."

  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » string length issue


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