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:

const
  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):

const
  RightDoubleQuote = Char('”');

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

Advertisements

New revision of AutoCorrect components (1.0)

Changes since previous release:

  • Fixed spurious ‘duplicate entry’ exceptions.
  • Fixed the bugs in my previous attempt at fixing the rich edit DLL’s streaming bugs…
  • Split into Delphi 7 and Delphi 2005+ versions.
  • Added LoadFromIniFile and SaveToIniFile methods to TAutoCorrectEngine; default is to save to UTF8-encoded files.

Download from the same place, namely here.

Revised AutoCorrect components (0.9.5)

Only been a few days, but I quickly came across a couple of bugs in my own code and decided to set about working around bugs in the rich edit control, so a new revision of my AutoCorrect components is up. Change log:

  • Fixed backspace not working when acAutoIndent is amongst the options but there is no indentation to reduce.
  • Fixed the return key not acting as a trigger key for rich edits that use v3 of the rich edit DLL.
  • Moved the TXXXWithAutoCorrect classes to a separate unit, CCR.AutoCorrect.Controls.pas.
  • TRichEditWithAutoCorrect now does a bit more than simply expose an AutoCorrectEngine property, since it now works around bugs in the underlying control — specifically, the uncurling of smart quotes and mis-loading of dashes as hyphens when RTF is either streamed or pasted in.
  • Fixed ‘enable runtime themes’ option not being checked for the demos’ D2007 project files.
  • Added better for/in support to TAutoCorrectEntries when compiling in D2005 or above.

Download from the same place as before; any comments and suggestions are welcome.

AutoCorrect components uploaded (0.9.0)

I’ve just uploaded some code to implement AutoCorrect functionality in TCustomEdit and TCustomCombo descendents (TComboBox, TRichEdit, etc.) — check out the link to your right or here. Basically, it’s a non-visual component (TAutoCorrectEngine) together with simple descendents of TComboBox, TEdit, TMemo and TRichEdit that enable hooking things up at design-time, though it’s easy to hook things up manually at runtime too (a demo shows how). The code runs in Delphi 2005 or above ‘as is’, and is Delphi 7 compatible too with one small fix-up (in short, replacing a UTF-8 encoded source file with an ANSI one).