tb_wchar_t* tb_wcsistr(tb_wchar_t const* s1, tb_wchar_t const* s2) { // check tb_assert_and_check_return_val(s1 && s2, tb_null); // init __tb_register__ tb_wchar_t const* s = s1; __tb_register__ tb_wchar_t const* p = s2; // done do { if (!*p) return (tb_wchar_t* )s1; if ((*p == *s) || (tb_tolower(*p) == tb_tolower(*s))) { ++p; ++s; } else { p = s2; if (!*s) return tb_null; s = ++s1; } } while (1); return tb_null; }
/* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ tb_char_t* tb_strnistr(tb_char_t const* s1, tb_size_t n1, tb_char_t const* s2) { // check tb_assert_and_check_return_val(s1 && s2 && n1, tb_null); // init __tb_register__ tb_char_t const* s = s1; __tb_register__ tb_char_t const* p = s2; __tb_register__ tb_size_t n = n1; // done do { if (!*p) return (tb_char_t* )s1; if (n && ((*p == *s) || (tb_tolower(*((tb_byte_t*)p)) == tb_tolower(*((tb_byte_t*)s))))) { ++p; ++s; --n; } else { p = s2; if (!*s || !n) return tb_null; s = ++s1; n = --n1; } } while (1); // no found return tb_null; }
static tb_long_t tb_stricmp_impl(tb_char_t const* s1, tb_char_t const* s2) { // check tb_assert_and_check_return_val(s1 && s2, 0); tb_check_return_val(s1 != s2, 0); // done tb_long_t r = 0; while (((s1 == s2) || !(r = ((tb_long_t)(tb_tolower(*((tb_byte_t* )s1)))) - tb_tolower(*((tb_byte_t* )s2)))) && (++s2, *s1++)); return r; }
tb_long_t tb_wcsicmp(tb_wchar_t const* s1, tb_wchar_t const* s2) { // check tb_assert_and_check_return_val(s1 && s2, 0); tb_check_return_val(s1 != s2, 0); // done while ((*s1 == *s2) || (tb_tolower(*s1) == tb_tolower(*s2))) { if (!*s1++) return 0; ++s2; } return (((tb_wchar_t)tb_tolower(*s1)) < ((tb_wchar_t)tb_tolower(*s2))) ? -1 : 1; }
tb_long_t tb_wcsnicmp(tb_wchar_t const* s1, tb_wchar_t const* s2, tb_size_t n) { // check tb_assert_and_check_return_val(s1 && s2, 0); tb_check_return_val(s1 != s2, 0); // done while (n && ((*s1 == *s2) || (tb_tolower(*s1) == tb_tolower(*s2)))) { if (!*s1++) return 0; ++s2; --n; } return n? ((((tb_wchar_t)tb_tolower(*s1)) < ((tb_wchar_t)tb_tolower(*s2))) ? -1 : 1) : 0; }
tb_wchar_t* tb_wcsnirchr(tb_wchar_t const* s, tb_size_t n, tb_wchar_t c) { // check tb_assert_and_check_return_val(s, tb_null); // done tb_wchar_t const* p = s + n - 1; tb_wchar_t b = tb_tolower(c); while (p >= s && *p) { if (tb_tolower(*p) == b) return (tb_wchar_t*)p; p--; } return tb_null; }
static tb_void_t tb_check_tolower() { tb_int_t i = 0; for (i = 0; i < 256; i++) { if ((tb_tolower(i)? 1 : 0) != (tolower(i)? 1 : 0)) tb_printf("[e] tolower: 0x%02x = 0x%02x\n", i, tolower(i)); } }
tb_char_t* tb_strichr(tb_char_t const* s, tb_char_t c) { // check tb_assert_and_check_return_val(s, tb_null); // init tb_byte_t const* p = (tb_byte_t const*)s; tb_byte_t b = tb_tolower((tb_byte_t)c); // find while (*p) { if (tb_tolower(*p) == b) return (tb_char_t*)p; p++; } return tb_null; }