Current section
Files
Jump to
Current section
Files
lib/barytherium/frame/value_encoding.ex
defmodule Barytherium.Frame.ValueEncoding do
@moduledoc """
ValueEncoding functions for STOMP. This is handled automatically by functions in Barytherium.Frame
Value Encoding is the limited set of escape characters used in headers to avoid control characters
In short:
* Colon (`:`) → `\c`
* Backslash (`\`) → `\\`
* Carriage Return (`\r`) → `\r`
* Line feed (`\n`) → `\n`
Any other characters aren't valid and should be treated as an error (but aren't here, yet)
https://stomp.github.io/stomp-specification-1.2.html#Value_Encoding
"""
def decode(text) do
decode(text, [])
end
defp decode(<<"\\c", rest::binary>>, accumulated) do
decode(rest, [":" | accumulated])
end
defp decode(<<"\\n", rest::binary>>, accumulated) do
decode(rest, [?\n | accumulated])
end
defp decode(<<"\\r", rest::binary>>, accumulated) do
decode(rest, [?\r | accumulated])
end
defp decode(<<"\\\\", rest::binary>>, accumulated) do
decode(rest, [?\\ | accumulated])
end
defp decode(<<ch, rest::binary>>, accumulated) do
decode(rest, [ch | accumulated])
end
defp decode(<<>>, accumulated) do
:binary.list_to_bin(Enum.reverse(accumulated))
end
def encode(text) do
encode(text, [])
end
defp encode(<<":", rest::binary>>, accumulated) do
encode(rest, ["\\c" | accumulated])
end
defp encode(<<"\n", rest::binary>>, accumulated) do
encode(rest, ["\\n" | accumulated])
end
defp encode(<<"\r", rest::binary>>, accumulated) do
encode(rest, ["\\r" | accumulated])
end
defp encode(<<"\\", rest::binary>>, accumulated) do
encode(rest, ["\\\\" | accumulated])
end
defp encode(<<ch, rest::binary>>, accumulated) do
encode(rest, [ch | accumulated])
end
defp encode(<<>>, accumulated) do
:binary.list_to_bin(:lists.flatten(Enum.reverse(accumulated)))
end
end