Skip to content

Commit

Permalink
TS-513: Add JPrintPreallocated to note-c. (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenroche5 authored May 17, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 168490e commit f0d4c04
Showing 2 changed files with 13 additions and 1 deletion.
13 changes: 12 additions & 1 deletion n_cjson.c
Original file line number Diff line number Diff line change
@@ -1102,7 +1102,7 @@ N_CJSON_PUBLIC(char *) JPrintBuffered(const J *item, int prebuffer, Jbool fmt)
return (char*)p.buffer;
}

N_CJSON_PUBLIC(Jbool) JPrintPreallocated(J *item, char *buf, const int len, const Jbool fmt)
static Jbool printPreallocated(J *item, char *buf, const int len, const Jbool fmt, const Jbool omit)
{
printbuffer p = { 0, 0, 0, 0, 0, 0, 0 };

@@ -1118,10 +1118,21 @@ N_CJSON_PUBLIC(Jbool) JPrintPreallocated(J *item, char *buf, const int len, cons
p.offset = 0;
p.noalloc = true;
p.format = fmt;
p.omitempty = omit;

return print_value(item, &p);
}

N_CJSON_PUBLIC(Jbool) JPrintPreallocatedOmitEmpty(J *item, char *buf, const int len, const Jbool fmt)
{
return printPreallocated(item, buf, len, fmt, true);
}

N_CJSON_PUBLIC(Jbool) JPrintPreallocated(J *item, char *buf, const int len, const Jbool fmt)
{
return printPreallocated(item, buf, len, fmt, false);
}

/* Parser core - when encountering text, process appropriately. */
static Jbool parse_value(J * const item, parse_buffer * const input_buffer)
{
1 change: 1 addition & 0 deletions n_cjson.h
Original file line number Diff line number Diff line change
@@ -200,6 +200,7 @@ N_CJSON_PUBLIC(char *) JPrintBuffered(const J *item, int prebuffer, Jbool fmt);
/* Render a J entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
/* NOTE: J is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
N_CJSON_PUBLIC(Jbool) JPrintPreallocated(J *item, char *buffer, const int length, const Jbool format);
N_CJSON_PUBLIC(Jbool) JPrintPreallocatedOmitEmpty(J *item, char *buffer, const int length, const Jbool format);
/* Delete a J entity and all subentities. */
N_CJSON_PUBLIC(void) JDelete(J *c);

0 comments on commit f0d4c04

Please sign in to comment.