ztring.c: A few important extension for <string.h>
|
A few string functions to enhance C's <string.h> library. More...
#include <cstring>
#include <climits>
#include <cstddef>
Go to the source code of this file.
Macros | |
#define | removeAccented(str) strxacct( str ) /* mnemonic name */ |
#define | strxltn1_translate |
Translation table for strxltn1(). More... | |
Functions | |
char * | ztrcpy (size_t size, char *dest, const char *src) |
Copies up to 'size' characters from 'src' to 'dest'. More... | |
char * | ztrcat (size_t size, char *dest, const char *src) |
Append characters to a string. More... | |
char * | ztrins (size_t size, char *dest, size_t i, const char *insert) |
Inserts string 'insert' into 'dest' at position 'n'. More... | |
char * | strdel (char *dest, size_t len) |
Deletes the leading 'len' characters from 'str'. More... | |
char * | ztrsub (size_t size, char *dest, const char *src, size_t len) |
Copies the first 'len' characters from 'src' to 'dest'. More... | |
char * | strltrim (const char *str, char tr) |
Returns a pointer to the first character in 'str' different from 'tr'. More... | |
char * | strrtrim (char *str, char tr) |
Removes from 'str' all trailing characters that are equal to 'tr'. More... | |
char * | strtrim (char *str, char tr) |
return strrtrim( strltrim(s,tr),tr ). More... | |
size_t | memczap (size_t size, void *mem, int ch) |
Removes every ocurrence of 'ch' from 'mem'. More... | |
int | strpfx (const char *str, const char *prefix) |
Returns '1' if 'prefix' is a prefix of 'str'. More... | |
int | strsffx (const char *str, const char *suffix) |
Returns '1' if 'suffix' is a suffix of 'str'. More... | |
size_t | strrspn (const char *str, char a, char z) |
Get span until character in character range '[a..z]'. More... | |
char | strxltn1 (char accented_latin_1) |
Translates characters in range [192<–>192+63] into letters or ASCII symbols that look similar. More... | |
char * | strxacct (char *str) |
Uses strxltn1() to convert all letters in 'str'. More... | |
#define removeAccented | ( | str | ) | strxacct( str ) /* mnemonic name */ |
#define strxltn1_translate |
Translation table for strxltn1().
char* ztrcpy | ( | size_t | size, |
char * | dest, | ||
const char * | src | ||
) |
Copies up to 'size' characters from 'src' to 'dest'.
This is a 'size' checked versions of 'strcpy()'. The 'dest' memory block will always be a null terminated C string (unless 'size' is zero or 'dest' is NULL).
Stops copying when the end of the source C string is found (which is signaled by a null-character), even if less than 'size' characters have been copied (but 'dest' is not padded with zeros).
A null-character is implicitly appended at the end of 'dest' if the length of the 'src' C string is 'size' or bigger; in this case, only a portion of the leading characters from 'src' get copied into 'dest'.
'dest' and 'src' shall not overlap (see 'memmove()' for a safer alternative when overlapping).
size | Size of the 'dest' memory block. |
dest | Pointer to the destination array where the content is to be copied. |
src | C string to be copied. |
char* ztrcat | ( | size_t | size, |
char * | dest, | ||
const char * | src | ||
) |
Append characters to a string.
This is a 'size' checked versions of 'strcat()'. The 'dest' memory block will always be a null terminated C string (unless 'size' is zero or 'dest' is NULL in which case no characters would be appended).
If appending all characters from 'src' would result in a string with 'size' or more characters, a null-character is implicitly appended to 'dest' to ensure that its length is less than 'size'; in this case, only a portion of the leading characters from 'src' would be appended.
size | Size of the 'dest' memory block. |
dest | Pointer to the destination array of 'size' characters. |
src | C string to be appended. |
char* ztrins | ( | size_t | size, |
char * | dest, | ||
size_t | n, | ||
const char * | insert | ||
) |
Inserts string 'insert' into 'dest' at position 'n'.
If inserting all characters from 'dest' would result in a string with 'size' or more characters, a null-character is implicitly appended to 'dest' to ensure that its length is less than 'size'; in this case, only a portion of the leading characters from 'insert' would be inserted into 'dest'. The 'dest' memory block will always be a null terminated C string.
Any of the following conditions leaves the value in 'dest' unchanged:
Any of the following conditions will null terminate 'dest':
size | Size of the 'dest' memory block. |
dest | Pointer to the destination array of 'size' characters. |
insert | C string to be inserted. |
n | Position in 'dest' where the string will be inserted. |
char* strdel | ( | char * | dest, |
size_t | len | ||
) |
Deletes the leading 'len' characters from 'str'.
When 'dest' has less than 'len' characters, it becomes the null string. Any of the following conditions leaves the value in 'dest' unchanged:
dest | Pointer to the destination array of characters. |
len | Number of characters to remove from 'dest'. |
char* ztrsub | ( | size_t | size, |
char * | dest, | ||
const char * | src, | ||
size_t | len | ||
) |
Copies the first 'len' characters from 'src' to 'dest'.
The string in 'dest' is zero terminated. No more than 'size' characters in 'dest' get overwritten.
size | Size of the 'dest' memory block. |
dest | C substring to be produced. |
src | Source string. |
len | maximum length of substring to be produced. |
char* strltrim | ( | const char * | src, |
char | tr | ||
) |
Returns a pointer to the first character in 'str' different from 'tr'.
Does not change 'str' but returns a pointer inside it.
src | Source string. |
tr | Character to trim from left of 'src'. |
char* strrtrim | ( | char * | src, |
char | tr | ||
) |
Removes from 'str' all trailing characters that are equal to 'tr'.
Changes string 'str' and returns a pointer to it. All trailing characters equal to 'tr' are removed inserting one char(0).
src | Source string. |
tr | Character to trim from right of 'src'. |
char* strtrim | ( | char * | src, |
char | tr | ||
) |
size_t memczap | ( | size_t | size, |
void * | mem, | ||
int | ch | ||
) |
Removes every ocurrence of 'ch' from 'mem'.
Mnemonic: memczap <==> memory-zap-char.
Scans the memory buffer 'mem' for every occurrence of character 'ch' and removes it, for up to 'size' characters.
size | Size of memory block 'mem'. |
mem | Memory block of characters. |
ch | Character to trim from right of 'src'. |
int strpfx | ( | const char * | str, |
const char * | prefix | ||
) |
Returns '1' if 'prefix' is a prefix of 'str'.
Otherwise, returns '0'.
str | C string to be scanned. |
prefix | C string containing the sequence of characters to match. |
int strsffx | ( | const char * | str, |
const char * | suffix | ||
) |
Returns '1' if 'suffix' is a suffix of 'str'.
Otherwise, returns '0'.
str | C string to be scanned. |
suffix | C string containing the sequence of characters to match. |
size_t strrspn | ( | const char * | str, |
char | a, | ||
char | z | ||
) |
Get span until character in character range '[a..z]'.
Scans 'str' for the first occurrence of any of the characters that are part of character range begining in 'a' and ending in 'z', returning the number of characters of 'str' read before this first occurrence. The search includes the terminating null-characters. Therefore, the function will return the length of 'str' if none of the characters in range '[a..z]' are found in 'str'.
str | C string to be scanned. |
a | First character in character range. |
z | Last character in character range. |
|
inline |
Translates characters in range [192<–>192+63] into letters or ASCII symbols that look similar.
This has the effect of removing accents in many of those letters; for example, 'á' is translated as 'a' and 'ÿ' as 'y'. The translated characters are the upper 8 bit characters in the Latin 1 alphabet, also know as Windows-1252 and ISO/IEC 8859-1.
This is the translation table used:
char* strxacct | ( | char * | str | ) |
Uses strxltn1() to convert all letters in 'str'.
This has the effect of removing accents in many of those letters; for example, 'á' is translated as 'a' and 'ÿ' as 'y'. The translated characters are the upper 8 bit characters in the Latin 1 alphabet, also know as Windows-1252 and ISO/IEC 8859-1.
This is the translation table used: