From 9d8f99daaf1a165634775f3a338c528e9c15bc32 Mon Sep 17 00:00:00 2001 From: tobigun Date: Sat, 8 Nov 2008 11:45:31 +0000 Subject: FPC still does not convert the #0 char to a WideString with length 1. WideStringUpperCase overloaded to handle this correctly (was WideCharUpperCase previously) git-svn-id: svn://svn.code.sf.net/p/ultrastardx/svn/branches/experimental@1512 b956fd51-792f-4845-bead-9b4dfca2ff2c --- unicode/src/base/UUnicodeUtils.pas | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'unicode') diff --git a/unicode/src/base/UUnicodeUtils.pas b/unicode/src/base/UUnicodeUtils.pas index c39ec211..3608444e 100644 --- a/unicode/src/base/UUnicodeUtils.pas +++ b/unicode/src/base/UUnicodeUtils.pas @@ -88,7 +88,8 @@ function UCS4CharToString(ch: UCS4Char): UCS4String; * character access, convert to UCS-4 where each character is represented by * one UCS4Char. *) -function WideStringUpperCase(const str: WideString) : WideString; +function WideStringUpperCase(const str: WideString) : WideString; overload; +function WideStringUpperCase(ch: WideChar): WideString; overload; implementation @@ -193,18 +194,35 @@ begin Result[1] := 0; end; +function WideStringUpperCase(ch: WideChar): WideString; +begin + // If WideChar #0 is converted to a WideString in Delphi, a string with + // length 1 and a single char #0 is returned. In FPC an empty (length=0) + // string will be returned. This will crash, if a non printable key was + // pressed, its char code (#0) is translated to upper-case and the the first + // character is accessed with Result[1]. + // We cannot catch this error in the WideString parameter variant as the string + // has length 0 already. + + // Force min. string length of 1 + if (ch = #0) then + Result := #0 + else + Result := WideStringUpperCase(WideString(ch)); +end; + function WideStringUpperCase(const str: WideString): WideString; begin // On Linux and MacOSX the cwstring unit is necessary for Unicode function-calls. // Otherwise you will get an EIntOverflow exception (thrown by unimplementedwidestring()). // The Unicode manager cwstring does not work with MacOSX at the moment because - // of missing references to iconv. So we have to use Ansi... for the moment. + // of missing references to iconv. {.$IFNDEF DARWIN} {$IFDEF NOIGNORE} Result := WideUpperCase(str) {$ELSE} - Result := UTF8Decode(AnsiUpperCase(UTF8Encode(str))); + Result := UTF8Decode(UpperCase(UTF8Encode(str))); {$ENDIF} end; -- cgit v1.2.3