Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed code point decoding #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
.PHONY: all build clean test

build:
jbuilder build --dev @install
dune build @install

all: build

test:
jbuilder runtest --dev
dune runtest

install:
jbuilder install
dune install

uninstall:
jbuilder uninstall
dune uninstall

clean:
rm -rf _build *.install
2 changes: 2 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(lang dune 1.0)
(name jsonaf)
8 changes: 4 additions & 4 deletions jsonaf.opam
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ homepage: "https://github.com/inhabitedtype/jsonaf"
bug-reports: "https://github.com/inhabitedtype/jsonaf/issues"
dev-repo: "https://github.com/inhabitedtype/jsonaf.git"
build: [
["jbuilder" "subst"] {pinned}
["jbuilder" "build" "-p" name "-j" jobs]
["dune" "subst"] {pinned}
["dune" "build" "-p" name "-j" jobs]
]
build-test: [
["jbuilder" "runtest" "-p" name]
["dune" "runtest" "-p" name "-j" jobs]
]
depends: [
"jbuilder" {build & >= "1.0+beta10"}
"dune" {build & >= "1.0"}
"alcotest" {test}
"angstrom" {>= "0.7.0"}
"faraday" {>= "0.5.0"}
Expand Down
5 changes: 5 additions & 0 deletions lib/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(library
(name jsonaf)
(public_name jsonaf)
(libraries angstrom faraday result)
(flags :standard -safe-string))
8 changes: 0 additions & 8 deletions lib/jbuild

This file was deleted.

46 changes: 43 additions & 3 deletions lib/json_string.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,48 @@ module Parse = struct
| 'A' .. 'F' -> Char.code c - 55
| _ -> 255

(* Copied from Yojson *)
let utf8_of_code buf x =

(* Straight <= doesn't work with signed 31-bit ints *)
let maxbits n x = x lsr n = 0 in

if maxbits 7 x then
(* 7 *)
Buffer.add_char buf (Char.unsafe_chr x)
else if maxbits 11 x then begin
(* 5 + 6 *)
Buffer.add_char buf (Char.unsafe_chr (0b11000000 lor ((x lsr 6) land 0b00011111)));
Buffer.add_char buf (Char.unsafe_chr (0b10000000 lor (x land 0b00111111)))
end else if maxbits 16 x then begin
(* 4 + 6 + 6 *)
Buffer.add_char buf (Char.unsafe_chr (0b11100000 lor ((x lsr 12) land 0b00001111)));
Buffer.add_char buf (Char.unsafe_chr (0b10000000 lor ((x lsr 6) land 0b00111111)));
Buffer.add_char buf (Char.unsafe_chr (0b10000000 lor (x land 0b00111111)))
end else if maxbits 21 x then begin
(* 3 + 6 + 6 + 6 *)
Buffer.add_char buf (Char.unsafe_chr (0b11110000 lor ((x lsr 18) land 0b00000111)));
Buffer.add_char buf (Char.unsafe_chr (0b10000000 lor ((x lsr 12) land 0b00111111)));
Buffer.add_char buf (Char.unsafe_chr (0b10000000 lor ((x lsr 6) land 0b00111111)));
Buffer.add_char buf (Char.unsafe_chr (0b10000000 lor (x land 0b00111111)));
end else if maxbits 26 x then begin
(* 2 + 6 + 6 + 6 + 6 *)
Buffer.add_char buf (Char.unsafe_chr (0b11111000 lor ((x lsr 24) land 0b00000011)));
Buffer.add_char buf (Char.unsafe_chr (0b10000000 lor ((x lsr 18) land 0b00111111)));
Buffer.add_char buf (Char.unsafe_chr (0b10000000 lor ((x lsr 12) land 0b00111111)));
Buffer.add_char buf (Char.unsafe_chr (0b10000000 lor ((x lsr 6) land 0b00111111)));
Buffer.add_char buf (Char.unsafe_chr (0b10000000 lor (x land 0b00111111)));
end else begin
assert (maxbits 31 x);
(* 1 + 6 + 6 + 6 + 6 + 6 *)
Buffer.add_char buf (Char.unsafe_chr (0b11111100 lor ((x lsr 30) land 0b00000001)));
Buffer.add_char buf (Char.unsafe_chr (0b10000000 lor ((x lsr 24) land 0b00111111)));
Buffer.add_char buf (Char.unsafe_chr (0b10000000 lor ((x lsr 18) land 0b00111111)));
Buffer.add_char buf (Char.unsafe_chr (0b10000000 lor ((x lsr 12) land 0b00111111)));
Buffer.add_char buf (Char.unsafe_chr (0b10000000 lor ((x lsr 6) land 0b00111111)));
Buffer.add_char buf (Char.unsafe_chr (0b10000000 lor (x land 0b00111111)));
end

let utf_8 buf d = function
| [c;b;a] ->
let a = hex a and b = hex b and c = hex c and d = hex d in
Expand All @@ -88,9 +130,7 @@ module Parse = struct
if cp >= 0xd800 && cp <= 0xdbff then
`UTF16(cp, `S)
else begin
Buffer.add_char buf (Char.unsafe_chr (0b11100000 lor ((cp lsr 12) land 0b00001111)));
Buffer.add_char buf (Char.unsafe_chr (0b10000000 lor ((cp lsr 6) land 0b00111111)));
Buffer.add_char buf (Char.unsafe_chr (0b10000000 lor (cp land 0b00111111)));
utf8_of_code buf cp ;
`Unescaped
end
| cs -> `UTF8 (d::cs)
Expand Down
4 changes: 2 additions & 2 deletions lib/jsonaf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ module With_number = struct
let parse = Parser.parse

let of_string number_of_string string =
Angstrom.parse_string (parse number_of_string) string
Angstrom.parse_string ~consume:Angstrom.Consume.All (parse number_of_string) string

let of_bigstring number_of_string bigstring =
Angstrom.parse_bigstring (parse number_of_string) bigstring
Angstrom.parse_bigstring ~consume:Angstrom.Consume.All (parse number_of_string) bigstring

let rec serialize serialize_number t faraday =
match t with
Expand Down