1

In Delphi 10.3.3, which is the most simple and fastest and most efficient way to loop through each visible Char (i.e. excluding non-printable characters such as e.g. #13) of a (multiline) TRichEdit text? I then need to get and set the color of each char according to my calculations.

I have tried this one:

function GetCharByIndex(Index: Integer): Char;
begin
  RichEdit1.SelStart := Index;
  RichEdit1.SelLength := 1;
  Result := RichEdit1.SelText[1];
end;

RichLen := RichEdit1.GetTextLen - RichEdit1.Lines.Count;
for i := 0 to RichLen - 1 do
begin
  c := GetCharByIndex(i);
  if c = #13 then CONTINUE;
  // ... do my stuff here
end;

But I am sure there must be a better way.

user1580348
  • 5,721
  • 4
  • 43
  • 105
  • Related: [Faster Rich Edit Syntax Highlighting](http://bcbjournal.org/articles/vol3/9910/Faster_rich_edit_syntax_highlighting.htm). It is written for C++Builder but everything it says applies to Delphi, too – Remy Lebeau Jan 13 '20 at 15:35
  • How did the text get into the edit box in the first place? What about formatting it while it is being input rather than afterwards - you should have more information at that point to be able to do it more intelligently. Hard to say more without understanding the larger problem... – J... Jan 13 '20 at 15:41
  • RichEdit is relatively slow in general. If you're after syntax highlighting kind of control, I would suggest for example SynEdit (or Scintilla for Delphi); those are for such purpose faster than RichEdit control. – TLama Jan 13 '20 at 20:41
  • @TLama I am creating something totally new: A way to show the differences between two texts using colors. – user1580348 Jan 13 '20 at 21:45
  • It's not a new application; we normally use such for comparing source code. Anyway, from my experience I'd not use RichEdit. Controls I've mentioned are (or at least were) relatively faster. – TLama Jan 14 '20 at 00:22
  • @user1580348 There are lots of optimized libraries for generating and viewing diffs. Reinventing the wheel can be instructive, but when you can use something that already exists it's usually well worth your while. – J... Jan 14 '20 at 01:11
  • @J... "There are lots of optimized libraries for generating and viewing diffs." - Could you name a few you consider the best? – user1580348 Jan 14 '20 at 12:56
  • @user1580348 No, because I've not had need of them. You might find [this](https://cc.embarcadero.com/item/17118) useful, however. Generally third-party component recommendations are off topic. – J... Jan 14 '20 at 13:28

1 Answers1

1
var
  i: Integer;  
  c: Char;
  cord: Integer;
...
i := -1;
for c in RichEdit1.Text do
begin
  Inc(i);
  cord := ord(c);
  if (cord = 13) then
    Dec(i);
  if (cord >= 32) and (not ((cord > 126) and (cord < 161))) then
  begin
    // do your stuff here, for example exchanging red and green colors:
    RichEdit1.SelStart := i;
    RichEdit1.SelLength := 1;
    if RichEdit1.SelAttributes.Color = clGreen then
      RichEdit1.SelAttributes.Color := clRed
    else if RichEdit1.SelAttributes.Color = clRed then
      RichEdit1.SelAttributes.Color := clGreen;
  end;
end;
user1580348
  • 5,721
  • 4
  • 43
  • 105