larskanis/fxruby

findText() in FXtext

Closed this issue · 4 comments

findText() seems to only find the first instance of the search text.

eg: first, last = @txt.findText("def", :start => pos)
Text: "test text def end def end def"
In this case the array first and last have only one element.

Can you show what first and last values are you expecting and what values do they actually acquire?

At first sight, I think that you're doing things wrong there, you have 3 "def", so your last variable may not acquire the last "def" result if you start from pos = 0.

According to the documentation the first array is an array of the start positions of each instance of the search text and the last array holds the end position + 1 of each instance. Thus the array should hold something like the following:

test text def end def end def

First[1] is 10 last[1] is: 13
First[2] is 18 last[2] is: 21
First[3] is 26 last[3] is: 29

This is basically the output I get for my self-coded search function through this text using findText(), but calling it in a loop starting from the end of the previous instance found until it wraps around.

Yes, I can confirm this is bugged. I've tested the behavior on my machine.

ruby 2.4.4p296 (2018-03-28 revision 63013) [i386-mingw32]
FXRuby version 1.6.39 x86-mingw32
Windows 10 x64 Enterprise

Sorry for the late response! The description of the method was not very clear, but the methods works as intended. It always returns the first match only. I improved the related documentation in commit ecce29d.

To find all occurences of a searchtext you have to use a loop like the following:

    @text.text = "searchtext searchtext..."
    e = Enumerator.new do |y|
      pos = 0
      while a=@text.findText("searchtext", pos, SEARCH_FORWARD|SEARCH_EXACT)
        y << [a[0][0], a[1][0]]
        pos = a[1][0]
      end
    end
    p e.to_a  # => [[0,10], [11, 21]]