File: byte.c

package info (click to toggle)
dq 20230101-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,020 kB
  • sloc: ansic: 8,269; makefile: 363; sh: 176; python: 82
file content (84 lines) | stat: -rw-r--r-- 1,632 bytes parent folder | download | duplicates (5)
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
/*
20130505
Jan Mojzis
Public domain.
*/

#include "byte.h"

void byte_copy(void *yv, long long ylen, const void *xv) {
    
    long long i;
    const char *x = xv;
    char *y = yv;

    for (i = 0; i < ylen; ++i) y[i] = x[i];
}

void byte_copyr(void *yv, long long ylen, const void *xv) {
    
    long long i;
    const char *x = xv;
    char *y = yv;

    for (i = ylen - 1; i >= 0; --i) y[i] = x[i];
}


long long byte_chr(const void *yv, long long ylen, int c) {

    long long i;
    const char *y = yv;
    char ch = c;

    if (ylen <= 0) return ylen;

    for (i = 0; i < ylen; ++i) if (y[i] == ch) break;
    return i;
}

long long byte_rchr(const void *yv, long long ylen, int c) {

    long long i, u = -1;
    const char *y = yv;
    char ch = c;

    if (ylen <= 0) return ylen;

    for (i = ylen - 1; i >= 0; --i) if (y[i] == ch) { u = i; break; }
    if (u == -1) return ylen;
    return u;
}

void byte_zero(void *yv, long long ylen) {

    long long i;
    char *y = yv;

    for (i = 0; i < ylen; ++i) y[i] = 0;
}

int byte_isequal(const void *yv, long long ylen, const void *xv) {

    long long i;
    const unsigned char *y = yv;
    const unsigned char *x = xv;
    unsigned char diff = 0;

    for (i = 0; i < ylen; ++i) diff |= x[i] ^ y[i];
    return (256 - (unsigned int) diff) >> 8;
}

int byte_diff(const void *yv, long long ylen, const void *xv) {

    long long i;
    const unsigned char *y = yv;
    const unsigned char *x = xv;

    for (i = 0; i < ylen; ++i) {
        if (x[i] == y[i]) continue;
        return ((int)(unsigned int)y[i]) - ((int)(unsigned int)x[i]);
    }
    return 0;
    
}