[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: A little bit of golf
- From: Andrew Starks <andrew.starks@...>
- Date: Wed, 8 Apr 2015 06:12:14 -0500
On Tuesday, April 7, 2015, Rena <hyperhacker@gmail.com> wrote:
On Tue, Apr 7, 2015 at 11:06 PM, Andrew Starks <andrew.starks@trms.com> wrote:
>
>
> On Tuesday, April 7, 2015, Sean Conner <sean@conman.org> wrote:
>>
>> It was thus said that the Great Daurnimator once stated:
>> > On 8 April 2015 at 10:12, Hisham <h@hisham.hm> wrote:
>> > > Ah! NO LPEG ALLOWED! :)
>> >
>> > Why not?
>>
>> Way too easy.
>>
>> lpeg = require "lpeg"
>> id = lpeg.R("AZ","az","09","__")^1 / variables
>> expand_variables = lpeg.Cs((
>> (lpeg.P"${") / "" * id * (lpeg.P"}"/"")
>> + (lpeg.P"$") / "" * id
>> + lpeg.C(lpeg.P(1))
>> )^0)
>>
>> -spc (Could be made even smaller ... )
>>
>>
>
> Right. Too easy and too readable. :)
>
> -Andrew
That's your idea of readable? Yikes.
Maybe not shortest, but somewhat clear solution:
local vars = setmetatable({foo="bar"}, {
__index = function(self, k)
return type(k) == 'string' and rawget(self, k:match('{?([%w_]+)}?'))
end,
})
function expand_variables(str)
return str:gsub('%$(%S+)', vars)
end
--
Sent from my Game Boy.
Something about PCRE/Lua Patterns makes me want to remind my mom that I'm still using the computer and to please get off of the phone before I loose my PPP connection.
-Andrew