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