Created
June 8, 2017 16:09
-
-
Save e-ivaldi/0f8a112fa9061daa1062e4b72f706205 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rec encode (o:obj) = | |
let x = ofObject o in | |
let b = toBytes x in | |
let length = List.length b in | |
let lenghtInByte = List.head (toBytes (Int length)) in | |
let lenghtOfLenghtBinaryFormInByte = List.head (toBytes(Int(String.length (intToBinary length)))) in | |
match x with | |
| String s -> | |
match length with | |
| 0 -> [0x80uy] | |
| 1 when List.head b <= 0x7fuy -> [List.head b] | |
| n when n < 56 -> (0x80uy + lenghtInByte) :: b | |
| x -> [0xb7uy + lenghtOfLenghtBinaryFormInByte] @ toBytes (Int length) @ b | |
| Items i -> | |
match i with | |
| [] -> [] | |
| x -> | |
let serializedList = x |> List.map encode |> List.concat in | |
match length with | |
| n when n < 56 -> [ 0xc0uy + lenghtInByte ] @ serializedList | |
| _ -> [0x7fuy + lenghtOfLenghtBinaryFormInByte] @ toBytes (Int length) @ serializedList |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment