diff options
author | Ben Pfaff <blp@nicira.com> | 2015-04-02 22:36:31 -0700 |
---|---|---|
committer | Ben Pfaff <blp@nicira.com> | 2015-04-20 14:02:48 -0700 |
commit | 3b6267714bd4a426cbd4d8da1d8328f5f6760446 (patch) | |
tree | ab1d1f06bd8b1d4dee96b9413f484ec5e6a60601 | |
parent | f4471a05421e69a3be621b26176bf20b1f7e627e (diff) | |
download | openvswitch-3b6267714bd4a426cbd4d8da1d8328f5f6760446.tar.gz |
json: New function json_string_escape().
This saves some cut-and-paste duplicated code elsewhere and will have
additional users in upcoming commits.
Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Russell Bryant <rbryant@redhat.com>
-rw-r--r-- | lib/json.c | 12 | ||||
-rw-r--r-- | lib/json.h | 3 | ||||
-rw-r--r-- | ovn/lib/expr.c | 12 | ||||
-rw-r--r-- | ovn/lib/lex.c | 16 |
4 files changed, 16 insertions, 27 deletions
diff --git a/lib/json.c b/lib/json.c index f004771e1..11cf03846 100644 --- a/lib/json.c +++ b/lib/json.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010, 2011, 2012, 2014 Nicira, Inc. + * Copyright (c) 2009, 2010, 2011, 2012, 2014, 2015 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -879,6 +879,16 @@ exit: return ok; } +void +json_string_escape(const char *in, struct ds *out) +{ + struct json json = { + .type = JSON_STRING, + .u.string = CONST_CAST(char *, in), + }; + json_to_ds(&json, 0, out); +} + static void json_parser_input_string(struct json_parser *p, const char *s) { diff --git a/lib/json.h b/lib/json.h index cfe9457ef..3497035df 100644 --- a/lib/json.h +++ b/lib/json.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010 Nicira, Inc. + * Copyright (c) 2009, 2010, 2015 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -131,6 +131,7 @@ void json_to_ds(const struct json *, int flags, struct ds *); /* JSON string formatting operations. */ bool json_string_unescape(const char *in, size_t in_len, char **outp); +void json_string_escape(const char *in, struct ds *out); #ifdef __cplusplus } diff --git a/ovn/lib/expr.c b/ovn/lib/expr.c index 507f34b92..fb9b1cfe0 100644 --- a/ovn/lib/expr.c +++ b/ovn/lib/expr.c @@ -283,16 +283,6 @@ find_bitwise_range(const union mf_subvalue *sv, int width, } static void -expr_format_string(const char *s, struct ds *ds) -{ - struct json json = { - .type = JSON_STRING, - .u.string = CONST_CAST(char *, s), - }; - json_to_ds(&json, 0, ds); -} - -static void expr_format_cmp(const struct expr *e, struct ds *s) { /* The common case is numerical comparisons. @@ -300,7 +290,7 @@ expr_format_cmp(const struct expr *e, struct ds *s) if (!e->cmp.symbol->width) { ds_put_format(s, "%s %s ", e->cmp.symbol->name, expr_relop_to_string(e->cmp.relop)); - expr_format_string(e->cmp.string, s); + json_string_escape(e->cmp.string, s); return; } diff --git a/ovn/lib/lex.c b/ovn/lib/lex.c index 96aa35977..824c0bca7 100644 --- a/ovn/lib/lex.c +++ b/ovn/lib/lex.c @@ -128,16 +128,6 @@ lex_token_format_masked_integer(const struct lex_token *token, struct ds *s) } } -static void -lex_token_format_string(const char *s, struct ds *ds) -{ - struct json json = { - .type = JSON_STRING, - .u.string = CONST_CAST(char *, s), - }; - json_to_ds(&json, 0, ds); -} - /* Appends a string representation of 'token' to 's', in a format that can be * losslessly parsed back by the lexer. (LEX_T_END and LEX_T_ERROR can't be * parsed back.) */ @@ -155,14 +145,12 @@ lex_token_format(struct lex_token *token, struct ds *s) case LEX_T_ERROR: ds_put_cstr(s, "error("); - lex_token_format_string(token->s, s); + json_string_escape(token->s, s); ds_put_char(s, ')'); break; case LEX_T_STRING: - lex_token_format_string(token->s, s); - break; - + json_string_escape(token->s, s); break; case LEX_T_INTEGER: |