Skip to content
This repository

string length feature #80

Open
wants to merge 1 commit into from

2 participants

ari edelkind Lloyd Hilaiel
ari edelkind
- NOTE: any references to yajl_val->u.string must be changed to
  yajl_val->u.string.s in existing code (should be minimal)

- use YAJL_GET_STRINGLEN(v) macro to get (size_t)length

This allows one to use the macros for strings with embedded binary 0s.

ari edelkind Support for tracking and getting the string len
    - NOTE: any references to yajl_val->u.string must be changed to
      yajl_val->u.string.s

    - use YAJL_GET_STRINGLEN(v) macro to get (size_t)length
3f9cedc
Lloyd Hilaiel
Owner

I like this change, but it's API breaking. it would require a 3.x version of yajl.

ari edelkind

It's been many months since I looked at this, but IIRC, I was under the impression that the API was intended to be accessed by the macros, and the underlying yajl_val structure was to be considered opaque. Assuming that this is correct, it shouldn't break the API at all, and it also shouldn't break binary compatibility: since u.string.s is first in the yajl_val structure, it will be equivalent to (char*)u.string.

If the yajl_val structure is intended to be API-accessible, then u.string can be kept as (char*), and another structure can be added to the union (e.g. u.stringx) to track length. Then, u.string will be equivalent to u.stringx.s, and existing code that uses yajl_val->u.string will not break.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jun 27, 2012
ari edelkind Support for tracking and getting the string len
    - NOTE: any references to yajl_val->u.string must be changed to
      yajl_val->u.string.s

    - use YAJL_GET_STRINGLEN(v) macro to get (size_t)length
3f9cedc
This page is out of date. Refresh to see the latest.

Showing 2 changed files with 14 additions and 9 deletions. Show diff stats Hide diff stats

  1. +6 2 src/api/yajl_tree.h
  2. +8 7 src/yajl_tree.c
8 src/api/yajl_tree.h
@@ -74,7 +74,10 @@ struct yajl_val_s
74 74 * members. */
75 75 union
76 76 {
77   - char * string;
  77 + struct {
  78 + char * s;
  79 + size_t len;
  80 + } string;
78 81 struct {
79 82 long long i; /*< integer value, if representable. */
80 83 double d; /*< double value, if representable. */
@@ -158,7 +161,8 @@ YAJL_API yajl_val yajl_tree_get(yajl_val parent, const char ** path, yajl_type t
158 161
159 162 /** Given a yajl_val_string return a ptr to the bare string it contains,
160 163 * or NULL if the value is not a string. */
161   -#define YAJL_GET_STRING(v) (YAJL_IS_STRING(v) ? (v)->u.string : NULL)
  164 +#define YAJL_GET_STRING(v) (YAJL_IS_STRING(v) ? (v)->u.string.s : NULL)
  165 +#define YAJL_GET_STRINGLEN(v) (YAJL_IS_STRING(v) ? (v)->u.string.len : 0)
162 166
163 167 /** Get the string representation of a number. You should check type first,
164 168 * perhaps using YAJL_IS_NUMBER */
15 src/yajl_tree.c
@@ -242,8 +242,8 @@ static int context_add_value (context_t *ctx, yajl_val v)
242 242 "Object key is not a string (%#04x)",
243 243 v->type);
244 244
245   - ctx->stack->key = v->u.string;
246   - v->u.string = NULL;
  245 + ctx->stack->key = v->u.string.s;
  246 + v->u.string.s = NULL;
247 247 free(v);
248 248 return (0);
249 249 }
@@ -277,14 +277,15 @@ static int handle_string (void *ctx,
277 277 if (v == NULL)
278 278 RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory");
279 279
280   - v->u.string = malloc (string_length + 1);
281   - if (v->u.string == NULL)
  280 + v->u.string.s = malloc (string_length + 1);
  281 + if (v->u.string.s == NULL)
282 282 {
283 283 free (v);
284 284 RETURN_ERROR ((context_t *) ctx, STATUS_ABORT, "Out of memory");
285 285 }
286   - memcpy(v->u.string, string, string_length);
287   - v->u.string[string_length] = 0;
  286 + memcpy(v->u.string.s, string, string_length);
  287 + v->u.string.s[string_length] = 0;
  288 + v->u.string.len = string_length;
288 289
289 290 return ((context_add_value (ctx, v) == 0) ? STATUS_CONTINUE : STATUS_ABORT);
290 291 }
@@ -480,7 +481,7 @@ void yajl_tree_free (yajl_val v)
480 481
481 482 if (YAJL_IS_STRING(v))
482 483 {
483   - free(v->u.string);
  484 + free(v->u.string.s);
484 485 free(v);
485 486 }
486 487 else if (YAJL_IS_NUMBER(v))

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.