-
Notifications
You must be signed in to change notification settings - Fork 57
Netstrings
A netstring is a simple, human-readable protocol for sending sequences of strings. The format is just the length of the string, followed by the string itself:
<byte length>:<string>,
["Hello" "World"]
would encode to:
5:Hello,5:World,
Note that the length specified does not include the comma. This somewhat complicates the frame, but the definition is still straightforward:
(finite-frame
(prefix (string-integer :ascii :delimiters [":"])
inc
dec)
(string :utf-8 :suffix ","))
The prefix describes the byte-length, so we use (finite-frame ...)
to describe the entire frame. The prefix is a string-integer
, with the slight twist that the number is off-by-one from the actual byte-length that follows. To decode the length we must increment the value, and to get the value to encode we must decrement the length. The inner frame is just a string, terminated by a comma.