summaryrefslogtreecommitdiff
path: root/str.c
blob: 7d5ab5ee5b5031dd9fbbf478b739315754160b8e (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "str.h"
#include "stralloc.h"

/**
	@file str.c
	@author djb
	@source qmail
	@brief string handling functions
*/

unsigned int str_copy(register char *s,register const char *t)
{
  register int len;

  len = 0;
  for (;;) {
    if (!(*s = *t)) { return len; } ++s; ++t; ++len; 
    if (!(*s = *t)) { return len; } ++s; ++t; ++len;
    if (!(*s = *t)) { return len; } ++s; ++t; ++len;
    if (!(*s = *t)) { return len; } ++s; ++t; ++len;
  }
}

unsigned int str_copyb(register char *s,register const char *t,unsigned int max)
{
  register int len;

  len = 0;

  while (max-- > 0) {
    if (!(*s = *t)) { return len; } ++s; ++t; ++len;
    if (!(*s = *t)) { return len; } ++s; ++t; ++len;
    if (!(*s = *t)) { return len; } ++s; ++t; ++len;
    if (!(*s = *t)) { return len; } ++s; ++t; ++len;
  }

  return len;
}

int str_diff(register const char *s,register const char *t)
{
  register char x;

  for (;;) {
    x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t;
    x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t;
    x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t;
    x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t;
  }
  return ((int)(unsigned int)(unsigned char) x)
       - ((int)(unsigned int)(unsigned char) *t);
}

int str_diffn(register const char *s,register const char *t,unsigned int len)
{
  register char x;

  for (;;) {
    if (!len--) { return 0; } x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t;
    if (!len--) { return 0; } x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t;
    if (!len--) { return 0; } x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t;
    if (!len--) { return 0; } x = *s; if (x != *t) { break; } if (!x) { break; } ++s; ++t;
  }
  return ((int)(unsigned int)(unsigned char) x)
       - ((int)(unsigned int)(unsigned char) *t);
}

unsigned int str_len(register const char *s)
{
  register const char *t;

  t = s;
  for (;;) {
    if (!*t) { return t - s; } ++t;
    if (!*t) { return t - s; } ++t;
    if (!*t) { return t - s; } ++t;
    if (!*t) { return t - s; } ++t;
  }
}

unsigned int str_chr(register const char *s,int c)
{
  register char ch;
  register const char *t;

  ch = c;
  t = s;
  for (;;) {
    if (!*t) { break; } if (*t == ch) { break; } ++t;
    if (!*t) { break; } if (*t == ch) { break; } ++t;
    if (!*t) { break; } if (*t == ch) { break; } ++t;
    if (!*t) { break; } if (*t == ch) { break; } ++t;
  }
  return t - s;
}

unsigned int str_rchr(register const char *s,int c)
{
  register char ch;
  register const char *t;
  register const char *u;

  ch = c;
  t = s;
  u = 0;
  for (;;) {
    if (!*t) { break; } if (*t == ch) { u = t; } ++t;
    if (!*t) { break; } if (*t == ch) { u = t; } ++t;
    if (!*t) { break; } if (*t == ch) { u = t; } ++t;
    if (!*t) { break; } if (*t == ch) { u = t; } ++t;
  }
  if (!u) u = t;
  return u - s;
}

int str_start(register const char *s,register const char *t)
{
  register char x;

  for (;;) {
    x = *t++; if (!x) return 1; if (x != *s++) return 0;
    x = *t++; if (!x) return 1; if (x != *s++) return 0;
    x = *t++; if (!x) return 1; if (x != *s++) return 0;
    x = *t++; if (!x) return 1; if (x != *s++) return 0;
  }
}

char *str_append(char *dest, char const *s)
{
  static stralloc sa = {0};

  stralloc_copys(&sa,dest);
  stralloc_catb(&sa,s,sizeof(s));
  return sa.s;
}