diff options
author | Jannis Hoffmann <jannis@fehcom.de> | 2024-07-09 13:02:45 +0200 |
---|---|---|
committer | Jannis Hoffmann <jannis@fehcom.de> | 2024-07-09 13:02:45 +0200 |
commit | 96cf8dffe4f7b0b910f790066ae622dc429eb522 (patch) | |
tree | cc1343a0ac92bb4836cae2dd63a97fa045765e7f /str.c |
initial commit of version 23fehQlibs-23
Diffstat (limited to 'str.c')
-rw-r--r-- | str.c | 135 |
1 files changed, 135 insertions, 0 deletions
@@ -0,0 +1,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; +} |