mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-04-23 00:39:44 +02:00
lazutils: paswstring: one string less in Wide2AnsiMove and Ansi2WideMove
git-svn-id: trunk@47086 -
This commit is contained in:
parent
bc7eca9321
commit
1b03376550
@ -135,8 +135,10 @@ function ConvertUTF16ToUTF8(Dest: PChar; DestCharCount: SizeUInt;
|
||||
Src: PWideChar; SrcWideCharCount: SizeUInt; Options: TConvertOptions;
|
||||
out ActualCharCount: SizeUInt): TConvertResult;
|
||||
|
||||
function UTF8ToUTF16(const S: AnsiString): UnicodeString;
|
||||
function UTF16ToUTF8(const S: UnicodeString): AnsiString;
|
||||
function UTF8ToUTF16(const S: AnsiString): UnicodeString; overload;
|
||||
function UTF8ToUTF16(const P: PChar; ByteCnt: SizeUInt): UnicodeString; overload;
|
||||
function UTF16ToUTF8(const S: UnicodeString): AnsiString; overload;
|
||||
function UTF16ToUTF8(const P: PWideChar; WideCnt: SizeUInt): AnsiString; overload;
|
||||
|
||||
// locale
|
||||
procedure LazGetLanguageIDs(var Lang, FallbackLang: String);
|
||||
@ -3209,18 +3211,19 @@ end;
|
||||
copy
|
||||
------------------------------------------------------------------------------}
|
||||
function UTF8ToUTF16(const S: AnsiString): UnicodeString;
|
||||
begin
|
||||
Result:=UTF8ToUTF16(PChar(S),length(S));
|
||||
end;
|
||||
|
||||
function UTF8ToUTF16(const P: PChar; ByteCnt: SizeUInt): UnicodeString;
|
||||
var
|
||||
L: SizeUInt;
|
||||
begin
|
||||
if S = ''
|
||||
then begin
|
||||
Result := '';
|
||||
Exit;
|
||||
end;
|
||||
|
||||
SetLength(Result, Length(S));
|
||||
if ByteCnt=0 then
|
||||
exit('');
|
||||
SetLength(Result, ByteCnt);
|
||||
// wide chars of UTF-16 <= bytes of UTF-8 string
|
||||
if ConvertUTF8ToUTF16(PWideChar(Result), Length(Result) + 1, PChar(S), Length(S),
|
||||
if ConvertUTF8ToUTF16(PWideChar(Result), Length(Result) + 1, P, ByteCnt,
|
||||
[toInvalidCharToSymbol], L) = trNoError
|
||||
then SetLength(Result, L - 1)
|
||||
else Result := '';
|
||||
@ -3234,22 +3237,26 @@ end;
|
||||
Converts the specified UTF-16 encoded string (system endian) to UTF-8 encoded
|
||||
------------------------------------------------------------------------------}
|
||||
function UTF16ToUTF8(const S: UnicodeString): AnsiString;
|
||||
begin
|
||||
Result := UTF16ToUTF8(PWideChar(S),length(S));
|
||||
end;
|
||||
|
||||
function UTF16ToUTF8(const P: PWideChar; WideCnt: SizeUInt): AnsiString;
|
||||
var
|
||||
L: SizeUInt;
|
||||
R: AnsiString;
|
||||
begin
|
||||
Result := '';
|
||||
if S = '' then Exit;
|
||||
if WideCnt=0 then
|
||||
exit('');
|
||||
|
||||
SetLength(R, Length(S) * 3);
|
||||
SetLength(Result, WideCnt * 3);
|
||||
// bytes of UTF-8 <= 3 * wide chars of UTF-16 string
|
||||
// e.g. %11100000 10100000 10000000 (UTF-8) is $0800 (UTF-16)
|
||||
if ConvertUTF16ToUTF8(PChar(R), Length(R) + 1, PWideChar(S), Length(S),
|
||||
if ConvertUTF16ToUTF8(PChar(Result), Length(Result) + 1, P, WideCnt,
|
||||
[toInvalidCharToSymbol], L) = trNoError then
|
||||
begin
|
||||
SetLength(R, L - 1);
|
||||
Result := R;
|
||||
end;
|
||||
SetLength(Result, L - 1);
|
||||
end else
|
||||
Result := '';
|
||||
end;
|
||||
|
||||
procedure LazGetLanguageIDs(var Lang, FallbackLang: String);
|
||||
|
@ -1,4 +1,10 @@
|
||||
{
|
||||
*****************************************************************************
|
||||
paswstring.pas
|
||||
|
||||
A widestring manager written in Pascal
|
||||
and optimized for DefaultSystemCodePage CP_UTF8.
|
||||
|
||||
*****************************************************************************
|
||||
This file is part of the LazUtils package
|
||||
|
||||
@ -6,7 +12,7 @@
|
||||
for details about the license.
|
||||
*****************************************************************************
|
||||
}
|
||||
unit paswstring;
|
||||
unit PasWString;
|
||||
|
||||
{$mode objfpc}
|
||||
{$inline on}
|
||||
@ -17,7 +23,7 @@ unit paswstring;
|
||||
interface
|
||||
|
||||
uses
|
||||
SysUtils, lazutf8
|
||||
SysUtils, LazUTF8
|
||||
{$ifdef PASWSTRING_SUPPORT_NONUTF8_ANSISTRING}, lconvencoding{$endif}
|
||||
;
|
||||
|
||||
@ -32,16 +38,9 @@ procedure fpc_rangeerror; [external name 'FPC_RANGEERROR'];
|
||||
|
||||
// len comes in widechars, not bytes
|
||||
procedure Wide2AnsiMove(source:pwidechar;var dest:ansistring;len:SizeInt);
|
||||
var
|
||||
widestr: widestring;
|
||||
begin
|
||||
{$ifdef PASWSTRING_VERBOSE}WriteLn('Wide2AnsiMove START');{$endif}
|
||||
// Copy the originating string taking into account the specified length
|
||||
SetLength(widestr, len);
|
||||
System.Move(source^, widestr[1], len * SizeOf(WideChar));
|
||||
|
||||
// Now convert it, using UTF-16 -> UTF-8
|
||||
dest := UTF16ToUTF8(widestr);
|
||||
dest := UTF16ToUTF8(Source,len);
|
||||
{$ifdef PASWSTRING_SUPPORT_NONUTF8_ANSISTRING}
|
||||
// And correct to the real Ansi encoding
|
||||
dest := ConvertEncoding(dest, EncodingUTF8, GetDefaultTextEncoding());
|
||||
@ -49,20 +48,24 @@ begin
|
||||
end;
|
||||
|
||||
procedure Ansi2WideMove(source:pchar;var dest:widestring;len:SizeInt);
|
||||
{$ifdef PASWSTRING_SUPPORT_NONUTF8_ANSISTRING}
|
||||
var
|
||||
ansistr: ansistring;
|
||||
{$endif}
|
||||
begin
|
||||
{$ifdef PASWSTRING_VERBOSE}WriteLn('Ansi2WideMove START');{$endif}
|
||||
|
||||
{$ifdef PASWSTRING_SUPPORT_NONUTF8_ANSISTRING}
|
||||
// Copy the originating string taking into account the specified length
|
||||
SetLength(ansistr, len);
|
||||
System.Move(source^, ansistr[1], len);
|
||||
|
||||
{$ifdef PASWSTRING_SUPPORT_NONUTF8_ANSISTRING}
|
||||
// Convert to UTF-8
|
||||
ansistr := ConvertEncoding(ansistr, GetDefaultTextEncoding(), EncodingUTF8);
|
||||
{$endif}
|
||||
// Now convert it, using UTF-8 -> UTF-16
|
||||
dest := UTF8ToUTF16(ansistr);
|
||||
{$else}
|
||||
dest := UTF8ToUTF16(source,len);
|
||||
{$endif}
|
||||
end;
|
||||
|
||||
function LowerWideString(const s : WideString) : WideString;
|
||||
|
Loading…
Reference in New Issue
Block a user