| 1 | /* Copyright (c) 2000, 2006, 2007 MySQL AB |
| 2 | Use is subject to license terms |
| 3 | |
| 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. |
| 7 | |
| 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. |
| 12 | |
| 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 Street, Fifth Floor, Boston, MA 02110-1301, USA */ |
| 16 | |
| 17 | #include <my_global.h> |
| 18 | #include <m_string.h> |
| 19 | |
| 20 | /* |
| 21 | my_memmem, port of a GNU extension. |
| 22 | |
| 23 | Returns a pointer to the beginning of the substring, needle, or NULL if the |
| 24 | substring is not found in haystack. |
| 25 | */ |
| 26 | |
| 27 | void *my_memmem(const void *haystack, size_t haystacklen, |
| 28 | const void *needle, size_t needlelen) |
| 29 | { |
| 30 | const unsigned char *cursor; |
| 31 | const unsigned char *last_possible_needle_location = |
| 32 | (unsigned char *)haystack + haystacklen - needlelen; |
| 33 | |
| 34 | /* Easy answers */ |
| 35 | if (needlelen > haystacklen) return(NULL); |
| 36 | if (needle == NULL) return(NULL); |
| 37 | if (haystack == NULL) return(NULL); |
| 38 | if (needlelen == 0) return(NULL); |
| 39 | if (haystacklen == 0) return(NULL); |
| 40 | |
| 41 | for (cursor = haystack; cursor <= last_possible_needle_location; cursor++) { |
| 42 | if (memcmp(needle, cursor, needlelen) == 0) { |
| 43 | return((void *) cursor); |
| 44 | } |
| 45 | } |
| 46 | return(NULL); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | |
| 51 | #ifdef MAIN |
| 52 | #include <assert.h> |
| 53 | |
| 54 | int main(int argc, char *argv[]) { |
| 55 | char haystack[10], needle[3]; |
| 56 | |
| 57 | memmove(haystack, "0123456789" , 10); |
| 58 | |
| 59 | memmove(needle, "no" , 2); |
| 60 | assert(my_memmem(haystack, 10, needle, 2) == NULL); |
| 61 | |
| 62 | memmove(needle, "345" , 3); |
| 63 | assert(my_memmem(haystack, 10, needle, 3) != NULL); |
| 64 | |
| 65 | memmove(needle, "789" , 3); |
| 66 | assert(my_memmem(haystack, 10, needle, 3) != NULL); |
| 67 | assert(my_memmem(haystack, 9, needle, 3) == NULL); |
| 68 | |
| 69 | memmove(needle, "012" , 3); |
| 70 | assert(my_memmem(haystack, 10, needle, 3) != NULL); |
| 71 | assert(my_memmem(NULL, 10, needle, 3) == NULL); |
| 72 | |
| 73 | assert(my_memmem(NULL, 10, needle, 3) == NULL); |
| 74 | assert(my_memmem(haystack, 0, needle, 3) == NULL); |
| 75 | assert(my_memmem(haystack, 10, NULL, 3) == NULL); |
| 76 | assert(my_memmem(haystack, 10, needle, 0) == NULL); |
| 77 | |
| 78 | assert(my_memmem(haystack, 1, needle, 3) == NULL); |
| 79 | |
| 80 | printf("success\n" ); |
| 81 | return(0); |
| 82 | } |
| 83 | |
| 84 | #endif |
| 85 | |