commit ba895ac

shrub  ·  2026-07-12 23:22:44 +0000 UTC
parent ed8861c
use djb string bits and add djb code from publicfile
14 files changed,  +203, -14
+0, -0
+2, -3
 1@@ -6,7 +6,6 @@
 2 #include <strerr.h>
 3 #include <glob.h>
 4 #include <sys/stat.h>
 5-#include <string.h>
 6 
 7 #include "parse.h"
 8 #include "mdoc.h"
 9@@ -53,8 +52,8 @@ progname(const char *arg, const char *def)
10 
11 	if (!arg)
12 		return def;
13-	slash = strrchr(arg, '/');
14-	return slash ? slash + 1 : arg;
15+	slash = arg + str_rchr((char *)arg, '/');
16+	return *slash ? slash + 1 : arg;
17 }
18 
19 static bool
+24, -7
 1@@ -2,12 +2,12 @@
 2 #include "defs"
 3 
 4 #include <ctype.h>
 5-#include <stdio.h>
 6 #include <stdlib.h>
 7 #include <string.h>
 8 
 9 #include <alloc.h>
10 #include <byte.h>
11+#include <fmt.h>
12 #include <str.h>
13 #include <stralloc.h>
14 #include <subgetopt.h>
15@@ -100,11 +100,26 @@ format_box_label(char *buf, unsigned int buf_len, const char *text,
16     const char *fallback)
17 {
18 	int r;
19+	unsigned int n;
20+	const char *src;
21 
22-	r = snprintf(buf, buf_len, "[%s]", text);
23-	if (r < 0 || (unsigned int)r >= buf_len)
24-		r = snprintf(buf, buf_len, "[%s]", fallback);
25-	return (r <= 0) ? 0 : str_len(buf);
26+	if (buf_len < 3)
27+		return 0;
28+	src = text;
29+	n = str_len(text);
30+	if (n + 2 >= buf_len) {
31+		src = fallback;
32+		n = str_len(fallback);
33+		if (n + 2 >= buf_len)
34+			n = buf_len - 3;
35+	}
36+	buf[0] = '[';
37+	n = fmt_str(buf + 1, (char *)src);
38+	if (n + 2 >= buf_len)
39+		n = buf_len - 3;
40+	buf[n + 1] = ']';
41+	buf[n + 2] = '\0';
42+	return n + 2;
43 }
44 
45 static int
46@@ -322,9 +337,11 @@ process_blob(char *blob, const char *label, const struct options *opts)
47 	int line_ret;
48 
49 	while (*line) {
50-		next = strchr(line, '\n');
51-		if (next != NULL)
52+		next = line + str_chr(line, '\n');
53+		if (*next != '\0')
54 			*next = '\0';
55+		else
56+			next = 0;
57 		line_ret = process_key_line(line, label, opts);
58 		if (line_ret == 0) {
59 			ret = 0;
+2, -3
 1@@ -12,7 +12,6 @@
 2 #include <str.h>
 3 #include <strerr.h>
 4 
 5-#include <string.h>
 6 #include <unistd.h>
 7 
 8 static void
 9@@ -100,8 +99,8 @@ mkpar(char *s)
10 	char *p, *e;
11 	size_t n;
12 
13-	e = strrchr(s, '/');
14-	if (e == NULL || e == s)
15+	e = s + str_rchr(s, '/');
16+	if (*e == '\0' || e == s)
17 		return;
18 	n = (size_t)(e - s);
19 	p = alloc((unsigned int)n + 1);
+13, -0
 1@@ -0,0 +1,13 @@
 2+#ifndef CASE_H
 3+#define CASE_H
 4+
 5+extern void case_lowers();
 6+extern void case_lowerb();
 7+extern int case_diffs();
 8+extern int case_diffb();
 9+extern int case_starts();
10+extern int case_startb();
11+
12+#define case_equals(s,t) (!case_diffs((s),(t)))
13+
14+#endif
+19, -0
 1@@ -0,0 +1,19 @@
 2+#include "case.h"
 3+
 4+int case_diffs(s,t)
 5+register char *s;
 6+register char *t;
 7+{
 8+  register unsigned char x;
 9+  register unsigned char y;
10+
11+  for (;;) {
12+    x = *s++ - 'A';
13+    if (x <= 'Z' - 'A') x += 'a'; else x += 'A';
14+    y = *t++ - 'A';
15+    if (y <= 'Z' - 'A') y += 'a'; else y += 'A';
16+    if (x != y) break;
17+    if (!x) break;
18+  }
19+  return ((int)(unsigned int) x) - ((int)(unsigned int) y);
20+}
+14, -0
 1@@ -0,0 +1,14 @@
 2+#include "case.h"
 3+
 4+void case_lowerb(s,len)
 5+char *s;
 6+unsigned int len;
 7+{
 8+  unsigned char x;
 9+  while (len > 0) {
10+    --len;
11+    x = *s - 'A';
12+    if (x <= 'Z' - 'A') *s = x + 'a';
13+    ++s;
14+  }
15+}
+21, -0
 1@@ -0,0 +1,21 @@
 2+#include "case.h"
 3+
 4+int case_startb(s,len,t)
 5+register char *s;
 6+unsigned int len;
 7+register char *t;
 8+{
 9+  register unsigned char x;
10+  register unsigned char y;
11+
12+  for (;;) {
13+    y = *t++ - 'A';
14+    if (y <= 'Z' - 'A') y += 'a'; else y += 'A';
15+    if (!y) return 1;
16+    if (!len) return 0;
17+    --len;
18+    x = *s++ - 'A';
19+    if (x <= 'Z' - 'A') x += 'a'; else x += 'A';
20+    if (x != y) return 0;
21+  }
22+}
+12, -0
 1@@ -0,0 +1,12 @@
 2+#include "fmt.h"
 3+
 4+unsigned int
 5+fmt_str(char *s, const char *t)
 6+{
 7+  register unsigned int len;
 8+  char ch;
 9+  len = 0;
10+  if (s) { while ((ch = t[len]) != 0) s[len++] = ch; }
11+  else while (t[len]) len++;
12+  return len;
13+}
+15, -0
 1@@ -0,0 +1,15 @@
 2+#include "fmt.h"
 3+
 4+unsigned int
 5+fmt_xlong(char *s, unsigned long u)
 6+{
 7+  register unsigned int len; register unsigned long q; register char c;
 8+  len = 1; q = u;
 9+  while (q > 15) { ++len; q /= 16; }
10+  if (s) {
11+    s += len;
12+    do { c = '0' + (u & 15); if (c > '0' + 9) c += 'a' - '0' - 10;
13+    *--s = c; u /= 16; } while(u);
14+  }
15+  return len;
16+}
+21, -0
 1@@ -0,0 +1,21 @@
 2+#include "str.h"
 3+
 4+unsigned int
 5+str_rchr(const char *s, int c)
 6+{
 7+  register char ch;
 8+  register const char *t;
 9+  register const char *u;
10+
11+  ch = c;
12+  t = s;
13+  u = 0;
14+  for (;;) {
15+    if (!*t) break; if (*t == ch) u = t; ++t;
16+    if (!*t) break; if (*t == ch) u = t; ++t;
17+    if (!*t) break; if (*t == ch) u = t; ++t;
18+    if (!*t) break; if (*t == ch) u = t; ++t;
19+  }
20+  if (!u) u = t;
21+  return (unsigned int)(u - s);
22+}
+8, -0
1@@ -0,0 +1,8 @@
2+#include "byte.h"
3+#include "stralloc.h"
4+
5+int
6+stralloc_copy(stralloc *sato, const stralloc *safrom)
7+{
8+  return stralloc_copyb(sato,safrom->s,safrom->len);
9+}
+35, -0
 1@@ -0,0 +1,35 @@
 2+#include "stralloc.h"
 3+
 4+int stralloc_catulong0(sa,u,n)
 5+stralloc *sa;
 6+unsigned long u;
 7+unsigned int n;
 8+{
 9+  unsigned int len;
10+  unsigned long q;
11+  char *s;
12+
13+  len = 1;
14+  q = u;
15+  while (q > 9) { ++len; q /= 10; }
16+  if (len < n) len = n;
17+
18+  if (!stralloc_readyplus(sa,len)) return 0;
19+  s = sa->s + sa->len;
20+  sa->len += len;
21+  while (len) { s[--len] = '0' + (u % 10); u /= 10; }
22+
23+  return 1;
24+}
25+
26+int stralloc_catlong0(sa,l,n)
27+stralloc *sa;
28+long l;
29+unsigned int n;
30+{
31+  if (l < 0) {
32+    if (!stralloc_append(sa,"-")) return 0;
33+    l = -l;
34+  }
35+  return stralloc_catulong0(sa,l,n);
36+}
+17, -1
 1@@ -36,24 +36,38 @@ build shared/byte_zero.o: cc shared/byte_zero.c
 2   cflags = $base_cflags -Ishared
 3 build shared/fmt_ulong.o: cc shared/fmt_ulong.c
 4   cflags = $base_cflags -Ishared
 5+build shared/fmt_str.o: cc shared/fmt_str.c
 6+  cflags = $base_cflags -Ishared
 7 build shared/fmt_uint.o: cc shared/fmt_uint.c
 8   cflags = $base_cflags -Ishared
 9 build shared/fmt_uint0.o: cc shared/fmt_uint0.c
10   cflags = $base_cflags -Ishared
11+build shared/fmt_xlong.o: cc shared/fmt_xlong.c
12+  cflags = $base_cflags -Ishared
13 build shared/str_len.o: cc shared/str_len.c
14   cflags = $base_cflags -Ishared
15 build shared/str_chr.o: cc shared/str_chr.c
16   cflags = $base_cflags -Ishared
17 build shared/str_diff.o: cc shared/str_diff.c
18   cflags = $base_cflags -Ishared
19+build shared/str_rchr.o: cc shared/str_rchr.c
20+  cflags = $base_cflags -Ishared
21 build shared/str_start.o: cc shared/str_start.c
22   cflags = $base_cflags -Ishared
23+build shared/case_diffs.o: cc shared/case_diffs.c
24+  cflags = $base_cflags -Ishared
25+build shared/case_lowerb.o: cc shared/case_lowerb.c
26+  cflags = $base_cflags -Ishared
27+build shared/case_startb.o: cc shared/case_startb.c
28+  cflags = $base_cflags -Ishared
29 build shared/scan_ulong.o: cc shared/scan_ulong.c
30   cflags = $base_cflags -Ishared
31 build shared/stralloc_eady.o: cc shared/stralloc_eady.c
32   cflags = $base_cflags -Ishared
33 build shared/stralloc_opyb.o: cc shared/stralloc_opyb.c
34   cflags = $base_cflags -Ishared
35+build shared/stralloc_copy.o: cc shared/stralloc_copy.c
36+  cflags = $base_cflags -Ishared
37 build shared/stralloc_catb.o: cc shared/stralloc_catb.c
38   cflags = $base_cflags -Ishared
39 build shared/stralloc_cats.o: cc shared/stralloc_cats.c
40@@ -64,6 +78,8 @@ build shared/stralloc_opys.o: cc shared/stralloc_opys.c
41   cflags = $base_cflags -Ishared
42 build shared/stralloc_cat.o: cc shared/stralloc_cat.c
43   cflags = $base_cflags -Ishared
44+build shared/stralloc_num.o: cc shared/stralloc_num.c
45+  cflags = $base_cflags -Ishared
46 build shared/sgetopt.o: cc shared/sgetopt.c
47   cflags = $base_cflags -Ishared
48 build shared/subgetopt.o: cc shared/subgetopt.c
49@@ -83,5 +99,5 @@ build shared/wait_pid.o: cc shared/wait_pid.c
50 build shared/wait_nohang.o: cc shared/wait_nohang.c
51   cflags = $base_cflags -Ishared
52 
53-build shared/libdjb.a: ar shared/error.o shared/error_str.o shared/alloc.o shared/alloc_re.o shared/buffer.o shared/buffer_get.o shared/buffer_put.o shared/buffer_read.o shared/buffer_write.o shared/buffer_0.o shared/buffer_1.o shared/buffer_2.o shared/byte_chr.o shared/byte_copy.o shared/byte_cr.o shared/byte_diff.o shared/byte_rchr.o shared/byte_zero.o shared/fmt_ulong.o shared/fmt_uint.o shared/fmt_uint0.o shared/str_len.o shared/str_chr.o shared/str_diff.o shared/str_start.o shared/scan_ulong.o shared/stralloc_eady.o shared/stralloc_opyb.o shared/stralloc_catb.o shared/stralloc_cats.o shared/stralloc_pend.o shared/stralloc_opys.o shared/stralloc_cat.o shared/sgetopt.o shared/subgetopt.o shared/readclose.o shared/open_read.o shared/openreadclose.o shared/strerr_die.o shared/strerr_sys.o shared/wait_pid.o shared/wait_nohang.o
54+build shared/libdjb.a: ar shared/error.o shared/error_str.o shared/alloc.o shared/alloc_re.o shared/buffer.o shared/buffer_get.o shared/buffer_put.o shared/buffer_read.o shared/buffer_write.o shared/buffer_0.o shared/buffer_1.o shared/buffer_2.o shared/byte_chr.o shared/byte_copy.o shared/byte_cr.o shared/byte_diff.o shared/byte_rchr.o shared/byte_zero.o shared/fmt_str.o shared/fmt_ulong.o shared/fmt_uint.o shared/fmt_uint0.o shared/fmt_xlong.o shared/str_len.o shared/str_chr.o shared/str_diff.o shared/str_rchr.o shared/str_start.o shared/case_diffs.o shared/case_lowerb.o shared/case_startb.o shared/scan_ulong.o shared/stralloc_eady.o shared/stralloc_opyb.o shared/stralloc_copy.o shared/stralloc_catb.o shared/stralloc_cats.o shared/stralloc_pend.o shared/stralloc_opys.o shared/stralloc_cat.o shared/stralloc_num.o shared/sgetopt.o shared/subgetopt.o shared/readclose.o shared/open_read.o shared/openreadclose.o shared/strerr_die.o shared/strerr_sys.o shared/wait_pid.o shared/wait_nohang.o
55 build shared-all: phony shared/libdjb.a