json-gen-c  0.1.5
json-gen-c generate C code for json manipulation
sstr.c File Reference

Implementation of the sstr.h header file. More...

#include "sstr.h"
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
Include dependency graph for sstr.c:

Macros

#define JGENC_MALLOC(sz)   malloc(sz)
 
#define JGENC_REALLOC(p, sz)   realloc((p), (sz))
 
#define JGENC_FREE(p)   free(p)
 
#define STR   struct sstr_s
 
#define SSTR(s)   ((STR*)(s))
 
#define STR_PTR(s)
 
#define SSTR_INT32_LEN   (sizeof("-2147483648") - 1)
 
#define SSTR_INT64_LEN   (sizeof("-9223372036854775808") - 1)
 
#define SSTR_MAX_UINT32_VALUE   (uint32_t)0xffffffff
 
#define SSTR_MAX_INT32_VALUE   (uint32_t)0x7fffffff
 

Functions

sstr_t sstr_new ()
 Create an empty sstr_t.
 
void sstr_free (sstr_t s)
 delete a sstr_t.
 
sstr_t sstr_of (const void *data, size_t length)
 Create a sstr_t from data with length bytes.
 
sstr_t sstr_ref (const void *data, size_t length)
 Create a sstr_t from data with length bytes. The data is not copied, but have a pointer to data.
 
sstr_t sstr (const char *cstr)
 Create a sstr_t from C-style (NULL-terminated) string str.
 
char * sstr_cstr (sstr_t s)
 Return C-style string representation of s.
 
int sstr_compare (sstr_t a, sstr_t b)
 Compare a and b return 0 if equal, <0 if a < b, >0 if a > b.
 
int sstr_compare_c (sstr_t a, const char *b)
 compare sstr_t a and c-style string b
 
void sstr_append_zero (sstr_t s, size_t length)
 Extends the sstr_t by appending additional '\0' characters at the end of its current value.
 
void sstr_append_of (sstr_t s, const void *data, size_t length)
 Extends the sstr_t by appending additional characters in data with length of length at the end of its current value .
 
void sstr_append (sstr_t dst, sstr_t src)
 Extends the sstr_t by appending additional characters contained in src.
 
void sstr_append_cstr (sstr_t dst, const char *src)
 Extends the sstr_t by appending additional characters contained in src.
 
sstr_t sstr_dup (sstr_t s)
 Duplicate s and return.
 
sstr_t sstr_substr (sstr_t s, size_t index, size_t len)
 Get substring of s starting at index with length bytes.
 
void sstr_clear (sstr_t s)
 clear the sstr_t. After this call, the sstr_t is empty.
 
sstr_t sstr_printf (const char *fmt,...)
 printf implement.
 
sstr_t sstr_printf_append (sstr_t buf, const char *fmt,...)
 Same as sstr_printf(), but but print to buf instead of create a new one.
 
sstr_t sstr_vslprintf (const char *fmt, va_list args)
 Printf implement.
 
sstr_t sstr_vslprintf_append (sstr_t buf, const char *fmt, va_list args)
 Same as sstr_vslprintf, but print to buf instead of create a new one.
 
const char * sstr_version ()
 return version string.
 
void sstr_append_int_str (sstr_t s, int i)
 Append the decimal string representation of an int to a string.
 
int sstr_parse_long (sstr_t s, long *v)
 Parse a string as a long integer.
 
int sstr_parse_int (sstr_t *s, int *v)
 Parse a string as an int, consuming the parsed portion.
 
void sstr_append_long_str (sstr_t s, long l)
 Append the decimal string representation of a long to a string.
 
void sstr_append_uint32_str (sstr_t s, uint32_t u)
 Append the decimal string representation of a uint32_t to a string.
 
void sstr_append_uint64_str (sstr_t s, uint64_t u)
 Append the decimal string representation of a uint64_t to a string.
 
void sstr_append_float_str (sstr_t s, float f, int precission)
 Append the string representation of a float to a string.
 
void sstr_append_double_str (sstr_t s, double f, int precision)
 Append the string representation of a double to a string.
 
int sstr_parse_double (sstr_t s, double *v)
 Parse a string as a double.
 
int sstr_json_escape_string_append (sstr_t out, sstr_t in)
 Escape a string for JSON output and append it to a buffer.
 
void sstr_append_of_if (sstr_t s, const void *data, size_t length, bool cond)
 Append if cond is true, otherwise do nothing.
 
void sstr_append_indent (sstr_t s, size_t indent)
 append spaces at the end of the sstr_t.
 

Detailed Description

Implementation of the sstr.h header file.

Macro Definition Documentation

◆ STR_PTR

#define STR_PTR (   s)
Value:
((SSTR(s))->type == SSTR_TYPE_SHORT \
? (SSTR(s))->un.short_str \
: (SSTR(s)->type == SSTR_TYPE_LONG ? (SSTR(s)->un.long_str.data) \
: (SSTR(s)->un.ref_str.data)))

Function Documentation

◆ sstr()

sstr_t sstr ( const char *  cstr)

Create a sstr_t from C-style (NULL-terminated) string str.

The cstr is copied to the new sstr_t, so you can free cstr after calling this function.

Parameters
cstrC-style string to copy to the result sstr_t.
Returns
sstr_t containing data copied from cstr.

◆ sstr_append()

void sstr_append ( sstr_t  dst,
sstr_t  src 
)

Extends the sstr_t by appending additional characters contained in src.

Parameters
dstdestination sstr_t.
srcsource sstr_t.

◆ sstr_append_cstr()

void sstr_append_cstr ( sstr_t  dst,
const char *  src 
)

Extends the sstr_t by appending additional characters contained in src.

Parameters
dstdestination sstr_t.
srcsource C-style string.

◆ sstr_append_double_str()

void sstr_append_double_str ( sstr_t  s,
double  f,
int  precision 
)

Append the string representation of a double to a string.

Parameters
sthe sstr_t to append to.
fthe double value to convert and append.
precisionnumber of decimal places to output.

◆ sstr_append_float_str()

void sstr_append_float_str ( sstr_t  s,
float  f,
int  precission 
)

Append the string representation of a float to a string.

Parameters
sthe sstr_t to append to.
fthe float value to convert and append.
precissionnumber of decimal places to output.

◆ sstr_append_indent()

void sstr_append_indent ( sstr_t  s,
size_t  indent 
)

append spaces at the end of the sstr_t.

Parameters
sthe sstr_t to append spaces to.
indentnumbers of spaces to append.

◆ sstr_append_int_str()

void sstr_append_int_str ( sstr_t  s,
int  i 
)

Append the decimal string representation of an int to a string.

Parameters
sthe sstr_t to append to.
ithe integer value to convert and append.

◆ sstr_append_long_str()

void sstr_append_long_str ( sstr_t  s,
long  l 
)

Append the decimal string representation of a long to a string.

Parameters
sthe sstr_t to append to.
lthe long value to convert and append.

◆ sstr_append_of()

void sstr_append_of ( sstr_t  s,
const void *  data,
size_t  length 
)

Extends the sstr_t by appending additional characters in data with length of length at the end of its current value .

Parameters
sdestination sstr_t.
datadata to append.
lengthlength of data.

◆ sstr_append_of_if()

void sstr_append_of_if ( sstr_t  s,
const void *  data,
size_t  length,
bool  cond 
)

Append if cond is true, otherwise do nothing.

Parameters
sthe sstr_t to append to.
datadata to append.
lengthlength of data.
condcondition

◆ sstr_append_uint32_str()

void sstr_append_uint32_str ( sstr_t  s,
uint32_t  u 
)

Append the decimal string representation of a uint32_t to a string.

Parameters
sthe sstr_t to append to.
uthe uint32_t value to convert and append.

◆ sstr_append_uint64_str()

void sstr_append_uint64_str ( sstr_t  s,
uint64_t  u 
)

Append the decimal string representation of a uint64_t to a string.

Parameters
sthe sstr_t to append to.
uthe uint64_t value to convert and append.

◆ sstr_append_zero()

void sstr_append_zero ( sstr_t  s,
size_t  length 
)

Extends the sstr_t by appending additional '\0' characters at the end of its current value.

Parameters
sdestination sstr_t.
lengthlength of '\0' to append.

◆ sstr_clear()

void sstr_clear ( sstr_t  s)

clear the sstr_t. After this call, the sstr_t is empty.

Parameters
ssstr_t instance to clear.

◆ sstr_compare()

int sstr_compare ( sstr_t  a,
sstr_t  b 
)

Compare a and b return 0 if equal, <0 if a < b, >0 if a > b.

Parameters
asstr_t to be compared.
bsstr_t to be compared to.
Returns
int the compare result.
0 They compare equal.
<0 Either the value of the first character that does not match is lower in the compared string, or all compared characters match but the compared string is shorter.
>0 Either the value of the first character that does not match is greater in the compared string, or all compared characters match but the compared string is longer.
Note
This function is case sensitive.

◆ sstr_compare_c()

int sstr_compare_c ( sstr_t  a,
const char *  b 
)

compare sstr_t a and c-style string b

just like sstr_compare, but compare a and c-style string b.

Returns
int

◆ sstr_cstr()

char * sstr_cstr ( sstr_t  s)

Return C-style string representation of s.

This function return a pointer to the internal C-style string, it has a null-terminal character at the end. So you can use it as a C-style string. The returned pointer is valid until sstr_free()/sstr_append()/sstr_append_of() or any functions that may modify the contents of sstr_t is called.

Parameters
ssstr_t instance to convert to C-style string.
Returns
char* C-style string representation of s.
Note
The returned string is reused by s, do not free it yourself.

◆ sstr_dup()

sstr_t sstr_dup ( sstr_t  s)

Duplicate s and return.

Parameters
ssstr_t to duplicate.
Returns
sstr_t duplicate of s.

◆ sstr_free()

void sstr_free ( sstr_t  s)

delete a sstr_t.

Parameters
ssstr_t instance to delete.

◆ sstr_json_escape_string_append()

int sstr_json_escape_string_append ( sstr_t  out,
sstr_t  in 
)

Escape a string for JSON output and append it to a buffer.

Converts special characters (\, ", newlines, tabs, etc.) to their JSON escape sequences and appends the result to out.

Parameters
outthe output sstr_t to append the escaped string to.
inthe input sstr_t containing the raw string to escape.
Returns
0 on success, non-zero on error.

◆ sstr_new()

sstr_t sstr_new ( void  )

Create an empty sstr_t.

Returns
sstr_t

◆ sstr_of()

sstr_t sstr_of ( const void *  data,
size_t  length 
)

Create a sstr_t from data with length bytes.

The data is copied to the new sstr_t, so you can free data after calling this function.

Parameters
datadata to copy to the result sstr_t.
lengthlength of data.
Returns
sstr_t containing data copied from data.

◆ sstr_parse_double()

int sstr_parse_double ( sstr_t  s,
double *  v 
)

Parse a string as a double.

Parameters
sthe sstr_t to parse.
vpointer to receive the parsed value.
Returns
0 on success, non-zero on parse error.

◆ sstr_parse_int()

int sstr_parse_int ( sstr_t s,
int *  v 
)

Parse a string as an int, consuming the parsed portion.

Parameters
spointer to the sstr_t to parse (advanced past consumed input).
vpointer to receive the parsed value.
Returns
0 on success, non-zero on parse error or overflow.

◆ sstr_parse_long()

int sstr_parse_long ( sstr_t  s,
long *  v 
)

Parse a string as a long integer.

Parameters
sthe sstr_t to parse.
vpointer to receive the parsed value.
Returns
0 on success, non-zero on parse error or overflow.

◆ sstr_printf()

sstr_t sstr_printf ( const char *  fmt,
  ... 
)

printf implement.

Parameters
fmtformat, like C printf()
...arguments, like C printf()
Returns
sstr_t result string.

◆ sstr_printf_append()

sstr_t sstr_printf_append ( sstr_t  buf,
const char *  fmt,
  ... 
)

Same as sstr_printf(), but but print to buf instead of create a new one.

Parameters
bufbuffer to print to.
fmtformat string.
...arguments.
Returns
sstr_t the result string.

◆ sstr_ref()

sstr_t sstr_ref ( const void *  data,
size_t  length 
)

Create a sstr_t from data with length bytes. The data is not copied, but have a pointer to data.

Parameters
datadata of the result sstr_t.
lengthlength of data.
Returns
sstr_t
Note
The result sstr_t does not own data, but have a pointer to data. It is a reference, not a copy.
You cannot append a sstr_ref() result.

◆ sstr_substr()

sstr_t sstr_substr ( sstr_t  s,
size_t  index,
size_t  len 
)

Get substring of s starting at index with length bytes.

Parameters
ssstr_t instance to get substring of.
indexindex of the first byte of the substring.
lennumber of bytes of the substring.
Returns
sstr_t substring of s. if index is out of range, return an empty string.

◆ sstr_version()

const char * sstr_version ( void  )

return version string.

Returns
const char* static version string.

◆ sstr_vslprintf()

sstr_t sstr_vslprintf ( const char *  fmt,
va_list  args 
)

Printf implement.

supported formats:

  • %[0][width]T time_t
  • %[0][width][u][x|X]z ssize_t/size_t
  • %[0][width][u][x|X]d int/u_int
  • %[0][width][u][x|X]l long
  • %[0][width][u][x|X]D int32_t/uint32_t
  • %[0][width][u][x|X]L int64_t/uint64_t
  • %[0][width][.width]f double, max valid number fits to %18.15f
  • p void *
  • %[x|X]S sstr_t, if x, print as hexadecimal
  • s null-terminated string
  • %*s length and string
  • Z '\0'
  • N '
    '
  • c char
  • %% %

reserved:

  • C wchar

if u/x/X, tailing d can be ignore

◆ sstr_vslprintf_append()

sstr_t sstr_vslprintf_append ( sstr_t  buf,
const char *  fmt,
va_list  args 
)

Same as sstr_vslprintf, but print to buf instead of create a new one.

Parameters
bufresult sstr_t to print to.
fmtformat string.
argsarguments.
Returns
sstr_t the result string.