Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions stdlib/socket.rail
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,16 @@ accept_tcp listen_fd =
cfd

-- Copy `n` bytes from buf[off..off+n) into a Rail string.
-- Uses char_from_int + join; O(N²) alloc, fine for small reads.
-- Builds a list of single-char strings then joins once: O(N) allocation
-- instead of O(N²) from repeated prefix-copying concat.
tcp_bytes_to_str buf off n acc =
if n <= 0 then acc
join "" (tcp_bytes_to_list buf off n)

tcp_bytes_to_list buf off n =
if n <= 0 then []
else
let c = byte_at buf off
tcp_bytes_to_str buf (off + 1) (n - 1) (join "" [acc, char_from_int c])
(char_from_int c) : (tcp_bytes_to_list buf (off + 1) (n - 1))

-- Receive bytes until the HTTP header terminator \r\n\r\n appears, then
-- continue reading Content-Length bytes of body if present. Returns the
Expand Down
Loading