function SkipWhiteSpace(var C : PChar) : boolean;
const
WhiteSpace = [#9, #10, #13, ' '];
begin
While (C^ <> #0) and (C^ in WhiteSpace) do
inc(C);
Result := C^ <> #0; //fail if at the end
end;
(C^ in WhiteSpace)
to
(CharInSet(C, WhiteSpace))
Inc(C);
you could use
C := StrNextChar(C);
type
WideCharSet = record
Chars : string;
class operator Add(a, b: WideCharSet): WideCharSet;
class operator Implicit(a : WideCharSet) : string;
class operator Implicit(a : string) : WideCharSet;
end;
class operator WideCharSet.Implicit(a : string) : WideCharSet;
var
I : integer;
C, D, E : Char;
begin
Result.Chars := '';
//have to have at least []
assert(Length(a) >= 2, 'Invalid string used in convert to SetOfWideChar');
I := 1;
SkipWhiteSpace(a, I);
assert(a[I] = '[', 'Invalid string used in convert to SetOfWideChar, ' +
'missing opening square bracket');
inc(I);
while (I < Length(a)) and (a[I] <> ']') do
begin
C := ReadChar(a, I);
SkipWhiteSpace(a, I);
if a[I] = '.' then
begin
inc(I);
if I > Length(a) then
raise Exception.Create(InvalidSetOfWideCharString);
if a[I] <> '.' then
raise Exception.Create(InvalidSetOfWideCharString);
inc(I);
D := ReadChar(a, I);
for E := C to D do
InsertSorted(E, Result.Chars);
end
else
InsertSorted(C,Result.Chars);
if a[I] = ',' then
begin
inc(I);
if I > Length(a) then
raise Exception.Create(InvalidSetOfWideCharString);
end;
end;
Assert(I <= Length(A), InvalidSetOfWideCharString);
Assert(a[I] = ']', InvalidSetOfWideCharString);
end;
class operator WideCharSet.Implicit(a : WideCharSet) : string;
var
Line : string;
I : integer;
FirstChar, LastChar : Char;
Start : integer;
R1, R2 : string; //possible range representations for characters
begin
Line := '';
{Start at the first character and see how far we can go until we run into a
non sequential character}
I := 1;
While I < Length(a.Chars) do
begin
Start := I;
FirstChar := a.Chars[I];
repeat
inc(I);
until (I > Length(a.Chars)) or
(ord(a.Chars[I]) <> Ord(FirstChar) + I - Start);
LastChar := a.Chars[Pred(I)];
if Line <> '' then
Line := Line + ', ';
R1 := CharToStringRep(FirstChar);
Line := Line + R1;
if I > Start + 1 then
begin
R2 := CharToStringRep(LastChar);
Line := Line + '..' + R2;
end;
end;
Result := '[' + Line + ']';
end;
class operator WideCharSet.In(a : Char; b : WideCharSet) : Boolean;
begin
Result := SortedPos(a, b.Chars) <> 0;
end;
Procedure MyTest;
var
Numeric : WideCharSet;
WhiteSpace : WideCharSet;
C : Char;
begin
WhiteSpace := '[#9..#10, #13, " "]';
Numeric := '["0".."9"]';
C := '6';
if C in WhiteSpace then
begin
//obviously not
end;
if C in Numeric then
begin
//Bingo
end;
end;
class operator Add(a, b: WideCharSet): WideCharSet;
class operator Subtract(a, b: WideCharSet): WideCharSet;
class operator Multiply(a, b : WideCharSet) : WideCharSet;
class operator Equal(a, b: WideCharSet) : Boolean;
class operator LessThanOrEqual(a, b : WideCharSet) : Boolean;
class operator GreaterThanOrEqual(a, b : WideCharSet) : Boolean;
class operator NotEqual(a, b : WideCharSet) : Boolean;
Procedure MyTest;
var
Numeric : WideCharSet;
WhiteSpace : WideCharSet;
LowerCaseAlpha : WideCharSet;
UpperCaseAlpha : WideCharSet;
Alpha : WideCharSet;
AlphaNumeric : WideCharSet;
CommonNumericAlphaNumeric : WideCharSet;
CommonUpperAndAlphaNumeric : WideCharSet;
AlphaNumericMinusNumeric : WideCharSet;
AlphaNumericDefinition : string;
begin
WhiteSpace := '[#9..#10, #13, " "]';
Numeric := '["0".."9"]';
LowerCaseAlpha := '["a".."z"]';
UpperCaseAlpha := '["A".."Z"]';
Alpha := LowerCaseAlpha + UpperCaseAlpha;
AlphaNumeric := Alpha + Numeric;
CommonNumericAlphaNumeric := Numeric * AlphaNumeric;
CommonUpperAndAlphaNumeric := UpperCaseAlpha * AlphaNumeric;
AlphaNumericMinusNumeric := AlphaNumeric - Numeric;
AlphaNumericDefinition := AlphaNumeric + '["&"]';
end;
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)