luautf8 icon indicating copy to clipboard operation
luautf8 copied to clipboard

Efficient use of find/sub

Open johnd0e opened this issue 4 years ago • 0 comments

Consider such example:

  repeat
    pos = utf8.find(str,"\\",pos+1)
  until not pos or utf8.sub(str,1,pos) ~= utf8.sub(str2,1,pos)

I have some doubt about efficiency of this code.

  1. find in loop pos value have to be translated to byte offset, and this get even worse with every iteration as we move further from beginning. So question: is there some internal optimization for loop usage?
  2. sub after find It has to repeat exactly the same translation of pos, which already was done in find. So question: how to rewrite above example in more efficient way?

johnd0e avatar Sep 04 '19 13:09 johnd0e