FreeBASIC  0.91.0
strw_instrrev.c
Go to the documentation of this file.
1 /* instrrevw function */
2 
3 #include "fb.h"
4 
5 FBCALL ssize_t fb_WstrInstrRev( const FB_WCHAR *src, const FB_WCHAR *patt, ssize_t start )
6 {
7  if( (src != NULL) && (patt != NULL) )
8  {
9  ssize_t size_src = fb_wstr_Len(src);
10  ssize_t size_patt = fb_wstr_Len(patt);
11  ssize_t i, j;
12 
13  if( (size_src != 0) && (size_patt != 0) && (size_patt <= size_src) && (start != 0))
14  {
15  /* handle signed/unsigned comparisons of start and size_* vars */
16  if( start < 0 )
17  start = size_src - size_patt + 1;
18  else if( start > size_src )
19  start = 0;
20  else if(start > size_src - size_patt)
21  start = size_src - size_patt + 1;
22 
23  /*
24  There is no wcsrstr() function,
25  so instead do a brute force scan.
26  */
27 
28  for( i=0; i<start; ++i )
29  {
30  for( j=0; j!=size_patt; ++j )
31  {
32  if( src[start-i+j-1] != patt[j] )
33  break;
34  }
35  if( j==size_patt )
36  return start - i;
37  }
38  }
39  }
40 
41  return 0;
42 }