Posted on 31st Mar 2018 in Delphi FMX
So here I was upgrading an App that used TCharacter in Berlin 10.1. Next thing a warning message is shown?
[dcc32 Warning] Unit4.pas(50): W1000 Symbol 'TCharacter' is deprecated: 'Use TCharHelper'…
I have learnt that when you see this in the IDE, it is best to make the changes immediately or else you will find that when you upgrade later the app will fail. When applying some simple changes the results were not as transparent as it would seem and there is very little documentation to support as to why these errors were present and I delved deeper into the code.
Why is TCharacter being deprecated, well it seems that both uses the latest definitions of Unicode character type data, and it is good to know that it can convert Unicode characters from UTF-32 to UTF-16, and backwards.
So what are the differences actually?
Well TCharset checks for a zero string range and TCharacter check string range. This will catch you out in you code if you using a for-loop through the string and present an error.
TCharset is using a UCS4Char which works with 4–byte Unicode characters.
So the original test code looks like this...
implementation uses System.Character; var LNumber,LLength,LLetters,LLower,LUpper,I:integer; str:String; begin str:= '123456789abcdefABCDEF'; LNumber:= 0; LLength:= 0; LLetters := 0; LLower := 0; LUpper := 0; memo1.Lines.Add('String Length: '+Length(str).tostring); memo1.Lines.Add(''); for I := 1 to Length(Str) do begin if TCharacter.IsNumber(Str,I) then Inc(LNumber); if Tcharacter.isletter(Str,I) then Inc(LLetters); if Tcharacter.IsLower(Str,I) then Inc(LLower); if Tcharacter.IsUpper(Str,I) then Inc(LUpper); end;
Making the direct change with TCharhelper will result with a debugger notice…
What are these errors?
As you can see that the Zero checking with TCharHelper will create an error if you do not change the Length(str) – 1 from normal string checking Length(Str) .
‘Event Log = First chance exception at $76A5CBB2. Exception class EArgumentOutOfRangeException with message 'String index out of range (21). Must be >= 0 and <= 20'. ….(7076) ‘.
Why did Embarcadero change the range format? I am out of answers as to why this was implemented and could be a different coding approach.
The new code evolves to the following..
implementation uses System.Character; var LNumber,LLength,LLetters,LLower,LUpper,I:integer; str:String; CharHelper:Char; // added to the code begin str:= '123456789abcdefABCDEF'; LNumber:= 0; LLength:= 0; LLetters := 0; LLower := 0; LUpper := 0; memo2.Lines.Add('String Length: '+Length(str).tostring); memo2.Lines.Add(''); for I := 0 to Length(Str) -1 do // changing this to length str -1 will adjust for zerocheckingstr change begin if CharHelper.IsNumber(Str,I) then Inc(LNumber); if CharHelper.IsLetter(Str,I) then Inc(LLetters); if CharHelper.IsLower(Str,I) then Inc(LLower); if CharHelper.IsUpper(Str,I) then Inc(LUpper); end; memo2.Lines.Add('Number '+LNumber.tostring); memo2.Lines.Add('Total Letters '+LLetters.tostring); memo2.Lines.Add('Lower '+LLower.tostring); memo2.Lines.Add('Upper '+Lupper.tostring); end;
So how does this effect mobile? This change works perfectly cross-Platform and works as it did before.
October 2018Delphi VCL Buttons in DBGrid
September 2018Delphi Libusb Library Introduction
August 2018Delphi Object directly to a Json string in a REST Client
July 2018Delphi FMX Leaflet Plotter using OSM Maps
June 2018C2PAS32 Convertor Application
May 2018Delphi PDF Embedded viewer with PDF.js
March 2018Delphi FMX - Changing TCharacter to TCharHelper
January 2018Delphi FMX Dashboard using Chart.JS
December 2017PHP Slim REST Server & Delphi Auth Part 5
November 2017Delphi FMX REST Client App Part 4
October 2017Delphi VCL REST Pricing Client App Part 3
September 2017Delphi REST VCL Client Basic Auth Part 2B
August 2017Delphi REST Client Part 2A
July 2017PHP REST Server and Delphi Client Intro
June 2017Delphi SQLite Encryptor-Decryptor Tool
May 2017Create a Visual IP Address Geolocation with PHP
March 2017PHP Downloader using Countdown timer
January 2017Morris Charts and PHP-PDO
December 2016CSS to create a functional Toggle Button