summaryrefslogtreecommitdiffstats
path: root/Objects/uniops.h
blob: 06a0b4ee32473fa19b73bbf1f35938d3fed69d02 (plain)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

size_t
UNIOP(strlen)(const UNIOP_t *u)
{
    int res = 0;
    while(*u++)
        res++;
    return res;
}

UNIOP_t*
UNIOP(strcpy)(UNIOP_t *s1, const UNIOP_t *s2)
{
    UNIOP_t *u = s1;
    while ((*u++ = *s2++));
    return s1;
}

UNIOP_t*
UNIOP(strncpy)(UNIOP_t *s1, const UNIOP_t *s2, size_t n)
{
    UNIOP_t *u = s1;
    while ((*u++ = *s2++))
        if (n-- == 0)
            break;
    return s1;
}

UNIOP_t*
UNIOP(strcat)(UNIOP_t *s1, const UNIOP_t *s2)
{
    UNIOP_t *u1 = s1;
    u1 += UNIOP(strlen(u1));
    UNIOP(strcpy(u1, s2));
    return s1;
}

int
UNIOP(strcmp)(const UNIOP_t *s1, const UNIOP_t *s2)
{
    while (*s1 && *s2 && *s1 == *s2)
        s1++, s2++;
    if (*s1 && *s2)
        return (*s1 < *s2) ? -1 : +1;
    if (*s1)
        return 1;
    if (*s2)
        return -1;
    return 0;
}

int
UNIOP(strncmp)(const UNIOP_t *s1, const UNIOP_t *s2, size_t n)
{
    register UNIOP_t u1, u2;
    for (; n != 0; n--) {
        u1 = *s1;
        u2 = *s2;
        if (u1 != u2)
            return (u1 < u2) ? -1 : +1;
        if (u1 == '\0')
            return 0;
        s1++;
        s2++;
    }
    return 0;
}

UNIOP_t*
UNIOP(strchr)(const UNIOP_t *s, UNIOP_t c)
{
    const UNIOP_t *p;
    for (p = s; *p; p++)
        if (*p == c)
            return (UNIOP_t*)p;
    return NULL;
}

UNIOP_t*
UNIOP(strrchr)(const UNIOP_t *s, UNIOP_t c)
{
    const UNIOP_t *p;
    p = s + UNIOP(strlen)(s);
    while (p != s) {
        p--;
        if (*p == c)
            return (UNIOP_t*)p;
    }
    return NULL;
}