Use-cases for string.gsub(str, "().", "%1")

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Use-cases for string.gsub(str, "().", "%1")

Soni "They/Them" L.
Are there any use-cases for string.gsub(str, "().", "%1")?

If not, can it please become an error? It's just... weird... unexpected...

(I'm talking about the very weird number to string coercion which
happens there)

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: Use-cases for string.gsub(str, "().", "%1")

Matthew Wild
On 19 August 2015 at 21:18, Soni L. <[hidden email]> wrote:
> Are there any use-cases for string.gsub(str, "().", "%1")?
>
> If not, can it please become an error? It's just... weird... unexpected...
>
> (I'm talking about the very weird number to string coercion which happens
> there)

I wanted to triumphantly say there was a use-case, in converting text
to netstrings. However unfortunately the result is always off-by-one:

  ("hello world!"):gsub("^(.*)()$", "%2:%1,") -- 13:hello world!,

See https://en.wikipedia.org/wiki/Netstring

However in any case I don't think the fact that position captures can
be used directly as replacements is really an issue, it's pretty much
exactly what I would expect to happen.

Regards,
Matthew

Reply | Threaded
Open this post in threaded view
|

Re: Use-cases for string.gsub(str, "().", "%1")

Soni "They/Them" L.


On 20/08/15 11:07 AM, Matthew Wild wrote:

> On 19 August 2015 at 21:18, Soni L. <[hidden email]> wrote:
>> Are there any use-cases for string.gsub(str, "().", "%1")?
>>
>> If not, can it please become an error? It's just... weird... unexpected...
>>
>> (I'm talking about the very weird number to string coercion which happens
>> there)
> I wanted to triumphantly say there was a use-case, in converting text
> to netstrings. However unfortunately the result is always off-by-one:
>
>    ("hello world!"):gsub("^(.*)()$", "%2:%1,") -- 13:hello world!,
>
> See https://en.wikipedia.org/wiki/Netstring
>
> However in any case I don't think the fact that position captures can
> be used directly as replacements is really an issue, it's pretty much
> exactly what I would expect to happen.
>
> Regards,
> Matthew
>
This is Lua.

("hello world!"):gsub("^(.-)()(.?)$", "%2:%1%3,") -- 13:hello world!,

(note: doesn't work for empty string)

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: Use-cases for string.gsub(str, "().", "%1")

Fidelis Assis-2
Can also be used for expansion of positional notation

s = '1010' 
print((string.gsub(s, "()(.)", function (p, d) return ' + ' .. d .. '*2^' .. #s-p end))

+ 1*2^3 + 0*2^2 + 1*2^1 + 0*2^0

and base conversion.

-- Fidelis

Reply | Threaded
Open this post in threaded view
|

Re: Use-cases for string.gsub(str, "().", "%1")

Soni "They/Them" L.


On 20/08/15 03:54 PM, Fidelis Assis wrote:

> Can also be used for expansion of positional notation
>
> s = '1010'
> print((string.gsub(s, "()(.)", function (p, d) return ' + ' .. d ..
> '*2^' .. #s-p end))
>
> + 1*2^3 + 0*2^2 + 1*2^1 + 0*2^0
>
> and base conversion.
>
> -- Fidelis
>
Not the same as using it in the replacement string.

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: Use-cases for string.gsub(str, "().", "%1")

Fidelis Assis-2
2015-08-20 15:57 GMT-03:00 Soni L. <[hidden email]>:


On 20/08/15 03:54 PM, Fidelis Assis wrote:
Can also be used for expansion of positional notation

s = '1010'
print((string.gsub(s, "()(.)", function (p, d) return ' + ' .. d .. '*2^' .. #s-p end))

+ 1*2^3 + 0*2^2 + 1*2^1 + 0*2^0

and base conversion.

-- Fidelis

Not the same as using it in the replacement string.

You can do that without the function, just ugly.

print ((string.gsub(s, "()(.)", ' + %2*2^(' .. #s ..'-%1)')))
 
-- Fidelis