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
toasty4mo ago
Probably missing the null terminator
aurelian
aurelianOP4mo ago
do you mean String's init is not adding it?
toasty
toasty4mo 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
aurelianOP4mo ago
lol ok
toasty
toasty4mo ago
I don’t like the null terminator shenanigans for strings either lol
aurelian
aurelianOP4mo ago
I mean, regardless of that, it should be automated but yeah... yep, appending 0 works gonna dust off the PDP-11
toasty
toasty4mo ago
Yeah, automating it would be nice. I’ve shot myself in the foot plenty of times while missing the null terminator
aurelian
aurelianOP4mo 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
toasty4mo ago
Yeah! I try to use string slice where I can too
aurelian
aurelianOP4mo ago
a safe version would be really useful
Martin Vuyk
Martin Vuyk2mo 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
aurelianOP2mo ago
I meant a version that throws
Martin Vuyk
Martin Vuyk2mo 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
MM2mo 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
ModularBot2mo ago
Congrats @MM, you just advanced to level 4!
Martin Vuyk
Martin Vuyk2mo 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

Did you find this page helpful?