CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
Comparison

Functions

bool strEq (strref s1, strref s2)
 
bool strEqi (strref s1, strref s2)
 
int32 strCmp (strref s1, strref s2)
 
int32 strCmpi (strref s1, strref s2)
 
bool strRangeEq (strref str, strref sub, int32 off, uint32 len)
 
bool strRangeEqi (strref str, strref sub, int32 off, uint32 len)
 
int32 strRangeCmp (strref str, strref sub, int32 off, uint32 len)
 
int32 strRangeCmpi (strref str, strref sub, int32 off, uint32 len)
 
bool strBeginsWith (strref str, strref sub)
 
bool strBeginsWithi (strref str, strref sub)
 
bool strEndsWith (strref str, strref sub)
 
bool strEndsWithi (strref str, strref sub)
 

Detailed Description

String comparison functions for equality testing and lexicographic ordering. All comparison functions support case-sensitive and case-insensitive variants.

Case-insensitive comparisons use ASCII tolower() and do not properly handle multi-byte UTF-8 characters.

Function Documentation

◆ strBeginsWith()

bool strBeginsWith ( strref  str,
strref  sub 
)

Tests if a string begins with a specific prefix (case-sensitive)

Checks if the first bytes of str match sub. This is equivalent to strRangeEq(str, sub, 0, strLen(sub)).

Parameters
strString to check
subPrefix to test for
Returns
true if str starts with sub, false otherwise

Example:

if (strBeginsWith(path, _S"/home/")) {
// Path starts with /home/
}
bool strBeginsWith(strref str, strref sub)
#define _S
Creates a static ASCII string from a string literal.
Definition strbase.h:392

◆ strBeginsWithi()

bool strBeginsWithi ( strref  str,
strref  sub 
)

Tests if a string begins with a specific prefix (case-insensitive)

Like strBeginsWith(), but performs case-insensitive comparison. This is an ASCII-only comparison.

Parameters
strString to check
subPrefix to test for
Returns
true if str starts with sub (ignoring case), false otherwise

Example:

if (strBeginsWithi(str, _S"http")) {
// Matches "http", "HTTP", "Http", etc.
}
bool strBeginsWithi(strref str, strref sub)

◆ strCmp()

int32 strCmp ( strref  s1,
strref  s2 
)

Compares two strings lexicographically (case-sensitive)

Performs a binary comparison of string bytes. This is NOT intended for locale-aware lexical sorting - use a proper collation function for that.

Parameters
s1First string to compare
s2Second string to compare
Returns
< 0 if s1 comes before s2, 0 if equal, > 0 if s1 comes after s2

Example:

int32 result = strCmp(_S"apple", _S"banana");
if (result < 0) {
// "apple" comes before "banana"
}
int32 strCmp(strref s1, strref s2)

◆ strCmpi()

int32 strCmpi ( strref  s1,
strref  s2 
)

Compares two strings lexicographically (case-insensitive)

Like strCmp(), but performs case-insensitive comparison using tolower(). This is an ASCII-only comparison - multi-byte UTF-8 characters are not properly case-folded.

Parameters
s1First string to compare
s2Second string to compare
Returns
< 0 if s1 comes before s2 (ignoring case), 0 if equal, > 0 if s1 comes after s2

Example:

int32 result = strCmpi(_S"Apple", _S"BANANA");
int32 strCmpi(strref s1, strref s2)

◆ strEndsWith()

bool strEndsWith ( strref  str,
strref  sub 
)

Tests if a string ends with a specific suffix (case-sensitive)

Checks if the last bytes of str match sub. This is equivalent to strRangeEq(str, sub, -strLen(sub), strLen(sub)).

Parameters
strString to check
subSuffix to test for
Returns
true if str ends with sub, false otherwise

Example:

if (strEndsWith(filename, _S".txt")) {
// File has .txt extension
}
bool strEndsWith(strref str, strref sub)

◆ strEndsWithi()

bool strEndsWithi ( strref  str,
strref  sub 
)

Tests if a string ends with a specific suffix (case-insensitive)

Like strEndsWith(), but performs case-insensitive comparison. This is an ASCII-only comparison.

Parameters
strString to check
subSuffix to test for
Returns
true if str ends with sub (ignoring case), false otherwise

Example:

if (strEndsWithi(filename, _S".txt")) {
// Matches ".txt", ".TXT", ".Txt", etc.
}
bool strEndsWithi(strref str, strref sub)

◆ strEq()

bool strEq ( strref  s1,
strref  s2 
)

Tests if two strings are equal (case-sensitive)

This is slightly faster than strCmp() when you only need to check equality rather than ordering. Performs an early-out optimization by comparing lengths first.

Parameters
s1First string to compare
s2Second string to compare
Returns
true if the strings are equal, false otherwise

Example:

if (strEq(s1, _S"hello")) {
// Strings match exactly
}
bool strEq(strref s1, strref s2)

◆ strEqi()

bool strEqi ( strref  s1,
strref  s2 
)

Tests if two strings are equal (case-insensitive)

Like strEq(), but performs case-insensitive comparison using tolower(). This is an ASCII-only comparison - multi-byte UTF-8 characters are not properly case-folded.

Parameters
s1First string to compare
s2Second string to compare
Returns
true if the strings are equal ignoring case, false otherwise

Example:

if (strEqi(_S"Hello", _S"HELLO")) {
// Returns true
}
bool strEqi(strref s1, strref s2)

◆ strRangeCmp()

int32 strRangeCmp ( strref  str,
strref  sub,
int32  off,
uint32  len 
)

Compares a substring range with another string (case-sensitive)

Compares up to len bytes of str starting at offset off with sub. Negative offsets are relative to the end of str.

Parameters
strString containing the range to compare
subString to compare against
offStarting offset in str (negative = from end)
lenMaximum number of bytes to compare
Returns
< 0 if the range comes before sub, 0 if equal, > 0 if the range comes after sub

Example:

// Compare 5 bytes starting at position 10
int32 result = strRangeCmp(str, _S"test", 10, 5);
int32 strRangeCmp(strref str, strref sub, int32 off, uint32 len)

◆ strRangeCmpi()

int32 strRangeCmpi ( strref  str,
strref  sub,
int32  off,
uint32  len 
)

Compares a substring range with another string (case-insensitive)

Like strRangeCmp(), but performs case-insensitive comparison. This is an ASCII-only comparison.

Parameters
strString containing the range to compare
subString to compare against
offStarting offset in str (negative = from end)
lenMaximum number of bytes to compare
Returns
< 0 if the range comes before sub (ignoring case), 0 if equal, > 0 if the range comes after sub

◆ strRangeEq()

bool strRangeEq ( strref  str,
strref  sub,
int32  off,
uint32  len 
)

Tests if a substring range equals another string (case-sensitive)

Compares up to len bytes of str starting at offset off with sub. Negative offsets are relative to the end of str. If the offset is out of bounds or the ranges don't match in length (after clamping), returns false.

Parameters
strString containing the range to compare
subString to compare against
offStarting offset in str (negative = from end)
lenMaximum number of bytes to compare
Returns
true if the ranges are equal, false otherwise

Example:

// Check if characters 2-6 of str equal "hello"
if (strRangeEq(str, _S"hello", 2, 5)) {
// Match found
}
bool strRangeEq(strref str, strref sub, int32 off, uint32 len)

◆ strRangeEqi()

bool strRangeEqi ( strref  str,
strref  sub,
int32  off,
uint32  len 
)

Tests if a substring range equals another string (case-insensitive)

Like strRangeEq(), but performs case-insensitive comparison. This is an ASCII-only comparison.

Parameters
strString containing the range to compare
subString to compare against
offStarting offset in str (negative = from end)
lenMaximum number of bytes to compare
Returns
true if the ranges are equal ignoring case, false otherwise