src/string.c

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 576
ba0c4ff6698e
child 580
aac47db8da0b
permissions
-rw-r--r--

start implementing string functions

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include "cx/string.h"
    30 #include "cx/utils.h"
    32 #include <string.h>
    33 #include <stdarg.h>
    34 #include <stdint.h>
    36 cxmutstr cx_mutstr(char *cstring) {
    37     return (cxmutstr) {cstring, strlen(cstring)};
    38 }
    40 cxmutstr cx_mutstrn(
    41         char *cstring,
    42         size_t length
    43 ) {
    44     return (cxmutstr) {cstring, length};
    45 }
    47 cxstring cx_str(const char *cstring) {
    48     return (cxstring) {cstring, strlen(cstring)};
    49 }
    51 cxstring cx_strn(
    52         const char *cstring,
    53         size_t length
    54 ) {
    55     return (cxstring) {cstring, length};
    56 }
    58 cxstring cx_strcast(cxmutstr str) {
    59     return (cxstring) {str.ptr, str.length};
    60 }
    62 void cx_strfree(cxmutstr *str) {
    63     free(str->ptr);
    64     str->ptr = NULL;
    65     str->length = 0;
    66 }
    68 size_t cx_strlen(
    69         size_t count,
    70         ...
    71 ) {
    72     if (count == 0) return 0;
    74     va_list ap;
    75     va_start(ap, count);
    76     size_t size = 0;
    77     cx_for_n(i, count) {
    78         cxstring str = va_arg(ap, cxstring);
    79         size += str.length;
    80     }
    81     va_end(ap);
    83     return size;
    84 }
    86 cxmutstr cx_strcat_a(
    87         CxAllocator *alloc,
    88         size_t count,
    89         ...
    90 ) {
    91     cxstring *strings = calloc(count, sizeof(cxstring));
    92     if (!strings) abort();
    94     va_list ap;
    95     va_start(ap, count);
    97     // get all args and overall length
    98     size_t slen = 0;
    99     cx_for_n(i, count) {
   100         cxstring s = va_arg (ap, cxstring);
   101         strings[i] = s;
   102         slen += s.length;
   103     }
   105     // create new string
   106     cxmutstr result;
   107     result.ptr = cxMalloc(alloc, slen + 1);
   108     result.length = slen;
   109     if (result.ptr == NULL) abort();
   111     // concatenate strings
   112     size_t pos = 0;
   113     cx_for_n(i, count) {
   114         cxstring s = strings[i];
   115         memcpy(result.ptr + pos, s.ptr, s.length);
   116         pos += s.length;
   117     }
   119     // terminate string
   120     result.ptr[result.length] = '\0';
   122     // free temporary array
   123     free(strings);
   125     return result;
   126 }

mercurial