You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by kx...@apache.org on 2015/07/29 00:42:59 UTC

[34/54] jiffy commit: updated refs/heads/master to ef77de4

Add an option to escape forward slashes

This brings back escaping forward slashes as an option during encoding.
Default is still not to escape.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-jiffy/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-jiffy/commit/1784ca2a
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-jiffy/tree/1784ca2a
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-jiffy/diff/1784ca2a

Branch: refs/heads/master
Commit: 1784ca2ab567a3b52b8916565bb41db716c2b9ca
Parents: cd1b263
Author: Jeremie Lasalle Ratelle <je...@gmail.com>
Authored: Wed May 20 18:20:17 2015 -0400
Committer: Jeremie Lasalle Ratelle <je...@gmail.com>
Committed: Wed Jul 15 13:56:51 2015 -0400

----------------------------------------------------------------------
 c_src/encoder.c                | 17 +++++++++++++++++
 c_src/jiffy.c                  |  1 +
 c_src/jiffy.h                  |  1 +
 test/jiffy_04_string_tests.erl | 14 +++++++++++++-
 4 files changed, 32 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-jiffy/blob/1784ca2a/c_src/encoder.c
----------------------------------------------------------------------
diff --git a/c_src/encoder.c b/c_src/encoder.c
index 471cfcc..59b1b52 100644
--- a/c_src/encoder.c
+++ b/c_src/encoder.c
@@ -34,6 +34,7 @@ typedef struct {
     int             uescape;
     int             pretty;
     int             use_nil;
+    int             escape_forward_slashes;
 
     int             shiftcnt;
     int             count;
@@ -77,6 +78,7 @@ enc_new(ErlNifEnv* env)
     e->uescape = 0;
     e->pretty = 0;
     e->use_nil = 0;
+    e->escape_forward_slashes = 0;
     e->shiftcnt = 0;
     e->count = 0;
 
@@ -281,6 +283,12 @@ enc_string(Encoder* e, ERL_NIF_TERM val)
                 esc_extra += 1;
                 i++;
                 continue;
+            case '/':
+                if(e->escape_forward_slashes) {
+                    esc_extra += 1;
+                    i++;
+                    continue;
+                }
             default:
                 if(data[i] < 0x20) {
                     esc_extra += 5;
@@ -348,6 +356,13 @@ enc_string(Encoder* e, ERL_NIF_TERM val)
                 e->p[e->i++] = 't';
                 i++;
                 continue;
+            case '/':
+                if(e->escape_forward_slashes) {
+                    e->p[e->i++] = '\\';
+                    e->u[e->i++] = data[i];
+                    i++;
+                    continue;
+                }
             default:
                 if(data[i] < 0x20) {
                     ulen = unicode_uescape(data[i], &(e->p[e->i]));
@@ -591,6 +606,8 @@ encode_init(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
             e->uescape = 1;
         } else if(enif_compare(val, e->atoms->atom_pretty) == 0) {
             e->pretty = 1;
+        } else if(enif_compare(val, e->atoms->atom_escape_forward_slashes) == 0) {
+            e->escape_forward_slashes = 1;
         } else if(enif_compare(val, e->atoms->atom_use_nil) == 0) {
             e->use_nil = 1;
         } else if(enif_compare(val, e->atoms->atom_force_utf8) == 0) {

http://git-wip-us.apache.org/repos/asf/couchdb-jiffy/blob/1784ca2a/c_src/jiffy.c
----------------------------------------------------------------------
diff --git a/c_src/jiffy.c b/c_src/jiffy.c
index c18a111..9048f5e 100644
--- a/c_src/jiffy.c
+++ b/c_src/jiffy.c
@@ -29,6 +29,7 @@ load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info)
     st->atom_nil = make_atom(env, "nil");
     st->atom_use_nil = make_atom(env, "use_nil");
     st->atom_null_term = make_atom(env, "null_term");
+    st->atom_escape_forward_slashes = make_atom(env, "escape_forward_slashes");
 
     // Markers used in encoding
     st->ref_object = make_atom(env, "$object_ref$");

http://git-wip-us.apache.org/repos/asf/couchdb-jiffy/blob/1784ca2a/c_src/jiffy.h
----------------------------------------------------------------------
diff --git a/c_src/jiffy.h b/c_src/jiffy.h
index ecad9da..7a2b7d5 100644
--- a/c_src/jiffy.h
+++ b/c_src/jiffy.h
@@ -31,6 +31,7 @@ typedef struct {
     ERL_NIF_TERM    atom_nil;
     ERL_NIF_TERM    atom_use_nil;
     ERL_NIF_TERM    atom_null_term;
+    ERL_NIF_TERM    atom_escape_forward_slashes;
 
     ERL_NIF_TERM    ref_object;
     ERL_NIF_TERM    ref_array;

http://git-wip-us.apache.org/repos/asf/couchdb-jiffy/blob/1784ca2a/test/jiffy_04_string_tests.erl
----------------------------------------------------------------------
diff --git a/test/jiffy_04_string_tests.erl b/test/jiffy_04_string_tests.erl
index f2136d8..0d642d5 100644
--- a/test/jiffy_04_string_tests.erl
+++ b/test/jiffy_04_string_tests.erl
@@ -23,6 +23,8 @@ string_error_test_() ->
 string_utf8_test_() ->
     [gen(utf8, Case) || Case <- cases(utf8)].
 
+string_escaped_slashes_test_() ->
+    [gen(escaped_slashes, Case) || Case <- cases(escaped_slashes)].
 
 gen(ok, {J, E}) ->
     gen(ok, {J, E, J});
@@ -50,8 +52,13 @@ gen(utf8, {Case, Fixed}) ->
         ?_assertThrow({error, {invalid_string, _}}, jiffy:encode(Case)),
         ?_assertEqual(Fixed2, jiffy:encode(Case, [force_utf8])),
         ?_assertThrow({error, {_, invalid_string}}, jiffy:decode(Case2))
-    ]}.
+    ]};
 
+gen(escaped_slashes, {J, E}) ->
+    {msg("escaped_slashes - ~s", [J]), [
+        {"Decode", ?_assertEqual(E, dec(J))},
+        {"Encode", ?_assertEqual(J, enc(E, [escape_forward_slashes]))}
+    ]}.
 
 cases(ok) ->
     [
@@ -143,4 +150,9 @@ cases(utf8) ->
         {<<16#F8, 16#84, 16#80, 16#80, 16#80>>, <<16#EF, 16#BF, 16#BD>>},
         {<<16#FC, 16#80, 16#80, 16#80, 16#80, 16#80>>, <<16#EF, 16#BF, 16#BD>>},
         {<<16#FC, 16#82, 16#80, 16#80, 16#80, 16#80>>, <<16#EF, 16#BF, 16#BD>>}
+    ];
+
+cases(escaped_slashes) ->
+    [
+        {<<"\"\\/\"">>, <<"/">>}
     ].