qLibc
qstring.c File Reference

String APIs. More...

Go to the source code of this file.

Functions

char * qstrtrim (char *str)
 Remove whitespace, including CR and LF, from both ends of a string.
char * qstrtrim_head (char *str)
 Remove leading whitespace from a string.
char * qstrtrim_tail (char *str)
 Remove trailing whitespace, including CR and LF, from a string.
char * qstrunchar (char *str, char head, char tail)
 Remove matching characters from the start and end of a string.
char * qstrreplace (const char *mode, char *srcstr, const char *tokstr, const char *word)
 Replace tokens or strings in a source string using the given mode.
char * qstrcpy (char *dst, size_t size, const char *src)
 Copy src string to dst.
char * qstrncpy (char *dst, size_t size, const char *src, size_t nbytes)
 Copy src string to dst no more than n bytes.
char * qstrdupf (const char *format,...)
 Duplicate a formatted string.
char * qstrdup_between (const char *str, const char *start, const char *end)
 Duplicate a substring between two markers.
void * qmemdup (const void *data, size_t size)
 Duplicate a block of memory.
char * qstrcatf (char *str, const char *format,...)
 Append formatted text to the end of a string.
char * qstrgets (char *buf, size_t size, char **offset)
 Read one line from a string.
char * qstrrev (char *str)
 Reverse the order of characters in the string.
char * qstrupper (char *str)
 Convert character to bigger character.
char * qstrlower (char *str)
 Convert character to lower character.
char * qstrtok (char *str, const char *delimiters, char *retstop, int *offset)
 Split a string into tokens.
qlist_t * qstrtokenizer (const char *str, const char *delimiters)
 Tokenize a string.
char * qstrunique (const char *seed)
 Generate a unique ID.
char * qstr_comma_number (int number)
 Convert an integer to a comma-separated string.
bool qstrtest (int(*testfunc)(int), const char *str)
 Test whether a string matches a character rule.
bool qstr_is_email (const char *email)
 Test for an email-address formatted string.
bool qstr_is_ip4addr (const char *str)
 Test for an IPv4 address string.
char * qstr_conv_encoding (const char *str, const char *fromcode, const char *tocode, float mag)
 Convert character encoding.

Detailed Description

String APIs.

Definition in file qstring.c.

Function Documentation

◆ qstrtrim()

char * qstrtrim ( char * str)

Remove whitespace, including CR and LF, from both ends of a string.

Parameters
strsource string
Returns
pointer to str on success, or NULL on failure.
Note
This function modifies the source string in place.

Definition at line 55 of file qstring.c.

◆ qstrtrim_head()

char * qstrtrim_head ( char * str)

Remove leading whitespace from a string.

Parameters
strsource string
Returns
pointer to str on success, or NULL on failure.
Note
This function modifies the source string in place.

Definition at line 90 of file qstring.c.

◆ qstrtrim_tail()

char * qstrtrim_tail ( char * str)

Remove trailing whitespace, including CR and LF, from a string.

Parameters
strsource string
Returns
pointer to str on success, or NULL on failure.
Note
This function modifies the source string in place.

Definition at line 116 of file qstring.c.

◆ qstrunchar()

char * qstrunchar ( char * str,
char head,
char tail )

Remove matching characters from the start and end of a string.

Parameters
strsource string
headleading character
tailtrailing character
Returns
pointer to str on success, or NULL on failure.
Note
This function modifies the source string in place.
char *str = strdup(" \"hello world\" ");
qstrtrim(str); // remove whitespace
qstrunchar(str, '"', '"'); // remove quotes
char * qstrtrim(char *str)
Remove whitespace, including CR and LF, from both ends of a string.
Definition qstring.c:55
char * qstrunchar(char *str, char head, char tail)
Remove matching characters from the start and end of a string.
Definition qstring.c:149

Definition at line 149 of file qstring.c.

◆ qstrreplace()

char * qstrreplace ( const char * mode,
char * srcstr,
const char * tokstr,
const char * word )

Replace tokens or strings in a source string using the given mode.

Parameters
modereplace mode
srcstrsource string
tokstrtoken or string to match
wordreplacement string
Returns
pointer to the result string on success, or NULL on failure.
Note
The mode has two characters.

The first character selects how matching works:

  • t matches each character in tokstr as a token.
  • s matches tokstr as a full string.

The second character selects where the result is stored:

  • n returns a newly allocated string.
  • r writes the result back into srcstr.

When r is used, srcstr must have enough space for the result, and it must point to writable memory.

Supported modes:

  • tn : token replace, return a new string
  • tr : token replace, update srcstr
  • sn : string replace, return a new string
  • sr : string replace, update srcstr
char srcstr[256], *retstr;
char mode[4][2+1] = {"tn", "tr", "sn", "sr"};
for (i = 0; i < 4; i++) {
strcpy(srcstr, "Welcome to The qDecoder Project.");
printf("before %s : srcstr = %s\n", mode[i], srcstr);
retstr = qstrreplace(mode[i], srcstr, "The", "_");
printf("after %s : srcstr = %s\n", mode[i], srcstr);
printf(" retstr = %s\n\n", retstr);
if (mode[i][1] == 'n') free(retstr);
}
char * qstrreplace(const char *mode, char *srcstr, const char *tokstr, const char *word)
Replace tokens or strings in a source string using the given mode.
Definition qstring.c:209

Definition at line 209 of file qstring.c.

◆ qstrcpy()

char * qstrcpy ( char * dst,
size_t size,
const char * src )

Copy src string to dst.

The dst string array will be always terminated by NULL character. Also allows overlap between src and dst.

Parameters
dstpointer to the string to be copied
sizethe size of dst character arrary
srcpointer to source string
Returns
always returns pointer to dst

Definition at line 298 of file qstring.c.

◆ qstrncpy()

char * qstrncpy ( char * dst,
size_t size,
const char * src,
size_t nbytes )

Copy src string to dst no more than n bytes.

The dst string array will be always terminated by NULL character. Also allows overlap between src and dst.

Parameters
dstpointer to the string to be copied
sizethe size of dst character arrary
srcpointer to source string
nbytesnumber of bytes to copy
Returns
always returns pointer to dst

Definition at line 317 of file qstring.c.

◆ qstrdupf()

char * qstrdupf ( const char * format,
... )

Duplicate a formatted string.

Parameters
formatstring format
Returns
newly allocated string on success, or NULL on failure.

Definition at line 336 of file qstring.c.

◆ qstrdup_between()

char * qstrdup_between ( const char * str,
const char * start,
const char * end )

Duplicate a substring between two markers.

Parameters
strsource string
startstart marker
endend marker
Returns
newly allocated string on success, or NULL on failure.

Definition at line 357 of file qstring.c.

◆ qmemdup()

void * qmemdup ( const void * data,
size_t size )

Duplicate a block of memory.

Parameters
datasource data
sizedata size
Returns
pointer to newly allocated data on success, or NULL on failure.

Definition at line 386 of file qstring.c.

◆ qstrcatf()

char * qstrcatf ( char * str,
const char * format,
... )

Append formatted text to the end of a string.

Parameters
strdestination string
formatstring format to append
Returns
pointer to str on success, or NULL on failure.

Definition at line 408 of file qstring.c.

◆ qstrgets()

char * qstrgets ( char * buf,
size_t size,
char ** offset )

Read one line from a string.

Parameters
bufbuffer pointer
sizebuffer size
offsetpointer to the current position in the source string
Returns
pointer to buf on success, or NULL on EOF.
Note
CR and LF will not be stored.
char *text="Hello\nWorld";
char *offset = text;
char buf[1024];
while(qstrgets(buf, sizeof(buf), &offset) == NULL) {
printf("%s\n", buf);
}
char * qstrgets(char *buf, size_t size, char **offset)
Read one line from a string.
Definition qstring.c:441

Definition at line 441 of file qstring.c.

◆ qstrrev()

char * qstrrev ( char * str)

Reverse the order of characters in the string.

Parameters
strpointer to source string
Returns
always returns pointer to str
Note
This function modifies str directly.

Definition at line 473 of file qstring.c.

◆ qstrupper()

char * qstrupper ( char * str)

Convert character to bigger character.

Parameters
strpointer to source string
Returns
always returns pointer to str
Note
This function modifies str directly.

Definition at line 496 of file qstring.c.

◆ qstrlower()

char * qstrlower ( char * str)

Convert character to lower character.

Parameters
strpointer to source string
Returns
always returns pointer to str
Note
This function modifies str directly.

Definition at line 516 of file qstring.c.

◆ qstrtok()

char * qstrtok ( char * str,
const char * delimiters,
char * retstop,
int * offset )

Split a string into tokens.

Parameters
strsource string
delimitersstring that specifies the delimiter characters
retstopstop delimiter character is stored here. This can be NULL if you do not need it.
offsetinteger pointer used to store the last position. It must be reset to 0 before the first call.
Returns
pointer to the first byte of a token on success, or NULL on failure.
char *str = strdup("Hello,world|Thank,you");
char *token;
int offset = 0;
while((token = qstrtok(str, "|,", NULL, &offset)) != NULL) {
printf("%s\n", token);
}
char * qstrtok(char *str, const char *delimiters, char *retstop, int *offset)
Split a string into tokens.
Definition qstring.c:554
Note
This may modify str argument. The major difference between qstrtok() and standard strtok() is that qstrtok() can returns empty string tokens. If the str is "a:b::d", qstrtok() returns "a", "b", "", "d". But strtok() returns "a","b","d".

Definition at line 554 of file qstring.c.

◆ qstrtokenizer()

qlist_t * qstrtokenizer ( const char * str,
const char * delimiters )

Tokenize a string.

Parameters
strsource string
delimitersstring that specifies a set of delimiters that may surround the token being extracted
Returns
qlist_t pointer on success, or NULL on failure.
qlist_t *tokens = qstr_tokenizer("a:b:c", ":");
char *str;
while((str = tokens->popfirst(tokens, NULL)) != NULL) {
printf("%s\n", str);
}
tokens->free(tokens);

Definition at line 600 of file qstring.c.

◆ qstrunique()

char * qstrunique ( const char * seed)

Generate a unique ID.

Parameters
seedadditional seed string. This can be NULL.
Returns
newly allocated string.
Note
The length of returned string is 32+1 bytes long including terminating NULL character. It's a good idea to call srand() once before calling this because it uses rand().

Definition at line 631 of file qstring.c.

◆ qstr_comma_number()

char * qstr_comma_number ( int number)

Convert an integer to a comma-separated string.

Parameters
numberinteger
Returns
newly allocated string on success, or NULL on failure.

Definition at line 661 of file qstring.c.

◆ qstrtest()

bool qstrtest ( int(* testfunc )(int),
const char * str )

Test whether a string matches a character rule.

Parameters
testfunctest function for each character
strstring to test
Returns
true if all characters match, otherwise false.
if(qstrtest(isalnum, "hello1234") == true) {
printf("It is alpha-numeric string.");
}
if(qstrtest(isdigit, "0123456789") == true) {
printf("It is alpha-numeric string.");
}
bool qstrtest(int(*testfunc)(int), const char *str)
Test whether a string matches a character rule.
Definition qstring.c:722
Note
You can use the standard functions below without writing your own version. Make sure <ctype.h> header should be included before using any of these functions. isalnum - checks for an alphanumeric character. isalpha - checks for an alphabetic character. isascii - checks whether c is a 7-bit unsigned char value that fits into the ASCII character set. isblank - checks for a blank character; that is, a space or a tab. iscntrl - checks for a control character. isdigit - checks for a digit (0 through 9). isgraph - checks for any printable character except space. islower - checks for a lower-case character. isprint - checks for any printable character including space. ispunct - checks for any printable character which is not a space or an alphanumeric character. isspace - checks for white-space characters. isupper - checks for an uppercase letter. isxdigit - checks for a hexadecimal digits. See man isalnum for more details about these functions.

Definition at line 722 of file qstring.c.

◆ qstr_is_email()

bool qstr_is_email ( const char * email)

Test for an email-address formatted string.

Parameters
emailemail-address formatted string
Returns
true on success, otherwise false

Definition at line 737 of file qstring.c.

◆ qstr_is_ip4addr()

bool qstr_is_ip4addr ( const char * str)

Test for an IPv4 address string.

Parameters
urlIPv4 address string
Returns
true on success, otherwise false
if(qstr_is_ip4addr("1.2.3.4") == true) {
printf("It is IPv4 address string.");
}
bool qstr_is_ip4addr(const char *str)
Test for an IPv4 address string.
Definition qstring.c:795

Definition at line 795 of file qstring.c.

◆ qstr_conv_encoding()

char * qstr_conv_encoding ( const char * str,
const char * fromcode,
const char * tocode,
float mag )

Convert character encoding.

Parameters
strsource string. This can be NULL.
fromcodesource encoding
tocodetarget encoding
magsize multiplier between fromcode and tocode
Returns
newly allocated converted string on success, or NULL on failure.
qCharEncode("KOREAN_EUCKR_STRING", "EUC-KR", "UTF-8", 1.5);

Definition at line 835 of file qstring.c.