Not sure why these strings are missing the last char

buf = InlineArray[UInt8, 32](0)
while True:
client = server.accept()
read = client.read_into(buf)
if read == 0:
print("missing first argument")
continue

var delim: Int
for i in range(read):
if buf[i] == 32:
delim = i
break
else:
print("missing second argument")
continue

span1 = Span(buf)[:delim]
for v in span1:
print(chr(int(v[])), sep="", end="")
print()

span2 = Span(buf)[delim + 1 : read - 1]
for v in span2:
print(chr(int(v[])), sep="", end="")
print()

arg1 = String(Span(buf)[:delim])
arg2 = String(Span(buf)[delim + 1 : read - 1])
print("arg1:", arg1)
print("arg2:", arg2)
buf = InlineArray[UInt8, 32](0)
while True:
client = server.accept()
read = client.read_into(buf)
if read == 0:
print("missing first argument")
continue

var delim: Int
for i in range(read):
if buf[i] == 32:
delim = i
break
else:
print("missing second argument")
continue

span1 = Span(buf)[:delim]
for v in span1:
print(chr(int(v[])), sep="", end="")
print()

span2 = Span(buf)[delim + 1 : read - 1]
for v in span2:
print(chr(int(v[])), sep="", end="")
print()

arg1 = String(Span(buf)[:delim])
arg2 = String(Span(buf)[delim + 1 : read - 1])
print("arg1:", arg1)
print("arg2:", arg2)
move
south
arg1: mov
arg2: sout
move
south
arg1: mov
arg2: sout
10 Replies
toasty
toasty4d ago
Probably missing the null terminator
aurelian
aurelian4d ago
do you mean String's init is not adding it?
toasty
toasty4d ago
My guess is that the span is being converted to a list, and that’s being used for the string construction. If that Span’s last element is not a null terminator, then you’d see the clipping that you are now Try constructing a list from the span, and append a 0 to it. Then create the string from that list
aurelian
aurelian4d ago
lol ok
toasty
toasty4d ago
I don’t like the null terminator shenanigans for strings either lol
aurelian
aurelian4d ago
I mean, regardless of that, it should be automated but yeah... yep, appending 0 works gonna dust off the PDP-11
toasty
toasty4d ago
Yeah, automating it would be nice. I’ve shot myself in the foot plenty of times while missing the null terminator
aurelian
aurelian4d ago
so making a string slice doesn't need the append
arg1 = StringSlice(unsafe_from_utf8=span1)
arg2 = StringSlice(unsafe_from_utf8=span2)
arg1 = StringSlice(unsafe_from_utf8=span1)
arg2 = StringSlice(unsafe_from_utf8=span2)
move
south
arg1: move
arg2: south
move
south
arg1: move
arg2: south
@toasty good catch, thanks!
toasty
toasty4d ago
Yeah! I try to use string slice where I can too
aurelian
aurelian4d ago
a safe version would be really useful
Want results from more Discord servers?
Add your server