String classification and conversion


Detailed Description

The char family of functions check whether ch, which must have the value of an unsigned char, falls into a certain character class.

The str_check family of functions extend the classification of single characters to strings. The str_check() function checks the string pointed to by str for a set of allowed character classes. As soon as a character is found that is not allowed checking stops and 0 is returned.

The str_cmp() function compares the string pointed to by str1 to the string pointed to by str2. It returns an integer less than, equal to, or greater than zero if str1 is found, respectively, to be less than, to match, or be greater than str2.

The strcpy() function copies the string pointed to by src (including the terminating `\0' character) to the array pointed to by dst. The strings may not overlap, and the destination string dst must be large enough to receive the copy. The strncpy() function is similar, except that not more than n bytes of src are copied. Thus, if there is no null byte among the first n bytes of src, the result will not be null-terminated.

The str_dup() function returns a pointer to a new string which is a duplicate of the string str. The str_dupn() function is similar, but only copies at most n characters. If s is longer than n, only n characters are copied, and a terminating null byte is added.

The str_index() returns a pointer to the first occurence of the character c in the string pointed to by str.

The str_len() function calculates the length of the string str, not including the terminating `\0' character.

The str_path_concat() function concatenates the directory name pointed to by dirname and file name pointed to by basename and checks that the latter does not contain any dot entries.

The str_path_isabs() and str_path_isdot() functions check if the file path pointed to by str is absolute or contains dots, respectively.

The str_toupper() and str_tolower() functions map lower-case to upper case and vice-versa, respectively.

The str_zero() function sets the first n bytes of the byte area starting at s to zero (bytes containing '\0').

The str_toumax() function converts the string pointed to by str to an unsigned long long int val using base as conversion base.


Defines

#define char_isascii(ch)   ((unsigned int)(ch) < 128u)
 check for an ASCII character
#define char_isblank(ch)   (ch == ' ' || ch == '\t')
 check for a blank character (space, horizontal tab)
#define char_iscntrl(ch)   ((unsigned int)(ch) < 32u || ch == 127)
 check for an ASCII control character
#define char_isdigit(ch)   ((unsigned int)(ch - '0') < 10u)
 check for a digit character (0-9)
#define char_isgraph(ch)   ((unsigned int)(ch - '!') < 94u)
 check for graphable characters (excluding space)
#define char_islower(ch)   ((unsigned int)(ch - 'a') < 26u)
 check for a lower-case character
#define char_isprint(ch)   ((unsigned int)(ch - ' ') < 95u)
 check for a printable character (including space)
#define char_isspace(ch)   ((unsigned int)(ch - '\t') < 5u || ch == ' ')
 check for a whitespace character (\t, \n, \v, \f, \r)
#define char_isupper(ch)   ((unsigned int)(ch - 'A') < 26u)
 check for an upper-case character
#define char_isxdigit(ch)
 check for a hexadecimal character
#define char_isalpha(ch)   (char_islower(ch) || char_isupper(ch))
 check for an upper- or lower-case character
#define char_isalnum(ch)   (char_isalpha(ch) || char_isdigit(ch))
 check for an upper-, lower-case or digit character
#define char_ispunct(ch)
 check for a punctuation character
#define char_tolower(ch)   do { if (char_isupper(ch)) ch += 32; } while(0)
 convert character to lower-case
#define char_toupper(ch)   do { if (char_islower(ch)) ch -= 32; } while(0)
 convert character to upper-case
#define CC_ALNUM   (1 << 1)
 class for alpha-numerical characters
#define CC_ALPHA   (1 << 2)
 class for upper- or lower-case characters
#define CC_ASCII   (1 << 3)
 class for ASCII characters
#define CC_BLANK   (1 << 4)
 class for blank characters
#define CC_CNTRL   (1 << 5)
 class for ASCII control characters
#define CC_DIGIT   (1 << 6)
 class for digit characters
#define CC_GRAPH   (1 << 7)
 class for graphable characters
#define CC_LOWER   (1 << 8)
 class for lower-case characters
#define CC_PRINT   (1 << 9)
 class for printable characters
#define CC_PUNCT   (1 << 10)
 class for punctuation characters
#define CC_SPACE   (1 << 11)
 class for white space characters
#define CC_UPPER   (1 << 12)
 class for upper-case characters
#define CC_XDIGIT   (1 << 13)
 class for hexadecimal characters
#define str_isempty(str)   (!str || str_check(str, CC_SPACE))
 check if string is empty
#define str_isalnum(str)   str_check(str, CC_ALNUM)
 check string for alpha-numerical characters
#define str_isalpha(str)   str_check(str, CC_ALPHA)
 check string for upper- or lower-case characters
#define str_isascii(str)   str_check(str, CC_ASCII)
 check string for ASCII characters
#define str_isdigit(str)   str_check(str, CC_DIGIT)
 check string for digit characters
#define str_isgraph(str)   str_check(str, CC_GRAPH)
 check string for graphable characters
#define str_islower(str)   str_check(str, CC_LOWER)
 check string for lower-case characters
#define str_isprint(str)   str_check(str, CC_PRINT)
 check string for printable characters
#define str_isupper(str)   str_check(str, CC_UPPER)
 check string for upper-case characters
#define str_isxdigit(str)   str_check(str, CC_XDIGIT)
 check string for hexadecimal characters

Functions

int str_check (const char *str, int allowed)
 check string against classes of allowed characters
int str_cmp (const char *str1, const char *str2)
 compare two strings
int str_cpy (char *dst, const char *src)
 copy a string
int str_cpyn (void *dst, const void *src, int n)
 copy a string
char * str_dup (const char *str)
 duplicate a string
char * str_dupn (const char *str, int n)
 duplicate a string
char * str_index (const char *str, int c, int n)
 scan string for character
int str_len (const char *str)
 calculate the length of a string
void str_zero (void *str, int n)
 write zero-valued bytes
char * str_path_concat (const char *dirname, const char *basename)
 concatenate dirname and basename
int str_path_isabs (const char *str)
 check if path is absolute and contains no dot entries or ungraphable characters
int str_path_isdot (const char *str)
 check if given path contains . or .. entries
char * str_tolower (char *str)
 convert string to lower-case
char * str_toupper (char *str)
 convert string to upper-case
int str_toumax (const char *str, unsigned long long int *val, int base, int n)
 convert string to integer


Define Documentation

#define char_isascii ( ch   )     ((unsigned int)(ch) < 128u)

check for an ASCII character

Definition at line 75 of file str.h.

Referenced by str_check().

#define char_isblank ( ch   )     (ch == ' ' || ch == '\t')

check for a blank character (space, horizontal tab)

Definition at line 78 of file str.h.

Referenced by str_check().

#define char_iscntrl ( ch   )     ((unsigned int)(ch) < 32u || ch == 127)

check for an ASCII control character

Definition at line 81 of file str.h.

Referenced by str_check().

#define char_isdigit ( ch   )     ((unsigned int)(ch - '0') < 10u)

check for a digit character (0-9)

Definition at line 84 of file str.h.

Referenced by str_check().

#define char_isgraph ( ch   )     ((unsigned int)(ch - '!') < 94u)

check for graphable characters (excluding space)

Definition at line 87 of file str.h.

Referenced by str_check().

#define char_islower ( ch   )     ((unsigned int)(ch - 'a') < 26u)

check for a lower-case character

Definition at line 90 of file str.h.

Referenced by str_check().

#define char_isprint ( ch   )     ((unsigned int)(ch - ' ') < 95u)

check for a printable character (including space)

Definition at line 93 of file str.h.

Referenced by str_check().

#define char_isspace ( ch   )     ((unsigned int)(ch - '\t') < 5u || ch == ' ')

check for a whitespace character (\t, \n, \v, \f, \r)

Definition at line 96 of file str.h.

Referenced by _lucid_vsscanf(), argv_from_str(), flist32_from_str(), flist64_from_str(), str_check(), and str_toumax().

#define char_isupper ( ch   )     ((unsigned int)(ch - 'A') < 26u)

check for an upper-case character

Definition at line 99 of file str.h.

Referenced by str_check().

#define char_isxdigit ( ch   ) 

Value:

(char_isdigit(ch) || \
                          (unsigned int)(ch - 'a') < 6u || \
                          (unsigned int)(ch - 'A') < 6u)
check for a hexadecimal character

Definition at line 102 of file str.h.

Referenced by str_check().

#define char_isalpha ( ch   )     (char_islower(ch) || char_isupper(ch))

check for an upper- or lower-case character

Definition at line 108 of file str.h.

Referenced by str_check().

#define char_isalnum ( ch   )     (char_isalpha(ch) || char_isdigit(ch))

check for an upper-, lower-case or digit character

Definition at line 111 of file str.h.

Referenced by str_check().

#define char_ispunct ( ch   ) 

Value:

(char_isprint(ch) && \
                         !char_isalnum(ch) && \
                         !char_isspace(ch))
check for a punctuation character

Definition at line 114 of file str.h.

Referenced by str_check().

#define char_tolower ( ch   )     do { if (char_isupper(ch)) ch += 32; } while(0)

convert character to lower-case

Definition at line 120 of file str.h.

Referenced by str_tolower().

#define char_toupper ( ch   )     do { if (char_islower(ch)) ch -= 32; } while(0)

convert character to upper-case

Definition at line 123 of file str.h.

Referenced by str_toupper().

#define CC_ALNUM   (1 << 1)

class for alpha-numerical characters

Definition at line 127 of file str.h.

Referenced by str_check().

#define CC_ALPHA   (1 << 2)

class for upper- or lower-case characters

Definition at line 130 of file str.h.

Referenced by str_check().

#define CC_ASCII   (1 << 3)

class for ASCII characters

Definition at line 133 of file str.h.

Referenced by str_check().

#define CC_BLANK   (1 << 4)

class for blank characters

Definition at line 136 of file str.h.

Referenced by str_check().

#define CC_CNTRL   (1 << 5)

class for ASCII control characters

Definition at line 139 of file str.h.

Referenced by str_check().

#define CC_DIGIT   (1 << 6)

class for digit characters

Definition at line 142 of file str.h.

Referenced by str_check().

#define CC_GRAPH   (1 << 7)

class for graphable characters

Definition at line 145 of file str.h.

Referenced by str_check().

#define CC_LOWER   (1 << 8)

class for lower-case characters

Definition at line 148 of file str.h.

Referenced by str_check().

#define CC_PRINT   (1 << 9)

class for printable characters

Definition at line 151 of file str.h.

Referenced by str_check().

#define CC_PUNCT   (1 << 10)

class for punctuation characters

Definition at line 154 of file str.h.

Referenced by str_check().

#define CC_SPACE   (1 << 11)

class for white space characters

Definition at line 157 of file str.h.

Referenced by str_check().

#define CC_UPPER   (1 << 12)

class for upper-case characters

Definition at line 160 of file str.h.

Referenced by str_check().

#define CC_XDIGIT   (1 << 13)

class for hexadecimal characters

Definition at line 163 of file str.h.

Referenced by str_check().

#define str_isempty ( str   )     (!str || str_check(str, CC_SPACE))

check if string is empty

Definition at line 176 of file str.h.

Referenced by addr_from_str(), flist32_from_str(), flist64_from_str(), mkdirnamep(), mkdirp(), str_path_concat(), str_path_isabs(), and str_path_isdot().

#define str_isalnum ( str   )     str_check(str, CC_ALNUM)

check string for alpha-numerical characters

Definition at line 179 of file str.h.

#define str_isalpha ( str   )     str_check(str, CC_ALPHA)

check string for upper- or lower-case characters

Definition at line 182 of file str.h.

#define str_isascii ( str   )     str_check(str, CC_ASCII)

check string for ASCII characters

Definition at line 185 of file str.h.

#define str_isdigit ( str   )     str_check(str, CC_DIGIT)

check string for digit characters

Definition at line 188 of file str.h.

Referenced by addr_from_str().

#define str_isgraph ( str   )     str_check(str, CC_GRAPH)

check string for graphable characters

Definition at line 191 of file str.h.

Referenced by str_path_isabs().

#define str_islower ( str   )     str_check(str, CC_LOWER)

check string for lower-case characters

Definition at line 194 of file str.h.

#define str_isprint ( str   )     str_check(str, CC_PRINT)

check string for printable characters

Definition at line 197 of file str.h.

#define str_isupper ( str   )     str_check(str, CC_UPPER)

check string for upper-case characters

Definition at line 200 of file str.h.

#define str_isxdigit ( str   )     str_check(str, CC_XDIGIT)

check string for hexadecimal characters

Definition at line 203 of file str.h.


Function Documentation

int str_check ( const char *  str,
int  allowed 
)

check string against classes of allowed characters

Parameters:
[in] str string to check
[in] allowed allowed classes of characters (multiple classes by ORing)
Returns:
1 if all characters are valid, 0 otherwise

Definition at line 20 of file str_check.c.

References CC_ALNUM, CC_ALPHA, CC_ASCII, CC_BLANK, CC_CNTRL, CC_DIGIT, CC_GRAPH, CC_LOWER, CC_PRINT, CC_PUNCT, CC_SPACE, CC_UPPER, CC_XDIGIT, char_isalnum, char_isalpha, char_isascii, char_isblank, char_iscntrl, char_isdigit, char_isgraph, char_islower, char_isprint, char_ispunct, char_isspace, char_isupper, char_isxdigit, and str_len().

00021 {
00022         int i, n;
00023         
00024         if (!str)
00025                 return 1;
00026         
00027         n = str_len(str);
00028         
00029         for (i = 0; i < n; i++) {
00030                 if (allowed & CC_ALNUM  && char_isalnum (str[i])) continue;
00031                 if (allowed & CC_ALPHA  && char_isalpha (str[i])) continue;
00032                 if (allowed & CC_ASCII  && char_isascii (str[i])) continue;
00033                 if (allowed & CC_BLANK  && char_isblank (str[i])) continue;
00034                 if (allowed & CC_CNTRL  && char_iscntrl (str[i])) continue;
00035                 if (allowed & CC_DIGIT  && char_isdigit (str[i])) continue;
00036                 if (allowed & CC_GRAPH  && char_isgraph (str[i])) continue;
00037                 if (allowed & CC_LOWER  && char_islower (str[i])) continue;
00038                 if (allowed & CC_PRINT  && char_isprint (str[i])) continue;
00039                 if (allowed & CC_PUNCT  && char_ispunct (str[i])) continue;
00040                 if (allowed & CC_SPACE  && char_isspace (str[i])) continue;
00041                 if (allowed & CC_UPPER  && char_isupper (str[i])) continue;
00042                 if (allowed & CC_XDIGIT && char_isxdigit(str[i])) continue;
00043                 
00044                 return 0;
00045         }
00046         
00047         return 1;
00048 }

int str_cmp ( const char *  str1,
const char *  str2 
)

compare two strings

Parameters:
[in] str1 first string
[in] str2 second string
Returns:
An integer greater than, equal to, or less than 0, if the string pointed to by str1 is greater than, equal to, or less than the string pointed to by str2, respectively.

Definition at line 20 of file str_cmp.c.

Referenced by flist32_getval(), flist64_getval(), and str_path_isdot().

00021 {
00022         while (*str1 && *str2 && *str1 == *str2)
00023                 str1++, str2++;
00024         
00025         return *str1 - *str2;
00026 }

int str_cpy ( char *  dst,
const char *  src 
)

copy a string

Parameters:
[out] dst destination string
[in] src source string
Returns:
Number of bytes that have been copied.

Definition at line 20 of file str_cpy.c.

References str_cpyn(), and str_len().

00021 {
00022         return str_cpyn(dst, src, str_len(src));
00023 }

int str_cpyn ( void *  dst,
const void *  src,
int  n 
)

copy a string

Parameters:
[out] dst destination string
[in] src source string
[in] n copy first n bytes
Returns:
Number of bytes that have been copied.

Definition at line 20 of file str_cpyn.c.

Referenced by log_init(), str_cpy(), str_dupn(), stralloc_catb(), stralloc_copyb(), and whirlpool_finalize().

00021 {
00022         char *d = dst;
00023         const char *s = src;
00024         
00025         while (n--)
00026                 *d++ = *s++;
00027         
00028         return d - (char *) dst;
00029 }

char* str_dup ( const char *  str  ) 

duplicate a string

Parameters:
[in] str source string
Returns:
A pointer to the duplicated string, or NULL if insufficient memory was available.

Definition at line 20 of file str_dup.c.

References str_dupn(), and str_len().

Referenced by flist32_from_str(), flist64_from_str(), mkdirnamep(), mkdirp(), str_path_isabs(), and str_path_isdot().

00021 {
00022         return str_dupn(str, str_len(str));
00023 }

char* str_dupn ( const char *  str,
int  n 
)

duplicate a string

Parameters:
[in] str source string
[in] n string length
Returns:
A pointer to the duplicated string, or NULL if insufficient memory was available.

Definition at line 22 of file str_dupn.c.

References str_cpyn().

Referenced by argv_to_str(), flist32_to_str(), flist64_to_str(), io_read_eof(), io_read_eol(), io_read_len(), str_dup(), and whirlpool_digest().

00023 {
00024         char *buf = calloc(n + 1, sizeof(char));
00025         
00026         if (buf)
00027                 str_cpyn(buf, str, n);
00028         
00029         return buf;
00030 }

char* str_index ( const char *  str,
int  c,
int  n 
)

scan string for character

Parameters:
[in] str string to scan
[in] c character to look for
[in] n scan first n bytes
Returns:
A pointer to the matched character or NULL if the character is not found.

Definition at line 20 of file str_index.c.

Referenced by _lucid_vsnprintf(), addr_from_str(), flist32_from_str(), flist64_from_str(), str_path_isabs(), and str_path_isdot().

00021 {
00022         char *p = (char *) str;
00023         
00024         for (; n; p++, n--)
00025                 if (*p == c)
00026                         return p;
00027         
00028         return 0;
00029 }

int str_len ( const char *  str  ) 

calculate the length of a string

Parameters:
[in] str source string
Returns:
number of characters in str

Definition at line 20 of file str_len.c.

Referenced by _lucid_vsnprintf(), _lucid_vsscanf(), addr_from_str(), flist32_from_str(), flist64_from_str(), log_init(), str_check(), str_cpy(), str_dup(), str_path_isabs(), str_path_isdot(), stralloc_cats(), stralloc_copys(), and whirlpool_digest().

00021 {
00022         int i = 0;
00023         
00024         while (*str++)
00025                 i++;
00026         
00027         return i;
00028 }

void str_zero ( void *  str,
int  n 
)

write zero-valued bytes

Parameters:
[out] str destination string
[in] n write first n bytes

Definition at line 20 of file str_zero.c.

Referenced by tcp_connect(), tcp_listen(), whirlpool_finalize(), and whirlpool_init().

00021 {
00022         char *p = str;
00023         
00024         while (n--)
00025                 *p++ = 0;
00026 }

char* str_path_concat ( const char *  dirname,
const char *  basename 
)

concatenate dirname and basename

Parameters:
[in] dirname directory part
[in] basename basename part
Returns:
A pointer to the newly allocated string or NULL if insufficent memory was available.

Definition at line 22 of file str_path_concat.c.

References _lucid_asprintf(), str_isempty, and str_path_isdot().

00023 {
00024         char *path = 0;
00025         
00026         if (str_isempty(dirname) || str_path_isdot(basename))
00027                 return 0;
00028         
00029         _lucid_asprintf(&path, "%s/%s", dirname, basename);
00030         
00031         return path;
00032 }

int str_path_isabs ( const char *  str  ) 

check if path is absolute and contains no dot entries or ungraphable characters

Parameters:
[in] str path to check
Returns:
1 if str is an absolute pathname, 0 otherwise
Note:
this function does not check if the path exists

Definition at line 22 of file str_path_isabs.c.

References str_dup(), str_index(), str_isempty, str_isgraph, str_len(), and str_path_isdot().

00023 {
00024         char *buf, *p, *o;
00025         
00026         if (str_isempty(str))
00027                 return 0;
00028         
00029         if (*str != '/')
00030                 return 0;
00031         
00032         if (str_path_isdot(str))
00033                 return 0;
00034         
00035         buf = p = o = str_dup(str);
00036         
00037         while (1) {
00038                 p = str_index(p, '/', str_len(p));
00039                 
00040                 if (p)
00041                         *p++ = '\0';
00042                 
00043                 if (!str_isgraph(o)) {
00044                         free(buf);
00045                         return 0;
00046                 }
00047                 
00048                 if (!p)
00049                         break;
00050                 else
00051                         o = p;
00052         }
00053         
00054         free(buf);
00055         return 1;
00056 }

int str_path_isdot ( const char *  str  ) 

check if given path contains . or .. entries

Parameters:
[in] str path to check
Returns:
1 if str has dot entries, 0 otherwise

Definition at line 22 of file str_path_isdot.c.

References str_cmp(), str_dup(), str_index(), str_isempty, and str_len().

Referenced by str_path_concat(), and str_path_isabs().

00023 {
00024         char *buf, *p, *o;
00025         
00026         if (str_isempty(str))
00027                 return 0;
00028         
00029         buf = p = o = str_dup(str);
00030         
00031         while (1) {
00032                 p = str_index(p, '/', str_len(p));
00033                 
00034                 if (p)
00035                         *p++ = '\0';
00036                 
00037                 if (str_cmp(o, ".") == 0 || str_cmp(o, "..") == 0) {
00038                         free(buf);
00039                         return 1;
00040                 }
00041                 
00042                 if (!p)
00043                         break;
00044                 else
00045                         o = p;
00046         }
00047         
00048         free(buf);
00049         return 0;
00050 }

char* str_tolower ( char *  str  ) 

convert string to lower-case

Parameters:
[out] str string to convert
Returns:
pointer to str

Definition at line 20 of file str_tolower.c.

References char_tolower.

00021 {
00022         char *p = str;
00023         
00024         while (*p) {
00025                 char_tolower(*p);
00026                 p++;
00027         }
00028         
00029         return str;
00030 }

char* str_toupper ( char *  str  ) 

convert string to upper-case

Parameters:
[out] str string to convert
Returns:
pointer to str

Definition at line 20 of file str_toupper.c.

References char_toupper.

00021 {
00022         char *p = str;
00023         
00024         while (*p) {
00025                 char_toupper(*p);
00026                 p++;
00027         }
00028         
00029         return str;
00030 }

int str_toumax ( const char *  str,
unsigned long long int *  val,
int  base,
int  n 
)

convert string to integer

Parameters:
[in] str source string
[out] val destination integer
[in] base conversion base
[in] n convert first n bytes
Returns:
Number of bytes read from str

Definition at line 36 of file str_toumax.c.

References char_isspace.

Referenced by _lucid_vsscanf(), and addr_from_str().

00037 {
00038         char c;
00039         const char *p = str;
00040         int d, minus = 0;
00041         unsigned long long int v = 0;
00042         
00043         while (n && char_isspace((unsigned char) *p)) {
00044                 p++;
00045                 n--;
00046         }
00047         
00048         /* Single optional + or - */
00049         if (n) {
00050                 c = *p;
00051                 
00052                 if (c == '-' || c == '+') {
00053                         minus = (c == '-');
00054                         p++;
00055                         n--;
00056                 }
00057         }
00058         
00059         if (base == 0) {
00060                 if (n >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
00061                         n -= 2;
00062                         p += 2;
00063                         base = 16;
00064                 }
00065                 
00066                 else if (n >= 1 && p[0] == '0') {
00067                         n--;
00068                         p++;
00069                         base = 8;
00070                 }
00071                 
00072                 else {
00073                         base = 10;
00074                 }
00075         }
00076         
00077         else if (base == 16) {
00078                 if (n >= 2 && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
00079                         n -= 2;
00080                         p += 2;
00081                 }
00082         }
00083         
00084         while (n && (d = char_todigit(*p)) >= 0 && d < base) {
00085                 v = v * base + d;
00086                 n--;
00087                 p++;
00088         }
00089         
00090         if (p - str > 0)
00091                 *val = minus ? -v : v;
00092         
00093         return p - str;
00094 }


Generated on Sun Dec 3 17:46:16 2006 for lucid by  doxygen 1.5.1