Home » Archimedes archive » Acorn User » AU 1997-10 A.adf » Extras » Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/h/string/instr

Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/h/string/instr

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Archimedes archive » Acorn User » AU 1997-10 A.adf » Extras
Filename: Apple][e/PD/BOB/ARMBOB/!ArmBob/progs/h/string/instr
Read OK:
File size: 01E3 bytes
Load address: 0000
Exec address: 0000
File contents
/* GCW 02/03/94 */
/* return index at which substring b first occurs in string a, after
   index i, or -1 if no occurrence */

instr(a,b,i)
{
 local a_l,b_l,j,k;
 a_l = sizeof(a);
 b_l = sizeof(b);
 if ( b_l == 0 ) return(i);
 if ( a_l < (b_l + i) ) return(-1);
 repeat
  {
   j = 0;
   while ( (i < a_l) && (a[i] != b[j]) ) i++;
   k = i;
   while ( (i < a_l) && (j < b_l) && (a[i] == b[j]) ) 
       { j++; i++;}
  }
 while ( (j < b_l) && (i < a_l) );
 return (j == b_l)?k:(-1);
}
00000000  2f 2a 20 47 43 57 20 30  32 2f 30 33 2f 39 34 20  |/* GCW 02/03/94 |
00000010  2a 2f 0a 2f 2a 20 72 65  74 75 72 6e 20 69 6e 64  |*/./* return ind|
00000020  65 78 20 61 74 20 77 68  69 63 68 20 73 75 62 73  |ex at which subs|
00000030  74 72 69 6e 67 20 62 20  66 69 72 73 74 20 6f 63  |tring b first oc|
00000040  63 75 72 73 20 69 6e 20  73 74 72 69 6e 67 20 61  |curs in string a|
00000050  2c 20 61 66 74 65 72 0a  20 20 20 69 6e 64 65 78  |, after.   index|
00000060  20 69 2c 20 6f 72 20 2d  31 20 69 66 20 6e 6f 20  | i, or -1 if no |
00000070  6f 63 63 75 72 72 65 6e  63 65 20 2a 2f 0a 0a 69  |occurrence */..i|
00000080  6e 73 74 72 28 61 2c 62  2c 69 29 0a 7b 0a 20 6c  |nstr(a,b,i).{. l|
00000090  6f 63 61 6c 20 61 5f 6c  2c 62 5f 6c 2c 6a 2c 6b  |ocal a_l,b_l,j,k|
000000a0  3b 0a 20 61 5f 6c 20 3d  20 73 69 7a 65 6f 66 28  |;. a_l = sizeof(|
000000b0  61 29 3b 0a 20 62 5f 6c  20 3d 20 73 69 7a 65 6f  |a);. b_l = sizeo|
000000c0  66 28 62 29 3b 0a 20 69  66 20 28 20 62 5f 6c 20  |f(b);. if ( b_l |
000000d0  3d 3d 20 30 20 29 20 72  65 74 75 72 6e 28 69 29  |== 0 ) return(i)|
000000e0  3b 0a 20 69 66 20 28 20  61 5f 6c 20 3c 20 28 62  |;. if ( a_l < (b|
000000f0  5f 6c 20 2b 20 69 29 20  29 20 72 65 74 75 72 6e  |_l + i) ) return|
00000100  28 2d 31 29 3b 0a 20 72  65 70 65 61 74 0a 20 20  |(-1);. repeat.  |
00000110  7b 0a 20 20 20 6a 20 3d  20 30 3b 0a 20 20 20 77  |{.   j = 0;.   w|
00000120  68 69 6c 65 20 28 20 28  69 20 3c 20 61 5f 6c 29  |hile ( (i < a_l)|
00000130  20 26 26 20 28 61 5b 69  5d 20 21 3d 20 62 5b 6a  | && (a[i] != b[j|
00000140  5d 29 20 29 20 69 2b 2b  3b 0a 20 20 20 6b 20 3d  |]) ) i++;.   k =|
00000150  20 69 3b 0a 20 20 20 77  68 69 6c 65 20 28 20 28  | i;.   while ( (|
00000160  69 20 3c 20 61 5f 6c 29  20 26 26 20 28 6a 20 3c  |i < a_l) && (j <|
00000170  20 62 5f 6c 29 20 26 26  20 28 61 5b 69 5d 20 3d  | b_l) && (a[i] =|
00000180  3d 20 62 5b 6a 5d 29 20  29 20 0a 20 20 20 20 20  |= b[j]) ) .     |
00000190  20 20 7b 20 6a 2b 2b 3b  20 69 2b 2b 3b 7d 0a 20  |  { j++; i++;}. |
000001a0  20 7d 0a 20 77 68 69 6c  65 20 28 20 28 6a 20 3c  | }. while ( (j <|
000001b0  20 62 5f 6c 29 20 26 26  20 28 69 20 3c 20 61 5f  | b_l) && (i < a_|
000001c0  6c 29 20 29 3b 0a 20 72  65 74 75 72 6e 20 28 6a  |l) );. return (j|
000001d0  20 3d 3d 20 62 5f 6c 29  3f 6b 3a 28 2d 31 29 3b  | == b_l)?k:(-1);|
000001e0  0a 7d 0a                                          |.}.|
000001e3