ValidateStr

Table of Contents

ValidateStr

Description

Validate whether a given Str object is valid. Not foolproof but will work most of the time. Aborts if provided Str is not valid.

Parameters

NameDirectionDescription
sinPointer to Str object to validate.

Success

Continue execution, meaning given Str object is most probably valid.

Failure

abort

Usage example (Cross-references)

    }
    
    ValidateStr(o);
    ValidateStr(s);
    
    ValidateStr(o);
    ValidateStr(s);
    
    // Store original length to calculate content size later
    }
    
    ValidateStr(o);
    
    // Store original length to calculate content size later
    LOG_FATAL("Invalid arguments");
    
    ValidateStr(s);
    
    // Check for case conversion flags
    }
    
    ValidateStr(o);
    ValidateBitVec(bv);
    
    Str* StrPrintf(Str* str, const char* fmt, ...) {
    ValidateStr(str);
    
    StrClear(str);
    
    Str* StrAppendf(Str* str, const char* fmt, ...) {
    ValidateStr(str);
    
    va_list args;
    
    static Str* string_va_printf(Str* str, const char* fmt, va_list args) {
    ValidateStr(str);
    
    va_list args_copy;
    
    bool StrInitCopy(Str* dst, const Str* src) {
    ValidateStr(src);
    
    MemSet(dst, 0, sizeof(Str));
    
    VecMergeR(dst, src);
    ValidateStr(dst);
    
    return true;
    
    void StrDeinit(Str* copy) {
    ValidateStr(copy);
    if (copy->data) {
    FREE(copy->data);
    
    StrIters StrSplitToIters(Str* s, const char* key) {
    ValidateStr(s);
    
    StrIters sv     = VecInit();
    
    Strs StrSplit(Str* s, const char* key) {
    ValidateStr(s);
    
    Strs sv     = VecInitWithDeepCopy(NULL, StrDeinit);
    //                 = 1 means from right
    Str strip_str(Str* s, const char* chars_to_strip, int split_direction) {
    ValidateStr(s);
    
    const char* strip_chars = chars_to_strip ? chars_to_strip : " \t\n\r\v\f";
    
    bool StrStartsWithZstr(const Str* s, const char* prefix) {
    ValidateStr(s);
    return starts_with(s->data, s->length, prefix, ZstrLen(prefix));
    }
    
    bool StrEndsWithZstr(const Str* s, const char* suffix) {
    ValidateStr(s);
    return ends_with(s->data, s->length, suffix, ZstrLen(suffix));
    }
    
    bool StrStartsWithCstr(const Str* s, const char* prefix, size prefix_len) {
    ValidateStr(s);
    return starts_with(s->data, s->length, prefix, prefix_len);
    }
    
    bool StrEndsWithCstr(const Str* s, const char* suffix, size suffix_len) {
    ValidateStr(s);
    return ends_with(s->data, s->length, suffix, suffix_len);
    }
    
    bool StrStartsWith(const Str* s, const Str* prefix) {
    ValidateStr(s);
    return starts_with(s->data, s->length, prefix->data, prefix->length);
    }
    
    bool StrEndsWith(const Str* s, const Str* suffix) {
    ValidateStr(s);
    return ends_with(s->data, s->length, suffix->data, suffix->length);
    }
    static void
    str_replace(Str* s, const char* match, size match_len, const char* replacement, size replacement_len, size count) {
    ValidateStr(s);
    size i        = 0;
    size replaced = 0;
    
    void StrReplaceZstr(Str* s, const char* match, const char* replacement, size count) {
    ValidateStr(s);
    str_replace(s, match, ZstrLen(match), replacement, ZstrLen(replacement), count);
    }
    size        count
    ) {
    ValidateStr(s);
    str_replace(s, match, match_len, replacement, replacement_len, count);
    }
    
    void StrReplace(Str* s, const Str* match, const Str* replacement, size count) {
    ValidateStr(s);
    str_replace(s, match->data, match->length, replacement->data, replacement->length, count);
    }
    
    Str* StrFromU64(Str* str, u64 value, const StrIntFormat* config) {
    ValidateStr(str);
    
    if (!config) {
    
    Str* StrFromI64(Str* str, i64 value, const StrIntFormat* config) {
    ValidateStr(str);
    
    if (!config) {
    
    Str* StrFromF64(Str* str, f64 value, const StrFloatFormat* config) {
    ValidateStr(str);
    
    if (!config) {
    
    bool StrToU64(const Str* str, u64* value, const StrParseConfig* config) {
    ValidateStr(str);
    
    if (!value) {
    
    bool StrToI64(const Str* str, i64* value, const StrParseConfig* config) {
    ValidateStr(str);
    
    if (!value) {
    
    bool StrToF64(const Str* str, f64* value, const StrParseConfig* config) {
    ValidateStr(str);
    
    if (!value) {
    }
    
    void ValidateStr(const Str* s) {
    return ValidateVec(s);
    }
    
    void ValidateStrs(const Strs* vs) {
    VecForeachPtr(vs, sp, { ValidateStr(sp); });
    }
    // Test ValidateStr macro
    bool test_validate_str(void) {
    printf("Testing ValidateStr macro\n");
    
    // Create a valid Str
    
    // This should not crash
    ValidateStr(&s);
    
    // Note: We can't really test invalid strings here as ValidateStr
    // Deadend test: Test ValidateStr with invalid string (should crash/abort)
    bool test_validate_invalid_str(void) {
    printf("Testing ValidateStr with invalid string (should abort)\n");
    
    // Create an invalid Str by corrupting its fields
    
    // This should abort the program
    ValidateStr(&s);
    
    // Should never reach here
    
    // Validate the string
    ValidateStr(&s);
    
    // Check that it's initialized correctly
    
    // Validate the string
    ValidateStr(&s);
    
    // Check that it's initialized correctly
    
    // Validate the string
    ValidateStr(&s);
    
    // Check that it's initialized correctly
    
    // Validate both strings
    ValidateStr(&src);
    ValidateStr(&dst);
    // Validate both strings
    ValidateStr(&src);
    ValidateStr(&dst);
    
    // Check that dst is initialized correctly
    
    // Validate both strings
    ValidateStr(&src);
    ValidateStr(&dst);
    // Validate both strings
    ValidateStr(&src);
    ValidateStr(&dst);
    
    // Check that dst is initialized correctly
    
    // Validate the string
    ValidateStr(&s);
    
    // Check that it's initialized correctly
    
    // Validate the string
    ValidateStr(&stack_str);
    
    // Check that it works correctly
    
    // Validate both strings
    ValidateStr(&src);
    ValidateStr(&dst);
    // Validate both strings
    ValidateStr(&src);
    ValidateStr(&dst);
    
    // Check that the copy was successful
    
    // Validate the string before deinit
    ValidateStr(&s);
    
    // Deinit the string

Share :