2011-10-02 10:05:45 +00:00
|
|
|
#ifdef NALL_STRING_INTERNAL_HPP
|
2010-08-09 13:28:56 +00:00
|
|
|
|
|
|
|
//usage example:
|
2011-08-06 14:03:52 +00:00
|
|
|
//if(auto position = strpos(str, key)) print(position(), "\n");
|
|
|
|
//prints position of key within str; but only if it is found
|
2010-08-09 13:28:56 +00:00
|
|
|
|
|
|
|
namespace nall {
|
|
|
|
|
2011-08-06 14:03:52 +00:00
|
|
|
template<bool Insensitive, bool Quoted>
|
|
|
|
optional<unsigned> ustrpos(const char *str, const char *key) {
|
|
|
|
const char *base = str;
|
2010-08-09 13:28:56 +00:00
|
|
|
|
2011-08-06 14:03:52 +00:00
|
|
|
while(*str) {
|
|
|
|
if(quoteskip<Quoted>(str)) continue;
|
|
|
|
for(unsigned n = 0;; n++) {
|
|
|
|
if(key[n] == 0) return { true, (unsigned)(str - base) };
|
|
|
|
if(str[n] == 0) return { false, 0 };
|
|
|
|
if(!chrequal<Insensitive>(str[n], key[n])) break;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
2011-08-06 14:03:52 +00:00
|
|
|
str++;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return { false, 0 };
|
|
|
|
}
|
|
|
|
|
2011-08-06 14:03:52 +00:00
|
|
|
optional<unsigned> strpos(const char *str, const char *key) { return ustrpos<false, false>(str, key); }
|
|
|
|
optional<unsigned> istrpos(const char *str, const char *key) { return ustrpos<true, false>(str, key); }
|
|
|
|
optional<unsigned> qstrpos(const char *str, const char *key) { return ustrpos<false, true>(str, key); }
|
|
|
|
optional<unsigned> iqstrpos(const char *str, const char *key) { return ustrpos<true, true>(str, key); }
|
|
|
|
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|