Skip to content

Commit 39c749d

Browse files
author
Gordon Guthrie
committed
Bug fix for unicode in riak-shell
Unicode in input was being handled correctly by the riak_shell lexer/parser (and by the SQL one) but not by the cmd pretty printer This is used in writing the history and the logs and it was borking on unicode - in particular smart quotes inserted into SQL on pasting from some editors. The regular expression clean up and output munging is now unicode friendly. riak_shell doesn't log errored commands so it is hard to regression test for this.
1 parent 287cc4c commit 39c749d

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/history_EXT.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ show_history(Cmd, #state{history = Hist} = S) ->
6262
Msg1 = "The history contains:\n",
6363
FormatFn = fun({N, Cmd1}) ->
6464
Cmd2 = riak_shell_util:pretty_pr_cmd(Cmd1),
65-
{N, io_lib:format("~s", [Cmd2])}
65+
{N, io_lib:format("~ts", [Cmd2])}
6666
end,
6767
Hist2 = [FormatFn(X) || X <- Hist],
6868
Msg2 = riak_shell_util:print_key_vals(lists:reverse(Hist2)),

src/riak_shell_util.erl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,13 @@ to_list(F) when is_float(F) -> mochinum:digits(F);
8686
to_list(L) when is_list(L) -> L.
8787

8888
pretty_pr_cmd(Cmd) ->
89-
Cmd2 = re:replace(Cmd, "\n", " ", [global, {return, list}]),
90-
Cmd3 = re:replace(Cmd2, "[ ]+", " ", [global, {return, list}]),
91-
_Cmd4 = re:replace(Cmd3, "^ ", "", [{return, list}]).
89+
%% complex list-to-binary unicode dance to make
90+
%% regexs work with unicode input
91+
CmdBin = unicode:characters_to_binary(Cmd),
92+
{ok, Regex1} = re:compile("(\n|[ ]+)", [unicode]),
93+
CmdBin2 = re:replace(CmdBin, Regex1, " ", [global, {return, binary}]),
94+
{ok, Regex2} = re:compile("^ ", [unicode]),
95+
_Cmd2 = re:replace(CmdBin2, Regex2, "", [{return, list}]).
9296

9397
datetime() ->
9498
{{Y, M, D}, {H, Mn, S}} = calendar:universal_time(),
@@ -106,3 +110,13 @@ datetime() ->
106110

107111
pad(X) when is_integer(X) ->
108112
io_lib:format("~2.10.0B", [X]).
113+
114+
-ifdef(TEST).
115+
-include_lib("eunit/include/eunit.hrl").
116+
117+
pretty_pr_with_unicode_test() ->
118+
%% 8217 is smart quotes
119+
Input = [8217, 65, 8217],
120+
?assertEqual(Input, pretty_pr_cmd(Input)).
121+
122+
-endif.

0 commit comments

Comments
 (0)