Skip to content

Add function leak_sv_to_cstr #139

@benjkr

Description

@benjkr

Can be cool to have a function that will create heap allocated string from String_View. Name of the function can be anything, I just thought that 'leak' is a good indication that the returned string is the user's responsibility.

Some rough sketch:

char *leak_sv_to_cstr(Nob_String_View *sv)
{
    if (sv->count == 0) return NULL;
    char *p = calloc(sv->count + 1, 1); // +1 because of null byte at the end
    memcpy(p, sv->data, sv->count);

    return p;
}

My use-case: I am writing a lexer that uses String_View for returning the last lexed word/number/section on the original src string! So for saving important lexed idents I need to convert to cstr. leak_sv_to_cstr can be very useful.

Happy to work on this :) Happy to hear some thoughts!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions