Skip to content

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)
    // Test string starts/ends with functions
    bool test_str_starts_ends_with(void) {
        WriteFmt("Testing StrStartsWith and StrEndsWith variants\n");
        DefaultAllocator alloc = DefaultAllocatorInit();
    
        // Test Str-form
        bool result = StrStartsWith(&s, &prefix);
    
        // Test Str-form
    
        // Test Zstr-form (string literal)
        result = result && StrStartsWith(&s, "Hello");
        result = result && !StrStartsWith(&s, "World");
        // Test Zstr-form (string literal)
        result = result && StrStartsWith(&s, "Hello");
        result = result && !StrStartsWith(&s, "World");
    
        // Test Zstr-form (string literal)
    
        // Test Cstr-form (fixed-length view)
        result = result && StrStartsWith(&s, "Hell", 4);
        result = result && !StrStartsWith(&s, "Worl", 4);
        // Test Cstr-form (fixed-length view)
        result = result && StrStartsWith(&s, "Hell", 4);
        result = result && !StrStartsWith(&s, "Worl", 4);
    
        // Test Cstr-form (fixed-length view)
    // 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();
        Str s = StrInitFromZstr("Hello", &alloc);
    
        bool result = (StrStartsWith(&s, "Hello") == true);
        if (!result) {
            WriteFmt("    FAIL: Expected StrStartsWith true for full-string prefix\n");
        bool result = (StrStartsWith(&s, "Hello") == true);
        if (!result) {
            WriteFmt("    FAIL: Expected StrStartsWith true for full-string prefix\n");
        }
    //   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();
        Str prefix = StrInitFromZstr("Hello", &alloc);
    
        bool result = (StrStartsWith(&s, &prefix) == true);
        if (!result) {
            WriteFmt("    FAIL: expected true for prefix 'Hello'\n");
    // (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);
        WriteFmt("Testing StrStartsWith(Cstr) aborts on NULL Str\n");
    
        (void)StrStartsWith((Str *)NULL, "x", (size)1);
    
        return false;
        s.__magic  = 0;
    
        (void)StrStartsWith(&s, &prefix);
    
        StrDeinit(&prefix);
Last updated on