Permalink
Browse files

Optimise JSON unescaping by working with code units not code points.

  • Loading branch information...
wangp committed Mar 26, 2012
1 parent c4d93cc commit d8de8e3095f9452bf9f5b1028512fe59fbdbf3a5
Showing with 13 additions and 9 deletions.
  1. +13 −9 json.m
View
22 json.m
@@ -137,16 +137,20 @@
pieces::in, pieces::out) is det.
unescape_loop(S, AnchorPos, Pos0, !RevPieces) :-
- ( string.unsafe_index_next(S, Pos0, Pos1, C0) ->
- ( C0 = ('\\') ->
- add_substring_piece(S, AnchorPos, Pos0, !RevPieces),
- unescape_sequence(S, Pos1, Pos2, !RevPieces),
- unescape_loop(S, Pos2, Pos2, !RevPieces)
- ;
- unescape_loop(S, AnchorPos, Pos1, !RevPieces)
- )
- ;
+ % This relies on unsafe_index_code_unit returning 0 when indexing the NUL
+ % terminator at the end of the string; officially unspecified behaviour
+ % but unlikely to change.
+ string.unsafe_index_code_unit(S, Pos0, C0),
+ ( C0 = 0 ->
add_substring_piece(S, AnchorPos, Pos0, !RevPieces)
+ ; C0 = char.to_int('\\') ->
+ Pos1 = Pos0 + 1,
+ add_substring_piece(S, AnchorPos, Pos0, !RevPieces),
+ unescape_sequence(S, Pos1, Pos2, !RevPieces),
+ unescape_loop(S, Pos2, Pos2, !RevPieces)
+ ;
+ Pos1 = Pos0 + 1,
+ unescape_loop(S, AnchorPos, Pos1, !RevPieces)
).
:- pred add_substring_piece(string::in, int::in, int::in,

0 comments on commit d8de8e3

Please sign in to comment.