Add strings.ncompare (and backing strncmp_mem) to compare up to n characters of a string. (#164)

* Add strings.ncompare (and backing strncmp_mem) to compare up to n characters of a string.

* Document strings.ncompare.
This commit is contained in:
gillham
2025-03-07 16:53:56 -06:00
committed by GitHub
parent ef23d52ed7
commit 81c255c450
6 changed files with 56 additions and 1 deletions

View File

@@ -304,6 +304,34 @@ _c1_zero lda (P8ZP_SCRATCH_W2),y
+ rts
.pend
strncmp_mem .proc
; -- compares strings in s1 (AY) and s2 (P8ZP_SCRATCH_W2).
; Compares up to maximum length specified in X.
; Returns -1,0,1 in A, depending on the ordering. Clobbers X & Y.
sta P8ZP_SCRATCH_W1
sty P8ZP_SCRATCH_W1+1
ldy #0
_loop lda (P8ZP_SCRATCH_W1),y
beq _c1_zero
cmp (P8ZP_SCRATCH_W2),y
beq _equal
bmi _less
lda #1
rts
_less lda #-1
rts
_equal dex
bne +
lda #0
rts
+ iny
bne _loop
_c1_zero lda (P8ZP_SCRATCH_W2),y
beq +
lda #-1
+ rts
.pend
strlen .proc
; -- returns the number of bytes in the string in AY, in Y. Clobbers A.