From d589e6221ffcafc077eeefaa60cdc3e33a800558 Mon Sep 17 00:00:00 2001 From: s_alexander Date: Sat, 5 Dec 2009 12:26:00 +0000 Subject: added autodetection of utf8 used w3c regex to match all song lines whether they are utf8 lines and decode it on match as utf8 and as latin1 otherwise git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/trunk@1964 b956fd51-792f-4845-bead-9b4dfca2ff2c --- src/encoding/Auto.inc | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 src/encoding/Auto.inc (limited to 'src/encoding/Auto.inc') diff --git a/src/encoding/Auto.inc b/src/encoding/Auto.inc new file mode 100644 index 00000000..bf512f95 --- /dev/null +++ b/src/encoding/Auto.inc @@ -0,0 +1,127 @@ +{* UltraStar Deluxe - Karaoke Game + * + * UltraStar Deluxe is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + *} + +// Auto +// try to match the w3c regex and decode as unicode on match and as fallback if not match +// (copied from http://www.w3.org/International/questions/qa-forms-utf-8.en.php) +// +// m/\A( +// [\x09\x0A\x0D\x20-\x7E] # ASCII +// | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte +// | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs +// | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte +// | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates +// | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 +// | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 +// | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 +// )*\z/x + +type + TEncoderAuto = class(TEncoder) + public + function GetName(): AnsiString; override; + function Encode(const InStr: UCS4String; out OutStr: AnsiString): boolean; override; + function Decode(const InStr: AnsiString; out OutStr: UCS4String): boolean; override; + + constructor Create(const UTF8Encoder, FallbackEncoder: IEncoder); + + private + FallbackEncoder: IEncoder; + UTF8Encoder: IEncoder; + Regex: PPCRE; + RegexExtra: PPCREExtra; + end; + +function PCREGetMem(Size: SizeInt): Pointer; cdecl; +begin + GetMem(Result, Size); +end; + +procedure PCREFreeMem(P: Pointer); cdecl; +begin + FreeMem(P); +end; + +constructor TEncoderAuto.Create(const UTF8Encoder, FallbackEncoder: IEncoder); +var + Error: PChar; + ErrorOffset: Integer; +begin + // NOTICE: Log.LogError() is not possible here because it isn't loaded + inherited Create(); + self.FallbackEncoder := FallbackEncoder; + self.UTF8Encoder := UTF8Encoder; + + // Load and initialize PCRE Library + LoadPCRE(); + SetPCREMallocCallback(PCREGetMem); + SetPCREFreeCallback(PCREFreeMem); + + // compile regex + self.Regex := pcre_compile('\A([\x09\x0A\x0D\x20-\x7E]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2})*\z', 0, @Error, @ErrorOffset, nil); + + if self.Regex = Nil then + begin + writeln('ERROR: UTF8 Regex compilation failed: ', AnsiString(Error), ' at ', ErrorOffset); + end + else + begin + // if compiled successfull, try to get more informations the speed up the matching + self.RegexExtra := pcre_study(self.Regex, 0, @Error); + + if Error <> Nil then + begin + writeln('ERROR: UTF8 Regex study failed: ', AnsiString(Error)); + end; + end; +end; + +function TEncoderAuto.GetName(): AnsiString; +begin + Result := 'Auto'; +end; + +function TEncoderAuto.Decode(const InStr: AnsiString; out OutStr: UCS4String): boolean; +var + RegexResults: Integer; +begin + if (self.Regex <> Nil) then + begin + RegexResults := pcre_exec(Regex, RegexExtra, PChar(InStr), Length(InStr), 0, 0, Nil, 0); + + if RegexResults >= 0 then + begin + Result := UTF8Encoder.Decode(InStr, OutStr); + Exit; + end; + end; + + Result := FallbackEncoder.Decode(InStr, OutStr); +end; + +function TEncoderAuto.Encode(const InStr: UCS4String; out OutStr: AnsiString): boolean; +begin + Result := UTF8Encoder.Encode(InStr, OutStr); +end; -- cgit v1.2.3 From 4c927bd3abb27cb477db47b42c368d356c7002cb Mon Sep 17 00:00:00 2001 From: s_alexander Date: Sat, 5 Dec 2009 12:26:56 +0000 Subject: disable autodetection, if pcre-lib did not load if the pcre-lib could not be load, do not try to compile the regex and so do not execute the regex and allways use the fallback encoding git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/trunk@1966 b956fd51-792f-4845-bead-9b4dfca2ff2c --- src/encoding/Auto.inc | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'src/encoding/Auto.inc') diff --git a/src/encoding/Auto.inc b/src/encoding/Auto.inc index bf512f95..3d415095 100644 --- a/src/encoding/Auto.inc +++ b/src/encoding/Auto.inc @@ -75,27 +75,28 @@ begin self.UTF8Encoder := UTF8Encoder; // Load and initialize PCRE Library - LoadPCRE(); - SetPCREMallocCallback(PCREGetMem); - SetPCREFreeCallback(PCREFreeMem); - - // compile regex - self.Regex := pcre_compile('\A([\x09\x0A\x0D\x20-\x7E]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2})*\z', 0, @Error, @ErrorOffset, nil); - - if self.Regex = Nil then - begin - writeln('ERROR: UTF8 Regex compilation failed: ', AnsiString(Error), ' at ', ErrorOffset); - end - else + if LoadPCRE() then begin - // if compiled successfull, try to get more informations the speed up the matching - self.RegexExtra := pcre_study(self.Regex, 0, @Error); + // compile regex + self.Regex := pcre_compile('\A([\x09\x0A\x0D\x20-\x7E]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2})*\z', 0, @Error, @ErrorOffset, nil); - if Error <> Nil then + if self.Regex = Nil then begin - writeln('ERROR: UTF8 Regex study failed: ', AnsiString(Error)); + writeln('ERROR: UTF8 Regex compilation failed: ', AnsiString(Error), ' at ', ErrorOffset); + end + else + begin + // if compiled successfull, try to get more informations the speed up the matching + self.RegexExtra := pcre_study(self.Regex, 0, @Error); + + if Error <> Nil then + begin + writeln('ERROR: UTF8 Regex study failed: ', AnsiString(Error)); + end; end; - end; + end + else + writeln('ERROR: pcre not loaded. utf-8 autodetection will not work.'); end; function TEncoderAuto.GetName(): AnsiString; -- cgit v1.2.3 From 35b181b5b78b5ce81409ec8ebe69b47d0e4ec716 Mon Sep 17 00:00:00 2001 From: s_alexander Date: Sat, 5 Dec 2009 14:19:51 +0000 Subject: only use writeln if CONSOLE is defined (*hope* that fixes the exception without pcre3.dll on windows) git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/trunk@1969 b956fd51-792f-4845-bead-9b4dfca2ff2c --- src/encoding/Auto.inc | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/encoding/Auto.inc') diff --git a/src/encoding/Auto.inc b/src/encoding/Auto.inc index 3d415095..2f7faa0c 100644 --- a/src/encoding/Auto.inc +++ b/src/encoding/Auto.inc @@ -82,7 +82,9 @@ begin if self.Regex = Nil then begin + {$IFDEF CONSOLE} writeln('ERROR: UTF8 Regex compilation failed: ', AnsiString(Error), ' at ', ErrorOffset); + {$ENDIF} end else begin @@ -91,12 +93,18 @@ begin if Error <> Nil then begin + {$IFDEF CONSOLE} writeln('ERROR: UTF8 Regex study failed: ', AnsiString(Error)); + {$ENDIF} end; end; end else + begin + {$IFDEF CONSOLE} writeln('ERROR: pcre not loaded. utf-8 autodetection will not work.'); + {$ENDIF} + end; end; function TEncoderAuto.GetName(): AnsiString; -- cgit v1.2.3 From 78b2bd7d0d408a7f8d679a4e97a0260900211fb6 Mon Sep 17 00:00:00 2001 From: k-m_schindler Date: Thu, 22 Apr 2010 17:50:50 +0000 Subject: use Log.LogError instead of writeln git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/trunk@2278 b956fd51-792f-4845-bead-9b4dfca2ff2c --- src/encoding/Auto.inc | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'src/encoding/Auto.inc') diff --git a/src/encoding/Auto.inc b/src/encoding/Auto.inc index 2f7faa0c..487e2e42 100644 --- a/src/encoding/Auto.inc +++ b/src/encoding/Auto.inc @@ -82,9 +82,7 @@ begin if self.Regex = Nil then begin - {$IFDEF CONSOLE} - writeln('ERROR: UTF8 Regex compilation failed: ', AnsiString(Error), ' at ', ErrorOffset); - {$ENDIF} + Log.LogError ('UTF8 Regex compilation failed: ' + AnsiString(Error) + ' at ' + IntToStr(ErrorOffset), 'EncoderAuto.Create'); end else begin @@ -93,17 +91,13 @@ begin if Error <> Nil then begin - {$IFDEF CONSOLE} - writeln('ERROR: UTF8 Regex study failed: ', AnsiString(Error)); - {$ENDIF} + Log.LogError ('UTF8 regex study failed: ' + AnsiString(Error), 'EncoderAuto.Create'); end; end; end else begin - {$IFDEF CONSOLE} - writeln('ERROR: pcre not loaded. utf-8 autodetection will not work.'); - {$ENDIF} + Log.LogError ('pcre not loaded. utf-8 autodetection will not work.', 'EncoderAuto.Create'); end; end; -- cgit v1.2.3 From c9a13f8923d1cc8798bcb07a0808855380d083c8 Mon Sep 17 00:00:00 2001 From: tobigun Date: Fri, 23 Apr 2010 12:16:36 +0000 Subject: reverted revision 2278 - Auto.inc: Log.LogError is not possible as ULog is not loaded at this point -> used ConsoleWriteLn instead git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/trunk@2279 b956fd51-792f-4845-bead-9b4dfca2ff2c --- src/encoding/Auto.inc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/encoding/Auto.inc') diff --git a/src/encoding/Auto.inc b/src/encoding/Auto.inc index 487e2e42..8c32b5d0 100644 --- a/src/encoding/Auto.inc +++ b/src/encoding/Auto.inc @@ -69,7 +69,6 @@ var Error: PChar; ErrorOffset: Integer; begin - // NOTICE: Log.LogError() is not possible here because it isn't loaded inherited Create(); self.FallbackEncoder := FallbackEncoder; self.UTF8Encoder := UTF8Encoder; @@ -82,7 +81,8 @@ begin if self.Regex = Nil then begin - Log.LogError ('UTF8 Regex compilation failed: ' + AnsiString(Error) + ' at ' + IntToStr(ErrorOffset), 'EncoderAuto.Create'); + // NOTICE: Log.LogError() is not possible here because it isn't loaded + ConsoleWriteLn(Format('ERROR: UTF8 Regex compilation failed: %s at %d', [Error, ErrorOffset])); end else begin @@ -91,13 +91,15 @@ begin if Error <> Nil then begin - Log.LogError ('UTF8 regex study failed: ' + AnsiString(Error), 'EncoderAuto.Create'); + // NOTICE: Log.LogError() is not possible here because it isn't loaded + ConsoleWriteLn('ERROR: UTF8 Regex study failed: ' + Error); end; end; end else begin - Log.LogError ('pcre not loaded. utf-8 autodetection will not work.', 'EncoderAuto.Create'); + // NOTICE: Log.LogError() is not possible here because it isn't loaded + ConsoleWriteLn('ERROR: pcre not loaded. utf-8 autodetection will not work.'); end; end; -- cgit v1.2.3 From d5a50482317c90ef83bfa4787b392b34f59d0989 Mon Sep 17 00:00:00 2001 From: tobigun Date: Fri, 23 Apr 2010 21:08:06 +0000 Subject: With FPC ConsoleWriteLn is not initialized. Fallback to AlexanderS original solution but make the warning more explicit. git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/trunk@2287 b956fd51-792f-4845-bead-9b4dfca2ff2c --- src/encoding/Auto.inc | 269 +++++++++++++++++++++++++------------------------- 1 file changed, 137 insertions(+), 132 deletions(-) (limited to 'src/encoding/Auto.inc') diff --git a/src/encoding/Auto.inc b/src/encoding/Auto.inc index 8c32b5d0..f404c2f6 100644 --- a/src/encoding/Auto.inc +++ b/src/encoding/Auto.inc @@ -1,132 +1,137 @@ -{* UltraStar Deluxe - Karaoke Game - * - * UltraStar Deluxe is the legal property of its developers, whose names - * are too numerous to list here. Please refer to the COPYRIGHT - * file distributed with this source distribution. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301, USA. - * - * $URL$ - * $Id$ - *} - -// Auto -// try to match the w3c regex and decode as unicode on match and as fallback if not match -// (copied from http://www.w3.org/International/questions/qa-forms-utf-8.en.php) -// -// m/\A( -// [\x09\x0A\x0D\x20-\x7E] # ASCII -// | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte -// | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs -// | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte -// | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates -// | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 -// | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 -// | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 -// )*\z/x - -type - TEncoderAuto = class(TEncoder) - public - function GetName(): AnsiString; override; - function Encode(const InStr: UCS4String; out OutStr: AnsiString): boolean; override; - function Decode(const InStr: AnsiString; out OutStr: UCS4String): boolean; override; - - constructor Create(const UTF8Encoder, FallbackEncoder: IEncoder); - - private - FallbackEncoder: IEncoder; - UTF8Encoder: IEncoder; - Regex: PPCRE; - RegexExtra: PPCREExtra; - end; - -function PCREGetMem(Size: SizeInt): Pointer; cdecl; -begin - GetMem(Result, Size); -end; - -procedure PCREFreeMem(P: Pointer); cdecl; -begin - FreeMem(P); -end; - -constructor TEncoderAuto.Create(const UTF8Encoder, FallbackEncoder: IEncoder); -var - Error: PChar; - ErrorOffset: Integer; -begin - inherited Create(); - self.FallbackEncoder := FallbackEncoder; - self.UTF8Encoder := UTF8Encoder; - - // Load and initialize PCRE Library - if LoadPCRE() then - begin - // compile regex - self.Regex := pcre_compile('\A([\x09\x0A\x0D\x20-\x7E]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2})*\z', 0, @Error, @ErrorOffset, nil); - - if self.Regex = Nil then - begin - // NOTICE: Log.LogError() is not possible here because it isn't loaded - ConsoleWriteLn(Format('ERROR: UTF8 Regex compilation failed: %s at %d', [Error, ErrorOffset])); - end - else - begin - // if compiled successfull, try to get more informations the speed up the matching - self.RegexExtra := pcre_study(self.Regex, 0, @Error); - - if Error <> Nil then - begin - // NOTICE: Log.LogError() is not possible here because it isn't loaded - ConsoleWriteLn('ERROR: UTF8 Regex study failed: ' + Error); - end; - end; - end - else - begin - // NOTICE: Log.LogError() is not possible here because it isn't loaded - ConsoleWriteLn('ERROR: pcre not loaded. utf-8 autodetection will not work.'); - end; -end; - -function TEncoderAuto.GetName(): AnsiString; -begin - Result := 'Auto'; -end; - -function TEncoderAuto.Decode(const InStr: AnsiString; out OutStr: UCS4String): boolean; -var - RegexResults: Integer; -begin - if (self.Regex <> Nil) then - begin - RegexResults := pcre_exec(Regex, RegexExtra, PChar(InStr), Length(InStr), 0, 0, Nil, 0); - - if RegexResults >= 0 then - begin - Result := UTF8Encoder.Decode(InStr, OutStr); - Exit; - end; - end; - - Result := FallbackEncoder.Decode(InStr, OutStr); -end; - -function TEncoderAuto.Encode(const InStr: UCS4String; out OutStr: AnsiString): boolean; -begin - Result := UTF8Encoder.Encode(InStr, OutStr); -end; +{* UltraStar Deluxe - Karaoke Game + * + * UltraStar Deluxe is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + *} + +// Auto +// try to match the w3c regex and decode as unicode on match and as fallback if not match +// (copied from http://www.w3.org/International/questions/qa-forms-utf-8.en.php) +// +// m/\A( +// [\x09\x0A\x0D\x20-\x7E] # ASCII +// | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte +// | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs +// | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte +// | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates +// | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 +// | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 +// | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 +// )*\z/x + +type + TEncoderAuto = class(TEncoder) + public + function GetName(): AnsiString; override; + function Encode(const InStr: UCS4String; out OutStr: AnsiString): boolean; override; + function Decode(const InStr: AnsiString; out OutStr: UCS4String): boolean; override; + + constructor Create(const UTF8Encoder, FallbackEncoder: IEncoder); + + private + FallbackEncoder: IEncoder; + UTF8Encoder: IEncoder; + Regex: PPCRE; + RegexExtra: PPCREExtra; + end; + +function PCREGetMem(Size: SizeInt): Pointer; cdecl; +begin + GetMem(Result, Size); +end; + +procedure PCREFreeMem(P: Pointer); cdecl; +begin + FreeMem(P); +end; + +// NOTICE: Log.LogError/ConsoleWriteLn/DebugWriteLn are initialized yet +procedure ShowError(const msg: string); +begin + {$IFDEF CONSOLE} + WriteLn('ERROR: ', msg); + {$ENDIF} +end; + +constructor TEncoderAuto.Create(const UTF8Encoder, FallbackEncoder: IEncoder); +var + Error: PChar; + ErrorOffset: Integer; +begin + inherited Create(); + self.FallbackEncoder := FallbackEncoder; + self.UTF8Encoder := UTF8Encoder; + + // Load and initialize PCRE Library + if LoadPCRE() then + begin + // compile regex + self.Regex := pcre_compile('\A([\x09\x0A\x0D\x20-\x7E]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2})*\z', 0, @Error, @ErrorOffset, nil); + + if self.Regex = Nil then + begin + ShowError(Format('UTF8 Regex compilation failed: %s at %d', [Error, ErrorOffset])); + end + else + begin + // if compiled successfull, try to get more informations the speed up the matching + self.RegexExtra := pcre_study(self.Regex, 0, @Error); + + if Error <> Nil then + begin + ShowError('UTF8 Regex study failed: ' + Error); + end; + end; + end + else + begin + ShowError('pcre not loaded. utf-8 autodetection will not work.'); + end; +end; + +function TEncoderAuto.GetName(): AnsiString; +begin + Result := 'Auto'; +end; + +function TEncoderAuto.Decode(const InStr: AnsiString; out OutStr: UCS4String): boolean; +var + RegexResults: Integer; +begin + if (self.Regex <> Nil) then + begin + RegexResults := pcre_exec(Regex, RegexExtra, PChar(InStr), Length(InStr), 0, 0, Nil, 0); + + if RegexResults >= 0 then + begin + Result := UTF8Encoder.Decode(InStr, OutStr); + Exit; + end; + end; + + Result := FallbackEncoder.Decode(InStr, OutStr); +end; + +function TEncoderAuto.Encode(const InStr: UCS4String; out OutStr: AnsiString): boolean; +begin + Result := UTF8Encoder.Encode(InStr, OutStr); +end; -- cgit v1.2.3