Permalink
Browse files

add %S, with explicit string length, to pack and unpack

  • Loading branch information...
1 parent 335764e commit 9e8bf14d940ec6ec0ac5ef4441522f30867fd5ff Chip Salzenberg committed Apr 4, 2012
Showing with 26 additions and 7 deletions.
  1. +26 −7 src/pack_unpack.c
View
@@ -178,17 +178,23 @@ static json_t *pack(scanner_t *s, va_list *ap)
return pack_array(s, ap);
case 's': /* string */
+ case 'S': /* string and length */
{
const char *str = va_arg(*ap, const char *);
+ size_t len;
if(!str) {
set_error(s, "<args>", "NULL string argument");
return NULL;
}
- if(!utf8_check_string(str, -1)) {
+ if(s->token == 'S')
+ len = va_arg(*ap, size_t);
+ else
+ len = strlen(str);
+ if(!utf8_check_string(str, len)) {
set_error(s, "<args>", "Invalid UTF-8 string");
return NULL;
}
- return json_string_nocheck(str);
+ return json_string_nocheck_ex(str, len, 0);
}
case 'n': /* null */
@@ -401,23 +407,36 @@ static int unpack(scanner_t *s, json_t *root, va_list *ap)
return unpack_array(s, root, ap);
case 's':
+ case 'S':
if(root && !json_is_string(root)) {
set_error(s, "<validation>", "Expected string, got %s",
type_name(root));
return -1;
}
if(!(s->flags & JSON_VALIDATE_ONLY)) {
- const char **target;
+ const char **str_target;
+ size_t *len_target = NULL;
- target = va_arg(*ap, const char **);
- if(!target) {
+ str_target = va_arg(*ap, const char **);
+ if(!str_target) {
set_error(s, "<args>", "NULL string argument");
return -1;
}
- if(root)
- *target = json_string_value(root);
+ if(s->token == 'S') {
+ len_target = va_arg(*ap, size_t *);
+ if(!len_target) {
+ set_error(s, "<args>", "NULL string length argument");
+ return -1;
+ }
+ }
+
+ if(root) {
+ *str_target = json_string_value(root);
+ if(len_target)
+ *len_target = json_string_len(root);
+ }
}
return 0;

0 comments on commit 9e8bf14

Please sign in to comment.