start implementing string functions

Tue, 30 Aug 2022 19:56:07 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 30 Aug 2022 19:56:07 +0200
changeset 579
bbc46dcd5255
parent 578
0b2c0cb280a9
child 580
aac47db8da0b

start implementing string functions

src/string.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/string.c	Tue Aug 30 19:55:56 2022 +0200
     1.2 +++ b/src/string.c	Tue Aug 30 19:56:07 2022 +0200
     1.3 @@ -27,3 +27,103 @@
     1.4   */
     1.5  
     1.6  #include "cx/string.h"
     1.7 +#include "cx/utils.h"
     1.8 +
     1.9 +#include <string.h>
    1.10 +#include <stdarg.h>
    1.11 +#include <stdint.h>
    1.12 +
    1.13 +cxmutstr cx_mutstr(char *cstring) {
    1.14 +    return (cxmutstr) {cstring, strlen(cstring)};
    1.15 +}
    1.16 +
    1.17 +cxmutstr cx_mutstrn(
    1.18 +        char *cstring,
    1.19 +        size_t length
    1.20 +) {
    1.21 +    return (cxmutstr) {cstring, length};
    1.22 +}
    1.23 +
    1.24 +cxstring cx_str(const char *cstring) {
    1.25 +    return (cxstring) {cstring, strlen(cstring)};
    1.26 +}
    1.27 +
    1.28 +cxstring cx_strn(
    1.29 +        const char *cstring,
    1.30 +        size_t length
    1.31 +) {
    1.32 +    return (cxstring) {cstring, length};
    1.33 +}
    1.34 +
    1.35 +cxstring cx_strcast(cxmutstr str) {
    1.36 +    return (cxstring) {str.ptr, str.length};
    1.37 +}
    1.38 +
    1.39 +void cx_strfree(cxmutstr *str) {
    1.40 +    free(str->ptr);
    1.41 +    str->ptr = NULL;
    1.42 +    str->length = 0;
    1.43 +}
    1.44 +
    1.45 +size_t cx_strlen(
    1.46 +        size_t count,
    1.47 +        ...
    1.48 +) {
    1.49 +    if (count == 0) return 0;
    1.50 +
    1.51 +    va_list ap;
    1.52 +    va_start(ap, count);
    1.53 +    size_t size = 0;
    1.54 +    cx_for_n(i, count) {
    1.55 +        cxstring str = va_arg(ap, cxstring);
    1.56 +        size += str.length;
    1.57 +    }
    1.58 +    va_end(ap);
    1.59 +
    1.60 +    return size;
    1.61 +}
    1.62 +
    1.63 +cxmutstr cx_strcat_a(
    1.64 +        CxAllocator *alloc,
    1.65 +        size_t count,
    1.66 +        ...
    1.67 +) {
    1.68 +    cxstring *strings = calloc(count, sizeof(cxstring));
    1.69 +    if (!strings) abort();
    1.70 +
    1.71 +    va_list ap;
    1.72 +    va_start(ap, count);
    1.73 +
    1.74 +    // get all args and overall length
    1.75 +    size_t slen = 0;
    1.76 +    cx_for_n(i, count) {
    1.77 +        cxstring s = va_arg (ap, cxstring);
    1.78 +        strings[i] = s;
    1.79 +        slen += s.length;
    1.80 +    }
    1.81 +
    1.82 +    // create new string
    1.83 +    cxmutstr result;
    1.84 +    result.ptr = cxMalloc(alloc, slen + 1);
    1.85 +    result.length = slen;
    1.86 +    if (result.ptr == NULL) abort();
    1.87 +
    1.88 +    // concatenate strings
    1.89 +    size_t pos = 0;
    1.90 +    cx_for_n(i, count) {
    1.91 +        cxstring s = strings[i];
    1.92 +        memcpy(result.ptr + pos, s.ptr, s.length);
    1.93 +        pos += s.length;
    1.94 +    }
    1.95 +
    1.96 +    // terminate string
    1.97 +    result.ptr[result.length] = '\0';
    1.98 +
    1.99 +    // free temporary array
   1.100 +    free(strings);
   1.101 +
   1.102 +    return result;
   1.103 +}
   1.104 +
   1.105 +
   1.106 +

mercurial