/* Returns: a pointer to the found occurrence, or NULL if not found.  */
gchar* g_strrstr_len(const gchar *haystack, gssize haystack_len, const gchar *needle)
/* Returns: a pointer to the found occurrence, or NULL if not found.  */
gchar* g_strrstr_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 g_strrstr (haystack, needle);
  else
    {
      gsize needle_len = strlen (needle);
      const gchar *haystack_max = haystack + haystack_len;
      const gchar *p = haystack;
      gsize i;
      while (p < haystack_max && *p)
        p++;
      if (p < haystack + needle_len)
        return NULL;
      p -= needle_len;
      while (p >= haystack)
        {
          for (i = 0; i < needle_len; i++)
            if (p[i] != needle[i])
              goto next;
          return (gchar *)p;
        next:
          p--;
        }
      return NULL;
    }
}