[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  the lua pcre lib seems to be not usefully complete
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

the lua pcre lib seems to be not usefully complete

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Fiendish   USA  (1,641 posts)  [Biography] bio   Global Moderator
Date Fri 04 Mar 2016 01:52 AM (UTC)

Amended on Fri 04 Mar 2016 02:00 AM (UTC) by Fiendish

Message
There are only 3 exposed functions in MUSHclient's PCRE object (re:exec, re:match, re:gmatch). As far as I can tell, it's impossible to implement something like gsub using them (lrexlib has this). Adding either re:gsub or something that enables the possibility of making a re:gsub would be very useful.
Confirm/Deny?

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Fri 04 Mar 2016 02:44 AM (UTC)
Message
It doesn't look like that library has been updated since 2004, so you are probably correct.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Fiendish   USA  (1,641 posts)  [Biography] bio   Global Moderator
Date Reply #2 on Fri 04 Mar 2016 03:25 PM (UTC)

Amended on Fri 04 Mar 2016 03:26 PM (UTC) by Fiendish

Message
Well, I was wrong. It's not impossible!

Can this be added somehow?

-- like string.gsub but using PCRE
rex.gsub = function(str, re, rep)
   if type(re) == "string" then
      re = rex.new(re)
   end

   local output = {}
      
   local startfrom = 1
   local s, e, t = re:exec(str, startfrom)
   while s ~= nil do
      local captures = {}
      for i=1,#t,2 do
         table.insert(captures, str:sub(t[i], t[i+1]))
      end
      
      local filled_rep = rep:gsub("%%(%d+)", function(index) return captures[tonumber(index)] or "" end)
      table.insert(output, str:sub(startfrom, s-1))
      table.insert(output, filled_rep)

      startfrom = e+1
      s, e, t = re:exec(str, startfrom)
   end
   table.insert(output, str:sub(startfrom))

   return table.concat(output)
end

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Fiendish   USA  (1,641 posts)  [Biography] bio   Global Moderator
Date Reply #3 on Fri 04 Mar 2016 09:42 PM (UTC)

Amended on Fri 04 Mar 2016 09:47 PM (UTC) by Fiendish

Message
Actually, my tests seem to show that this is about 25% faster (aardwolf, so luajit 2.1) using
\\\time = utils.timer(); for i=1,100000 do rex.gsub("Nick goes East Man goes West HE GOES HOME", "(\\w+) goes (\\w+)", "%1 de ho") end print(utils.timer()-time)



rex.gsub = function(str, re, rep)
   if type(re) == "string" then
      re = rex.new(re)
   end
   output = ""
      
   local startfrom = 1
   local s, e, t = re:exec(str, startfrom)
   while s ~= nil do
      local filled_rep = rep:gsub("%%(%d+)", 
         function(index) 
            local i = tonumber(index)*2
            return str:sub(t[i-1], t[i]) or ""
         end)
      output = output..str:sub(startfrom, s-1)..filled_rep
      
      startfrom = e+1
      s, e, t = re:exec(str, startfrom)
   end

   return output..str:sub(startfrom)
end


I guess that's what I get for premature optimization.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Nick Gammon   Australia  (21,324 posts)  [Biography] bio   Forum Administrator
Date Reply #4 on Sat 05 Mar 2016 05:27 AM (UTC)
Message
"This" is faster than what? You posted two lots of code. (I think).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Fiendish   USA  (1,641 posts)  [Biography] bio   Global Moderator
Date Reply #5 on Sat 05 Mar 2016 12:01 PM (UTC)
Message
Nick Gammon said:

"This" is faster than what? You posted two lots of code. (I think).


The code in #3 is faster for me than the code in #2 using the simple test in post #3.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


2,467 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]