FreeBASIC  0.91.0
str_instrany.c
Go to the documentation of this file.
1 /* instrany function */
2 
3 #include "fb.h"
4 
5 FBCALL ssize_t fb_StrInstrAny( ssize_t start, FBSTRING *src, FBSTRING *patt )
6 {
7  ssize_t r;
8 
9  if( (src == NULL) || (src->data == NULL) || (patt == NULL) || (patt->data == NULL) )
10  {
11  r = 0;
12  }
13  else
14  {
15  ssize_t size_src = FB_STRSIZE(src);
16  ssize_t size_patt = FB_STRSIZE(patt);
17 
18  if( (size_src == 0) || (size_patt == 0) || (start < 1) || (start > size_src) )
19  {
20  r = 0;
21  }
22  else
23  {
24  ssize_t i, found, search_len = size_src - start + 1;
25  const char *pachText = src->data + start - 1;
26  r = search_len;
27 
28  for( i=0; i!=size_patt; ++i )
29  {
30  const char *pszEnd = (const char *) FB_MEMCHR( pachText, patt->data[i], r );
31  if( pszEnd!=NULL )
32  {
33  found = pszEnd - pachText;
34  if( found < r )
35  r = found;
36  }
37  }
38  if( r==search_len )
39  {
40  r = 0;
41  }
42  else
43  {
44  r += start;
45  }
46  }
47  }
48 
49  FB_STRLOCK();
50 
51  /* del if temp */
52  fb_hStrDelTemp_NoLock( src );
53  fb_hStrDelTemp_NoLock( patt );
54 
55  FB_STRUNLOCK();
56 
57  return r;
58 }