Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
ccan
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mirror
ccan
Commits
3ceb24bf
Commit
3ceb24bf
authored
May 21, 2019
by
Rusty Russell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
json_escape: add fast-path for when we don't need to escape.
Signed-off-by:
Rusty Russell
<
rusty@rustcorp.com.au
>
parent
45f9ce17
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
1 deletion
+59
-1
ccan/json_escape/json_escape.c
ccan/json_escape/json_escape.c
+22
-0
ccan/json_escape/json_escape.h
ccan/json_escape/json_escape.h
+2
-1
ccan/json_escape/test/run-take.c
ccan/json_escape/test/run-take.c
+35
-0
No files found.
ccan/json_escape/json_escape.c
View file @
3ceb24bf
...
@@ -19,6 +19,18 @@ bool json_escape_eq(const struct json_escape *a, const struct json_escape *b)
...
@@ -19,6 +19,18 @@ bool json_escape_eq(const struct json_escape *a, const struct json_escape *b)
return
streq
(
a
->
s
,
b
->
s
);
return
streq
(
a
->
s
,
b
->
s
);
}
}
bool
json_escape_needed
(
const
char
*
str
,
size_t
len
)
{
for
(
size_t
i
=
0
;
i
<
len
;
i
++
)
{
if
((
unsigned
)
str
[
i
]
<
' '
||
str
[
i
]
==
127
||
str
[
i
]
==
'"'
||
str
[
i
]
==
'\\'
)
return
true
;
}
return
false
;
}
static
struct
json_escape
*
escape
(
const
tal_t
*
ctx
,
static
struct
json_escape
*
escape
(
const
tal_t
*
ctx
,
const
char
*
str
TAKES
,
const
char
*
str
TAKES
,
size_t
len
,
size_t
len
,
...
@@ -27,6 +39,16 @@ static struct json_escape *escape(const tal_t *ctx,
...
@@ -27,6 +39,16 @@ static struct json_escape *escape(const tal_t *ctx,
struct
json_escape
*
esc
;
struct
json_escape
*
esc
;
size_t
i
,
n
;
size_t
i
,
n
;
/* Fast path: can steal, and nothing to escape. */
if
(
is_taken
(
str
)
&&
tal_count
(
str
)
>
len
&&
!
json_escape_needed
(
str
,
len
))
{
taken
(
str
);
esc
=
(
struct
json_escape
*
)
tal_steal
(
ctx
,
str
);
esc
->
s
[
len
]
=
'\0'
;
return
esc
;
}
/* Worst case: all \uXXXX */
/* Worst case: all \uXXXX */
esc
=
(
struct
json_escape
*
)
tal_arr
(
ctx
,
char
,
len
*
6
+
1
);
esc
=
(
struct
json_escape
*
)
tal_arr
(
ctx
,
char
,
len
*
6
+
1
);
...
...
ccan/json_escape/json_escape.h
View file @
3ceb24bf
...
@@ -27,7 +27,8 @@ struct json_escape *json_escape_len(const tal_t *ctx,
...
@@ -27,7 +27,8 @@ struct json_escape *json_escape_len(const tal_t *ctx,
struct
json_escape
*
json_partial_escape
(
const
tal_t
*
ctx
,
struct
json_escape
*
json_partial_escape
(
const
tal_t
*
ctx
,
const
char
*
str
TAKES
);
const
char
*
str
TAKES
);
/* Extract a JSON-escaped string. */
/* Do we need to escape this str? */
bool
json_escape_needed
(
const
char
*
str
,
size_t
len
);
/* Are two escape json strings identical? */
/* Are two escape json strings identical? */
bool
json_escape_eq
(
const
struct
json_escape
*
a
,
bool
json_escape_eq
(
const
struct
json_escape
*
a
,
...
...
ccan/json_escape/test/run-take.c
0 → 100644
View file @
3ceb24bf
#include <ccan/json_escape/json_escape.h>
/* Include the C files directly. */
#include <ccan/json_escape/json_escape.c>
#include <ccan/tap/tap.h>
int
main
(
void
)
{
const
tal_t
*
ctx
=
tal
(
NULL
,
char
);
struct
json_escape
*
e
;
char
*
p
;
/* This is how many tests you plan to run */
plan_tests
(
5
);
/* This should simply be tal_steal */
p
=
tal_dup_arr
(
NULL
,
char
,
"Hello"
,
6
,
0
);
e
=
json_escape
(
ctx
,
take
(
p
));
ok1
(
!
strcmp
(
e
->
s
,
"Hello"
));
ok1
((
void
*
)
e
==
(
void
*
)
p
);
ok1
(
tal_parent
(
e
)
==
ctx
);
/* This can't be tal_steal, but still should be freed. */
p
=
tal_dup_arr
(
NULL
,
char
,
"
\\\b\f\n\r\t\"
"
"
\\\\\\
b
\\
f
\\
n
\\
r
\\
t
\\\"
"
,
22
,
0
);
e
=
json_escape
(
ctx
,
take
(
p
));
ok1
(
tal_parent
(
e
)
==
ctx
);
ok1
(
!
strcmp
(
e
->
s
,
"
\\\\\\
b
\\
f
\\
n
\\
r
\\
t
\\\"
"
"
\\\\\\\\\\\\
b
\\\\
f
\\\\
n
\\\\
r
\\\\
t
\\\\\\\"
"
));
tal_free
(
ctx
);
/* This exits depending on whether all tests passed */
return
exit_status
();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment