Johny
Johny

Reputation: 419

Filtering non-Typeable characters in unicode

In an old application programmed under Delphi 6 (non-Unicode platform), i used to filter out non-typeable characters simply by referencing their cell numbers in the ANSI character table ( if (aKeyChar in [#32..#254]) then.... ).

Now that i shifted into Delphi 2010 where the platform is Unicode based, those character mappings are not relevant anymore. Is there a clean way to meet this objective in Delphi 2010?

Upvotes: 1

Views: 728

Answers (2)

Rudy Velthuis
Rudy Velthuis

Reputation: 28806

In Unicode, to determine if a code point is a control character (assuming that is what you meant with "typeable"), it is not good enough to see if the value is in a set. To get the "control characters", you can check with the System.Character class:

if Character.IsControl(aKeyChar) then

But note that you may have to check if the WideChar is a low or high surrogate too, e.g.

if Character.IsLowSurrogate(aKeyChar) then
  // is unprintable in and of itself and next WideChar must be a high surrogate.
  // the combination is printable.

Note that a surrogate pair (low surrogate + high surrogate together) can be printable again. A low surrogate alone is not printable.

Upvotes: 1

Remy Lebeau
Remy Lebeau

Reputation: 595827

Look at the various helper functions in the System.Character unit, such as GetUnicodeCategory(), IsControl(), IsLetterOrDigit(), IsWhiteSpace(), etc.

Upvotes: 2

Related Questions