Mstdlib-1.24.0
String Checking/Validation

Functions

const char * M_str_safe (const char *s)
 
M_bool M_str_isempty (const char *s) M_WARN_UNUSED_RESULT
 
M_bool M_str_istrue (const char *s) M_WARN_UNUSED_RESULT
 
size_t M_str_len (const char *s) M_WARN_UNUSED_RESULT
 
size_t M_str_len_max (const char *s, size_t max) M_WARN_UNUSED_RESULT
 
M_bool M_str_ispredicate (const char *s, M_chr_predicate_func pred)
 
M_bool M_str_isalnum (const char *s) M_WARN_UNUSED_RESULT
 
M_bool M_str_isalnumsp (const char *s) M_WARN_UNUSED_RESULT
 
M_bool M_str_isalpha (const char *s) M_WARN_UNUSED_RESULT
 
M_bool M_str_isalphasp (const char *s) M_WARN_UNUSED_RESULT
 
M_bool M_str_isspace (const char *s) M_WARN_UNUSED_RESULT
 
M_bool M_str_isascii (const char *s) M_WARN_UNUSED_RESULT
 
M_bool M_str_isgraph (const char *s) M_WARN_UNUSED_RESULT
 
M_bool M_str_isprint (const char *s) M_WARN_UNUSED_RESULT
 
M_bool M_str_ishex (const char *s) M_WARN_UNUSED_RESULT
 
M_bool M_str_isnum (const char *s) M_WARN_UNUSED_RESULT
 
M_bool M_str_isdec (const char *s) M_WARN_UNUSED_RESULT
 
M_bool M_str_ismoney (const char *s) M_WARN_UNUSED_RESULT
 
M_bool M_str_ischarset (const char *str, const char *charset)
 
M_bool M_str_isnotcharset (const char *str, const char *charset)
 
M_bool M_str_isstr (const unsigned char *s, size_t len)
 
M_bool M_str_ispredicate_max (const char *s, size_t max, M_chr_predicate_func pred)
 
M_bool M_str_isalnum_max (const char *s, size_t max) M_WARN_UNUSED_RESULT
 
M_bool M_str_isalnumsp_max (const char *s, size_t max) M_WARN_UNUSED_RESULT
 
M_bool M_str_isalpha_max (const char *s, size_t max) M_WARN_UNUSED_RESULT
 
M_bool M_str_isalphasp_max (const char *s, size_t max) M_WARN_UNUSED_RESULT
 
M_bool M_str_isspace_max (const char *s, size_t max) M_WARN_UNUSED_RESULT
 
M_bool M_str_isgraph_max (const char *s, size_t max) M_WARN_UNUSED_RESULT
 
M_bool M_str_isprint_max (const char *s, size_t max) M_WARN_UNUSED_RESULT
 
M_bool M_str_ishex_max (const char *s, size_t max) M_WARN_UNUSED_RESULT
 
M_bool M_str_isnum_max (const char *s, size_t max) M_WARN_UNUSED_RESULT
 
M_bool M_str_isdec_max (const char *s, size_t max) M_WARN_UNUSED_RESULT
 
int M_str_cmpsort (const char *s1, const char *s2) M_WARN_UNUSED_RESULT
 
int M_str_cmpsort_max (const char *s1, const char *s2, size_t max) M_WARN_UNUSED_RESULT
 
int M_str_casecmpsort (const char *s1, const char *s2) M_WARN_UNUSED_RESULT
 
int M_str_casecmpsort_max (const char *s1, const char *s2, size_t max) M_WARN_UNUSED_RESULT
 
M_bool M_str_eq_max (const char *s1, const char *s2, size_t max)
 
M_bool M_str_eq (const char *s1, const char *s2)
 
M_bool M_str_caseeq_max (const char *s1, const char *s2, size_t max)
 
M_bool M_str_caseeq (const char *s1, const char *s2)
 
M_bool M_str_eq_end (const char *s1, const char *s2)
 
M_bool M_str_caseeq_end (const char *s1, const char *s2)
 
M_bool M_str_eq_start (const char *s1, const char *s2)
 
M_bool M_str_caseeq_start (const char *s1, const char *s2)
 
M_bool M_str_pattern_match (const char *pattern, const char *s) M_WARN_UNUSED_RESULT
 
M_bool M_str_case_pattern_match (const char *pattern, const char *s) M_WARN_UNUSED_RESULT
 

Detailed Description

String Checking and Validation

Function Documentation

◆ M_str_safe()

const char * M_str_safe ( const char *  s)

Ensure a NULL will not be used as a string.

Parameters
[in]sNULL-terminated string.
Returns
"" if s is NULL or s.

◆ M_str_isempty()

M_bool M_str_isempty ( const char *  s)

Determines if the string is considered empty.

A string is considered empty if it is NULL or has a 0 length.

Parameters
[in]sNULL-terminated string.
Returns
M_TRUE if it is empty. Otherwise M_FALSE.

◆ M_str_istrue()

M_bool M_str_istrue ( const char *  s)

Check if a string is considered true.

A string is considered true when it equals any of the following (case insensitive):

  • t
  • true
  • y
  • yes
  • 1
  • on
Parameters
[in]sNULL-terminated string.
Returns
M_TRUE if it is considered true. Otherwise M_FALSE.

◆ M_str_len()

size_t M_str_len ( const char *  s)

A wrapper around strlen that treats NULL as a string with length 0.

Parameters
[in]sNULL-terminated string.
Returns
Length of string.

◆ M_str_len_max()

size_t M_str_len_max ( const char *  s,
size_t  max 
)

A wrapper around strlen that treats NULL as a string with length 0, but returns at most max bytes.

Parameters
[in]sNULL-terminated string.
[in]maxMaximum number of bytes to return.
Returns
Length of string up to max bytes.

◆ M_str_ispredicate()

M_bool M_str_ispredicate ( const char *  s,
M_chr_predicate_func  pred 
)

Determines if all characters of string s satisfy predicate pred.

Parameters
[in]sNULL-terminated string.
[in]predPredicate to apply to each character.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isalnum()

M_bool M_str_isalnum ( const char *  s)

Check whether each character of a string s are alphanumeric.

Parameters
[in]sNULL-terminated string.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isalnumsp()

M_bool M_str_isalnumsp ( const char *  s)

Check whether each character of a string s ar alphanumeric or contains spaces.

Parameters
[in]sNULL-terminated string.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isalpha()

M_bool M_str_isalpha ( const char *  s)

Check whether each character of a string s is alpha.

Parameters
[in]sNULL-terminated string.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isalphasp()

M_bool M_str_isalphasp ( const char *  s)

Check whether each character of a string s is alpha or contains spaces.

Parameters
[in]sNULL-terminated string.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isspace()

M_bool M_str_isspace ( const char *  s)

Check whether each character of a string s is a space.

Parameters
[in]sNULL-terminated string.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isascii()

M_bool M_str_isascii ( const char *  s)

Checks for 7-bit ASCII character.

Parameters
[in]sNULL-terminated string.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isgraph()

M_bool M_str_isgraph ( const char *  s)

Check whether each character of a string s is printable except space.

Parameters
[in]sNULL-terminated string.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isprint()

M_bool M_str_isprint ( const char *  s)

Check whether each character of a string s is printable.

Parameters
[in]sNULL-terminated string.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_ishex()

M_bool M_str_ishex ( const char *  s)

Check whether each character of a string s is a hexadecimal digit.

Parameters
[in]sNULL-terminated string.
Returns
M_TRUE if all characters match. Otherwise M_FALSE. otherwise M_TRUE

◆ M_str_isnum()

M_bool M_str_isnum ( const char *  s)

Check whether each character of a string s is a decimal digit 0-9.

Parameters
[in]sNULL-terminated string.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isdec()

M_bool M_str_isdec ( const char *  s)

Check whether each character of a string s is a decimal digit 0-9 or contains a decimal.

Parameters
[in]sNULL-terminated string.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_ismoney()

M_bool M_str_ismoney ( const char *  s)

Check whether each character of a string s is a money amount.

Assumes no more than 2 decimal places but does not require 2 decimial digits.

Parameters
[in]sNULL-terminated string.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_ischarset()

M_bool M_str_ischarset ( const char *  str,
const char *  charset 
)

Check whether each character of a string is in the given character set.

Parameters
[in]strstring to check (NULL-terminated).
[in]charsetlist of characters that are allowed in str (NULL-terminated).
Returns
M_TRUE if all characters in str match at least one char in charset.

◆ M_str_isnotcharset()

M_bool M_str_isnotcharset ( const char *  str,
const char *  charset 
)

Check whether each character of a string is not in the given character set.

Parameters
[in]strstring to check (NULL-terminated).
[in]charsetlist of characters that are not allowed in str (NULL-terminated).
Returns
M_TRUE if none of the characters in charset are present in str.

◆ M_str_isstr()

M_bool M_str_isstr ( const unsigned char *  s,
size_t  len 
)

Check whether or not the data provided is a string.

This is useful for parsing binary protocols that contain string data as a verification. The length passed in is the size of the buffer, the last byte of the buffer must be a NULL terminator or this function will fail (This means, of course, the string length should be exactly 1 byte less than the provided buffer size). Then the remainder of the buffer will be checked for printable data, otherwise it is not considered a string.

Parameters
[in]sBuffer to see if data is a null-terminated string
[in]lenSize of buffer, including NULL terminator
Returns
M_TRUE if buffer contains a string, M_FALSE otherwise

◆ M_str_ispredicate_max()

M_bool M_str_ispredicate_max ( const char *  s,
size_t  max,
M_chr_predicate_func  pred 
)

Determines if the first max characters of string s satisfy predicate pred up to max bytes.

Parameters
[in]sNULL-terminated string.
[in]maxMax number of characters to process of s.
[in]predPredicate to apply to each character.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isalnum_max()

M_bool M_str_isalnum_max ( const char *  s,
size_t  max 
)

Check whether each character of a string s are alphanumeric up to at most max bytes.

Parameters
[in]sNULL-terminated string.
[in]maxMaximum number of bytes.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isalnumsp_max()

M_bool M_str_isalnumsp_max ( const char *  s,
size_t  max 
)

Check whether each character of a string s ar alphanumeric or contains spaces up to at most max bytes.

Parameters
[in]sNULL-terminated string.
[in]maxMaximum number of bytes.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isalpha_max()

M_bool M_str_isalpha_max ( const char *  s,
size_t  max 
)

Check whether each character of a string s is alpha up to at most max bytes.

Parameters
[in]sNULL-terminated string.
[in]maxMaximum number of bytes.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isalphasp_max()

M_bool M_str_isalphasp_max ( const char *  s,
size_t  max 
)

Check whether each character of a string s is alpha or contains spaces up to at most max bytes.

Parameters
[in]sNULL-terminated string.
[in]maxMaximum number of bytes.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isspace_max()

M_bool M_str_isspace_max ( const char *  s,
size_t  max 
)

Check whether each character of a string s is a space up to at most max bytes.

Parameters
[in]sNULL-terminated string.
[in]maxMaximum number of bytes.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isgraph_max()

M_bool M_str_isgraph_max ( const char *  s,
size_t  max 
)

Check whether each character of a string s is printable except space up to at most max bytes.

Parameters
[in]sNULL-terminated string.
[in]maxMaximum number of bytes.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isprint_max()

M_bool M_str_isprint_max ( const char *  s,
size_t  max 
)

Check whether each character of a string s is printable up to at most max bytes.

Parameters
[in]sNULL-terminated string.
[in]maxMaximum number of bytes.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_ishex_max()

M_bool M_str_ishex_max ( const char *  s,
size_t  max 
)

Check whether each character of a string s is a hexadecimal digit up to at most max bytes.

Parameters
[in]sNULL-terminated string.
[in]maxMaximum number of bytes.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isnum_max()

M_bool M_str_isnum_max ( const char *  s,
size_t  max 
)

Check whether each character of a string s is a decimal digit 0-9 up to at most max bytes.

Parameters
[in]sNULL-terminated string.
[in]maxMaximum number of bytes.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_isdec_max()

M_bool M_str_isdec_max ( const char *  s,
size_t  max 
)

Check whether each character of a string s is a decimal digit 0-9 or contains a decimal up to at most max bytes.

Parameters
[in]sNULL-terminated string.
[in]maxMaximum number of bytes.
Returns
M_TRUE if all characters match. Otherwise M_FALSE.

◆ M_str_cmpsort()

int M_str_cmpsort ( const char *  s1,
const char *  s2 
)

A wrapper around strcmp that treats NULL as an empty string.

NOTE: this is not a constant-time comparison and thus should ONLY be used for sorting such as within qsort()!

Parameters
[in]s1NULL-terminated string.
[in]s2NULL-terminated string.
Returns
An integer less than, equal to, or greater than zero if s1 is less than, equal, or greater than s2 respectively.

◆ M_str_cmpsort_max()

int M_str_cmpsort_max ( const char *  s1,
const char *  s2,
size_t  max 
)

A wrapper around strcmp that treats NULL as an empty string, but limited to max characters.

NOTE: this is not a constant-time comparison and thus should ONLY be used for sorting such as within qsort()!

Parameters
[in]s1NULL-terminated string.
[in]s2NULL-terminated string.
[in]maxMax number of characters to process of s.
Returns
An integer less than, equal to, or greater than zero if s1 is less than, equal, or greater than s2 respectively.

◆ M_str_casecmpsort()

int M_str_casecmpsort ( const char *  s1,
const char *  s2 
)

A wrapper around strcmp that treats NULL as an empty string and compares case insensitive.

NOTE: this is not a constant-time comparison and thus should ONLY be used for sorting such as within qsort()!

Parameters
[in]s1NULL-terminated string.
[in]s2NULL-terminated string.
Returns
An integer less than, equal to, or greater than zero if s1 is less than, equal, or greater than s2 respectively.

◆ M_str_casecmpsort_max()

int M_str_casecmpsort_max ( const char *  s1,
const char *  s2,
size_t  max 
)

A wrapper around strcmp that treats NULL as an empty string and compares case insensitive, but limited to max characters.

NOTE: this is not a constant-time comparison and thus should ONLY be used for sorting such as within qsort()!

Parameters
[in]s1NULL-terminated string.
[in]s2NULL-terminated string.
[in]maxMax number of characters to process of s.
Returns
An integer less than, equal to, or greater than zero if s1 is less than, equal, or greater than s2 respectively.

◆ M_str_eq_max()

M_bool M_str_eq_max ( const char *  s1,
const char *  s2,
size_t  max 
)

Comparison for string equality.

This implementation is constant-time meaning it should not be vulnerable to timing-based attacks. Limited to first max bytes. NULL and "" are considered equal strings.

s1 will be evaluated to max regardless of the length of s2. s1 should be the input (user) string and s2 the known (internal) string. This order causes any timing information from this function to be the timing of reading s1. The length of s2 cannot be determined using this order. If s1 is the known (internal) string it is possible, though highly unlikely, to determine the length. While the evaluation will be constant time it will always be the time to scan the known string.

Parameters
[in]s1NULL-terminated string. Should be (user) input.
[in]s2NULL-terminated string. Should be known (internal) string.
[in]maxmaximum length to check.
Returns
M_TRUE if equal, M_FALSE if not equal.

◆ M_str_eq()

M_bool M_str_eq ( const char *  s1,
const char *  s2 
)

Comparison for string equality.

This implementation is constant-time meaning it should not be vulnerable to timing-based attacks. NULL and "" are considered equal strings.

s1 will be evaluated to end regardless of the length of s2. s1 should be the input (user) string and s2 the known (internal) string. This order causes any timing information from this function to be the timing of reading s1. The length of s2 cannot be determined using this order. If s1 is the known (internal) string it is possible though highly unlikely, to determine the length. While the evaluation will be constant time it will always be the time to scan the known string.

Parameters
[in]s1NULL-terminated string. Should be (user) input.
[in]s2NULL-terminated string. Should be known (internal) string.
Returns
M_TRUE if equal, M_FALSE if not equal.

◆ M_str_caseeq_max()

M_bool M_str_caseeq_max ( const char *  s1,
const char *  s2,
size_t  max 
)

Comparison for string equality in a case-insensitive manner.

This implementation is constant-time meaning it should not be vulnerable to timing-based attacks. Limited to first max bytes. NULL and "" are considered equal strings.

s1 will be evaluated to max regardless of the length of s2. s1 should be the input (user) string and s2 the known (internal) string. This order causes any timing information from this function to be the timing of reading s1. The length of s2 cannot be determined though highly unlikely, to determine the length. While the evaluation will be constant time it will always be the time to scan the known string.

Parameters
[in]s1NULL-terminated string.
[in]s2NULL-terminated string.
[in]maxmaximum length to check.
Returns
M_TRUE if equal, M_FALSE if not equal.

◆ M_str_caseeq()

M_bool M_str_caseeq ( const char *  s1,
const char *  s2 
)

Comparison for string equality in a case-insensitive manner.

This implementation is constant-time meaning it should not be vulnerable to timing-based attacks. NULL and "" are considered equal strings.

s1 will be evaluated to end regardless of the length of s2. s1 should be the input (user) string and s2 the known (internal) string. This order causes any timing information from this function to be the timing of reading s1. The length of s2 cannot be determined though highly unlikely, to determine the length. While the evaluation will be constant time it will always be the time to scan the known string.

Parameters
[in]s1NULL-terminated string. Should be (user) input.
[in]s2NULL-terminated string. Should be known (internal) string.
Returns
M_TRUE if equal, M_FALSE if not equal.

◆ M_str_eq_end()

M_bool M_str_eq_end ( const char *  s1,
const char *  s2 
)

Determine if a string ends with a given string.

The input is slighly different than M_str_eq(). The input reads: Check that s1 ends with s2.

s1 length is never (

See also
M_str_eq) known because evaluation will always be the length of s2.
Parameters
[in]s1NULL-terminated string, or non-terminated string that's at least as long as s2. Input string.
[in]s2NULL-terminated string. Ending string that's being compared to s1.
Returns
M_TRUE if s1 ends with s2, otherwise M_FALSE;

◆ M_str_caseeq_end()

M_bool M_str_caseeq_end ( const char *  s1,
const char *  s2 
)

Determine if a string ends with a given string in a case-insensitive manner.

The input is slighly different than M_str_caseeq(). The input reads: Check that s1 ends with s2.

s1 length is never (

See also
M_str_caseeq) known because evaluation will always be the length of s2.
Parameters
[in]s1NULL-terminated string, or non-terminated string that's at least as long as s2. Input string.
[in]s2NULL-terminated string. Ending string that's being compared to s1.
Returns
M_TRUE if s1 ends with s2, otherwise M_FALSE;

◆ M_str_eq_start()

M_bool M_str_eq_start ( const char *  s1,
const char *  s2 
)

Determine if a string starts with a given string.

The input is slighly different than M_str_eq(). The input reads: Check that s1 starts with s2.

s1 length is never (

See also
M_str_eq) known because evaluation will always be the length of s2.
Parameters
[in]s1NULL-terminated string, or non-terminated string that's at least as long as s2. Input string.
[in]s2NULL-terminated string. Staring string that's being compared to s1.
Returns
M_TRUE if s1 starts with s2, otherwise M_FALSE.

◆ M_str_caseeq_start()

M_bool M_str_caseeq_start ( const char *  s1,
const char *  s2 
)

Determine if a string starts with a given string in a case-insensitive manner.

The input is slightly different than M_str_caseeq(). The input reads: Check that s1 starts with s2.

s1 length is never (see M_str_caseeq()) known because evaluation will always be the length of s2.

Parameters
[in]s1NULL-terminated string, or non-terminated string that's at least as long as s2. Input string.
[in]s2NULL-terminated string. Staring string that's being compared to s1.
Returns
M_TRUE if s1 starts with s2 (case insensitive), otherwise M_FALSE.

◆ M_str_pattern_match()

M_bool M_str_pattern_match ( const char *  pattern,
const char *  s 
)

Match pattern against string as per 'man 7 glob'.

We don't support newer POSIX functions like named character classes (e.g. [:lower:]), collating symbols, or equivalence class expressions

Parameters
[in]patternThe pattern to match using.
[in]sNULL-terminated string.
Returns
M_TRUE if the pattern matches the string otherwise M_FALSE.

◆ M_str_case_pattern_match()

M_bool M_str_case_pattern_match ( const char *  pattern,
const char *  s 
)

Match pattern against string as per 'man 7 glob' case insensitive.

We don't support newer POSIX functions like named character classes (e.g. [:lower:]), collating symbols, or equivalence class expressions

Parameters
[in]patternThe pattern to match using.
[in]sNULL-terminated string.
Returns
M_TRUE if the pattern matches the string otherwise M_FALSE.
See also
M_str_pattern_match