簡潔版本
鑒于:
const
Whitespace = [$0009, $000A, $000C, $0020];
- 作品:
if ch in Whitespace then... - 失敗:
case ch of Whitespace: ...
長版
通常,如果您有一個case宣告,您可以為每種情況包含不同的值,例如:
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;
那行得通,但我希望這些值是常量:
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
是否有任何語法安排允許:
case一個Cardinal- 反對“一組
Cardinals”?
還是我永遠堅持以下?
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;
顯然,使用概念case匹配正在執行的邏輯;不得不做if-elseif的是……更少。
注意:我不需要它實際上是一個 Delphi " set",這是一個具有特定含義的特定術語。我只是想:
case ch of Whitespace: ...
以同樣的方式作業:
if ch in Whitespace then...
已經起作用了。
而且我們知道編譯器已經可以將 Cardinal 與“集合”進行比較,因為以下內容已經有效:
case ch of
$000A, $000D, $0009, $0032: ...
end;
它將紅衣主教與“一組數字”進行比較。
獎金閱讀
- Delphi 整數范圍的 case 陳述句
- 有效地將整數與 Delphi 中的靜態整數串列進行比較?
- 將整數變數與 if 陳述句中的整數串列進行比較的任何方法
- “整數集”的德爾福型別是什么?
uj5u.com熱心網友回復:
不,這不受支持。
根據官方檔案:
案例陳述具有以下形式:
case selectorExpression of caseList1: statement1; ... caseListn: statementn; end其中 [...] 每個
caseList都是以下之一:
- 編譯器可以在不執行程式的情況下計算的數字、宣告的常量或其他運算式。它必須是與 兼容的序數型別
selectorExpression。[...]- 具有以下形式的子范圍
First..Last, 其中First和Last都滿足上述標準并且First小于或等于Last。- 具有表單的串列
item1, ..., itemn,其中每個專案都滿足上述條件之一。
因此,這僅允許將單個值、顯式范圍和此類值的串列作為case語法的一部分。
盡管 Delphi 檔案很好,但它并不完美,您不能 100% 依賴它。但是,我相信所有有經驗的 Delphi 開發人員都會同意 acaseList不能是與selectorExpression.
您可以在Embarcadero 的 Jira提交功能請求。
但是您可以使用范圍語法和先前宣告的子范圍型別(未設定常量)來實作部分相似的內容:
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;
額外說明:事實上,檔案的非 100% 準確度可以在上面參考的部分中看到:
selectorExpression是小于 32 位的序數型別的任何運算式
那是胡說八道。selectorExpression當然可以是 32 位整數。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/424536.html
上一篇:如何從磁盤獲取SMART資訊?
