/* Return value: a pointer to the found occurrence, or NULL if not found.  */
gchar* g_strstr_len(const gchar *haystack, gssize haystack_len, const gchar *needle)
/* Return value: a pointer to the found occurrence, or NULL if not found.  */
gchar* g_strstr_len(const gchar *haystack, gssize haystack_len, const gchar *needle)
{
  g_return_val_if_fail (haystack != NULL, NULL);
  g_return_val_if_fail (needle != NULL, NULL);
  if (haystack_len < 0)
    return strstr (haystack, needle);
  else
    {
      const gchar *p = haystack;
      gsize needle_len = strlen (needle);
      const gchar *end;
      gsize i;
      if (needle_len == 0)
	return (gchar *)haystack;
      if (haystack_len < needle_len)
	return NULL;
      end = haystack + haystack_len - needle_len;
      while (*p && p <= end)
	{
	  for (i = 0; i < needle_len; i++)
	    if (p[i] != needle[i])
	      goto next;
	  return (gchar *)p;
	next:
	  p++;
	}
      return NULL;
    }
}