Skip to content

ZstrDupN

ZstrDupN

Description

Duplicates a string up to the specified length. Creates a new null-terminated string by allocating memory and copying at most n characters from the source string.

Parameters

Name Direction Description
src in Source string to duplicate.
n in Maximum number of characters to copy.

Success

Returns a pointer to the newly allocated duplicate string.

Failure

Returns NULL if memory allocation fails or if src is NULL.

Usage example (Cross-references)

Usage examples (Cross-references)
    }
    
    char *ZstrDupN(const char *src, size n) {
        if (!src) {
            LOG_FATAL("Invalid arguments");
    
    char *ZstrDup(const char *src) {
        return ZstrDupN(src, ZstrLen(src));
    }
    #    define StrInitFromCstr(cstr, len)                                                                                 \
            (Str {                                                                                                         \
                .data        = ZstrDupN((char *)(cstr), (len)),                                                            \
                .length      = (len),                                                                                      \
                .capacity    = (len),                                                                                      \
    ///
    #    define StrInitFromCstr(cstr, len)                                                                                 \
            ((Str) {.data        = ZstrDupN((char *)(cstr), (len)),                                                        \
                    .length      = (len),                                                                                  \
                    .capacity    = (len),                                                                                  \
Last updated on