いけむランド

はてダからやってきました

strcasestr

残念ながら cygwin には strcasestr がない。

strstr() 関数は、部分文字列 needle が文字列 haystack 中で最初に現れる位置を見つける。文字列を終端する '\0' 文字は比較されない。

strcasestr() 関数は strstr() 関数と同様だが、両方の引数に対して大文字小文字を無視する。

移植時に必要になった場合は適当に何処かから拾ってきて、static 関数として埋め込んだりして回避する。

#ifdef __CYGWIN__
static const char *strcasestr (const char *haystack, const char *needle)
{
  int haypos;
  int needlepos;

  haypos = 0;
  while (haystack[haypos]) {
    if (tolower (haystack[haypos]) == tolower(needle[0])) {
      needlepos = 1;
      while ( (needle[needlepos]) &&
              (tolower(haystack[haypos + needlepos])
               == tolower(needle[needlepos])) )
          ++needlepos;
      if (! needle[needlepos]) return (haystack + haypos);
    }
    ++haypos;
  }
  return NULL;
}
#endif // __CYGWIN__