]> git.sur5r.net Git - cc65/blob - libsrc/common/strnicmp.s
un-remove TABs in doc/using-make.sgml
[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, popptr1, __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     popptr1         ; get s1
36
37 ; Loop setup
38
39         ; ldy     #0            Y=0 guaranteed by popptr1
40
41 ; Start of compare loop. Check the counter.
42
43 Loop:   inc     ptr3
44         beq     IncHi           ; Increment high byte
45
46 ; Compare a byte from the strings
47
48 Comp:   lda     (ptr2),y
49         tax
50         lda     __ctype,x       ; get character classification
51         and     #CT_LOWER       ; lower case char?
52         beq     L1              ; jump if no
53         txa                     ; get character back
54         sec
55         sbc     #<('a'-'A')     ; make upper case char
56         tax                     ;
57 L1:     stx     tmp1            ; remember upper case equivalent
58
59         lda     (ptr1),y        ; get character from first string
60         tax
61         lda     __ctype,x       ; get character classification
62         and     #CT_LOWER       ; lower case char?
63         beq     L2              ; jump if no
64         txa                     ; get character back
65         sec
66         sbc     #<('a'-'A')     ; make upper case char
67         tax
68
69 L2:     cpx     tmp1            ; compare characters
70         bne     NotEqual        ; Jump if strings different
71         txa                     ; End of strings?
72         beq     Equal1          ; Jump if EOS reached, a/x == 0
73
74 ; Increment the pointers
75
76         iny
77         bne     Loop
78         inc     ptr1+1
79         inc     ptr2+1
80         bne     Loop            ; Branch always
81
82 ; Increment hi byte
83
84 IncHi:  inc     ptr3+1
85         bne     Comp            ; Jump if counter not zero
86
87 ; Exit code if strings are equal. a/x not set
88
89 Equal:  lda     #$00
90         tax
91 Equal1: rts
92
93 ; Exit code if strings not equal
94
95 NotEqual:
96         bcs     L3
97         ldx     #$FF            ; Make result negative
98         rts
99
100 L3:     ldx     #$01            ; Make result positive
101         rts