Bayesean Blog - Desktop, Mobile and IOT Developer Blog


Delphi FMX - Changing TCharacter to TCharHelper

Posted on 31st Mar 2018 in Delphi FMX


TCharacter2TCharhelper49.png

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?

 tcharacterdebugger44.png

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.

androidtcharacter5.png

Happy Coding...

 

 


Be the First to Comment...

Add a Comment

9+5

Recent News

Delphi Delimited String to Fields
Delphi A Professional VCL DBGrid Part Four
Delphi A Professional VCL DBGrid Part Three
Delphi A Professional VCL DBGrid Part Two
Delphi A Professional VCL DBGrid Part One
Delphi VCL Buttons in DBGrid
Two Helper Apps for Delphi LibUSB
Delphi Libusb Library Introduction

Categories

Bootstrap 4
Delphi VCL
Delphi FMX
Ajax
Bootstrap 3
CSS
XE4>Delphi > XE4
Delphi < XE4
PHP

Archives

August 2019

Delphi Delimited String to Fields

June 2019

Delphi A Professional VCL DBGrid Part Four

May 2019

Delphi A Professional VCL DBGrid Part Three

April 2019

Delphi A Professional VCL DBGrid Part Two

March 2019

Delphi A Professional VCL DBGrid Part One

November 2018

Delphi VCL Buttons in DBGrid

October 2018

Two Helper Apps for Delphi LibUSB

September 2018

Delphi Libusb Library Introduction

August 2018

Delphi Object directly to a Json string in a REST Client
Delphi using Environment Variables in your App

July 2018

Delphi FMX Leaflet Plotter using OSM Maps

June 2018

C2PAS32 Convertor Application
C to Delphi Open Source Convertors Shootout
Delphi command-line programs with DOSCommand

May 2018

Delphi PDF Embedded viewer with PDF.js

March 2018

Delphi FMX - Changing TCharacter to TCharHelper
Make Your Delphi App POP using Javascript!

January 2018

Delphi FMX Dashboard using Chart.JS
Delphi FMX Form Docking

December 2017

PHP Slim REST Server & Delphi Auth Part 5

November 2017

Delphi FMX REST Client App Part 4

October 2017

Delphi VCL REST Pricing Client App Part 3

September 2017

Delphi REST VCL Client Basic Auth Part 2B

August 2017

Delphi REST Client Part 2A
PHP PDO REST Server Part 1

July 2017

PHP REST Server and Delphi Client Intro

June 2017

Delphi SQLite Encryptor-Decryptor Tool
Updating Applications Manifest using Delphi

May 2017

Create a Visual IP Address Geolocation with PHP

March 2017

PHP Downloader using Countdown timer
PHP File Downloader from a Inbox Selection

February 2017

Javascript Image-File Uploader with ThumbViewer

January 2017

Morris Charts and PHP-PDO

December 2016

CSS to create a functional Toggle Button