xref: /csrg-svn/lib/libc/vax/string/strstr.s (revision 61222)
142084Sbostic/*-
2*61222Sbostic * Copyright (c) 1990, 1993
3*61222Sbostic *	The Regents of the University of California.  All rights reserved.
442084Sbostic *
542084Sbostic * %sccs.include.redist.c%
642084Sbostic */
742084Sbostic
842084Sbostic#if defined(LIBC_SCCS) && !defined(lint)
9*61222Sbostic	.asciz "@(#)strstr.s	8.1 (Berkeley) 06/04/93"
1042084Sbostic#endif /* LIBC_SCCS and not lint */
1142084Sbostic
1242084Sbostic/*
1342084Sbostic * Find the first occurrence of s2 as a substring in s1.
1442084Sbostic * If s2 is empty, return s1.
1542084Sbostic *
1642084Sbostic * char *strstr(s1, s2)
1742084Sbostic *	const char *s1, *s2;
1842084Sbostic */
1942084Sbostic#include "DEFS.h"
2042084Sbostic
2142084SbosticENTRY(strstr, 0)
2242084Sbostic	movq	4(ap),r3	/* r3 = s1, r4 = s2 */
2342084Sbostic	movzwl	$65535,r2	/* r2 = locc/matchc limit */
2442084Sbostic	locc	$0,r2,(r4)	/* find '\0' in s2 */
2542084Sbostic	beql	4f
2645053Sbostic	subl3	r4,r1,r5	/* r5 = strlen(s2) */
2742084Sbostic	beql	1f		/* if r5 == 0, return s1 */
2842084Sbostic
2942084Sbostic	/*
3042084Sbostic	 * s2 is short enough to apply matchc.
3142084Sbostic	 * If s1 is long, we have to do it in stages.
3242084Sbostic	 */
3342084Sbostic0:	locc	$0,r2,(r3)	/* find '\0' in s1 */
3442084Sbostic	beql	3f
3542084Sbostic
3642084Sbostic	/*
3742084Sbostic	 * Both strings are `short'; we can use matchc directly.
3842084Sbostic	 */
3945053Sbostic	subl3	r3,r1,r1	/* r1 = strlen(s1) */
4042084Sbostic	matchc	r5,(r4),r1,(r3)	/* find substring */
4142084Sbostic	bneq	2f
4242084Sbostic
4342084Sbostic	/*
4442084Sbostic	 * r3 points r5 bytes past match.  Return the match.
4542084Sbostic	 */
4642084Sbostic1:	subl3	r5,r3,r0	/* return (byte_past_match - strlen(s2)) */
4742084Sbostic	ret
4842084Sbostic
4942084Sbostic	/*
5042084Sbostic	 * There is no matching substring.
5142084Sbostic	 */
5242084Sbostic2:	clrl	r0		/* return NULL */
5342084Sbostic	ret
5442084Sbostic
5542084Sbostic	/*
5642084Sbostic	 * s1 is too long (> 65535 bytes) to apply matchc directly,
5742084Sbostic	 * but s2 is short enough.  Apply s2 to s1, then (if not
5842084Sbostic	 * found yet) advancing s1 by (65536-strlen(s2)) bytes and
5942084Sbostic	 * loop.
6042084Sbostic	 */
6142084Sbostic3:	matchc	r5,(r4),r2,(r3)	/* search */
6242084Sbostic	beql	1b		/* if found, go return it */
6342084Sbostic	decw	r2		/* from 0 to 65535 */
6442084Sbostic	incl	r3		/* already advanced 65535, now 65536 */
6542084Sbostic	subl2	r5,r3		/* ... minus strlen(s2) */
6642084Sbostic	brb	0b
6742084Sbostic
6842084Sbostic	/*
6942084Sbostic	 * s2 is too long (> 65535 bytes) to bother with matchc.
7042084Sbostic	 */
7142084Sbostic4:	locc	$0,r2,(r1)	/* continue working on strlen(s2) */
7242084Sbostic	beql	4b
7342084Sbostic	subl3	r1,r4,r5	/* r5 = strlen(s2) */
7442084Sbostic	movb	(r4)+,r2	/* r2 = *s2++ */
7542084Sbostic	decl	r5		/* fix up length */
7642084Sbostic5:	movb	(r3)+,r0	/* r0 = *s1++ */
7742084Sbostic	beql	2b		/* if '\0', return NULL */
7842084Sbostic	cmpb	r0,r2
7942084Sbostic	bneq	5b		/* loop until first char found */
8042084Sbostic	pushr	R5|R4|R3|R2	/* save c, s1, s2, n */
8142084Sbostic	pushr	R5|R4|R3	/* strncmp(s1, s2, n) */
8242084Sbostic	calls	$3,_strncmp
8342084Sbostic	popr	R2|R3|R4|R5	/* restore */
8442084Sbostic	tstl	r0
8542084Sbostic	bneq	5b		/* loop until strncmp says rest same too */
8642084Sbostic	subl3	$1,r3,r0	/* return previous s1 */
8742084Sbostic	ret
88