home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / mysql / mysql-3.23.28g-win-source.exe / strings / r_strinstr.c < prev    next >
C/C++ Source or Header  |  2000-08-31  |  2KB  |  53 lines

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This library is free software; you can redistribute it and/or
  4.    modify it under the terms of the GNU Library General Public
  5.    License as published by the Free Software Foundation; either
  6.    version 2 of the License, or (at your option) any later version.
  7.    
  8.    This library is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.    Library General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU Library General Public
  14.    License along with this library; if not, write to the Free
  15.    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  16.    MA 02111-1307, USA */
  17.  
  18. /*
  19.   Author : David
  20.   strintstr(src, from, pat) looks for an instance of pat in src
  21.   backwards from pos from.  pat is not a regex(3) pattern, it is a literal
  22.   string which must be matched exactly.
  23.   The result 0 if the pattern was not found else it is the start char of
  24.   the pattern counted from the begining of the string.
  25. */
  26.  
  27. #include <global.h>
  28. #include "m_string.h"
  29.  
  30. uint r_strinstr(reg1 my_string str,int from, reg4 my_string search)
  31. {
  32.   reg2 my_string    i, j;
  33.   uint        len = (uint) strlen(search);
  34.   /* pointer to the last char of buff */
  35.   my_string    start = str + from - 1;
  36.   /* pointer to the last char of search */
  37.   my_string    search_end = search + len - 1;
  38.  
  39.  skipp:
  40.   while (start >= str)        /* Cant be != because the first char */
  41.   {
  42.     if (*start-- == *search_end)
  43.     {
  44.       i = start; j = search_end - 1;
  45.       while (j >= search && start > str)
  46.     if (*i-- != *j--)
  47.       goto skipp;
  48.       return (uint) ((start - len) - str + 3);
  49.     }
  50.   }
  51.   return (0);
  52. }
  53.