summaryrefslogtreecommitdiff
path: root/case.c
diff options
context:
space:
mode:
authorJannis Hoffmann <jannis@fehcom.de>2024-07-09 13:02:45 +0200
committerJannis Hoffmann <jannis@fehcom.de>2024-07-09 13:02:45 +0200
commit96cf8dffe4f7b0b910f790066ae622dc429eb522 (patch)
treecc1343a0ac92bb4836cae2dd63a97fa045765e7f /case.c
initial commit of version 23fehQlibs-23
Diffstat (limited to 'case.c')
-rw-r--r--case.c134
1 files changed, 134 insertions, 0 deletions
diff --git a/case.c b/case.c
new file mode 100644
index 0000000..47eb8ed
--- /dev/null
+++ b/case.c
@@ -0,0 +1,134 @@
+#include "case.h"
+#include "str.h"
+
+/**
+ @file case.c
+ @author djb
+ @brief string comparison and helper functions; case insensitive
+*/
+
+int case_diffb(register char *s,unsigned int len,register char *t)
+{
+ register unsigned char x;
+ register unsigned char y;
+
+ while (len > 0) {
+ --len;
+ x = *s++ - 'A';
+ if (x <= 'Z' - 'A') x += 'a'; else x += 'A';
+ y = *t++ - 'A';
+ if (y <= 'Z' - 'A') y += 'a'; else y += 'A';
+ if (x != y)
+ return ((int)(unsigned int) x) - ((int)(unsigned int) y);
+ }
+ return 0;
+}
+
+int case_diffs(register char *s,register char *t)
+{
+ register unsigned char x;
+ register unsigned char y;
+
+ for (;;) {
+ x = *s++ - 'A';
+ if (x <= 'Z' - 'A') x += 'a'; else x += 'A';
+ y = *t++ - 'A';
+ if (y <= 'Z' - 'A') y += 'a'; else y += 'A';
+ if (x != y) break;
+ if (!x) break;
+ }
+ return ((int)(unsigned int) x) - ((int)(unsigned int) y);
+}
+
+int case_diffrs(register char *s,register char *t)
+{
+ register unsigned char x = 0;
+ register unsigned char y = 0;
+ unsigned int lens = str_len(s);
+ unsigned int lent = str_len(t);
+
+ while (lens > 0 && lent > 0) {
+ x = s[--lens] - 'A';
+ if (x <= 'Z' - 'A') x += 'a'; else x += 'A';
+ y = t[--lent] - 'A';
+ if (y <= 'Z' - 'A') y += 'a'; else y += 'A';
+ if (x != y) break;
+ if (!x) break;
+ if (!y) break;
+ }
+ return ((int)(unsigned int) x) - ((int)(unsigned int) y);
+}
+
+void case_lowerb(char *s,unsigned int len)
+{
+ unsigned char x;
+ while (len > 0) {
+ --len;
+ x = *s - 'A';
+ if (x <= 'Z' - 'A') *s = x + 'a';
+ ++s;
+ }
+}
+
+void case_lowers(char *s)
+{
+ unsigned char x;
+ while ((x = *s)) {
+ x -= 'A';
+ if (x <= 'Z' - 'A') *s = x + 'a';
+ ++s;
+ }
+}
+
+void case_upperb(char *s,unsigned int len)
+{
+ unsigned char x;
+ while (len > 0) {
+ --len;
+ x = *s - 'a';
+ if (x <= 'z' - 'a') *s = x + 'A';
+ ++s;
+ }
+}
+
+void case_uppers(char *s)
+{
+ unsigned char x;
+ while ((x = *s)) {
+ x -= 'a';
+ if (x <= 'z' - 'a') *s = x + 'A';
+ ++s;
+ }
+}
+
+int case_startb(register char *s,unsigned int len,register char *t)
+{
+ register unsigned char x;
+ register unsigned char y;
+
+ for (;;) {
+ y = *t++ - 'A';
+ if (y <= 'Z' - 'A') y += 'a'; else y += 'A';
+ if (!y) return 1;
+ if (!len) return 0;
+ --len;
+ x = *s++ - 'A';
+ if (x <= 'Z' - 'A') x += 'a'; else x += 'A';
+ if (x != y) return 0;
+ }
+}
+
+int case_starts(register char *s,register char *t)
+{
+ register unsigned char x;
+ register unsigned char y;
+
+ for (;;) {
+ x = *s++ - 'A';
+ if (x <= 'Z' - 'A') x += 'a'; else x += 'A';
+ y = *t++ - 'A';
+ if (y <= 'Z' - 'A') y += 'a'; else y += 'A';
+ if (!y) return 1;
+ if (x != y) return 0;
+ }
+}