1. Pascal / Говнокод #22991

    −1414

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    function Unescape(const s: AnsiString): widestring;
    var
      i: Integer;
      j: Integer;
      c: Integer;
    begin
      // Make result at least large enough. This prevents too many reallocs
      SetLength(Result, Length(s));
      i := 1;
      j := 1;
      while i <= Length(s) do begin
        if s[i] = '\' then begin
          if i < Length(s) then begin
            // escaped backslash?
            if s[i + 1] = '\' then begin
              Result[j] := '\';
              inc(i, 2);
            end
            // convert hex number to WideChar
            else if (s[i + 1] = 'u') and (i + 1 + 4 <= Length(s))
                    and TryStrToInt('$' + string(Copy(s, i + 2, 4)), c) then begin
              inc(i, 6);
              Result[j] := WideChar(c);
            end else begin
              raise Exception.CreateFmt('Invalid code at position %d', [i]);
            end;
          end else begin
            raise Exception.Create('Unexpected end of string');
          end;
        end else begin
          Result[j] := WideChar(s[i]);
          inc(i);
        end;
        inc(j);
      end;
    
      // Trim result in case we reserved too much space
      SetLength(Result, j - 1);
    end;

    Это не вирус. Просто в Delphi 7 не завезли JSon.

    Запостил: doctor_stertor, 07 Мая 2017

    Комментарии (57) RSS

    Добавить комментарий