Author: Tomas Rutkauskas
Is there any way to enumerate the contents of a set? Just now I have a set of char,
each char is a lower case character, and need to add the corresponding upper case
characters to the set. Any other way than looping through the set range and do an
inclusion check each time?
Answer:
No. If you are after building sets of letters for the currently selected language
you could do it using API functions like IsCharAlpha and IsCharUpper:
1 unit Charsets;
2
3 interface
4
5 type
6 TCharSet = set of AnsiChar;
7
8 const
9 Signs: TCharset = ['-', '+'];
10 Numerals: TCharset = ['0'..'9'];
11 HexNumerals: TCharset = ['A'..'F', 'a'..'f', '0'..'9'];
12 IntegerChars: TCharset = ['0'..'9', '-', '+'];
13 var
14 Letters, LowerCaseLetters, UpperCaseLetters: TCharSet;
15 FloatChars, SciFloatChars: TCharset;
16 AlphaNum, NonAlphaNum: TCharset;
17
18 { Need to call this again when locale changes. }
19 procedure SetupCharsets;
20
21 implementation
22
23 uses
24 Windows, Sysutils;
25
26 procedure SetupCharsets;
27 var
28 ch: AnsiChar;
29 begin
30 LowerCaseLetters := [];
31 UpperCaseLetters := [];
32 AlphaNum := [];
33 NonAlphaNum := [];
34 for ch := Low(ch) to High(ch) do
35 begin
36 if IsCharAlpha(ch) then
37 if IsCharUpper(ch) then
38 Include(UpperCaseLetters, ch)
39 else
40 Include(LowerCaseLetters, ch);
41 if IsCharAlphanumeric(ch) then
42 Include(AlphaNum, ch)
43 else
44 Include(NonAlphaNum, ch);
45 end;
46 Letters := LowerCaseLetters + UpperCaseLetters;
47 FloatChars := IntegerChars;
48 Include(FloatChars, DecimalSeparator);
49 SciFloatChars := FloatChars + ['e', 'E'];
50 end;
51
52 initialization
53 SetupCharsets;
54
55 end.
|