]> git.sur5r.net Git - cc65/blob - libsrc/common/strstr.s
This commit was generated by cvs2svn to compensate for changes in r2,
[cc65] / libsrc / common / strstr.s
1 ;
2 ; Ullrich von Bassewitz, 11.12.1998
3 ;
4 ; char* strstr (const char* haystack, const char* needle);
5 ;
6
7         .export         _strstr
8         .import         popax
9         .importzp       ptr1, ptr2, ptr3, ptr4, tmp1
10
11 _strstr:
12         sta     ptr2            ; Save needle
13         stx     ptr2+1
14         sta     ptr4            ; Setup temp copy for later
15
16         jsr     popax           ; Get haystack
17         sta     ptr1
18         stx     ptr1+1          ; Save haystack
19
20 ; If needle is empty, return haystack
21
22         ldy     #$00
23         lda     (ptr2),y        ; Get first byte of needle
24         beq     @Found          ; Needle is empty --> we're done
25
26 ; Search for the beginning of the string (this is not an optimal search
27 ; strategy [in fact, it's pretty dumb], but it's simple to implement).
28
29         sta     tmp1            ; Save start of needle
30 @L1:    lda     (ptr1),y        ; Get next char from haystack
31         beq     @NotFound       ; Jump if end
32         cmp     tmp1            ; Start of needle found?
33         beq     @L2             ; Jump if so
34         iny                     ; Next char
35         bne     @L1
36         inc     ptr1+1          ; Bump high byte
37         bne     @L1             ; Branch always
38
39 ; We found the start of needle in haystack
40
41 @L2:    tya                     ; Get offset
42         clc
43         adc     ptr1
44         sta     ptr1            ; Make ptr1 point to start
45         bcc     @L3
46         inc     ptr1+1
47
48 ; ptr1 points to the start of needle now. Setup temporary pointers for the
49 ; search. The low byte of ptr4 is already set.
50
51 @L3:    sta     ptr3
52         lda     ptr1+1
53         sta     ptr3+1
54         lda     ptr2+1
55         sta     ptr4+1
56         ldy     #1              ; First char is identical, so start on second
57
58 ; Do the compare
59
60 @L4:    lda     (ptr4),y        ; Get char from needle
61         beq     @Found          ; Jump if end of needle (-> found)
62         cmp     (ptr3),y        ; Compare with haystack
63         bne     @L5             ; Jump if not equal
64         iny                     ; Next char
65         bne     @L4
66         inc     ptr3+1
67         inc     ptr4+1          ; Bump hi byte of pointers
68         bne     @L4             ; Next char (branch always)
69
70 ; The strings did not compare equal, search next start of needle
71
72 @L5:    ldy     #1              ; Start after this char
73         bne     @L1             ; Branch always
74
75 ; We found the start of needle
76
77 @Found: lda     ptr1
78         ldx     ptr1+1
79         rts
80
81 ; We reached end of haystack without finding needle
82
83 @NotFound:
84         lda     #$00            ; return NULL
85         tax
86         rts
87
88
89
90
91
92
93
94
95
96
97