今天在CSDN回答网友问题,实际上就是JS字符串的解码.给他写了几行简单的代码,转义符也没全部处理.
闲着没事手痒痒,就把JS字符串的编解码都写出来,转义符也全部处理了.说不定以后用得上.
参考的Json.org上的编码规范.
function EncodeJSStr(const value: Widestring): Widestring; var P: PWideChar; begin Result := ''; P := PWideChar(value); while P^ <> #0 do begin case P^ of '"', '\', '/': Result := Result + '\' + P^; #$08: Result := Result + '\b'; #$0C: Result := Result + '\f'; #$0A: Result := Result + '\n'; #$0D: Result := Result + '\r'; #$09: Result := Result + '\t'; else if WORD(P^) > $FF then Result := Result + LowerCase(Format('\u%x', [WORD(P^)])) else Result := Result + P^; end; inc(P); end; end; function DecodeJSStr(const value: Widestring): Widestring; var P: PWideChar; v: WideChar; tmp: Widestring; begin Result := ''; P := PWideChar(value); while P^ <> #0 do begin v := #0; case P^ of '\': begin inc(P); case P^ of '"', '\', '/': v := P^; 'b': v := #$08; 'f': v := #$0C; 'n': v := #$0A; 'r': v := #$0D; 't': v := #$09; 'u': begin tmp := Copy(P, 2, 4); v := WideChar(StrToInt('$' + tmp)); inc(P, 4); end; end; end; else v := P^; end; Result := Result + v; inc(P); end; end;
You put the lime in the coconut and drink the atrcile up.
谢谢.