aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortobigun <tobigun@b956fd51-792f-4845-bead-9b4dfca2ff2c>2008-11-07 20:55:27 +0000
committertobigun <tobigun@b956fd51-792f-4845-bead-9b4dfca2ff2c>2008-11-07 20:55:27 +0000
commit2783f97c6f2e9e72a306e0325b911db4df425032 (patch)
tree2cfc4eb94b3200fcc928975a36a65bc2d8257fbd
parent17614ea059162f432f7feba5f39329667a335fa6 (diff)
downloadusdx-2783f97c6f2e9e72a306e0325b911db4df425032.tar.gz
usdx-2783f97c6f2e9e72a306e0325b911db4df425032.tar.xz
usdx-2783f97c6f2e9e72a306e0325b911db4df425032.zip
added
- CheckReplaceUTF8BOM() - ParseEncoding() - UTF8_BOM - RecodeStringUTF8 - RecodeString moved to RecodeStringWide git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/branches/experimental@1508 b956fd51-792f-4845-bead-9b4dfca2ff2c
-rw-r--r--unicode/src/base/UTextEncoding.pas102
1 files changed, 96 insertions, 6 deletions
diff --git a/unicode/src/base/UTextEncoding.pas b/unicode/src/base/UTextEncoding.pas
index ae36a270..5adc02f8 100644
--- a/unicode/src/base/UTextEncoding.pas
+++ b/unicode/src/base/UTextEncoding.pas
@@ -34,12 +34,53 @@ interface
{$I switches.inc}
uses
- SysUtils;
+ SysUtils,
+ StrUtils;
type
- TEncoding = (encCP1250, encCP1252, encUTF8, encNative);
+ TEncoding = (
+ encCP1250, // Windows-1250 Central/Eastern Europe (used by Ultrastar)
+ encCP1252, // Windows-1252 Western Europe (used by UltraStar Deluxe < 1.1)
+ encUTF8, // UTF-8
+ encLocale // current locale (needs cwstring on linux)
+ );
-function RecodeString(const Src: string; SrcEncoding: TEncoding): WideString;
+const
+ EncodingNames: array[TEncoding] of AnsiString = (
+ 'CP1250',
+ 'CP1252',
+ 'UTF8',
+ 'LOCALE'
+ );
+
+const
+ UTF8_BOM: AnsiString = #$EF#$BB#$BF;
+
+{**
+ * Changes encoding of string Src with encoding SrcEncoding to UTF-16
+ * If SrcEncoding is encUnknown the result is undefined.
+ *}
+function RecodeStringWide(const Src: string; SrcEncoding: TEncoding): WideString;
+
+{**
+ * Changes encoding of string Src with encoding SrcEncoding to UTF-8.
+ * If SrcEncoding is encUnknown the result is undefined.
+ *}
+function RecodeStringUTF8(const Src: string; SrcEncoding: TEncoding): UTF8String;
+
+{**
+ * If Text starts with an UTF-8 BOM, the BOM is removed and true will
+ * be returned.
+ *}
+function CheckReplaceUTF8BOM(var Text: string): boolean;
+
+{**
+ * Parses an encoding string to its TEncoding equivalent.
+ * Surrounding whitespace and dashes ('-') are removed, the upper-cased
+ * resulting value is then compared with TEncodingNames.
+ * If the encoding was not found, the result is set to the Default encoding.
+ *}
+function ParseEncoding(const EncodingStr: AnsiString; Default: TEncoding): TEncoding;
implementation
@@ -103,7 +144,9 @@ const
#$00F8, #$00F9, #$00FA, #$00FB, #$00FC, #$00FD, #$00FE, #$00FF
);
-
+{**
+ * Internal conversion function
+ *}
function Convert(const Src: string; const Table: TConversionTable): WideString;
var
SrcPos, DstPos: integer;
@@ -133,7 +176,7 @@ begin
SetLength(Result, DstPos-1);
end;
-function RecodeString(const Src: string; SrcEncoding: TEncoding): WideString;
+function RecodeStringWide(const Src: string; SrcEncoding: TEncoding): WideString;
begin
case SrcEncoding of
encCP1250:
@@ -142,9 +185,56 @@ begin
Result := Convert(Src, CP1252Table);
encUTF8:
Result := UTF8Decode(Src);
- encNative:
+ encLocale:
Result := UTF8Decode(AnsiToUtf8(Src));
+ else
+ Result := '';
+ end;
+end;
+
+function RecodeStringUTF8(const Src: string; SrcEncoding: TEncoding): UTF8String;
+begin
+ case SrcEncoding of
+ encCP1250:
+ Result := UTF8Encode(Convert(Src, CP1250Table));
+ encCP1252:
+ Result := UTF8Encode(Convert(Src, CP1252Table));
+ encUTF8:
+ Result := Src;
+ encLocale:
+ Result := AnsiToUtf8(Src);
+ else
+ Result := '';
end;
end;
+function CheckReplaceUTF8BOM(var Text: string): boolean;
+begin
+ if AnsiStartsStr(UTF8_BOM, Text) then
+ begin
+ Text := Copy(Text, Length(UTF8_BOM)+1, Length(Text)-Length(UTF8_BOM));
+ Result := true;
+ Exit;
+ end;
+ Result := false;
+end;
+
+function ParseEncoding(const EncodingStr: AnsiString; Default: TEncoding): TEncoding;
+var
+ PrepStr: string; // prepared encoding string
+ Encoding: TEncoding;
+begin
+ // remove surrounding whitespace, replace dashes, to upper case
+ PrepStr := UpperCase(AnsiReplaceStr(Trim(EncodingStr), '-', ''));
+ for Encoding := Low(EncodingNames) to High(EncodingNames) do
+ begin
+ if (EncodingNames[Encoding] = PrepStr) then
+ begin
+ Result := Encoding;
+ Exit;
+ end;
+ end;
+ Result := Default;
+end;
+
end.