]> git.sur5r.net Git - cc65/blob - libsrc/common/strnicmp.s
c3b89e4d87fed9e1940c231c0e8789d2a8345beb
[cc65] / libsrc / common / strnicmp.s
1 ;
2 ; Christian Groessler, 10.02.2009
3 ; derived from strncmp.s and stricmp.s
4 ;
5 ; int __fastcall__ strnicmp (const char* s1, const char* s2, size_t count);
6 ; int __fastcall__ strncasecmp (const char* s1, const char* s2, size_t count);
7 ;
8
9         .export         _strnicmp, _strncasecmp
10         .import         popax, __ctype
11         .importzp       ptr1, ptr2, ptr3, tmp1
12
13
14 _strnicmp:
15 _strncasecmp:
16
17 ; Convert the given counter value in a/x from a downward counter into an
18 ; upward counter, so we can increment the counter in the loop below instead
19 ; of decrementing it. This adds some overhead now, but is cheaper than
20 ; executing a more complex test in each iteration of the loop. We do also
21 ; correct the value by one, so we can do the test on top of the loop.
22
23         eor     #$FF
24         sta     ptr3
25         txa
26         eor     #$FF
27         sta     ptr3+1
28
29 ; Get the remaining arguments
30
31         jsr     popax           ; get s2
32         sta     ptr2
33         stx     ptr2+1
34         jsr     popax           ; get s1
35         sta     ptr1
36         stx     ptr1+1
37
38 ; Loop setup
39
40         ldy     #0
41
42 ; Start of compare loop. Check the counter.
43
44 Loop:   inc     ptr3
45         beq     IncHi           ; Increment high byte
46
47 ; Compare a byte from the strings
48
49 Comp:   lda     (ptr2),y
50         tax
51         lda     __ctype,x       ; get character classification
52         and     #$01            ; lower case char?
53         beq     L1              ; jump if no
54         txa                     ; get character back
55         sec
56         sbc     #<('a'-'A')     ; make upper case char
57         tax                     ;
58 L1:     stx     tmp1            ; remember upper case equivalent
59
60         lda     (ptr1),y        ; get character from first string
61         tax
62         lda     __ctype,x       ; get character classification
63         and     #$01            ; lower case char?
64         beq     L2              ; jump if no
65         txa                     ; get character back
66         sec
67         sbc     #<('a'-'A')     ; make upper case char
68         tax
69
70 L2:     cpx     tmp1            ; compare characters
71         bne     NotEqual        ; Jump if strings different
72         txa                     ; End of strings?
73         beq     Equal1          ; Jump if EOS reached, a/x == 0
74
75 ; Increment the pointers
76
77         iny
78         bne     Loop
79         inc     ptr1+1
80         inc     ptr2+1
81         bne     Loop            ; Branch always
82
83 ; Increment hi byte
84
85 IncHi:  inc     ptr3+1
86         bne     Comp            ; Jump if counter not zero
87
88 ; Exit code if strings are equal. a/x not set
89
90 Equal:  lda     #$00
91         tax
92 Equal1: rts
93
94 ; Exit code if strings not equal
95
96 NotEqual:
97         bcs     L3
98         ldx     #$FF            ; Make result negative
99         rts
100
101 L3:     ldx     #$01            ; Make result positive
102         rts