![]() |
|
|||
File indexing completed on 2025-08-03 08:20:53
0001 /* 0002 0003 This function is modelled after the "strstr" function in 0004 string.h. There is no standard function which locates a substring in a 0005 string which is not 0-terminated. In all other respects the strnstr 0006 function is suuposed to behave as strstr, that is, it returns a 0007 pointer to the first occurence of string s2 in string s1, only that we 0008 can deal here without not 0-terminated strings and rather tell the 0009 length of the strings by the s1len and s2len parameters. 0010 0011 On some systems the memmem function does this, but it is not available on 0012 all systems. 0013 0014 */ 0015 #include <iostream> 0016 #include <cstring> 0017 0018 char * strnstr (const char *s1, size_t s1len, const char *s2, size_t s2len) 0019 { 0020 0021 size_t i; 0022 char *c; 0023 0024 /* if s2len is 0, we are done */ 0025 if (s2len <= 0) return (char *) s1; 0026 0027 /* if s2len > s1len we will not find the substring here. */ 0028 if (s2len > s1len ) return 0; 0029 0030 char *s2copy = new char[s2len+1]; 0031 c = s2copy; 0032 for (i=0; i<s2len; i++) *c++ = s2[i]; 0033 *c = 0; 0034 0035 c = (char *) s1; 0036 for (i=0; i <= s1len - s2len; i++) 0037 { 0038 if (strncmp(c, s2copy, s2len) == 0) 0039 { 0040 delete [] s2copy; 0041 return c; 0042 } 0043 c++; 0044 } 0045 delete [] s2copy; 0046 return 0; 0047 } 0048
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |