7331 - hacky way to convert slice to string

This commit is contained in:
Kartik Agaram 2020-12-04 22:27:12 -08:00
parent 273c42c066
commit eea6659a40
3 changed files with 39 additions and 0 deletions

14
315slice.subx Normal file
View File

@ -0,0 +1,14 @@
== code
# variant of slice-to-string intended to be called from Mu
# Mu doesn't yet expose allocation-descriptors
_slice-to-string: # in: (addr slice), out: (addr handle array byte)
# . prologue
55/push-ebp
89/<- %ebp 4/r32/esp
#
(slice-to-string Heap *(ebp+8) *(ebp+0xc))
# . epilogue
89/<- %esp 5/r32/ebp
5d/pop-to-ebp
c3/return

5
400.mu
View File

@ -94,7 +94,9 @@ sig slice-equal? s: (addr slice), p: (addr array byte) -> _/eax: boolean
sig slice-starts-with? s: (addr slice), head: (addr array byte) -> _/eax: boolean
sig write-slice out: (addr stream byte), s: (addr slice)
sig write-slice-buffered out: (addr buffered-file), s: (addr slice)
# bad name alert
sig slice-to-string ad: (addr allocation-descriptor), in: (addr slice), out: (addr handle array byte)
sig _slice-to-string in: (addr slice), out: (addr handle array byte)
sig next-token in: (addr stream byte), delimiter: byte, out: (addr slice)
sig next-token-from-slice start: (addr byte), end: (addr byte), delimiter: byte, out: (addr slice)
sig skip-chars-matching in: (addr stream byte), delimiter: byte
@ -113,6 +115,9 @@ sig write-stream-data f: (addr buffered-file), s: (addr stream byte)
sig write-int32-decimal out: (addr stream byte), n: int
sig is-decimal-digit? c: grapheme -> _/eax: boolean
sig to-decimal-digit in: grapheme -> _/eax: int
# bad name alert
# next-word really tokenizes
# next-raw-word really reads whitespace-separated words
sig next-word line: (addr stream byte), out: (addr slice) # skips '#' comments
sig next-raw-word line: (addr stream byte), out: (addr slice) # does not skip '#' comments
sig has-metadata? word: (addr slice), s: (addr string) -> _/eax: boolean

View File

@ -157,6 +157,26 @@ fn main -> _/ebx: int {
var pid?/eax: boolean <- slice-equal? key-slice, "pid"
compare pid?, 0 # false
break-if-=
# convert val
var s: (handle array byte)
var s2: (addr handle array byte) <- address s
_slice-to-string val-slice, s2
# check length
var len/eax: int <- length s2
compare len, 9
{
break-if-=
curr-passport-field-count <- copy 8
}
# check valid decimal int
# parse-decimal-int-from-slice currently returns 0 on invalid parse,
# which isn't ideal but suffices for our purposes
var val/eax: int <- parse-decimal-int-from-slice val-slice
compare val, 0
{
break-if->
curr-passport-field-count <- copy 8
}
}
loop
}