View Single Post
08/09/15, 09:30 AM   #1
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
Exclamation [outdated] Bug in string.gsub

lua5.1 / lua5.2 console (from ubuntu packages):
Lua Code:
  1. function df(...) print(string.format(...)) end
  2. df("%q %d", string.gsub("une rune de puissance", "^(%l+) ", {une="<une>", de="<de>"}))
output:
Code:
"<une>rune de puissance"	1
This is correct output. The pattern "^(%l+) " is supposed to match a lower-case word and a space at the start of the string, and if that word is a key in the table ("une" or "de"), replace it with the corresponding value from the table. There's one such word, and the function makes 1 replacement.

lua5.1* in ESO:
Lua Code:
  1. /script df("%q %d", string.gsub("une rune de puissance", "^(%l+) ", {une="<une>", de="<de>"}))
output:
Code:
"<une>rune <de>puissance"	3
This is incorrect. First, the function replaces "de" that is not at the start of the string. Second, it says it made 3 replacements.

Edit: minor correction. The second value returned from gsub is the number of matches, not the number of replacements (words that match but are not in the table are not replaced). Of course 3 is still wrong, as there should be only 1 match.

Last edited by merlight : 08/10/15 at 10:34 AM.