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
16 Replies
toasty
toasty3mo ago
Probably missing the null terminator
aurelian
aurelianOP3mo ago
do you mean String's init is not adding it?
toasty
toasty3mo 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
aurelianOP3mo ago
lol ok
toasty
toasty3mo ago
I don’t like the null terminator shenanigans for strings either lol
aurelian
aurelianOP3mo ago
I mean, regardless of that, it should be automated but yeah... yep, appending 0 works gonna dust off the PDP-11
toasty
toasty3mo ago
Yeah, automating it would be nice. I’ve shot myself in the foot plenty of times while missing the null terminator
aurelian
aurelianOP3mo 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
toasty3mo ago
Yeah! I try to use string slice where I can too
aurelian
aurelianOP3mo ago
a safe version would be really useful
Martin Vuyk
Martin Vuyk5d ago
It's only unsafe in the sense that you don't know if the data is valid UTF-8 (and StringSlice is supposed to always be valid UTF-8. The constructor should have a debug_assert(_is_valid_utf8(span)) but there are issues with the function at compile time so it's not active. So there is no way to "safely" get data from the wire were you're sure it's valid UTF-8 from a generic API perspective
aurelian
aurelianOP5d ago
I meant a version that throws
Martin Vuyk
Martin Vuyk5d ago
Oh yeah that's totally in the roadmap. Once we have a Bytes type defined we'll have Bytes.decode() (or something of the like). At least it's my hope to be able to implement it and it getting merged 😄
MM
MM4d ago
Talking about the roadmap for StringSlice. Will the StringSlice eventually support slicing, i.e. return a range of characters by using the slice syntax like a[2:5]?
ModularBot
ModularBot4d ago
Congrats @MM, you just advanced to level 4!
Martin Vuyk
Martin Vuyk4d ago
100%, I'm leaving that off to the future because I'm waiting until I can make the switch to full unicode to not break too much code. The current String type is sliced by bytes, but Python's is by unicode codepoints. The current len(String) also returns byte_length() instead of unicode codepoint length, Python's is unicode codepoints. len(StringSlice) works by unicode codepoints, so adding slicing will confuse many people. I'm hoping to land this before the next stable release in 2025 but we'll see
Want results from more Discord servers?
Add your server