Main Page | Modules | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

String library


Files

file  otString.h
 String Library.

Modules

 Version History

Defines

#define __isalpha(c)   (c >'9')
#define __isspace(c)   ((c)==' ' || (c)=='\f' || (c)=='\n' || (c)=='\r' || (c)=='\t' || (c)=='\v')
#define __isupper(c)   !(c & 0x20)
#define __ishex(c)   (((c >= '0')&&(c <= '9'))||((c >= 'A')&&(c <= 'F')))
#define __ascii2hex(c)   ((c <= '9')? c-'0': c-'A'+10)
 Convert an hex decimal digit in binary.
#define __isdigit(c)   ((c)>='0' && (c)<='9')
#define __isblank(c)   ((c)==' ' || (c)=='\t')
#define __isprint(c)   (((c)>=32 && (c)<=126) || ((c)>=161 && (c)<=254))
#define __isalnum(c)
 Check if character is alphanumeric.

Functions

void _qsort (void *base, U32 num, U32 size, int(*compar)(const void *, const void *))
 Sort elements of array Sorts the num elements of the array pointed to by base, each element size bytes long, using the compar function to determine the order. The sorting algorithm used by this function compares pairs of elements by calling the specified compar function with pointers to them as argument. The function does not return any value, but modifies the content of the array pointed to by base reordering its elements as defined by compar. The order of equivalent elements is undefined.
S32 _strtol (const char *str, char **endptr, int base)
 Convert string to long integer Parses the C-string str interpreting its content as an integral number of the specified base, which is returned as a long int value. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax that depends on the base parameter, and interprets them as a numerical value. Finally, a pointer to the first character following the integer representation in str is stored in the object pointed by endptr. If the value of base is zero, the syntax expected is similar to that of integer constants, which is formed by a succession of: An optional sign character (+ or -) An optional prefix indicating octal or hexadecimal base ("0" or "0x"/"0X" respectively) A sequence of decimal digits (if no base prefix was specified) or either octal or hexadecimal digits if a specific prefix is present If the base value is between 2 and 36, the format expected for the integral number is a succession of any of the valid digits and/or letters needed to represent integers of the specified radix (starting from '0' and up to 'z'/'Z' for radix 36). The sequence may optionally be preceded by a sign (either + or -) and, if base is 16, an optional "0x" or "0X" prefix. If the first sequence of non-whitespace characters in str is not a valid integral number as defined above, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. For locales other than the "C" locale, additional subject sequence forms may be accepted.
U32 _strtoul (const char *str, char **endptr, int base)
 Convert string to unsigned long integer Parses the C-string str, interpreting its content as an integral number of the specified base, which is returned as an value of type unsigned long int. This function operates like strtol to interpret the string, but produces numbers of type unsigned long int (see strtol for details on the interpretation process).
S64 strtoll (const char *str, char **endptr, int base)
 Convert string to long long integer Parses the C-string str interpreting its content as an integral number of the specified base, which is returned as a value of type long long int. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.
U64 strtoull (const char *str, char **endptr, int base)
 Convert string to unsigned long long integer Parses the C-string str interpreting its content as an integral number of the specified base, which is returned as a value of type unsigned long long int. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.
int _tolower (int c)
 Convert uppercase letter to lowercase. Converts c to its lowercase equivalent if c is an uppercase letter and has a lowercase equivalent. If no such conversion is possible, the value returned is c unchanged. Notice that what is considered a letter may depend on the locale being used; In the default "C" locale, an uppercase letter is any of:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
which translate respectively to:
a b c d e f g h i j k l m n o p q r s t u v w x y z.
int _toupper (int c)
 Convert lowercase letter to uppercase. Converts c to its lowercase equivalent if c is an uppercase letter and has a lowercase equivalent. If no such conversion is possible, the value returned is c unchanged. Notice that what is considered a letter may depend on the locale being used; In the default "C" locale, an lowercase letter is any of:
a b c d e f g h i j k l m n o p q r s t u v w x y z
which translate respectively to:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z.
unsigned int _strlen (const char *str)
 Get string length. The length of a C string is determined by the terminating null-character:
A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).
unsigned dbl2stri (char *outbfr, double dbl, unsigned dec_digits)
 Convert a double to a C string.
int _atoi (const char *pString)
 Convert string to integer. Number starting with 0x or 0X are allowed and converted as hex decimal number.
long _atol (const char *nptr)
char * _strcat (char *destination, const char *source)
 Concatenate strings. Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination. destination and source shall not overlap.
char * _strstr (char *str1, char *str2)
 Locate substring. Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1. The matching process does not include the terminating null-characters, but it stops there.
char * _strtok (char *str, const char *delimiters)
 Split string into tokens. A sequence of calls to this function split str into tokens, which are sequences of contiguous characters separated by any of the characters that are part of delimiters. On a first call, the function expects a C string as argument for str, whose first character is used as the starting location to scan for tokens. In subsequent calls, the function expects a null pointer and uses the position right after the end of the last token as the new starting location for scanning. To determine the beginning and the end of a token, the function first scans from the starting location for the first character not contained in delimiters (which becomes the beginning of the token). And then scans starting from this beginning of the token for the first character contained in delimiters, which becomes the end of the token. The scan also stops if the terminating null character is found. This end of the token is automatically replaced by a null-character, and the beginning of the token is returned by the function. Once the terminating null character of str is found in a call to strtok, all subsequent calls to this function (with a null pointer as the first argument) return a null pointer. The point where the last token was found is kept internally by the function to be used on the next call (particular library implementations are not required to avoid data races).
void _strchop (char *str1)
 Remove last character.
char _strlastchar (const char *str1)
 Return last character of a string.
int _strcmp (const char *str1, const char *str2)
 Compare two strings. Compares the C string str1 to the C string str2. This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached. This function performs a binary comparison of the characters.
int _strncmp (const char *str1, const char *str2, int num)
 Compare characters of two strings. Compares up to num characters of the C string str1 to those of the C string str2. This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ, until a terminating null-character is reached, or until num characters match in both strings, whichever happens first.
const char * _strchr (const char *str, char character)
 Locate first occurrence of character in string.
char * _strcpy (char *destination, const char *source)
 Copy string. Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point). To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.
char * _strncpy (char *destination, const char *source, int num)
 Copy characters from string. Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it. No null-character is implicitly appended at the end of destination if source is longer than num. Thus, in this case, destination shall not be considered a null terminated C string (reading it as such would overflow).
void _reverse (char *str)
 Reverse the order of stored characters.
unsigned _str_split (char *string, char charSep, int maxArgs, char *argv)
 Splits the string into substrings wherever charSep occurs, and returns the list of those strings. If charSep does not match anywhere in the string, _str_split() returns a single-element list containing this string. The output is a sequence of string separated by 0. Use _str_field the get selected field.
void _str_leftShift (char *string, int index)
 Left shift a string starting from index.
void _str_simplified (char *string)
 Returns a string that has whitespace removed from the start and the end, and that has each sequence of internal whitespace replaced with a single space.
void _str_toupper (char *text)
 Convert the string to upper case.
void _str_tolower (char *text)
 Convert the string to lower case.
const char * _str_field (const char *string, int field)
 Return nth string field of a string list.
void _replace (char *str, char oldChar, char newChar)
 Replace a character in a string.
int _strEndsWith (const char *src, const char *txt)
 Check if a string ends with a specified string.
char _byteHex (U8 val)
 Convert a binary number (nibble) into an ASCII character. Example: 00001100 -> 01000011 or in C language 0x0C -> 'C'.
void _binHex (U8 *buf, U8 v)
 Convert a byte in two ASCII characters. Example: 0x3C -> '3C'.
void _binHex16 (U8 *buf, U32 v)
 Convert a word in four ASCII characters. Example: 0x3CFF -> '3CFF'.
void _binHex32 (U8 *buf, U32 v)
 Convert a double word in eight ASCII characters. Example: 0x12343CFF -> '12343CFF'.
U8 _asciiToBin (U8 ch)
 Converts a hexadecimal number (one digits), encoded in ASCII, to binary. Example: 'F' -> 0x0F.
U8 _hexBin (const U8 *str)
 Converts a hexadecimal number (two digits), encoded in ASCII, to binary. Example: 'FF' -> 0xFF.
U16 _hexBin16 (const U8 *str)
 Converts a hexadecimal number (four digits), encoded in ASCII, to binary. Example: 'FFEE' -> 0xFFEE.
U32 _hexBin32 (const U8 *str)
 Converts a hexadecimal number (eight digits), encoded in ASCII, to binary. Example: 'FFEE1234' -> 0xFFEE1234.
double _atof (const char *str)
 Parses the C string str, interpreting its content as a floating point number and returns its value as a double.
double __atof (const char *s, char **es, bool *done)
 Parses the C string str, interpreting its content as a floating point number and returns its value as a double.

Detailed Description

Common string library.
ASCII TABLE:
asciiCode.png

Define Documentation

#define __ascii2hex  )     ((c <= '9')? c-'0': c-'A'+10)
 

#define __isalnum  ) 
 

Value:

( ( c >= '0' && c <= '9' ) ||  \
                                                ( c >= 'A' && c <= 'Z' ) ||  \
                                                ( c >= 'a' && c <= 'z' ) )

#define __isalpha  )     (c >'9')
 

Check if character is alphabetic. Return a value different from zero if indeed c is an alphabetic letter. Zero (i.e., false) otherwise.

#define __isblank  )     ((c)==' ' || (c)=='\t')
 

Checks whether c is a blank character. A blank character is a space character used to separate words within a line of text. The standard "C" locale considers blank characters the tab character ('\t') and the space character (' ').

#define __isdigit  )     ((c)>='0' && (c)<='9')
 

Checks whether c is a decimal digit character. Decimal digits are any of: 0 1 2 3 4 5 6 7 8 9

#define __ishex  )     (((c >= '0')&&(c <= '9'))||((c >= 'A')&&(c <= 'F')))
 

Checks whether c is a hexdecimal digit character.
Hexadecimal digits are any of: 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F

#define __isprint  )     (((c)>=32 && (c)<=126) || ((c)>=161 && (c)<=254))
 

Checks whether c is a printable character. A printable character is a character that occupies a printing position on a display. For the standard ASCII character set (used by the "C" locale), printing characters are all with an ASCII code greater than 0x1f (US), except 0x7f (DEL).

#define __isspace  )     ((c)==' ' || (c)=='\f' || (c)=='\n' || (c)=='\r' || (c)=='\t' || (c)=='\v')
 

Check if character is a white-space. For the "C" locale, white-space characters are any of: (0x20) space (SPC), (0x09) horizontal tab (TAB), (0x0a) newline (LF), (0x0b) vertical tab (VT), (0x0c) feed (FF), (0x0d) carriage return (CR)

#define __isupper  )     !(c & 0x20)
 

Checks if parameter c is an uppercase alphabetic letter. Notice that what is considered a letter may depend on the locale being used;
In the default "C" locale, an uppercase letter is any of: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z.


Function Documentation

double __atof const char *  s,
char **  es,
bool done
 

Parameters:
s C-string beginning with the representation of a floating-point number.
es End pointer
done True on success
Returns:
On success, the function returns the converted floating point number as a double value.

U8 _asciiToBin U8  ch  ) 
 

Parameters:
ch Hex number to be converted
Returns:
A byte

double _atof const char *  str  ) 
 

Parameters:
str C-string beginning with the representation of a floating-point number.
Returns:
On success, the function returns the converted floating point number as a double value.

int _atoi const char *  pString  ) 
 

Parameters:
pString C-string beginning with the representation of an integral number.
Returns:
On success, the function returns the converted integral number as an int value.

long _atol const char *  nptr  ) 
 

Convert string to long integer

Parameters:
nptr C-string containing the representation of an integral number.
Returns:
On success, the function returns the converted integral number as a long int value.

void _binHex U8 buf,
U8  v
 

Parameters:
buf Source string where store converted number
v Byte to be converted

void _binHex16 U8 buf,
U32  v
 

Parameters:
buf Source string where store converted number
v Word to be converted

void _binHex32 U8 buf,
U32  v
 

Parameters:
buf Source string where store converted number
v Double word to be converted

char _byteHex U8  val  ) 
 

Parameters:
val Nibble to be converted

U8 _hexBin const U8 str  ) 
 

Parameters:
str Pointer to an hex number to be converted
Returns:
A byte

U16 _hexBin16 const U8 str  ) 
 

Parameters:
str Pointer to an hex number to be converted
Returns:
A word

U32 _hexBin32 const U8 str  ) 
 

Parameters:
str Pointer to an hex number to be converted
Returns:
A double word

void _qsort void *  base,
U32  num,
U32  size,
int(*)(const void *, const void *)  compar
 

Parameters:
base Pointer to the first object of the array to be sorted, converted to a void*.
num Number of elements in the array pointed to by base.
size Size in bytes of each element in the array.
compar Pointer to a function that compares two elements.

void _replace char *  str,
char  oldChar,
char  newChar
 

Parameters:
str String pointer
oldChar Character to be changed.
newChar Substitute character

void _reverse char *  str  ) 
 

Parameters:
str C string.
Returns:
str contains the reversed string.

const char* _str_field const char *  string,
int  field
 

Parameters:
string C string list (multiple concatenated C strings)
field Index of requested field
Returns:
Requested field

void _str_leftShift char *  string,
int  index
 

Parameters:
string C string
index Starting point for left shift

void _str_simplified char *  string  ) 
 

Parameters:
string C string

unsigned _str_split char *  string,
char  charSep,
int  maxArgs,
char *  argv
 

Parameters:
string C string.
charSep Character separator
argv Output array, where will be loaded splitted string
maxArgs Maximum number of output string
Returns:
Number of found string.
See also:
_str_field

void _str_tolower char *  text  ) 
 

Parameters:
text C string

void _str_toupper char *  text  ) 
 

Parameters:
text C string

char* _strcat char *  destination,
const char *  source
 

Parameters:
destination Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string.
source C string to be appended. This should not overlap destination.
Returns:
destination is returned.

void _strchop char *  str1  ) 
 

Parameters:
str1 C string to be truncated.

const char* _strchr const char *  str,
char  character
 

Parameters:
str C string.
character Character to be located. It is passed as its int promotion, but it is internally converted back to char for the comparison.
Returns:
A pointer to the first occurrence of character in str.

int _strcmp const char *  str1,
const char *  str2
 

Parameters:
str1 C string to be compared.
str2 C string to be compared.
Returns:
Returns an integral value indicating the relationship between the strings:
<0 the first character that does not match has a lower value in ptr1 than in ptr2
0 the contents of both strings are equal
>0 the first character that does not match has a greater value in ptr1 than in ptr2

char* _strcpy char *  destination,
const char *  source
 

Parameters:
destination Pointer to the destination array where the content is to be copied.
source C string to be copied.
Returns:
destination is returned.

int _strEndsWith const char *  src,
const char *  txt
 

Parameters:
src Search string
txt Text to be checked
Returns:
0 if the string src ends with txt; otherwise returns != 0.

char _strlastchar const char *  str1  ) 
 

Parameters:
str1 C string to be analyzed.
Returns:
Return last character of a string.

unsigned int _strlen const char *  str  ) 
 

Parameters:
str C string.
Returns:
Returns the length of the C string str.

int _strncmp const char *  str1,
const char *  str2,
int  num
 

Parameters:
str1 C string to be compared.
str2 C string to be compared.
num Maximum number of characters to compare.
Returns:
Returns an integral value indicating the relationship between the strings:
<0 the first character that does not match has a lower value in ptr1 than in ptr2
0 the contents of both strings are equal
>0 the first character that does not match has a greater value in ptr1 than in ptr2

char* _strncpy char *  destination,
const char *  source,
int  num
 

Parameters:
destination Pointer to the destination array where the content is to be copied.
source C string to be copied.
num Maximum number of characters to be copied from source.
Returns:
A pointer to the first occurrence of character in str.

char* _strstr char *  str1,
char *  str2
 

Parameters:
str1 C string to be scanned.
str2 C string containing the sequence of characters to match.
Returns:
A pointer to the first occurrence in str1 of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.

char* _strtok char *  str,
const char *  delimiters
 

Parameters:
str C string to truncate. Notice that this string is modified by being broken into smaller strings (tokens). Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.
delimiters C string containing the delimiter characters. These can be different from one call to another.
Returns:
If a token is found, a pointer to the beginning of the token. Otherwise, a null pointer. A null pointer is always returned when the end of the string (i.e., a null character) is reached in the string being scanned.

S32 _strtol const char *  str,
char **  endptr,
int  base
 

Parameters:
str C-string beginning with the representation of an integral number.
endptr Reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value. This parameter can also be a null pointer, in which case it is not used.
base Numerical base (radix) that determines the valid characters and their interpretation. If this is 0, the base used is determined by the format in the sequence (see strtol for details).
Returns:
On success, the function returns the converted integral number as a long int value. If no valid conversion could be performed, a zero value is returned (0L). If the value read is out of the range of representable values by a long int, the function returns LONG_MAX or LONG_MIN

U32 _strtoul const char *  str,
char **  endptr,
int  base
 

Parameters:
str C-string beginning with the representation of an integral number.
endptr Reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value. This parameter can also be a null pointer, in which case it is not used.
base Numerical base (radix) that determines the valid characters and their interpretation. If this is 0, the base used is determined by the format in the sequence (see strtol for details).
Returns:
On success, the function returns the converted integral number as a long int value. If no valid conversion could be performed, a zero value is returned (0L). If the value read is out of the range of representable values by a long int, the function returns LONG_MAX or LONG_MIN

int _tolower int  c  ) 
 

Parameters:
c Character to be converted, casted to an int, or EOF.
Returns:
The lowercase equivalent to c, if such value exists, or c (unchanged) otherwise. The value is returned as an int value that can be implicitly casted to char.
See also:
_toupper

int _toupper int  c  ) 
 

Parameters:
c Character to be converted, casted to an int, or EOF.
Returns:
The uppercase equivalent to c, if such value exists, or c (unchanged) otherwise. The value is returned as an int value that can be implicitly casted to char.
See also:
_toupper

unsigned dbl2stri char *  outbfr,
double  dbl,
unsigned  dec_digits
 

Parameters:
outbfr Pointer to the output buffer.
dbl Double to be convert.
dec_digits Number of decimal digits.
Returns:
Returns the string length.

S64 strtoll const char *  str,
char **  endptr,
int  base
 

Parameters:
str C-string beginning with the representation of an integral number.
endptr Reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value. This parameter can also be a null pointer, in which case it is not used.
base Numerical base (radix) that determines the valid characters and their interpretation. If this is 0, the base used is determined by the format in the sequence (see strtol for details).
Returns:
On success, the function returns the converted integral number as a long long int value. If no valid conversion could be performed, a zero value is returned (0LL).
 int main ()
 {
      char    szNumbers[] = "1856892505 17b00a12b -01100011010110000010001101100 0x6fffff";
      char    * pEnd;
      long long int lli1, lli2, lli3, lli4;
      lli1 = strtoll(szNumbers, &pEnd, 10);
      lli2 = strtoll(pEnd, &pEnd, 16);
      lli3 = strtoll(pEnd, &pEnd, 2);
      lli4 = strtoll(pEnd, NULL, 0);
      printf("The decimal equivalents are: %lld, %lld, %lld and %lld.\n", lli1, lli2, lli3, lli4);
      return 0;
 }

U64 strtoull const char *  str,
char **  endptr,
int  base
 

Parameters:
str C-string beginning with the representation of an integral number.
endptr Reference to an object of type char*, whose value is set by the function to the next character in str after the numerical value. This parameter can also be a null pointer, in which case it is not used.
base Numerical base (radix) that determines the valid characters and their interpretation. If this is 0, the base used is determined by the format in the sequence (see strtol for details).
Returns:
On success, the function returns the converted integral number as an unsigned long long int value. If no valid conversion could be performed, a zero value is returned (0ULL).
footer

otStudio - Library Reference - (C) 2020-25 Officina Turini, All Rights Reserved
Document built with Doxygen 1.4.0