File: string-util-fundamental.c

package info (click to toggle)
account-utils 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 836 kB
  • sloc: ansic: 8,789; xml: 1,584; sh: 77; makefile: 10
file content (41 lines) | stat: -rw-r--r-- 756 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* SPDX-License-Identifier: LGPL-2.1-or-later */

// based on systemd v258

#include <assert.h>

#include "basics.h"

char *startswith(const char *s, const char *prefix) {
        size_t l;

        assert(s);
        assert(prefix);

        l = strlen(prefix);
        if (!strneq(s, prefix, l))
                return NULL;

        return (char*) s + l;
}

char* endswith(const char *s, const char *suffix) {
        size_t sl, pl;

        assert(s);
        assert(suffix);

        sl = strlen(s);
        pl = strlen(suffix);

        if (pl == 0)
                return (char*) s + sl;

        if (sl < pl)
                return NULL;

        if (!streq(s + sl - pl, suffix))
                return NULL;

        return (char*) s + sl - pl;
}