|
|
1.1 root 1: /*-
2: * Copyright (c) 1990 The Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that: (1) source distributions retain this entire copyright
7: * notice and comment, and (2) distributions including binaries display
8: * the following acknowledgement: ``This product includes software
9: * developed by the University of California, Berkeley and its contributors''
10: * in the documentation or other materials provided with the distribution
11: * and in all advertising materials mentioning features or use of this
12: * software. Neither the name of the University nor the names of its
13: * contributors may be used to endorse or promote products derived
14: * from this software without specific prior written permission.
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18: */
19:
20: #if defined(LIBC_SCCS) && !defined(lint)
21: .asciz "@(#)strstr.s 5.1 (Berkeley) 5/15/90"
22: #endif /* LIBC_SCCS and not lint */
23:
24: /*
25: * Find the first occurrence of s2 as a substring in s1.
26: * If s2 is empty, return s1.
27: *
28: * char *strstr(s1, s2)
29: * const char *s1, *s2;
30: */
31: #include "DEFS.h"
32:
33: ENTRY(strstr, 0)
34: movq 4(ap),r3 /* r3 = s1, r4 = s2 */
35: movzwl $65535,r2 /* r2 = locc/matchc limit */
36: locc $0,r2,(r4) /* find '\0' in s2 */
37: beql 4f
38: subl3 r1,r4,r5 /* r5 = strlen(s2) */
39: beql 1f /* if r5 == 0, return s1 */
40:
41: /*
42: * s2 is short enough to apply matchc.
43: * If s1 is long, we have to do it in stages.
44: */
45: 0: locc $0,r2,(r3) /* find '\0' in s1 */
46: beql 3f
47:
48: /*
49: * Both strings are `short'; we can use matchc directly.
50: */
51: subl3 r1,r3,r1 /* r1 = strlen(s1) */
52: matchc r5,(r4),r1,(r3) /* find substring */
53: bneq 2f
54:
55: /*
56: * r3 points r5 bytes past match. Return the match.
57: */
58: 1: subl3 r5,r3,r0 /* return (byte_past_match - strlen(s2)) */
59: ret
60:
61: /*
62: * There is no matching substring.
63: */
64: 2: clrl r0 /* return NULL */
65: ret
66:
67: /*
68: * s1 is too long (> 65535 bytes) to apply matchc directly,
69: * but s2 is short enough. Apply s2 to s1, then (if not
70: * found yet) advancing s1 by (65536-strlen(s2)) bytes and
71: * loop.
72: */
73: 3: matchc r5,(r4),r2,(r3) /* search */
74: beql 1b /* if found, go return it */
75: decw r2 /* from 0 to 65535 */
76: incl r3 /* already advanced 65535, now 65536 */
77: subl2 r5,r3 /* ... minus strlen(s2) */
78: brb 0b
79:
80: /*
81: * s2 is too long (> 65535 bytes) to bother with matchc.
82: */
83: 4: locc $0,r2,(r1) /* continue working on strlen(s2) */
84: beql 4b
85: subl3 r1,r4,r5 /* r5 = strlen(s2) */
86: movb (r4)+,r2 /* r2 = *s2++ */
87: decl r5 /* fix up length */
88: 5: movb (r3)+,r0 /* r0 = *s1++ */
89: beql 2b /* if '\0', return NULL */
90: cmpb r0,r2
91: bneq 5b /* loop until first char found */
92: pushr R5|R4|R3|R2 /* save c, s1, s2, n */
93: pushr R5|R4|R3 /* strncmp(s1, s2, n) */
94: calls $3,_strncmp
95: popr R2|R3|R4|R5 /* restore */
96: tstl r0
97: bneq 5b /* loop until strncmp says rest same too */
98: subl3 $1,r3,r0 /* return previous s1 */
99: ret
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.