Searches; Boyer-Moore-Horspool search algorithm. lazarus / 31-LCL ---------------------------------------------------------------------- arneolav · 2014-01-04 13:26 Many years ago I got this routin (included), and I'v been using it to search with "wildchars" to find words in Db files and textfiles in Delphi. This is very useful in a search for words in a large text or file, when you don't know exactly how word is written. In Lazarus I'v got a problem with characers like "í"  (apostroph over i) Example: Search like this "ten?s",  does find "tenis", but not the word "tenís".   (written "i" with an apostroph); I think this has something todo with the unicode. I'v been looking into the unit but i'm not able to correct it. Another problem is GetFileSize(filehandle), this need windows as  FileUtil don't take "handle", only GetFileSize(filename) Or.. is there any other way to do "wildchar"  search (unkwnown char's inside word) ? The code includs an example of how to use. Code: [Select]unit Searches; {$mode objfpc}{$H+} // Boyer-Moore-Horspool search algorithm.                    . interface uses   windows,   // FileUtil,   Need filename, does not take Handle   sysutils, classes; type   TBaseSearch = class(TComponent)   private      fPos            : pchar;      fEnd            : pchar;      fPattern        : string;      fPatLen         : integer;      fPatInitialized : boolean;      fCaseSensitive  : boolean;      JumpShift       : integer;      Shift           : array[#0..#255] of integer;      CaseBlindTable  : array[#0..#255] of char;      procedure InitPattern;      procedure MakeCaseBlindTable;      procedure SetCaseSensitive(CaseSens: boolean);      procedure SetPattern(const Pattern: string);      function  FindCaseSensitive: integer;      function  FindCaseInsensitive: integer;   protected      fStart          : pchar;      fDataLength     : integer;      procedure ClearData;      procedure SetData(Data: pchar; DataLength: integer); virtual;   public      constructor Create(aowner: tcomponent); override;      destructor  Destroy; override;      //The following Find functions return the 0 based offset of Pattern      //else POSITION_EOF (-1) if the Pattern is not found  ...      function  FindFirst: integer;      function  FindNext: integer;      function  FindFrom(StartPos: integer): integer;   published      property CaseSensitive: boolean read fCaseSensitive write SetCaseSensitive;      property  Pattern: string read fPattern write SetPattern;   end;   TSearch = class(TBaseSearch)   public     //Changes visibility of base SetData() method to public ...     //Note: TSearch does NOT own the data. To avoid the overhead of     //copying it, it just gets a pointer to it.     procedure SetData(Data: pchar; DataLength: integer); override;   end;   TFileSearch = class(TBaseSearch)   private     fFilename: string;     procedure SetFilename(const Filename: string);     procedure Closefile;   public     destructor Destroy; override;   published     //Assigning 'Filename' creates a memory map of the named file.     //This memory mapping will be closed when either the Filename property is     //assigned to '' or the FileSearch object is destroyed.     property Filename: string read fFilename write SetFilename;   end; procedure Register; const   POSITION_EOF = -1; implementation procedure Register; begin   RegisterComponents('Samples', [TSearch, TFileSearch]); end; //-------------------------------------------------------------------- ---------- // TBaseSearch methods ... //-------------------------------------------------------------------- ---------- procedure TBaseSearch.MakeCaseBlindTable; var   i: char; begin   for i:= #0 to #255 do      CaseBlindTable[i]:= ansilowercase(i)[1]; end; //-------------------------------------------------------------------- ---------- constructor TBaseSearch.Create(AOwner: TComponent); begin   inherited Create(AOwner);   fStart := nil;   fPattern := '';   fPatLen := 0;   MakeCaseBlindTable;   fCaseSensitive := false;      //Default to case insensitive searches.   fPatInitialized := false; end; //-------------------------------------------------------------------- ---------- destructor TBaseSearch.Destroy; begin   ClearData;   inherited Destroy; end; //-------------------------------------------------------------------- ---------- procedure TBaseSearch.ClearData; begin   fStart := nil;   fPos := nil;   fEnd := nil;   fDataLength := 0; end; //-------------------------------------------------------------------- ---------- procedure TBaseSearch.SetPattern(const Pattern: string); begin   if fPattern = Pattern then exit;   fPattern := Pattern;   fPatLen := length(Pattern);   fPatInitialized := false; end; //-------------------------------------------------------------------- ---------- procedure TBaseSearch.SetData(Data: pchar; DataLength: integer); begin   ClearData;   if (Data = nil) or (DataLength < 1) then exit;   fStart := Data;   fDataLength := DataLength;   fEnd := fStart + fDataLength; end; //-------------------------------------------------------------------- ---------- procedure TBaseSearch.SetCaseSensitive(CaseSens: boolean); begin   if fCaseSensitive = CaseSens then exit;   fCaseSensitive := CaseSens;   fPatInitialized := false; end; //-------------------------------------------------------------------- ---------- procedure TBaseSearch.InitPattern; var   j: integer;   i: char; begin   if fPatLen = 0 then exit;   for i := #0 to #255 do Shift[i]:= fPatLen;   if fCaseSensitive then   begin     for j := 1 to fPatLen-1 do       Shift[fPattern[j]]:= fPatLen - j;     JumpShift := Shift[fPattern[fPatLen]];     Shift[fPattern[fPatLen]] := 0;   end else   begin     for j := 1 to fPatLen-1 do       Shift[CaseBlindTable[fPattern[j]]]:= fPatLen - j;     JumpShift := Shift[CaseBlindTable[fPattern[fPatLen]]];     Shift[CaseBlindTable[fPattern[fPatLen]]] := 0;   end;   fPatInitialized := true; end; //-------------------------------------------------------------------- ---------- function TBaseSearch.FindFirst: integer; begin   fPos := fStart+fPatLen-1;   result := FindNext; end; //-------------------------------------------------------------------- ---------- function TBaseSearch.FindFrom(StartPos: integer): integer; begin   if StartPos < fPatLen-1 then  //ie: StartPos must never be less than fPatLen-1     fPos := fStart+fPatLen-1 else      fPos := fStart+StartPos;   result := FindNext; end; //-------------------------------------------------------------------- ---------- function TBaseSearch.FindNext: integer; begin   if not fPatInitialized then InitPattern;   if (fPatLen = 0) or (fPatLen > fDataLength) or (fPos >= fEnd) then //2003.05.07   begin      fPos := fEnd;      result := POSITION_EOF;      exit;   end;   if fCaseSensitive then     result := FindCaseSensitive else     result := FindCaseInsensitive; end; //-------------------------------------------------------------------- ---------- function TBaseSearch.FindCaseSensitive: integer; var   i: integer;   j: pchar; begin   result:= POSITION_EOF;   while fPos < fEnd do   begin     i := Shift[fPos^];        //test last character first     if i <> 0 then            //last char does not match       inc(fPos,i)     else     begin                     //last char matches at least       i := 1;       j := fPos - fPatLen;       while (i < fPatLen) and (fPattern[i] = (j+i)^) do inc(i);       if (i = fPatLen) then       begin          result:= fPos-fStart-fPatLen+1;          inc(fPos,fPatLen);          break;               //FOUND!       end       else         inc(fPos,JumpShift);     end;   end; end; //-------------------------------------------------------------------- ---------- function TBaseSearch.FindCaseInsensitive: integer; var   i: integer;   j: pchar; begin   result:= POSITION_EOF;   while fPos < fEnd do   begin     i := Shift[CaseBlindTable[fPos^]];   //test last character first     if i <> 0 then                       //last char does not match       inc(fPos,i)     else     begin                                //last char matches at least       i := 1;       j := fPos - fPatLen;       while (i < fPatLen) and             (CaseBlindTable[fPattern[i]] = CaseBlindTable[(j+i)^]) do inc(i);       if (i = fPatLen) then       begin          result:= fPos-fStart-fPatLen+1;          inc(fPos,fPatLen);          break;                          //FOUND!       end       else         inc(fPos,JumpShift);     end;   end; end; //-------------------------------------------------------------------- ---------- // TSearch methods ... //-------------------------------------------------------------------- ---------- procedure TSearch.SetData(Data: pchar; DataLength: integer); begin   inherited; //changes visibility of base method from protected to public end; //-------------------------------------------------------------------- ---------- // TFileSearch methods ... //-------------------------------------------------------------------- ---------- destructor TFileSearch.Destroy; begin   CloseFile;   inherited Destroy; end; //-------------------------------------------------------------------- ---------- procedure TFileSearch.SetFilename(const Filename: string); var    filehandle: integer;    filemappinghandle: thandle;    size, highsize: integer; begin   if (csDesigning in ComponentState) then   begin     fFilename := Filename;     exit;   end;   CloseFile;   if (Filename = '') or not FileExists(Filename) then exit;   filehandle := sysutils.FileOpen(Filename, fmopenread or fmsharedenynone);   if filehandle = 0 then exit;        //error   size := GetFileSize(filehandle, @highsize);   if (size <= 0) or (highsize <> 0) then      //nb: files >2 gig not supported   begin      CloseHandle(filehandle);      exit;   end;   filemappinghandle := CreateFileMapping(filehandle, nil, page_readonly, 0, 0, nil);   if GetLastError = error_already_exists then filemappinghandle := 0;   if filemappinghandle <> 0 then     SetData(MapViewOfFile(filemappinghandle,file_map_read,0,0,0),size);   if fStart <> nil then fFilename := Filename;   CloseHandle(filemappinghandle);   CloseHandle(filehandle); end; //-------------------------------------------------------------------- ---------- procedure TFileSearch.CloseFile; begin    if (csDesigning in ComponentState) then exit;    if (fStart <> nil) then UnmapViewOfFile(fStart);    fFilename := '';    ClearData; end; //-------------------------------------------------------------------- ---------- (*                  Main for DEMO of Searches unit DemoMain; interface uses   Windows, Messages, SysUtils, Classes, Controls, Forms, Dialogs,   Searches,   Menus, StdCtrls; type   TForm1 = class(TForm)     MainMenu1: TMainMenu;     File1: TMenuItem;     N1: TMenuItem;     Exit1: TMenuItem;     SearchFile1: TMenuItem;     OpenDialog1: TOpenDialog;     CaseSensitive1: TMenuItem;     N3: TMenuItem;     Memo1: TMemo;     SearchMemo1: TMenuItem;     procedure FormCreate(Sender: TObject);     procedure SearchFile1Click(Sender: TObject);     procedure Exit1Click(Sender: TObject);     procedure CaseSensitive1Click(Sender: TObject);     procedure SearchMemo1Click(Sender: TObject);   private     TextSearch: TSearch;     FileSearch: TFileSearch;   public     { Public declarations }   end; var   Form1: TForm1; implementation {$R *.DFM} //-------------------------------------------------------------------- ---------- procedure TForm1.FormCreate(Sender: TObject); begin   TextSearch := TSearch.create(self);   FileSearch := TFileSearch.create(self); end; //-------------------------------------------------------------------- ---------- procedure TForm1.Exit1Click(Sender: TObject); begin   close; end; //-------------------------------------------------------------------- ---------- procedure TForm1.CaseSensitive1Click(Sender: TObject); begin   CaseSensitive1.Checked := not CaseSensitive1.Checked;   FileSearch.CaseSensitive := CaseSensitive1.Checked;   TextSearch.CaseSensitive := CaseSensitive1.Checked; end; //-------------------------------------------------------------------- ---------- procedure TForm1.SearchMemo1Click(Sender: TObject); var   str: string;   i: integer; begin   //get the sub-text (pattern) ...   TextSearch.pattern := InputBox('Search for ...', '', TextSearch.pattern);   if TextSearch.pattern = '' then exit;   //assign the text to search ...   str := Memo1.text;   TextSearch.SetData(pchar(str),length(str));   //find the first occurrence ...   i := TextSearch.Findfirst;   if i = POSITION_EOF then   begin     beep; //not found!     exit;   end;   //highlight the first match ...   Memo1.SelStart := i;   Memo1.SelLength := length(TextSearch.pattern); end; //-------------------------------------------------------------------- ---------- procedure TForm1.SearchFile1Click(Sender: TObject); var   i, cnt: integer; begin   //get the file to be searched ...   if not OpenDialog1.execute then exit;   FileSearch.Filename := OpenDialog1.filename;   //get the sub-text ...   FileSearch.pattern :=     InputBox('Search for ...', '', FileSearch.pattern);   if FileSearch.pattern = '' then exit;   //do the search ...   cnt := 0;   //you could add a performance monitor to time this ...   i := FileSearch.Findfirst;   while i <> POSITION_EOF do   begin     inc(cnt);     i := FileSearch.FindNext;   end;   //report the number of occurrences ...   memo1.clear;   memo1.Lines.add(format('Searching the file "%s" ...', [OpenDialog1.filename]));   memo1.Lines.add(format('%d occurrences of "%s" found',     [cnt, FileSearch.pattern])); end; *) //-------------------------------------------------------------------- ---------- end. ---------------------------------------------------------------------- howardpc · 2014-01-04 14:10 Quote from: arneolav on January 04, 2014, 02:26:12 pmIn Lazarus I'v got a problem with characers like "í"  (apostroph over i) You can replace the windows GetFileSize() routine with the Lazarus FileUtil.FileSize() function. The unit is written for ansistrings in system encoding only, not for utf8 strings used everywhere in Lazarus. The algorithm assumes that each character in a string of characters is stored using one byte exactly. So a '?' wildcard maps to an ASCII 'i' character (one byte), but only maps to the first byte of a non-ASCII character such as 'í' which occupies more than one byte in a utf8 string. It assumes each subsequent byte is a distinct new character in the string. A utf8 search routine is able to distinguish correctly between single-byte and multi-byte utf8 characters. In other words the unit would need to be completely re-written for correct utf8 searching, though since the lower range of utf8 maps exactly to ASCII codes, the unit works perfectly well with the utf8-encoded strings Lazarus uses, provided none of the characters encountered have codepoints greater than the ASCII range. ---------------------------------------------------------------------- arneolav · 2014-01-04 15:11 Quote from: howardpc on January 04, 2014, 03:10:44 pmYou can replace the windows GetFileSize() routine with the Lazarus FileUtil.FileSize() function.  Yes, but as I have  written above: FileUtil.Filesize don't take "handle", only filename.... ---------------------------------------------------------------------- howardpc · 2014-01-04 16:02 Exactly - use the filename (passed in as a parameter to the routine) and forget about a handle altogether. So the routine becomes Code: [Select]procedure TFileSearch.SetFilename(const Filename: string); var    filemappinghandle: thandle;    size: integer; begin   if (csDesigning in ComponentState) then   begin     fFilename := Filename;     Exit;   end;   CloseFile;   if (Filename = '') or not FileExists(Filename) then exit;   size := FileSize(Filename);   if (size <= 0) then Exit;   //  ...   end; ---------------------------------------------------------------------- arneolav · 2014-01-04 16:16 Quote from: howardpc on January 04, 2014, 05:02:26 pmExactly - use the filename (passed in as a parameter to the routine) and forget about a handle altogether. Than'x, that seems to compile but... Well... a few lines more down, the handles are in use... How can this be done? CreateFileMapping..... Code: [Select].......   filemappinghandle := CreateFileMapping(filehandle, nil, page_readonly, 0, 0, nil);   if GetLastError = error_already_exists then filemappinghandle := 0;   if filemappinghandle <> 0 then     SetData(MapViewOfFile(filemappinghandle,file_map_read,0,0,0),size);   if fStart <> nil then fFilename := Filename;   CloseHandle(filemappinghandle);   CloseHandle(filehandle); ---------------------------------------------------------------------- howardpc · 2014-01-04 19:17 Ah, I did not notice that. AFAIK there is no exact cross-platform equivalent to the Windows MapViewOfFile API. However, I would have thought you could create a TFileStream and assign that to a TMemoryStream and use memory data successfully. ---------------------------------------------------------------------- arneolav · 2014-01-04 21:29 Ok, than'x, i'll try that. ---------------------------------------------------------------------- u2o · 2014-01-06 05:39 Hi all! I also had problems with accented characters. I had to make an application to manage loans of books in a library. In the spanish language very many words have accents. It might help this function to detect those chars and then decide what to do with them. Requires the unit LCLType Code: [Select]uses   LCLType; function IsUTF8Char(MyChar: char): boolean; var   bRes: boolean;   chrUTF8: TUTF8Char; begin   bRes := False;   chrUTF8 := AnsiToUTF8(MyChar);   case chrUTF8 of     'á', 'à', 'ã', 'ä', 'â', 'Á', 'À', 'Ã', 'Ä', 'Â': bRes := True;     'é', 'è', 'ë', 'ê', 'É', 'È', 'Ë', 'Ê': bRes := True;     'í', 'ì', 'ï', 'î', 'Í', 'Ì', 'Ï', 'Î': bRes := True;     'ó', 'ò', 'õ', 'ö', 'ô', 'Ó', 'Ò', 'Õ', 'Ö', 'Ô': bRes := True;     'ú', 'ù', 'û', 'Ú', 'Ù', 'Û': bRes := True;     'ç', 'Ç': bRes := True;     'ñ', 'Ñ': bRes := True;     'ü', 'Ü': bRes := True;     else bRes := False;   end;   Result := bRes; end; procedure TForm1.Button1Click(Sender: TObject); var   cChar: char; begin   cChar := leftstr(Edit1.Text, 1)[1];   if IsUTF8Char(cChar) then ShowMessage('true'); end;                                Greetings!  ------------ Edit ------------ Please read: http://wiki.freepascal.org/LCL_Unicode_Support and http://en.wikipedia.org/wiki/UTF-8 to understand a little bit more. ---------------------------------------------------------------------- arneolav · 2014-01-06 14:36 ¡Qué bueno! I'm re-writing an  translating Delphi application, translating from spanish to norwegian. This UTF-chars cause a lot of problems as there are a lot of character- and stringhandling in this app. ¡Gracias! ---------------------------------------------------------------------- Bart · 2014-01-06 16:11 Quote from: u2o on January 06, 2014, 06:39:25 amCode: [Select]uses   LCLType; function IsUTF8Char(MyChar: char): boolean; var   bRes: boolean;   chrUTF8: TUTF8Char; begin   bRes := False;   chrUTF8 := AnsiToUTF8(MyChar);   case chrUTF8 of     'á', 'à', 'ã', 'ä', 'â', 'Á', 'À', 'Ã', 'Ä', 'Â': bRes := True;     'é', 'è', 'ë', 'ê', 'É', 'È', 'Ë', 'Ê': bRes := True;     'í', 'ì', 'ï', 'î', 'Í', 'Ì', 'Ï', 'Î': bRes := True;     'ó', 'ò', 'õ', 'ö', 'ô', 'Ó', 'Ò', 'Õ', 'Ö', 'Ô': bRes := True;     'ú', 'ù', 'û', 'Ú', 'Ù', 'Û': bRes := True;     'ç', 'Ç': bRes := True;     'ñ', 'Ñ': bRes := True;     'ü', 'Ü': bRes := True;     else bRes := False;   end;   Result := bRes; end;                              This code is really, really wrong. Let's see what happens if eg. Edit1.Text = Ä (capital A with Umlaut) In UTF8 (and TEdit.Text is UTF8) this is encoded as #195#132 cChar := leftstr(Edit1.Text, 1) -> #195 Now you call IsUtf8Char(#195), but I guess you think you call IsUtf8Char('Ä') Then you try to convert #195 to Utf8 On my systems codepage AnsiToUtf8(#195) gives #195#131, which is à (capital A with circumflex), which is a totally different character than I typed in Edit1. In fact, almost every accented character in my language has #195 as it's first byte when UTF8 encoded. It is just mere luck that #195#131 is in one of your case labels! The LazUtf8 unit contains this function: Code: [Select]function FindInvalidUTF8Character(p: PChar; Count: PtrInt;                                   StopOnNonASCII: Boolean = false): PtrInt; You can use that to find out if a string is properly UTF8 encoded (it'll return -1 in that case). Bart ---------------------------------------------------------------------- u2o · 2014-01-06 17:33 Quote from: arneolav on January 06, 2014, 03:36:38 pm¡Qué bueno! I'm re-writing an  translating Delphi application, translating from spanish to norwegian. This UTF-chars cause a lot of problems as there are a lot of character- and stringhandling in this app. ¡Gracias! Spanish: Me alegro que te sirva el codigo! Me costo mucho resolver ese problema, y las explicaciones que hay son algo complejas cuando no se sabe nada del tema. Para mi, un ejemplo rápido siempre es mejor para entender. English : I'm glad I serve you the code! It cost to me a lot time to solve this issue, and explanations that there are, are somewhat complex when is not known anything about it. For me, a quick example is always better to understand. Quote from: Bart on January 06, 2014, 05:11:40 pmThis code is really, really wrong. Let's see what happens if eg. Edit1.Text = Ä (capital A with Umlaut) In UTF8 (and TEdit.Text is UTF8) this is encoded as #195#132 cChar := leftstr(Edit1.Text, 1) -> #195 Now you call IsUtf8Char(#195), but I guess you think you call IsUtf8Char('Ä') Then you try to convert #195 to Utf8 On my systems codepage AnsiToUtf8(#195) gives #195#131, which is à (capital A with circumflex), which is a totally different character than I typed in Edit1. In fact, almost every accented character in my language has #195 as it's first byte when UTF8 encoded. It is just mere luck that #195#131 is in one of your case labels! The LazUtf8 unit contains this function: Code: [Select]function FindInvalidUTF8Character(p: PChar; Count: PtrInt;                                   StopOnNonASCII: Boolean = false): PtrInt; You can use that to find out if a string is properly UTF8 encoded (it'll return -1 in that case). Bart Hi Bart! thanks for the info, really appreciated. I also need to improve my code. But the code written in my earlier post is just to understand how to detect these chars, he can add the missing chars. Edit: In tests, he will improve the code and he will also implement the procedures needed. Greetings! ---------------------------------------------------------------------- arneolav · 2014-01-06 17:34 FindInvalidUTF8Character Deprecated, see #LazUtils.LazUTF8 for replacements. ---------------------------------------------------------------------- Bart · 2014-01-06 22:02 Quote from: arneolav on January 06, 2014, 06:34:37 pmFindInvalidUTF8Character Deprecated, see #LazUtils.LazUTF8 for replacements. What exactly is deprecated here? Lazutf8.FindInvalidUTF8Character certainly isn't AFAIK. Bart ---------------------------------------------------------------------- cdbc · 2014-01-06 22:06 Hi Have a look at Theos UTF8Tools, the package is in the ccr... http://wiki.freepascal.org/Theodp Great tool  Regards Benny ---------------------------------------------------------------------- exdatis · 2014-01-06 22:19 Quote from: cdbc on January 06, 2014, 11:06:20 pmHi Have a look at Theos UTF8Tools, the package is in the ccr... http://wiki.freepascal.org/Theodp Thank you very much! ---------------------------------------------------------------------- arneolav · 2014-01-06 22:20 Quote from: Bart on January 06, 2014, 11:02:03 pmQuote from: arneolav on January 06, 2014, 06:34:37 pmFindInvalidUTF8Character Deprecated, see #LazUtils.LazUTF8 for replacements. What exactly is deprecated here? Lazutf8.FindInvalidUTF8Character certainly isn't AFAIK. Bart Just found this: http://lazarus- ccr.sourceforge.net/docs/lcl/lclproc/findinvalidutf8character.html ---------------------------------------------------------------------- engkin · 2014-01-06 22:27 It seems that there was/is a function with a similar name FindInvalidUTF8Character in LCLProc unit, and now it is in LazUTF8 unit. Name wise it was moved from one unit to another. ---------------------------------------------------------------------- u2o · 2014-10-21 11:30 Quote from: Bart on January 06, 2014, 05:11:40 pmThe LazUtf8 unit contains this function: Code: [Select]function FindInvalidUTF8Character(p: PChar; Count: PtrInt;                                   StopOnNonASCII: Boolean = false): PtrInt; You can use that to find out if a string is properly UTF8 encoded (it'll return -1 in that case). Bart Quote from: engkin on January 06, 2014, 11:27:20 pmIt seems that there was/is a function with a similar name FindInvalidUTF8Character in LCLProc unit, and now it is in LazUTF8 unit. Name wise it was moved from one unit to another. After a while I go back to experiment with utf8. These messages are confusing lot. I haven't defined that the Edit Control is utf8. The function: Code: [Select]LazUTF8.FindInvalidUTF8Character(pchar(Edit8.Text),lengt h(Edit8.Text), False) always returns -1 My system: Windows 7 x86 language and locale settings: Spanish Argentina. The only way to get the expected result (for now) is use my method ( h ttp://forum.lazarus.freepascal.org/index.php/topic,23136.msg137798.htm l#msg137798 ). Can you explain what happens? ---------------------------------------------------------------------- marcov · 2014-10-21 12:40 A lot of the general lazarus utf8 were moved out of the LCL to lazutils by Felipe so that non lazarus dependent projects (fpspreadsheet etc) could use it. ---------------------------------------------------------------------- u2o · 2014-10-21 14:12 Ok, it's in another unit. But why returns allways -1 ? Is a bug? Is my language and regional settings? Is the Windows Source DVD on Spanish? ---------------------------------------------------------------------- engkin · 2014-10-21 16:03 Quote from: u2o on October 21, 2014, 04:12:26 pmBut why returns allways -1 ? Because you are using Edit8.Text which is always a valid UTF8 encoded string. FindInvalidUTF8Character returns -1 is this case.