Recent

Author Topic: [SOLVED] Help with unicode...  (Read 535 times)

Espectr0

  • Full Member
  • ***
  • Posts: 217
[SOLVED] Help with unicode...
« on: August 19, 2022, 04:28:05 pm »
Hola!
I have a problem, the following code is part of Subtitle Workshop in which to add a Tag string in a TMemo, apparently it works correctly but with Chinese text for example this add a question mark and duplicate some text after that, which is wrong ?


Code: Pascal  [Select][+][-]
  1. procedure SetTextTag(const Tag: String);
  2.   var
  3.     Memo   : TMemo;
  4.     i      : Integer;
  5.     s1, s2 : String;
  6.   begin
  7.     Memo := GetMemoFocused;
  8.     if Memo = NIL then Exit;
  9.  
  10.     i  := Memo.SelStart;
  11.     s1 := Copy(Memo.Text, 1, Memo.SelStart);
  12.     if Memo.SelText <> '' then
  13.       s2 := Copy(Memo.Text, Memo.SelStart+Memo.SelLength+1, Length(Memo.Text)-(Memo.SelStart+Memo.SelLength))
  14.     else
  15.       s2 := Copy(Memo.Text, Memo.SelStart+1, Length(Memo.Text)-Memo.SelStart);
  16.     Memo.Text := Format('%s{\%s1}%s{\%s0}%s', [s1, Tag, Memo.SelText, Tag, s2]);
  17.   end;                    

Thanks in advance!
« Last Edit: August 20, 2022, 09:05:07 pm by Espectr0 »

dje

  • Full Member
  • ***
  • Posts: 134
Re: help with unicode
« Reply #1 on: August 19, 2022, 05:56:27 pm »
Seems like a lot of code for a SelText replacement. Assuming I understand, You should only need:

Code: Pascal  [Select][+][-]
  1. Memo.SelText := Format('{\%s1}%s{\%s0}', [Tag, Memo.SelText, Tag]);

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9794
  • Debugger - SynEdit - and more
    • wiki
Re: help with unicode
« Reply #2 on: August 19, 2022, 06:13:30 pm »
Looking at this code from the LCL:
Code: Pascal  [Select][+][-]
  1. function TCustomEdit.GetSelText : string;
  2. begin
  3.   Result := UTF8Copy(Text, SelStart + 1, SelLength)
  4. end;
  5.  

SelStart/SelLength seem to be the char position.

Normal "copy" takes the byte pos.


Espectr0

  • Full Member
  • ***
  • Posts: 217
[SOLVED] help with unicode
« Reply #3 on: August 20, 2022, 09:04:44 pm »
Thanks @Martin_fr and @dje!
I don't know why I assumed SelText was read-only :S

Regards

 

TinyPortal © 2005-2018