String matching: Difference between revisions

(add lambdatalk)
Line 3,025:
"The brown dog jumped jumped and jumped" does end with "jumped"
</pre>
 
=={{header|MIPS Assembly}}==
The function below returns the zero-based index where the string pointed to by <code>$a1</code> occurs in <code>$a0</code>.
* If it returns strlen(<code>$a0</code>), then <code>$a1</code> was not found.
* If it returns 0, then <code>$a0</code> begins with <code>$a1</code>.
* If it returns strlen(<code>$a0</code>)-strlen(<code>$a1</code>), then <code>$a0</code> ends with <code>$a1</code>.
* Otherwise, <code>$a0</code> contains <code>$a1</code> starting at the specified location.
<lang mips>InString:
;input: $a0 = ptr to string 1
; $a1 = ptr to string 2
; assumes len($a1) <= len($a0)
;out: $v0 = zero-based index where the second string is placed in the first.
;clobbers: $t0,$t1
subiu sp,sp,4 ;set up a stack frame of 4 bytes.
sw $a1,(sp)
li $v0,0
InString_again:
lbu $t0,($a0)
nop
beqz $t0,InString_terminated
nop
lbu $t1,($a1)
nop
beqz $t1,InString_terminated
nop
bne $t0,$t1,InString_noMatch
nop
b InString_overhead
addiu $a1,1
InString_noMatch:
lw $a1,(sp) ;reset the substring pointer if the letters don't match
addiu $v0,1 ;load delay slot
InString_overhead:
addiu $a0,1
b InString_Again
nop
InString_terminated:
addiu sp,sp,4
jr ra
nop</lang>
 
{{out}}
<lang mips>main:
la $a0,MyString
la $a1,Test4
jal InString
nop
jal Monitor
nop
shutdown:
nop ;Project 64 will detect an infinite loop and close the ROM if I don't have this nop here.
b shutdown
nop
 
 
MyString: ;this was loaded into $a0
.ascii "abcdefghijklmnopqrstuvwxyz"
.byte 0
.align 4
;each of these was loaded into $a1 individually for testing
Test1:
.ascii "abc" ;InString returned 0
.byte 0
.align 4
Test2:
.ascii "xyz" ;InString returned 0x17 (decimal 23)
.byte 0
.align 4
Test3:
.ascii "def" ;InString returned 3
.byte 0
.align 4
Test4:
.ascii "z",0 ;InString returned 0x19 (decimal 25)
.byte 0
.align 4
Test5:
.ascii "1",0; InString returned 0x1A (decimal 26)
.byte 0
.align 4</lang>
 
=={{header|NetRexx}}==
1,489

edits