Recent

Author Topic: [SOLVED] Any Idea why TSynEditSearch does not work with TStringList ?  (Read 3652 times)

guest58172

  • Guest
Run this in a simple program (add the SynEdit package as requirement)

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses classes, SynEditSearch, SynEditTypes;
  4.  
  5. procedure test;
  6. var
  7.   search: TSynEditSearch;
  8.   str: TStringList;
  9.   start, stop: TPoint;
  10.   startf, stopf: TPoint;
  11.   res: array of TPoint = nil;
  12. begin
  13.   str := TStringList.Create;
  14.   str.AddStrings(['a','z','e','r','t','y','u','i','o','p','q','s','d','f']);
  15.   search := TSynEditSearch.Create;
  16.   try
  17.     search.Pattern:= 'whatever';
  18.     start := Point(1,1);
  19.     stop := Point(high(integer), str.Count);
  20.     while search.FindNextOne(str, start, stop, startf, stopf) do
  21.     begin
  22.       setLength(res, length(res) + 1);
  23.       res[high(res)].X := startf.X;
  24.       res[high(res)].Y := startf.Y;
  25.       start := stopf;
  26.     end;
  27.   finally
  28.     search.free;
  29.   end;
  30. end;
  31.  
  32. begin
  33.   test;
  34. end.

bug in FindNextOne, related the the count of line in the TStrings.

I naively thought it would work since the function

Code: Pascal  [Select][+][-]
  1. function FindNextOne(Lines: TStrings; StartPos, EndPos: TPoint;
  2.                          out FoundStartPos, FoundEndPos: TPoint; ASupportUnicodeCase: Boolean=False): boolean;

accept all TStrings sub classes...

guest58172

  • Guest
Re: Any Idea why TSynEditSearch does not work with TStringList ?
« Reply #1 on: January 27, 2016, 10:25:15 am »
There is a small type safety issue here because the function takes any TStrings descendant but actually it'll only work with TSynEditStringList.

I'd propose a patch but the problem is that TSynEdit.Lines itself returns TStrings, this is messy...so this could break some 3rd party code.

Perhaps that a cast and a TException could prevent the error, even if not not good for the performance.
An assertion maybe, so that users could detect the problem when testing.

What do you think Martin fr ?

Thaddy

  • Hero Member
  • *****
  • Posts: 19128
  • Glad to be alive.
Re: Any Idea why TSynEditSearch does not work with TStringList ?
« Reply #2 on: January 27, 2016, 11:24:57 am »
There is a small type safety issue here because the function takes any TStrings descendant but actually it'll only work with TSynEditStringList.

If that's the case that is a bug, a major design flaw. If it advertizes TStrings it should accept Tstrings. If it doesn't that should be fixed.
objects are fine constructs. You can even initialize them with constructors.

 

TinyPortal © 2005-2018