Delphi 2009+ quirk/bug: non-ASCII character constants

Consider the following declaration, which happens to be from the AutoCorrect component code I posted a couple of months or so ago:

  RightDoubleQuote = '”';

As you can probably guess, RightDoubleQuote then gets assigned to the Key parameter of a control’s OnKeyPress event handler, as appropriate. Should work fine, right? Well, it indeed does in D2007 or earlier, but not in the D2010 trial I’ve been playing with (I assume it’s broken in D2009 too). Basically, RightDoubleQuote in D2010, like in D7 or D2007, is compiled as an AnsiChar; and since the Ansi and unicode ordinal values are different, and no conversion from Ansi to unicode is made when assigning the constant to a Char value (now a WideChar value in D2009+), the latter gets junk. Unhelpfully enough, the compiler doesn’t issue a warning, and the $HIGHCHARUNICODE directive makes no difference either.

Nevertheless, the workaround — compatible with older Delphi versions — is easy: just explicitly make the constant a Char value with a cast (casting to WideChar would work in D2009+ too, but this would change the type when compiled in legacy Delphi versions):

  RightDoubleQuote = Char('”');

I’ve uploaded an updated version of my AutoCorrect components as a result.

2 thoughts on “Delphi 2009+ quirk/bug: non-ASCII character constants

  1. Just curious what would I have to use in the coding below?
    Use ‘*’: or Char(‘*’): ?

    function MatchPattern(S, Pattern : PChar) :Boolean;
    while(True) do
    case Pattern[0] of
    ‘*’: begin


    • Michael – since an asterisk is within the ASCII range, I would have assumed just ‘*’ would be OK, though adding the cast should be harmless. (If a character is within the ASCII range, then its ordinal value for both AnsiChar and WideChar will be the same — it’s only when they differ that the D2009+ quirk/bug in question raises its head.)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s