#include "strutil.h" #include "string.h" uintptr_t strnhash(const char* s, size_t n) { static const size_t shift = sizeof(uintptr_t) * 8 - 4; uintptr_t hash = 0; for(size_t i = 0; i < n; ++i) { hash = ((hash << 4) | s[i]) ^ (((uintptr_t)0xF << shift) & hash); } return hash; } uintptr_t strhash(const char* s) { return strnhash(s, strlen(s)); } long strlast(const char* rbegin, const char* rend, char search) { const char* itr = rbegin; while(itr < rend && *itr != '\0') if(*itr == search) return itr - rend; else itr--; return -1; } long strfirst(const char* begin, const char* end, char search) { const char* itr = begin; while(itr < end && *itr != '\0') if(*itr == search) return itr - begin; else itr++; return -1; } long strcount(const char* begin, const char* end, char search) { long count = 0; for(;begin < end && *begin != '\0'; ++begin) if(*begin == search) ++count; return count; } long strfirst_pred(const char* begin, const char* end, CharPredFn pred) { const char* itr = begin; while(itr < end && *itr != '\0') if(pred(*itr)) return itr - begin; else itr++; return -1; } long strlast_pred(const char* rbegin, const char* rend, CharPredFn pred) { const char* itr = rbegin; while(itr < rend && *itr != '\0') if(pred(*itr)) return itr - rend; else itr--; return -1; }