char *strstr( char *haystack, char *needle ) {
char *hp;
char *np;
if( !*needle ) return haystack; // null needle; return haystack (strange POSIX specification requirement!)
while( *haystack ) { // until we reach the end of the haystack
hp = haystack; // copy of the haystack pointer
np = needle; // copy of the needle pointer
do {
if( !*np ) return haystack; // end of needle; match completed; return current value of haystack pointer
} while( *hp++ == *np++ ); // increment both pointer (copies) while the chars match
++haystack; // Got here means a mismatch; base pointer to next character.
}
return 0; // Got here means we hit the terminating null of the haystack; no match; return null.
}
####
U64 nextQuad( U64 **p, U8 o ) { // p: pointer to pointer to the 'current' quad; o: value of the unused bits at the beginning.
U64 hi = *( ++*p ) // increment the pointer at *p, and set hi to the value it points at.
, lo = *( *p+1 ); // and lo to the value + 1 it points at
return __shiftleft128( lo, hi, o ); // return the value from the higher location with o bits from lo shifted in from the right.
}
####
U64 nextBit( U64 **p, U8 *o ) { // p: pointer to pointer to current quad; o: pointer to current offset
U64 hi, lo;
if( ++( *o ) == 64 ) { // increment the offset and detect transitions across quad boundaries.
*o = 0; // reset the offset.
++*p; // increment the quad pointer.
}
hi = **p; // Get the 'current' (unshifted) quad value
lo = *( *p + 1 ); // And the next
return __shiftleft128( lo, hi, *o ); // return the value from the higher location with *o bits from lo shifted in from the right.
}