Quantcast
Channel: Rainmeter Forums
Viewing all articles
Browse latest Browse all 1536

Help: Rainmeter Skins • Re: How to escape meta characters when passing string from Lua to Rainmeter?

$
0
0
Tested but unfortunately same result.
Took a little longer than expected, but this works for me:

Code:

function Initialize()  tab = {some = 'Here is (something'}endfunction Esc(str)  --local esc = string.format('%q', str)  local esc = str:gsub('([%^%$%(%)%%%.%[%]%*%+%-%?])', '%%%1')  return escendfunction Output(key)  SKIN:Bang('!SetOption', 'Text', 'Text', tab[key])  SKIN:Bang('!Update')endfunction ShowText()  print(Esc(tab.some))  SKIN:Bang('!SetOption', 'TextMeasure', 'String', tab.some)  SKIN:Bang('!Update')  local txt = SKIN:GetMeasure('TextMeasure'):GetStringValue()  for line in txt:gmatch('[^\r\n]+') do    for key, val in pairs(tab) do      print(key, val, line)      if line:match(Esc(val)) then print('matched'); Output(key) else print('not matched') end    end  end  return 0end
I let the string.format('%q', ...) alternative there since it looks much simpler, but although it does escape "almost" everything as it should and advertised, it doesn't produce the expected result even if I use Esc(line) in the if statement.

What I initially recommended works though, and as I believe it should happen, only the literal pattern (i.e. val or tab[some]) needs escaping, nothing else. There is a catch though - I tried escaping stuff like \n or \s after in the main string, and except literally using \\n and \\s there or declaring it as tab = {some = [[Here is (something]]}, I got mixed results: it either didn't work (tried local esc = str:gsub('([%^%$%(%)%%%.%[%]%*%+%-%?])', '%%%1'):gsub('\\', '\\\\')), or it partially work and it kept the \ but not the n afterwards. You can use " aka double quotes in the string without having to escape them, but not ' aka apostrophes / single quotes, though you can invert that if you want.

Statistics: Posted by Yincognito — 54 minutes ago



Viewing all articles
Browse latest Browse all 1536

Trending Articles