|
|
1.1 root 1: /*
2: * Copyright (c) 1983 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 "@(#)rindex.s 5.6 (Berkeley) 6/1/90"
22: #endif /* LIBC_SCCS and not lint */
23:
24: /*
25: * Find the last occurence of c in the string cp.
26: * Return pointer to match or null pointer.
27: *
28: * char *
29: * rindex(cp, c)
30: * char *cp, c;
31: */
32: #include "DEFS.h"
33:
34: ENTRY(rindex, 0)
35: movq 4(ap),r1 # r1 = cp; r2 = c
36: tstl r2 # check for special case c == '\0'
37: bneq 2f
38: 1:
39: locc $0,$65535,(r1) # just find end of string
40: beql 1b # still looking
41: movl r1,r0 # found it
42: ret
43: 2:
44: moval tbl,r3 # r3 = address of table
45: bbss $0,(r3),5f # insure not reentering
46: movab (r3)[r2],r5 # table entry for c
47: incb (r5)
48: clrl r4 # last found
49: 3:
50: scanc $65535,(r1),(r3),$1 # look for c or '\0'
51: beql 3b # keep looking
52: tstb (r1) # if have found '\0'
53: beql 4f # we are done
54: movl r1,r4 # save most recently found
55: incl r1 # skip over character
56: jbr 3b # keep looking
57: 4:
58: movl r4,r0 # return last found (if any)
59: clrb (r5) # clean up table
60: clrb (r3)
61: ret
62:
63: .data
64: tbl: .space 256
65: .text
66:
67: /*
68: * Reentrant, but slower version of rindex
69: */
70: 5:
71: movl r1,r3
72: clrl r4 # r4 = pointer to last match
73: 6:
74: locc $0,$65535,(r3) # look for '\0'
75: bneq 8f
76: decw r0 # r0 = 65535
77: 1:
78: locc r2,r0,(r3) # look for c
79: bneq 7f
80: movl r1,r3 # reset pointer and ...
81: jbr 6b # ... try again
82: 7:
83: movl r1,r4 # stash pointer ...
84: addl3 $1,r1,r3 # ... skip over match and ...
85: decl r0 # ... decrement count
86: jbr 6b # ... try again
87: 8:
88: subl3 r3,r1,r0 # length of short block
89: incl r0 # +1 for '\0'
90: 9:
91: locc r2,r0,(r3) # look for c
92: beql 0f
93: movl r1,r4 # stash pointer ...
94: addl3 $1,r1,r3 # ... skip over match ...
95: decl r0 # ... adjust count and ...
96: jbr 9b # ... try again
97: 0:
98: movl r4,r0 # return stashed pointer
99: ret
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.