summaryrefslogtreecommitdiff
path: root/src/byte.c
blob: e113cc371ebaecba22c2fd121406de4a6c5b1a73 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "byte.h"

/**
	@file	byte.c
	@author djb
	@brief byte manipulation functions
*/

unsigned int byte_chr(char *s, register unsigned int n, int c)
{
  register char ch;
  register char *t;

  ch = c;
  t = s;
  for (;;) {
    // clang-format off
    if (!n) { break; } if (*t == ch) { break; } ++t; --n;
    if (!n) { break; } if (*t == ch) { break; } ++t; --n;
    if (!n) { break; } if (*t == ch) { break; } ++t; --n;
    if (!n) { break; } if (*t == ch) { break; } ++t; --n;
    // clang-format on
  }
  return t - s;
}

void byte_copy(register char *to, register unsigned int n, register char *from)
{
  for (;;) {
    // clang-format off
    if (!n) { return; } *to++ = *from++; --n;
    if (!n) { return; } *to++ = *from++; --n;
    if (!n) { return; } *to++ = *from++; --n;
    if (!n) { return; } *to++ = *from++; --n;
    // clang-format on
  }
}

void byte_copyr(register char *to, register unsigned int n, register char *from)
{
  to += n;
  from += n;

  for (;;) {
    // clang-format off
    if (!n) { return; } *--to = *--from; --n;
    if (!n) { return; } *--to = *--from; --n;
    if (!n) { return; } *--to = *--from; --n;
    if (!n) { return; } *--to = *--from; --n;
    // clang-format on
  }
}

int byte_diff(register char *s, register unsigned int n, register char *t)
{
  for (;;) {
    // clang-format off
    if (!n) { return 0; } if (*s != *t) { break; } ++s; ++t; --n;
    if (!n) { return 0; } if (*s != *t) { break; } ++s; ++t; --n;
    if (!n) { return 0; } if (*s != *t) { break; } ++s; ++t; --n;
    if (!n) { return 0; } if (*s != *t) { break; } ++s; ++t; --n;
    // clang-format on
  }
  return ((int)(unsigned int)(unsigned char)*s) - ((int)(unsigned int)(unsigned char)*t);
}

unsigned int byte_rchr(char *s, register unsigned int n, int c)
{
  register char ch;
  register char *t;
  register char *u;

  ch = c;
  t = s;
  u = 0;
  for (;;) {
    // clang-format off
    if (!n) { break; } if (*t == ch) { u = t; } ++t; --n; 
    if (!n) { break; } if (*t == ch) { u = t; } ++t; --n; 
    if (!n) { break; } if (*t == ch) { u = t; } ++t; --n;
    if (!n) { break; } if (*t == ch) { u = t; } ++t; --n;
    // clang-format on
  }
  if (!u) {
    u = t;
  }
  return u - s;
}

void byte_zero(char *s, register unsigned int n)
{
  for (;;) {
    // clang-format off
    if (!n) { break; } *s++ = 0; --n;
    if (!n) { break; } *s++ = 0; --n;
    if (!n) { break; } *s++ = 0; --n;
    if (!n) { break; } *s++ = 0; --n;
    // clang-format on
  }
}

void byte_fill(char *s, register unsigned int n, const int c)
{
  register char ch;

  ch = c;
  for (;;) {
    // clang-format off
    if (!n) { break; } *s++ = ch; --n;
    if (!n) { break; } *s++ = ch; --n;
    if (!n) { break; } *s++ = ch; --n;
    if (!n) { break; } *s++ = ch; --n;
    // clang-format on
  }
}