Recent

Author Topic: Search and Replace Unicode  (Read 1801 times)

rick2691

  • Sr. Member
  • ****
  • Posts: 374
Search and Replace Unicode
« on: March 24, 2017, 07:43:42 pm »
This is an example for search and replace...

Code: Pascal  [Select]
  1. // globally modifies unicode text within an active file. Colud be edited to work with a selection range.
  2. function SearchReplace(MemoString: TRichMemo;    
  3.                        SearchText, ReplaceText: widestring): Boolean;
  4. var startpos, startrun: longint;
  5.     opt: TSearchOptions;
  6. begin
  7.   with MemoString do    // ** needs editing for a selection range **
  8.        begin
  9.          opt:= [];  // no options set
  10.          SelStart:= 0;
  11.          Lines.BeginUpdate; // suspends visual update
  12.          while MemoString.Search(SearchText, SelStart, GetTextLen - SelStart, opt, startpos, startrun) do
  13.                begin
  14.                  SelStart:= startpos;
  15.                  SelLength:= startrun;
  16.                  SelText:= ReplaceText;
  17.                  SelStart:= startpos + 1;
  18.                end;
  19.          Lines.EndUpdate; // installs visual update
  20.        end;
  21. end;
  22.  
  23. //The following is an application of SearchReplace. It globally exchanges Syriac Western vowels with Eastern vowels.
  24. //There are additional codes that have to be developed to exhaustively manage the replacements ... but this is a starter.
  25. //Done is assigned for future needs but is not currently implemented to do anything.
  26.  
  27. var
  28.   fs: TFileStream;
  29.   Done: boolean;
  30. begin
  31. (*
  32. .... do stuff ... like DiskName:= OpenDialog1.Filename; {DiskName is global string} {PageMemo is RichMemo}
  33. *)
  34. try fs:= TFileStream.Create(Utf8ToAnsi(DiskName), fmOpenRead or fmShareDenyNone);
  35.      PageMemo.LoadRichText(fs);
  36.  
  37.      Done:= SearchReplace(PageMemo, UnicodeToUTF8(1840), UnicodeToUTF8(1842)); // ptx (ah) <dot above with dot below>
  38.      Done:= SearchReplace(PageMemo, UnicodeToUTF8(1843), UnicodeToUTF8(1845)); // zqp (au) <skewed double-dots above>
  39.      Done:= SearchReplace(PageMemo, UnicodeToUTF8(1846), UnicodeToUTF8(1848)); // rbs (eh) <two flat dots below>
  40.      Done:= SearchReplace(PageMemo, UnicodeToUTF8(1853), UnicodeToUTF8(1855)); // esa (oo) <single dot above>
  41.      Done:= SearchReplace(PageMemo, UnicodeToUTF8(1850), UnicodeToUTF8(1849)); // xbs (ih, no yud) <skewed double-dots below>
  42.      Done:= SearchReplace(PageMemo, UnicodeToUTF8(1864), UnicodeToUTF8(1863)); // silent oblique above
  43.  
  44.      Done:= SearchReplace(PageMemo, UnicodeToUTF8(1849)+UnicodeToUTF8(1821),  // zlm (ih & yud) <single dot below yud>
  45.                                     UnicodeToUTF8(1821)+UnicodeToUTF8(1852));
  46.  
  47.      PageMemo.Hint:= DiskName; // OpenDialog1.Filename;
  48.      finally
  49.      fs.Free;
  50.      end;
  51. end;
  52.  

Rick
« Last Edit: March 24, 2017, 07:45:59 pm by rick2691 »
Windows 10, LAZ 1.6.4, FPC 3.0.2, SVN 54278, i386-win32-win32/win64, forms use windows unit