Short Version
Given:
const
Whitespace = [$0009, $000A, $000C, $0020];
Works: if ch in Whitespace then...
Fails: case ch of Whitespace: ...
Long Version
Normally, if you were to have a case statement, you can include various values for each case, eg:
var
ch: UCS4Char; // unsigned 32-bit
case ch of
48..57: {asciiDigit}; //i.e. '0'..'9'
65..70: {asciiUpperHexDigit}; //i.e. 'A'..'F'
97..102: {asciiLowerHexDigit}; //i.e. 'a'..'f'
end;
That works, but I would like these values to be constants:
const
//https://infra.spec.whatwg.org/#code-points
asciiDigit = [Ord('0')..Ord('9')]; //https://infra.spec.whatwg.org/#ascii-digit
asciiUpperHexDigit = [Ord('A')..Ord('F')]; //https://infra.spec.whatwg.org/#ascii-upper-hex-digit
asciiLowerHexDigit = [Ord('a')..Ord('f')]; //https://infra.spec.whatwg.org/#ascii-lower-hex-digit
asciiHexDigit = asciiUpperHexDigit + asciiLowerHexDigit; //https://infra.spec.whatwg.org/#ascii-hex-digit
asciiUpperAlpha = [Ord('A')..Ord('Z')]; //https://infra.spec.whatwg.org/#ascii-upper-alpha
asciiLowerAlpha = [Ord('a')..Ord('z')]; //https://infra.spec.whatwg.org/#ascii-lower-alpha
asciiAlpha = asciiUpperAlpha + asciiLowerAlpha; //https://infra.spec.whatwg.org/#ascii-alpha
asciiAlphaNumeric = asciiDigit + asciiAlpha; //https://infra.spec.whatwg.org/#ascii-alphanumeric
Is there any arrangement of any syntax that will allow:
caseing a Cardinal
against a "set of Cardinals"?
Or am I permanently stuck with the following?
var
ch: UCS4Char; //unsigned 32-bit
case ch of
Ord('!'): FState := MarkupDeclarationOpenState;
Ord('/'): FState := EndTagOpenState;
Ord('?'): AddParseError('unexpected-question-mark-instead-of-tag-name');
UEOF: AddParseError('eof-before-tag-name parse error');
Whitespace: FState := SharkTankContosoGrobber;
else
if ch in asciiDigit then
begin
FReconsume := True;
FState := tsTagNameState;
end
else
AddParseError('invalid-first-character-of-tag-name parse error');
end;
Obviously, using the conceptual case matches the logic being performed; having to do if-elseif is...lesser.
Note: I don't need it to actually be a Delphi "set", that is a specific term with a specific meaning. I just want:
case ch of Whitespace: ...
to work the same way:
if ch in Whitespace then...
already does work.
And we know the compiler already is OK with comparing a Cardinal to a "set", because the following already works:
case ch of
$000A, $000D, $0009, $0032: ...
end;
It's comparing a Cardinal to a "set of numbers".
Bonus Reading
Delphi case statement for integer ranges
Efficiently compare an integer against a static list of integers in Delphi?
any way to compare an integer variable to a list of integers in if statement
What Delphi type for 'set of integer'?
No, this is not supported.
According to the official documentation:
A case statement has the form:
case selectorExpression of
caseList1: statement1;
...
caseListn: statementn;
end
where [...] each caseList is one of the following:
A numeral, declared constant, or other expression that the compiler can evaluate without executing your program. It must be of an ordinal type compatible with selectorExpression. [...]
A subrange having the form First..Last, where First and Last both satisfy the criterion above and First is less than or equal to Last.
A list having the form item1, ..., itemn, where each item satisfies one of the criteria above.
Hence, this only allows single values, explicit ranges, and lists of such values, as part of the case syntax.
Although the Delphi documentation is good, it isn't perfect and you cannot rely on it to 100%. However, I'm sure all experienced Delphi developers will agree that a caseList cannot be a predeclared single-identifier "collection" of ordinal values compatible with selectorExpression.
You may file a feature request at Embarcadero's Jira.
But you can use the range syntax and a previously declared subrange type (not set constant) to achieve something partly similar:
type
TAsciiDigit = '0'..'9';
TAsciiLatinCapitalLetter = 'A'..'Z';
TAsciiLatinSmallLetter = 'a'..'z';
procedure TForm1.FormCreate(Sender: TObject);
begin
var c := 'R';
case c of
Low(TAsciiDigit) .. High(TAsciiDigit):
ShowMessage('Digit!');
Low(TAsciiLatinCapitalLetter) .. High(TAsciiLatinCapitalLetter):
ShowMessage('Capital!');
Low(TAsciiLatinSmallLetter) .. High(TAsciiLatinSmallLetter):
ShowMessage('Small letter!');
else
ShowMessage('Something else.');
end;
end;
Bonus remark: In fact, the non-100% accuracy of the documentation can be seen in the section quoted above:
selectorExpression is any expression of an ordinal type smaller than 32 bits
That's nonsense. selectorExpression certainly can be a 32-bit integer.
Related
The way adding strings and variants behaves in Delphi (10.2 Tokyo) was a complete surprise to me. Can someone provide a reasonable explanation for this "feature" or shall we call it a bug ?
function unexpected: string;
var v: Variant;
begin
result := '3';
v := 2;
result := v + result;
ShowMessage(result); //displays 5, I expected 23
result := '3';
v := 2;
result := result + '-' + v;
ShowMessage(result) //displays -1, I expected 3-2
end;
result := v + result
Delphi's Variant type is a slightly extended version of the Win32 API's VARIANT type and supposed to be compatible with it so long as you do not use any Delphi-specific types. Additionally, when you use Delphi-specific string types, it is supposed to behave like it would with the OLE string type. In the Win32 API, it is specifically documented that adding a string and a number will result in a (numeric) addition, not a string concatenation, that you need to have two string operands to get a string concatenation:
VarAdd:
Condition Result
Both expressions are strings Concatenated
[...]
One expression is numeric and the other a string Addition
[...]
I suspect VarAdd is defined like that to make things easier for VB users.
result := result + '-' + v
Here result + '-' should perform string concatenation since both operands are strings. '3-' + v is then treated as a numeric addition, requiring 3- to be parsed as a number. I believe that since there are contexts in which the sign follows the digits, this parse succeeds and produces -3. Adding 2 to that results in -1.
This question already has an answer here:
Delphi check if character is in range 'A'..'Z' and '0'..'9'
(1 answer)
Closed 5 years ago.
for counter := 1 to lengthofpassword do
begin
currentletter:=password[counter];
currentascii:=Ord(currentletter);
if (96<currentascii<123) OR (64<currentascii<91) OR (47<currentascii<58) then
Writeln('valid')
else
asciicheck:=false;
end;
I know this code is wrong but I did it to explain what I want to ask. How can you specify ranges for an if statement? Before, I messed around with lots of if statements and my code wasn't working the way I wanted it to. Basically, I am making a procedure which checks the user input for anything other than uppercase and lowercase alphabet and numbers. This question is different because I was looking for how this problem could be solved using a Case Of statement.
for counter := 1 to lengthofpassword do
begin
currentletter:=password[counter];
currentascii:=Ord(currentletter);
if (currentascii<48) AND (currentascii>57) then
asciipoints:=asciipoints+1;
if (currentascii<65) AND (currentascii>90) then
asciipoints:=asciipoints+1;
if (currentascii<97) AND (currentascii>122) then
asciipoints:=asciipoints+1;
Writeln(asciipoints);
end;
I also tried to do it like this but then realised this wouldn't work because if one statement was satisfied, the others wouldn't be and the points based system wouldn't work either.
Glad you found the answer yourself.
Another way to make sure the password only contains upper and lower case characters and numbers is what I tried to point to: define a set of characters that are valid and check if each character in your password is in these valid characters.
So with a set defined like this:
const
ValidChars = ['A'..'Z', 'a'..'z', '0'..'9'];
you can use statements like
if password[I] in ValidChars then
This statement will however generate a compiler warning in Unicode Delphi, as the type in a set is limited to 256 possible values, and their ordinalities must fall between 0 and 255. This isn't the case for WideChar with 65.536 values. So the set of char defined is in fact a set of AnsiChar. For this task this is acceptable, as every character that needs to be checked is ASCII, so using the function CharInSet will not generate a compiler warning and have a defined behavior - returning False - if the password contains Unicode characters.
This is the resulting code:
const
ValidChars = ['A'..'Z', 'a'..'z', '0'..'9'];
var
I: Integer;
begin
for I := 1 to passwordlength do
begin
if CharInSet(password[I], ValidChars) then
Writeln('valid') // more likely to do nothing and invert the if statement
else
begin
asciicheck := False;
Break; // No need to look further, the check failed
end;
end;
end;
Multiple ranges is best expressed in a case statement:
begin
for counter := 1 to lengthofpassword do
begin
case Ord(password[counter]) of
48..57,
65..90,
97..122 :
Writeln('valid')
else
asciicheck:=false;
end;
end;
end;
Now, this works for characters < #128. If you are working in a unicode application and don't want the restriction of characters being the english alphabet, it is possible to use TCharHelper.IsLetterOrDigit.
if password[counter].IsLetterOrDigit then ...
Thanks to a comment up above, I have found a solution. I ended up using a Case Of statement like this:
for counter := 1 to lengthofpassword do
begin
currentletter:=password[counter];
currentascii:=Ord(currentletter);
case currentascii of
97..122 : asciicheck:=true;
65..90 : asciicheck:=true;
48..57 : asciicheck:=true;
else asciicheck:=false;
end;
end;
Thanks once again.
So I'm trying to determine if two different strings are the same with
if DerobModel.ConstructionCount > 22 then
begin
for i := 22 to DerobModel.ConstructionCount-1 do
begin
ConstructionName[i] := DerobModel.Constructions[i].Name;
ShowMessage(ConstructionName[i]);
ShowMessage(DerobModel.HouseProperties.StringValue['NWall']);
if ConstructionName[i]=DerobModel.HouseProperties.StringValue['NWall'] then
begin
ShowMessage('Hej');
igSurf[0]:=idWallCon[i];
end;
LayerCount[i] := DerobModel.Constructions[i].LayerCount;
idWallCon[i] := i+1;
end;
end;
The ShowMessage for both of the strings returns the same string but somehow it won't go in the if statement. Any ideas?
The = operator for strings is known to work. When strings s1 and s2 are equal, s1 = s2 evaluates true. Otherwise it evaluates false. The = operator has been known to work correctly in all versions of Delphi.
The conclusion to draw is that if the body of your if does not execute, then the two strings are not equal. Now that you know that the two strings are not equal, you can debug the program to work out why two things that you believed to be equal are in fact not equal.
Note that equality testing with = is exact. Letter case is significant. Whitespace is significant. And so on.
Your strings are different, simple as that.
If you want to figure out what exactly is different, you could write an else block portion to compare the strings in detail and show you exactly what is different.
if ConstructionName[i]=DerobModel.HouseProperties.StringValue['NWall'] then
begin
ShowMessage('Hej');
igSurf[0]:=idWallCon[i];
end
else
begin
if (Length(ConstructionName[i]) <>
Length(DerobModel.HouseProperties.StringValue['NWall'])) then
begin
ShowMessage('Length('+IntToStr(Length(ConstructionName[i]))+') <> Length('+
IntToStr(Length(DerobModel.HouseProperties.StringValue['NWall']))+')');
end
else
begin
for LCharPos := 1 to Length(ConstructionName[i]) do
begin
if (ConstructionName[i][LCharPos] <>
DerobModel.HouseProperties.StringValue['NWall'][LCharPos]) then
begin
//Here you might need to rather show the ordinal values of the
//characters to see the difference if they **look** the same due
//to the font of the message.
ShowMessage('Pos['+IntToStr(LCharPos)+'] "'+
ConstructionName[i][LCharPos]+'" <> "'+
DerobModel.HouseProperties.StringValue['NWall'][LCharPos]+'"');
end;
end;
end;
end;
The only thing I can think of that might unexpectedly cause "same" strings to be reported as different is: if they are different string types. E.g. if one is WideString and the other AnsiString, then:
There would have to be an implicit conversion to do the comparison.
And this means one of the strings would be changed.
The change could cause two strings that look the same to actually be different.
When I compile the following code in Delphi XE2 for the target platform 64-bit Windows ...
function HKeyToString(_HKey: HKey): string;
begin
case _HKey of
HKEY_CLASSES_ROOT: result := 'HKEY_CLASSES_ROOT'; // do not translate
HKEY_CURRENT_USER: result := 'HKEY_CURRENT_USER'; // do not translate
HKEY_LOCAL_MACHINE: result := 'HKEY_LOCAL_MACHINE'; // do not translate
HKEY_USERS: result := 'HKEY_USERS'; // do not translate
HKEY_PERFORMANCE_DATA: result := 'HKEY_PERFORMANCE_DATA'; // do not translate
HKEY_CURRENT_CONFIG: result := 'HKEY_CURRENT_CONFIG'; // do not translate
HKEY_DYN_DATA: result := 'HKEY_DYN_DATA'; // do not translate
else
Result := Format(_('unknown Registry Root Key %x'), [_HKey]);
end;
end;
... I get warnings for each of the HKEY_-Constants:
"W1012 Constant expression violates subrange bounds"
I checked the declarations in Winapi.Windows (with Ctrl+Leftclick on the identifiers):
type
HKEY = type UINT_PTR;
{...}
const
HKEY_CLASSES_ROOT = HKEY(Integer($80000000));
These look fine to me. Why does the compiler still think there is a problem?
On the 64 bit compiler the actual value of HKEY_CLASSES_ROOT is:
FFFFFFFF80000000
That's because the cast to Integer makes 80000000 into a negative number. And then the conversion to unsigned leads to FFFFFFFF80000000. Note that this value is correct. The declaration in the windows header file is:
#define HKEY_CLASSES_ROOT (( HKEY ) (ULONG_PTR)((LONG)0x80000000) )
and when you include the header file and inspect the value of HKEY_CLASSES_ROOT in a C++ program, it is the exact same value as for the Delphi declaration.
And then we can solve the puzzle from the Delphi documentation which states that the selectors in a case statement can only be:
any expression of an ordinal type smaller than 32 bits
You have no choice but to replace your case statement with an if statement.
HKEY=UINT_PTR is an unsigned 64 bit integer in your case, and case ... of statement seems not to handle it.
The XE2/XE3 compiler front-end still assumes it targets a 32bit platform, even if there is no technical reason for the compiler back-end not able to handle 64 bit case statements (with the classic sub register,constant; jz #... asm code generation pattern).
You can try to typecast everything to integer:
const
HKEY_CLASSES_ROOT32 = Integer($80000000);
...
function HKeyToString(_HKey: integer): string;
begin
case _HKey of
HKEY_CLASSES_ROOT32: result := 'HKEY_CLASSES_ROOT'; // do not translate
...
or just ignore the upmost 32 bits of the _HKey value (this is the same):
function HKeyToString(_HKey: HKey): string;
begin
case _HKey and $ffffffff of
HKEY_CLASSES_ROOT and $ffffffff: result := 'HKEY_CLASSES_ROOT'; // do not translate
...
It will work as expected under Windows: due to the limited number of HKEY_* constants, I think you can just ignore the upmost 32 bits of the _HKey value, and therefore use the buggy case .. of... statement. And it will work of course for both Win32 and Win64.
I suspect even ... and $f will be enough - see all HKEY_* constants.
Last (and certainly best solution) is to use good old nested if... else if... statements:
function HKeyToString(_HKey: HKey): string;
begin
if_HKey=HKEY_CLASSES_ROOT then
result := 'HKEY_CLASSES_ROOT' else // do not translate
if_HKey=HKEY_CURRENT_USER then
result := 'HKEY_CURRENT_USER' else // do not translate
....
I guess the last one is preferred, and not slower, with modern pipelines CPUs.
Delphi allows 3 versions of the Copy function:
function CopyTest(const S: string): string;
begin
Result:= Copy(S, 1, 5);
Result:= Copy(S, 1);
// Result:= Copy(S); // not allowed for strings, allowed for dyn arrays
end;
FreePascal seems to compile only the 1st (3-arg) version; for the others I have compile-time error
Error: Wrong number of parameters specified for call to "$fpc_ansistr_copy"
Am I missing some FPC compiler switch or Copy overloads are not available in Free Pascal?
The 'copy' node generator code is in inline_copy function of pinline.pas of FPC sources. Only for dynamic arrays variants 1 and 3 are valid (which generates code to pass -1 for second and third parameters to fpc_dynarray_copy in case of variant 3). For all other cases (ansi string, wide string, unicode string, char(*) and short string) 3 parameters are required (compiler generates a call to one of the copy functions (e.g. fpc_ansistr_copy in astrings.pas) without checking parameters, since the called function has no overloads or default parameters an exact match of parameters is required). No switches/directives involved.
(*) This one is a bit weird, it returns a shortstring of either itself or ''.
As I know the Free Pascal support default value parameters, so there is no need for overloading function. You can writte new Copy function something like...
function Copy(const S: string; From: integer = 1; Count: integer = MaxInt): string;
begin
//There is no need to check the string length
// if Count > Length(S) then
// Count := Length(S);
result := system.Copy(S, From, Count);
end;