nob.h icon indicating copy to clipboard operation
nob.h copied to clipboard

Add function `leak_sv_to_cstr`

Open benjkr opened this issue 3 months ago • 0 comments

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!

benjkr avatar Sep 21 '25 18:09 benjkr