]> git.sur5r.net Git - cc65/blob - libsrc/common/strpbrk.s
afe90ecabdca753e25b26ba77a3a921452814071
[cc65] / libsrc / common / strpbrk.s
1 ;                                                   
2 ; Ullrich von Bassewitz, 11.06.1998
3 ;
4 ; char* strpbrk (const char* s1, const char* s2);
5 ;
6
7         .export         _strpbrk
8         .import         popax, popptr1, return0
9         .importzp       ptr1, ptr2, tmp1, tmp2, tmp3
10
11 _strpbrk:
12         jsr     popax           ; get s2
13         sta     ptr2
14         stx     ptr2+1
15         jsr     popptr1         ; get s1
16         ; ldy     #$00           Y=0 guaranteed by popptr1
17
18 L1:     lda     (ptr1),y        ; get next char from s1
19         beq     L9              ; jump if done
20         sta     tmp2            ; save char
21         iny
22         bne     L2
23         inc     ptr1+1
24 L2:     sty     tmp3            ; save index into s1
25
26         ldy     #0              ; get index into s2
27 L3:     lda     (ptr2),y        ;
28         beq     L4              ; jump if done
29         cmp     tmp2
30         beq     L6
31         iny
32         bne     L3
33
34 ; The character was not found in s2. Increment the counter and start over
35
36 L4:     ldy     tmp3            ; reload index
37         inx
38         bne     L1
39         inc     tmp1
40         bne     L1
41
42 ; A character was found. Calculate a pointer to this char in s1 and return it.
43
44 L6:     ldx     ptr1+1
45         lda     tmp3            ; get y offset
46         clc
47         adc     ptr1
48         bcc     L7
49         inx
50 L7:     rts
51
52 ; None of the characters in s2 was found - return NULL
53
54 L9:     jmp     return0
55
56
57
58