StrStartsWith
Description
Check whether a Str starts with a prefix.
Three call shapes via OVERLOAD + _Generic on prefix: StrStartsWith(s, prefix) – prefix is Str * or Zstr. StrStartsWith(s, prefix, prefix_len) – prefix is a fixed-length view (Zstr, size).
Parameters
| Name | Direction | Description |
|---|---|---|
s |
in | Str to check. |
prefix |
in | Candidate prefix (Str * / Zstr). |
prefix_len |
in | Length of prefix when using the 3-arg fixed-length form. |
Success
Returns true when s starts with prefix. The string is not modified.
Failure
Returns false. The string is not modified.
Usage example (Cross-references)
Usage examples (Cross-references)
- In
Ops.c:126:
// Test string starts/ends with functions
bool test_str_starts_ends_with(void) {
WriteFmt("Testing StrStartsWith and StrEndsWith variants\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Ops.c:135:
// Test Str-form
bool result = StrStartsWith(&s, &prefix);
// Test Str-form
- In
Ops.c:141:
// Test Zstr-form (string literal)
result = result && StrStartsWith(&s, "Hello");
result = result && !StrStartsWith(&s, "World");- In
Ops.c:142:
// Test Zstr-form (string literal)
result = result && StrStartsWith(&s, "Hello");
result = result && !StrStartsWith(&s, "World");
// Test Zstr-form (string literal)
- In
Ops.c:149:
// Test Cstr-form (fixed-length view)
result = result && StrStartsWith(&s, "Hell", 4);
result = result && !StrStartsWith(&s, "Worl", 4);- In
Ops.c:150:
// Test Cstr-form (fixed-length view)
result = result && StrStartsWith(&s, "Hell", 4);
result = result && !StrStartsWith(&s, "Worl", 4);
// Test Cstr-form (fixed-length view)
- In
Ops.c:592:
// mutant rejects it outright.
static bool test_starts_with_full_string(void) {
WriteFmt("Testing StrStartsWith with a prefix equal to the whole string\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Ops.c:597:
Str s = StrInitFromZstr("Hello", &alloc);
bool result = (StrStartsWith(&s, "Hello") == true);
if (!result) {
WriteFmt(" FAIL: Expected StrStartsWith true for full-string prefix\n");- In
Ops.c:599:
bool result = (StrStartsWith(&s, "Hello") == true);
if (!result) {
WriteFmt(" FAIL: Expected StrStartsWith true for full-string prefix\n");
}- In
Ops.c:654:
// mutant : always false.
static bool test_starts_with_true_on_match(void) {
WriteFmt("Testing StrStartsWith returns true on a real prefix match (466:12)\n");
DefaultAllocator alloc = DefaultAllocatorInit();- In
Ops.c:660:
Str prefix = StrInitFromZstr("Hello", &alloc);
bool result = (StrStartsWith(&s, &prefix) == true);
if (!result) {
WriteFmt(" FAIL: expected true for prefix 'Hello'\n");- In
Ops.c:916:
// (Str.Mutants6).
static bool test_starts_with_cstr_null_aborts(void) {
WriteFmt("Testing StrStartsWith(Cstr) aborts on NULL Str\n");
(void)StrStartsWith((Str *)NULL, "x", (size)1);- In
Ops.c:918:
WriteFmt("Testing StrStartsWith(Cstr) aborts on NULL Str\n");
(void)StrStartsWith((Str *)NULL, "x", (size)1);
return false;- In
Ops.c:1089:
s.__magic = 0;
(void)StrStartsWith(&s, &prefix);
StrDeinit(&prefix);
Last updated on