mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / strings / r_strinstr.c
blobed766163dee7b7c6ab835912edec1621ef773625
1 /* Copyright (c) 2000, 2001, 2004, 2007 MySQL AB
2 Use is subject to license terms.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program 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
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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.
27 #include <my_global.h>
28 #include "m_string.h"
30 size_t r_strinstr(reg1 const char * str, size_t from, reg4 const char * search)
32 reg2 const char *i, *j;
33 size_t len = strlen(search);
34 /* pointer to the last char of buff */
35 const char * start = str + from - 1;
36 /* pointer to the last char of search */
37 const char * search_end = search + len - 1;
39 skip:
40 while (start >= str) /* Cant be != because the first char */
42 if (*start-- == *search_end)
44 i = start; j = search_end - 1;
45 while (j >= search && start > str)
46 if (*i-- != *j--)
47 goto skip;
48 return (size_t) ((start - len) - str + 3);
51 return (0);