add Berkeley specific header
[unix-history] / usr / src / usr.bin / tn3270 / general / genbsubs.c
index bf8e8e9..35c58ee 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- *     Copyright (c) 1984, 1985, 1986 by the Regents of the
+ *     Copyright (c) 1984-1987 by the Regents of the
  *     University of California and by Gregory Glenn Minshall.
  *
  *     Permission to use, copy, modify, and distribute these
  *     University of California and by Gregory Glenn Minshall.
  *
  *     Permission to use, copy, modify, and distribute these
@@ -20,7 +20,7 @@
  */
 
 #ifndef        lint
  */
 
 #ifndef        lint
-static char    sccsid[] = "@(#)genbsubs.c      3.1  10/29/86";
+static char sccsid[] = "@(#)genbsubs.c 3.1 (Berkeley) %G%";
 #endif /* ndef lint */
 
 /* The output of bunequal is the offset of the byte which didn't match;
 #endif /* ndef lint */
 
 /* The output of bunequal is the offset of the byte which didn't match;
@@ -61,3 +61,53 @@ register int b;
     }
     return(i-1);
 }
     }
     return(i-1);
 }
+
+/*
+ * memNSchr(const void *s, int c, size_t n, int and)
+ *
+ * Like memchr, but the comparison is '((*s)&and) == c',
+ * and we increment our way through s by "stride" ('s += stride').
+ *
+ * We optimize for the most used strides of +1 and -1.
+ */
+
+unsigned char *
+memNSchr(s, c, n, and, stride)
+char *s;
+int c;
+unsigned int n;
+int and;
+int stride;
+{
+    register unsigned char _c, *_s, _and;
+
+    _and = and;
+    _c = (c&_and);
+    _s = (unsigned char *)s;
+    switch (stride) {
+    case 1:
+       while (n--) {
+           if (((*_s)&_and) == _c) {
+               return _s;
+           }
+           _s++;
+       }
+       break;
+    case -1:
+       while (n--) {
+           if (((*_s)&_and) == _c) {
+               return _s;
+           }
+           _s--;
+       }
+       break;
+    default:
+       while (n--) {
+           if (((*_s)&_and) == _c) {
+               return _s;
+           }
+           _s += stride;
+       }
+    }
+    return 0;
+}