Einzelnen Beitrag anzeigen

Dejan Vu
(Gast)

n/a Beiträge
 
#13

AW: Codierung/Decodierung von Umlauten

  Alt 28. Okt 2014, 18:29
So würde ich das machen. Ich habe aber kein XE, insofern kann ich das nicht testen.
Delphi-Quellcode:
Function DecodeJScriptString(Const text : String) : String;
const
  Literals = '\/"''btnbfr';
  LiteralTranslations='\/"''#8#9#10#11#12#13';

Var
  i, p, length : Integer;
  c : Char;

  Function ByteToIso8859(b : Byte) : String;
  Begin
    result := TEncoding.Default.GetString(TArray<Byte>.Create(b));
  End;

  Function Unicode(w : Word) : String;
  Begin
    result := TEncoding.Unicode.GetString(TArray<Byte>.Create(Lo(w), Hi(w)));
  End;

  Function OctToInt(Const Value: string): integer;
  begin
    result := 0;
    for c in value do result := result * 8 + ord(c)-ord('0');
  end;

  Function HexToInt(Const Value: string): integer;
  begin
    result := StrToInt('$'+Value);
  end;

Begin
  i:=1;
  length := length(text);
  Result := '';
    while i<=l do begin
    if (text[i]<>'\') or (i=l) then
      result := result + text[i]
    else begin
      inc(i);
      c := Text[i];
      p := Pos(c,Literals);
      if p>0 then
        result := result + LiteralTranslations[p]
      else case c of
        '0'..'3' : begin
          result := result + ByteToIso8859(OctToInt(Copy(text,i,3)));
          inc(i,2);
          end;
        'x' : begin
          result := result + ByteToIso8859(HexToInt(Copy(text,i+1,2)));
          inc(i,2);
          end;
        'u' : begin
          result := result + Unicode(HexToInt(Copy(text,i+1,4)));
          inc(i,3);
          end;
      end
    end;
    inc(i);
  end;
end;
Ich will damit den Code von Sir Rufo nicht schmälern, aber das ist hier ja auch ein Programmiererforum, und da kann man ja eine andere/kürzere Möglichkeit aufzeigen. Ich persönlich finde zudem den Statemachine-Code schwer zu lesen und noch schwerer zu verstehen. Aber das ist ja Ansichtssache.
  Mit Zitat antworten Zitat