Quantcast
WoWInterface - View Single Post - Code optimization and issue with hyperlinks in chat
View Single Post
01-07-19, 11:50 AM   #15
jlam
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 29
Your function does this when it is invoked:
Code:
local words, coloredMessage = {strsplit(' ', chatMessage)}, ''
for i = 1, #words do
    ...
end
You are not using words in any way further on in the code other than to iterate through that array, so you can instead do this:
Code:
for word in gmatch(chatMessage, '%S') do
    ...
end
This more concisely iterates through the chatMessage string and selecting "words" composed of non-whitespace characters.

You are also building up a string coloredMessage by concatenating word by word, which is considered very inefficient in Lua. The usual practice is to place all of the words into an array and then call table.concat to form the complete string. You can do this as:
Code:
local modHexColorNames
do
    local coloredMessage = {} -- static local table
    modHexColorNames = function(chatMessage, modChatColor)
        wipe(coloredMessage)  -- empty the table before re-using.
        for word in gmatch(chatMessage, '%S') do
            local w
            -- do stuff to set w to the word for the colored message
            ...
            table.insert(coloredMessage, w)
        end
        return table.concat(coloredMessage, ' ')
    end
end
table.concat returns an empty string if the table is empty, so you will always get at least an empty string as the return value.
  Reply With Quote