/*
    This file is part of libtermui.

    libtermui is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    libtermui is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libtermui.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2006, Alexandre Becoulet <alexandre.becoulet@free.fr>

*/

#include <string.h>

#include <termui/strings.h>

termui_strlen_t
termui_str_match_len(const char *a, const char *b)
{
  termui_strlen_t i = 0;

  while (a[i] && (a[i] == b[i]))
    i++;

  return i;
}

termui_bool_t
termui_str_match(const char *pstart, const char *pend, const char *name)
{
  for (; pstart < pend && *name; pstart++, name++)
    {
      if (*pstart == '?')
	continue;

      if (*pstart == '*')
	{
	  while (*name)
	    if (termui_str_match(pstart + 1, pend, name++))
	      return 1;
	  return 0;
	}

      if (*pstart != *name)
	break;
    }

  return pstart == pend;
}

termui_bool_t
termui_str_strstr(const char *pstart, const char *pend, const char *name)
{
  termui_ssize_t len = strlen(name) - (pend - pstart);

  while (len-- >= 0)
    if (!strncmp(name++, pstart, (pend - pstart)))
      return 1;

  return 0;
}

