Registriert seit: 10. Jun 2003
Ort: Berlin
9.351 Beiträge
Delphi 11 Alexandria
|
Re: Prüfen, ob String eine gültige IPv6-Adresse ist
8. Mär 2009, 19:01
Ach, :: kann auch nur ein Block mit 0 sein.
Ok, dann so, sind nur zwei Zahlen anders, aber ich poste es mal trotzdem komplett nochmal:
Delphi-Quellcode:
// Autor: Sebastian Jänicke (jaenicke @ delphipraxis.net)
// [url]http://www.delphipraxis.net/post1011159.html#1011273[/url]
function SJCheckIPv6(Value: String): Boolean;
type
CheckIPState = (cisNone, cisDelim, cisDelimStart, cisDoubleDelim,
cisHex1, cisHex2, cisHex3, cisHex4);
var
DoubleDelim: Boolean;
i, CurBlock: Integer;
CurState: CheckIPState;
begin
Result := False;
DoubleDelim := False;
CurState := cisNone;
CurBlock := 0;
for i := 1 to Length(Value) do
case Value[i] of
' a'..' f', ' 0'..' 9':
case CurState of
cisNone, cisDelim:
begin
CurState := cisHex1;
Inc(CurBlock);
if CurBlock > 8 then
Exit; // mehr als 8 Blöcke geht nicht
end;
cisDelimStart:
Exit; // ein einzelnes : am Anfang geht nicht
cisDoubleDelim:
begin
CurState := cisHex1;
Inc(CurBlock, 2);
if CurBlock > 8 then
Exit; // :: steht für mind. 1 Block, mehr als 8 geht nicht
DoubleDelim := True;
end;
cisHex1:
CurState := cisHex2;
cisHex2:
CurState := cisHex3;
cisHex3:
CurState := cisHex4;
cisHex4:
Exit; // Mehr als 4 hexadezimale Zeichen hintereinander geht nicht
end;
' :':
case CurState of
cisNone:
CurState := cisDelimStart;
cisDelim:
if DoubleDelim or (CurBlock > 7) then
Exit // zweimal :: geht nicht,
// außerdem steht :: für mind. 1 Block, mehr als 8 geht nicht
else
CurState := cisDoubleDelim;
cisDelimStart:
CurState := cisDoubleDelim;
cisDoubleDelim:
Exit; // drittes : hintereinander ist nicht erlaubt
cisHex1, cisHex2, cisHex3, cisHex4:
CurState := cisDelim;
end;
else
Exit; // ungültiges Zeichen
end;
Result := (CurState <> cisDelim) and ((CurBlock = 8) or DoubleDelim);
end;
// EDIT:
Kommentare aktualisiert
// EDIT2:
Jedenfalls siehst du, dass das auf diese Weise nicht nur schnell geschrieben war (vorhin 10 Minuten oder so), sondern auch sehr einfach angepasst werden kann.
Sebastian Jänicke Alle eigenen Projekte sind eingestellt, ebenso meine Homepage, Downloadlinks usw. im Forum bleiben aktiv!
|
|
Zitat
|