Alister
Alister

Reputation: 6837

CharInSet bulk conversions when migrating from Delphi 2007

We are shifting a number of projects from Delphi 2007 to XE8 and have a number of the following warning (many hundreds of them):

[dcc32 Warning] X.PAS(1568): W1050 WideChar reduced to byte char in set expressions.  Consider using 'CharInSet' function in 'SysUtils' unit.`

It occurs to me that many of these are of the form

if x in ['1','2','3'] then

which need to be converted to

if CharInSet(x, ['1','2','3']) then

And this looks like there might be some sort of regular expression type search and replace that could be used to do these in bulk.

Can anyone think of a way to convert these in bulk?

Upvotes: 1

Views: 827

Answers (1)

fantaghirocco
fantaghirocco

Reputation: 4868

This can be done with Search/Replace in the IDE.

The following works for me in XE4.

  • search for:

    if {[a-z]} in \[{{'[0-9]+'\,? ?}+}\] then
    

    If you want to match a variable more than one character long, consider to use some quantifier like [a-z]+.

  • replace with:

    if CharInSet\(\0, \[\1\]\) then
    

Notice that the IDE uses {} for groups and \0, \1 ... as replacement placeholders.

Embarcadero Regular Expressions reference for Delphi XE4


IDE regular expressions search:

IDE regex search

The resulting unit:

IDE regex search replaced


You may also find this question useful for further reference.

Upvotes: 2

Related Questions