implement more string functions

Sat, 03 Sep 2022 14:56:07 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 03 Sep 2022 14:56:07 +0200
changeset 581
c067394737ca
parent 580
aac47db8da0b
child 582
96fa7fa6af4f

implement more string functions

src/cx/string.h file | annotate | diff | comparison | revisions
src/string.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/cx/string.h	Wed Aug 31 23:12:05 2022 +0200
     1.2 +++ b/src/cx/string.h	Sat Sep 03 14:56:07 2022 +0200
     1.3 @@ -658,7 +658,7 @@
     1.4   * \c false otherwise
     1.5   */
     1.6  __attribute__((__warn_unused_result__))
     1.7 -int cx_strsuffix(
     1.8 +bool cx_strsuffix(
     1.9          cxstring string,
    1.10          cxstring suffix
    1.11  );
    1.12 @@ -672,7 +672,7 @@
    1.13   * \c false otherwise
    1.14   */
    1.15  __attribute__((__warn_unused_result__))
    1.16 -int cx_strcaseprefix(
    1.17 +bool cx_strcaseprefix(
    1.18          cxstring string,
    1.19          cxstring prefix
    1.20  );
    1.21 @@ -686,7 +686,7 @@
    1.22   * \c false otherwise
    1.23   */
    1.24  __attribute__((__warn_unused_result__))
    1.25 -int cx_strcasesuffix(
    1.26 +bool cx_strcasesuffix(
    1.27          cxstring string,
    1.28          cxstring suffix
    1.29  );
     2.1 --- a/src/string.c	Wed Aug 31 23:12:05 2022 +0200
     2.2 +++ b/src/string.c	Sat Sep 03 14:56:07 2022 +0200
     2.3 @@ -32,6 +32,13 @@
     2.4  #include <string.h>
     2.5  #include <stdarg.h>
     2.6  #include <stdint.h>
     2.7 +#include <ctype.h>
     2.8 +
     2.9 +#ifndef _WIN32
    2.10 +
    2.11 +#include <strings.h> /* for strncasecmp() */
    2.12 +
    2.13 +#endif /* _WIN32 */
    2.14  
    2.15  cxmutstr cx_mutstr(char *cstring) {
    2.16      return (cxmutstr) {cstring, strlen(cstring)};
    2.17 @@ -336,3 +343,90 @@
    2.18      return cx_strsplit_a(allocator, cx_strcast(string),
    2.19                           delim, limit, (cxstring **) output);
    2.20  }
    2.21 +
    2.22 +int cx_strcmp(cxstring s1, cxstring s2) {
    2.23 +    if (s1.length == s2.length) {
    2.24 +        return memcmp(s1.ptr, s2.ptr, s1.length);
    2.25 +    } else if (s1.length > s2.length) {
    2.26 +        return 1;
    2.27 +    } else {
    2.28 +        return -1;
    2.29 +    }
    2.30 +}
    2.31 +
    2.32 +int cx_strcasecmp(cxstring s1, cxstring s2) {
    2.33 +    if (s1.length == s2.length) {
    2.34 +#ifdef _WIN32
    2.35 +        return _strnicmp(s1.ptr, s2.ptr, s1.length);
    2.36 +#else
    2.37 +        return strncasecmp(s1.ptr, s2.ptr, s1.length);
    2.38 +#endif
    2.39 +    } else if (s1.length > s2.length) {
    2.40 +        return 1;
    2.41 +    } else {
    2.42 +        return -1;
    2.43 +    }
    2.44 +}
    2.45 +
    2.46 +cxmutstr cx_strdup_a(CxAllocator *allocator, cxstring string) {
    2.47 +    cxmutstr result = {
    2.48 +            cxMalloc(allocator, string.length + 1),
    2.49 +            string.length
    2.50 +    };
    2.51 +    if (result.ptr == NULL) {
    2.52 +        result.length = 0;
    2.53 +        return result;
    2.54 +    }
    2.55 +    memcpy(result.ptr, string.ptr, string.length);
    2.56 +    result.ptr[string.length] = '\0';
    2.57 +    return result;
    2.58 +}
    2.59 +
    2.60 +cxstring cx_strtrim(cxstring string) {
    2.61 +    cxstring result = string;
    2.62 +    // TODO: optimize by comparing multiple bytes at once
    2.63 +    while (result.length > 0 && isspace(*result.ptr)) {
    2.64 +        result.ptr++;
    2.65 +        result.length--;
    2.66 +    }
    2.67 +    while (result.length > 0 && isspace(result.ptr[result.length - 1])) {
    2.68 +        result.length--;
    2.69 +    }
    2.70 +    return result;
    2.71 +}
    2.72 +
    2.73 +cxmutstr cx_strtrim_m(cxmutstr string) {
    2.74 +    cxstring result = cx_strtrim(cx_strcast(string));
    2.75 +    return (cxmutstr) {(char *) result.ptr, result.length};
    2.76 +}
    2.77 +
    2.78 +bool cx_strprefix(cxstring string, cxstring prefix) {
    2.79 +    if (string.length < prefix.length) return false;
    2.80 +    return memcmp(string.ptr, prefix.ptr, prefix.length) == 0;
    2.81 +}
    2.82 +
    2.83 +bool cx_strsuffix(cxstring string, cxstring suffix) {
    2.84 +    if (string.length < suffix.length) return false;
    2.85 +    return memcmp(string.ptr + string.length - suffix.length,
    2.86 +                  suffix.ptr, suffix.length) == 0;
    2.87 +}
    2.88 +
    2.89 +bool cx_casestrprefix(cxstring string, cxstring prefix) {
    2.90 +    if (string.length < prefix.length) return false;
    2.91 +#ifdef _WIN32
    2.92 +    return _strnicmp(string.ptr, prefix.ptr, prefix.length) == 0;
    2.93 +#else
    2.94 +    return strncasecmp(string.ptr, prefix.ptr, prefix.length) == 0;
    2.95 +#endif
    2.96 +}
    2.97 +
    2.98 +bool cx_casestrsuffix(cxstring string, cxstring suffix) {
    2.99 +    if (string.length < suffix.length) return false;
   2.100 +#ifdef _WIN32
   2.101 +    return _strnicmp(string.ptr+string.length-suffix.length,
   2.102 +                  suffix.ptr, suffix.length) == 0;
   2.103 +#else
   2.104 +    return strncasecmp(string.ptr + string.length - suffix.length,
   2.105 +                       suffix.ptr, suffix.length) == 0;
   2.106 +#endif
   2.107 +}

mercurial