Nilorea Library string api test.
char* chardest = NULL;
length = 0;
n_log(
LOG_INFO,
"charstr (%d/%d): %s", written, length, chardest);
n_log(
LOG_INFO,
"charstr (%d/%d): %s", written, length, chardest);
n_log(
LOG_INFO,
"charstr (%d/%d): %s", written, length, chardest);
n_log(
LOG_INFO,
"charstr (%d/%d): %s", written, length, chardest);
n_log(
LOG_INFO,
"charstr (%d/%d): %s", written, length, chardest);
n_log(
LOG_INFO,
"charstr (%d/%d): %s", written, length, chardest);
written = length = 0;
n_log(
LOG_INFO,
"charstr (%d/%d): %s", written, length, chardest);
n_log(
LOG_INFO,
"charstr (%d/%d): %s", written, length, chardest);
n_log(
LOG_INFO,
"charstr (%d/%d): %s", written, length, chardest);
n_log(
LOG_INFO,
"charstr (%d/%d): %s", written, length, chardest);
n_log(
LOG_INFO,
"charstr (%d/%d): %s", written, length, chardest);
n_log(
LOG_INFO,
"charstr (%d/%d): %s", written, length, chardest);
nstrprintf(nstr,
"Hello, file is %s line %d date %s", __FILE__, __LINE__, __TIME__);
nstrprintf_cat(nstr,
" - This will be added at file %s line %d date %s", __FILE__, __LINE__, __TIME__);
nstrprintf(nstr,
"Hello, file is %s line %d date %s", __FILE__, __LINE__, __TIME__);
nstrprintf_cat(nstr,
" - This will be added at file %s line %d date %s", __FILE__, __LINE__, __TIME__);
char data[1048576] = "";
for (int it = 0; it < 1048575; it++) {
data[it] = (char)(32 + rand() % 63);
}
data[1048574] = '\0';
for (int it = 0; it < 100; it++) {
}
char* trimmed =
trim(
" spaces around ");
if (trimmed) {
}
char trimbuf[] = " hello ";
int ival = 0;
}
long int lval = 0;
}
long long int llval = 0;
}
char upper[32] = "";
char lower[32] = "";
strup(
"hello world", upper);
strlo(
"HELLO WORLD", lower);
char** parts =
split(
"one:two:three:four",
":", 0);
if (parts) {
for (int it = 0; it < count; it++) {
}
char* joined =
join(parts,
" - ");
if (joined) {
}
}
char* replaced =
str_replace(
"hello world hello",
"hello",
"bye");
if (replaced) {
}
char sanitize_buf[] = "hello; rm -rf /";
if (file_content) {
}
FILE* empty_fp = fopen("nilorea_nstr_empty.txt", "wb");
if (empty_fp)
fclose(empty_fp);
if (empty_content && empty_content->
data && empty_content->
written == 0)
n_log(
LOG_INFO,
"file_to_nstr on an empty file: OK (empty string, no error)");
else
n_log(
LOG_ERR,
"file_to_nstr on an empty file: FAILED (expected an empty N_STR)");
if (empty_content)
char skipbuf[] = " hello";
skipw(skipbuf,
' ', &it, 1);
n_log(
LOG_INFO,
"skipw past spaces: pos=%zu char='%c'", it, skipbuf[it]);
it = 0;
skipu(skipbuf,
'h', &it, 1);
n_log(
LOG_INFO,
"skipu to 'h': pos=%zu char='%c'", it, skipbuf[it]);
if (
scan_dir(
".", dir_list, 0) == TRUE) {
}
int url_encode_errors = 0;
if (encoded != NULL) {
n_log(
LOG_ERR,
"FAIL: n_str_url_encode(NULL) expected NULL, got \"%s\"",
_nstr(encoded));
url_encode_errors++;
} else {
}
if (!encoded || strcmp(
_nstr(encoded),
"") != 0) {
n_log(
LOG_ERR,
"FAIL: n_str_url_encode(\"\") expected \"\", got \"%s\"", encoded ?
_nstr(encoded) :
"NULL");
url_encode_errors++;
} else {
}
if (!encoded || strcmp(
_nstr(encoded),
"Hello-World_1.0~") != 0) {
n_log(
LOG_ERR,
"FAIL: n_str_url_encode(\"Hello-World_1.0~\") expected verbatim, got \"%s\"", encoded ?
_nstr(encoded) :
"NULL");
url_encode_errors++;
} else {
n_log(
LOG_INFO,
"PASS: n_str_url_encode(\"Hello-World_1.0~\") == \"Hello-World_1.0~\"");
}
const char* expected_reserved = "foo%20bar%26baz%3Dqux%2Fpath%3Fq%3D1%23frag";
if (!encoded || strcmp(
_nstr(encoded), expected_reserved) != 0) {
n_log(
LOG_ERR,
"FAIL: n_str_url_encode(reserved chars) expected \"%s\", got \"%s\"", expected_reserved, encoded ?
_nstr(encoded) :
"NULL");
url_encode_errors++;
} else {
n_log(
LOG_INFO,
"PASS: n_str_url_encode(\"foo bar&baz=qux/path?q=1#frag\") == \"%s\"", expected_reserved);
}
if (!encoded || strcmp(
_nstr(encoded),
"%C3%A9%FF") != 0) {
n_log(
LOG_ERR,
"FAIL: n_str_url_encode(high bytes) expected \"%s\", got \"%s\"",
"%C3%A9%FF", encoded ?
_nstr(encoded) :
"NULL");
url_encode_errors++;
} else {
n_log(
LOG_INFO,
"PASS: n_str_url_encode(\"\\xC3\\xA9\\xFF\") == \"%s\"",
"%C3%A9%FF");
}
if (url_encode_errors > 0) {
n_log(
LOG_ERR,
"n_str_url_encode: %d test(s) failed", url_encode_errors);
exit(1);
}
exit(0);
}
#define Free(__ptr)
Free Handler to get errors.
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
size_t nb_items
number of item currently in the list
int list_destroy(LIST **list)
Empty and Free a list container.
LIST * new_generic_list(size_t max_items)
Initialiaze a generic list container to max_items pointers.
Structure of a generic LIST container.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_INFO
informational
size_t written
number of meaningful bytes in data, excluding the null terminator; the size including the null termin...
size_t length
total allocation (in bytes) of the data buffer, padding included
int str_to_long_long(const char *s, long long int *i, const int base)
Helper for string to integer.
char * trim_nocopy(char *s)
trim and zero end the string, WARNING: keep and original pointer to delete the string correctly
N_STR * n_str_url_encode(const char *src)
Percent-encode a C string per RFC 3986 (unreserved set kept as-is).
size_t NSTRBYTE
N_STR base unit.
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
int split_count(char **split_result)
Count split elements.
int nstr_to_file(N_STR *str, char *filename)
Write a N_STR content into a file.
char * join(char **splitresult, const char *delim)
join the array into a string
#define nstrcat(__nstr_dst, __nstr_src)
Macro to quickly concatenate two N_STR.
N_STR * nstrdup(N_STR *str)
Duplicate a N_STR.
int strlo(const char *string, char *dest)
Lower case a string.
int write_and_fit(char **dest, NSTRBYTE *size, NSTRBYTE *written, const char *src)
Append src to *dest.
#define nstrprintf_cat(__nstr_var, __format,...)
Macro to quickly allocate and sprintf and cat to a N_STR.
int resize_nstr(N_STR *nstr, size_t size)
reallocate a nstr internal buffer.
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
int skipu(const char *string, char toskip, NSTRBYTE *iterator, int inc)
skip until 'toskip' occurence is found from 'iterator' to the next 'toskip' value.
int empty_nstr(N_STR *nstr)
empty a N_STR string
int wildmatcase(register const char *text, register const char *p)
Written by Rich Salz rsalz at osf.org, refurbished by me.
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
char * str_replace(const char *string, const char *substr, const char *replacement)
Replace "substr" by "replacement" inside string taken from http://coding.debuntu.org/c-implementing-s...
char * trim(const char *s)
trim and put a \0 at the end, return new char *
int skipw(const char *string, char toskip, NSTRBYTE *iterator, int inc)
skip while 'toskip' occurence is found from 'iterator' to the next non 'toskip' position.
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
int scan_dir(const char *dir, LIST *result, const int recurse)
Scan a list of directory and return a list of char *file.
int str_sanitize(char *string, const char *mask, const char replacement)
clean a string by replacing evil characteres
char ** split(const char *str, const char *delim, int empty)
split the strings into a an array of char *pointer , ended by a NULL one.
int wildmat(register const char *text, register const char *p)
Written by Rich Salz rsalz at osf.org, refurbished by me.
int str_to_long(const char *s, long int *i, const int base)
Helper for string to integer.
int write_and_fit_ex(char **dest, NSTRBYTE *size, NSTRBYTE *written, const char *src, NSTRBYTE src_size, NSTRBYTE additional_padding)
Append src_size bytes from src to *dest, updating size/written, growing the buffer if needed.
N_STR * file_to_nstr(char *filename)
Load a whole file into a N_STR.
int free_split_result(char ***tab)
Free a split result allocated array.
int str_to_int(const char *s, int *i, const int base)
Helper for string to integer.
int strup(const char *string, char *dest)
Upper case a string.
A box including a string and his lenght.
N_STR and string function declaration.