|
json-gen-c
0.1.5
json-gen-c generate C code for json manipulation
|
sstr_t are objects that represent sequences of characters. More...
#include <stdarg.h>#include <stddef.h>#include <stdbool.h>#include <stdint.h>

Go to the source code of this file.
Classes | |
| struct | sstr_s |
Macros | |
| #define | SHORT_STR_CAPACITY 25 |
| #define | CAP_ADD_DELTA 256 |
| #define | SSTR_TYPE_SHORT 0 |
| #define | SSTR_TYPE_LONG 1 |
| #define | SSTR_TYPE_REF 2 |
| #define | sstr_length(s) ((struct sstr_s*)s)->length |
| Return the length of s, in terms of bytes. | |
| #define | sstr_append_cstr_if(dst, src, cond) sstr_append_of_if(dst, src, strlen(src), cond) |
| Append C style string if cond is true, otherwise do nothing. | |
Typedefs | |
| typedef void * | sstr_t |
| sstr_t are objects that represent sequences of characters. | |
Functions | |
| sstr_t | sstr_new (void) |
| 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_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. | |
| 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. | |
| void | sstr_append_of_if (sstr_t s, const void *data, size_t length, bool cond) |
| Append if cond is true, otherwise do nothing. | |
| 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_indent (sstr_t s, size_t indent) |
| append spaces at the end of the sstr_t. | |
| const char * | sstr_version (void) |
| return version string. | |
Numeric conversion helpers | |
Convert between sstr_t and numeric types. | |
| 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. | |
sstr_t are objects that represent sequences of characters.
The standard C style string is a sequence of characters terminated by a null character, which is easy to cause buffer overflow. And it's annoying to pass pointer and length of string to every whare.
The sequence of characters just like a string in C++, inside the sstr_t struct, it also has a null character at the end, so that we can use sstr_t as a C style string.
sstr_t contains a pointer to char sequence and its length, solves the security problems of standard C string. With functions bundle with sstr_t, you can easily manipulate the string just like standard C string, but in a safer way:
sstr_t stotal = sstr_new();
sstr_t s1 = sstr("hello");
sstr_t s2 = sstr("world");
sstr_append(stotal, s1);
sstr_append_of(stotal, " ", 1);
sstr_append(stotal, s2);
sstr_free(s1);
sstr_free(s2);
sstr_t result = sstr_printf("stotal=%S, c-str=%s, int=%d, long=%ld",
stotal, stotal, 123, (long)456);
puts(sstr_cstr(result));
sstr_free(result);
sstr_free(stotal);
| #define sstr_append_cstr_if | ( | dst, | |
| src, | |||
| cond | |||
| ) | sstr_append_of_if(dst, src, strlen(src), cond) |
Append C style string if cond is true, otherwise do nothing.
| dst | destination sstr_t to append to. |
| src | source C-style string to append |
| cond | condition |
| #define sstr_length | ( | s | ) | ((struct sstr_s*)s)->length |
Return the length of s, in terms of bytes.
This is the number of actual bytes that conform the contents of the sstr_t, which is not necessarily equal to its storage capacity.
Note that sstr_t objects handle bytes without knowledge of the encoding that may eventually be used to encode the characters it contains. Therefore, the value returned may not correspond to the actual number of encoded characters in sequences of multi-byte or variable-length characters (such as UTF-8).
| s | sstr_t instance to get length of. |
|
extern |
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.
| cstr | C-style string to copy to the result sstr_t. |
Extends the sstr_t by appending additional characters contained in src.
| dst | destination sstr_t. |
| src | source sstr_t. |
|
extern |
Extends the sstr_t by appending additional characters contained in src.
| dst | destination sstr_t. |
| src | source C-style string. |
|
extern |
Append the string representation of a double to a string.
| s | the sstr_t to append to. |
| f | the double value to convert and append. |
| precision | number of decimal places to output. |
|
extern |
Append the string representation of a float to a string.
| s | the sstr_t to append to. |
| f | the float value to convert and append. |
| precission | number of decimal places to output. |
|
extern |
append spaces at the end of the sstr_t.
| s | the sstr_t to append spaces to. |
| indent | numbers of spaces to append. |
|
extern |
Append the decimal string representation of an int to a string.
| s | the sstr_t to append to. |
| i | the integer value to convert and append. |
|
extern |
Append the decimal string representation of a long to a string.
| s | the sstr_t to append to. |
| l | the long value to convert and append. |
|
extern |
Extends the sstr_t by appending additional characters in data with length of length at the end of its current value .
| s | destination sstr_t. |
| data | data to append. |
| length | length of data. |
|
extern |
Append if cond is true, otherwise do nothing.
| s | the sstr_t to append to. |
| data | data to append. |
| length | length of data. |
| cond | condition |
|
extern |
Append the decimal string representation of a uint32_t to a string.
| s | the sstr_t to append to. |
| u | the uint32_t value to convert and append. |
|
extern |
Append the decimal string representation of a uint64_t to a string.
| s | the sstr_t to append to. |
| u | the uint64_t value to convert and append. |
|
extern |
Extends the sstr_t by appending additional '\0' characters at the end of its current value.
| s | destination sstr_t. |
| length | length of '\0' to append. |
|
extern |
clear the sstr_t. After this call, the sstr_t is empty.
| s | sstr_t instance to clear. |
Compare a and b return 0 if equal, <0 if a < b, >0 if a > b.
| a | sstr_t to be compared. |
| b | sstr_t to be compared to. |
|
extern |
compare sstr_t a and c-style string b
just like sstr_compare, but compare a and c-style string b.
|
extern |
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.
| s | sstr_t instance to convert to C-style string. |
Duplicate s and return.
| s | sstr_t to duplicate. |
|
extern |
delete a sstr_t.
| s | sstr_t instance to delete. |
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.
| out | the output sstr_t to append the escaped string to. |
| in | the input sstr_t containing the raw string to escape. |
|
extern |
Create an empty sstr_t.
|
extern |
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.
| data | data to copy to the result sstr_t. |
| length | length of data. |
|
extern |
Parse a string as a double.
| s | the sstr_t to parse. |
| v | pointer to receive the parsed value. |
|
extern |
Parse a string as an int, consuming the parsed portion.
| s | pointer to the sstr_t to parse (advanced past consumed input). |
| v | pointer to receive the parsed value. |
|
extern |
Parse a string as a long integer.
| s | the sstr_t to parse. |
| v | pointer to receive the parsed value. |
|
extern |
printf implement.
| fmt | format, like C printf() |
| ... | arguments, like C printf() |
Same as sstr_printf(), but but print to buf instead of create a new one.
| buf | buffer to print to. |
| fmt | format string. |
| ... | arguments. |
|
extern |
Create a sstr_t from data with length bytes. The data is not copied, but have a pointer to data.
| data | data of the result sstr_t. |
| length | length of data. |
Get substring of s starting at index with length bytes.
| s | sstr_t instance to get substring of. |
| index | index of the first byte of the substring. |
| len | number of bytes of the substring. |
|
extern |
return version string.
|
extern |
Printf implement.
supported formats:
reserved:
if u/x/X, tailing d can be ignore