git / brickware / marrow.git - 2cd3640

(2 months ago)commit 512c504: Slice forward approach

tree / slice.c

slice.c

raw

#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include "./slice.h"


struct slice
slice_fromcstr(const char *s) {
    struct slice result;

    result.ptr = s;
    result.size = strlen(s);

    return (result);
}

int32_t
slice_isempty(const struct slice s) {
    if (0 == s.size) return (1);
    return (0);
}

struct slice
slice_empty(void) {
    return (struct slice){
        .ptr = 0x00,
        .size = 0,
    };
}

char *
slice_tocstr(const struct slice s) {
    if (0 == s.ptr || 0 == s.size) {
        return 0x00;
    }

    char *str = malloc(s.size + 1);
    if (!str) {
        return 0x00;
    }

    memcpy(str, s.ptr, s.size);
    str[s.size] = '\0';
    return str;
}