Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mroch committed Feb 28, 2018
0 parents commit 99da01b
Show file tree
Hide file tree
Showing 12 changed files with 225 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
_build
*.install
.merlin
3 changes: 3 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Code of Conduct

Facebook has adopted a Code of Conduct that we expect project participants to adhere to. Please read the [full text](https://code.facebook.com/pages/876921332402685/open-source-code-of-conduct) so that you can understand what actions will and will not be tolerated.
35 changes: 35 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Contributing to ocaml-vlq

We want to make contributing to this project as easy and transparent as
possible.

## Pull Requests

We actively welcome your pull requests.

1. Fork the repo and create your branch from `master`.
2. If you've added code that should be tested, add tests.
3. If you've changed APIs, update the documentation.
4. Ensure the test suite passes.
5. Make sure your code lints.
6. If you haven't already, complete the Contributor License Agreement ("CLA").

## Contributor License Agreement ("CLA")

In order to accept your pull request, we need you to submit a CLA. You only need
to do this once to work on any of Facebook's open source projects.

Complete your CLA here: <https://code.facebook.com/cla>

## Issues

We use GitHub issues to track public bugs. Please ensure your description is
clear and has sufficient instructions to be able to reproduce the issue.

Facebook has a [bounty program](https://www.facebook.com/whitehat/) for the safe
disclosure of security bugs. In those cases, please go through the process
outlined on that page and do not file a public issue.

## License
By contributing to ocaml-vlq, you agree that your contributions will be licensed
under the LICENSE file in the root directory of this source tree.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018-present, Facebook, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# ocaml-vlq

A simple library for encoding [variable-length quantities](https://en.wikipedia.org/wiki/Variable-length_quantity).

It currently supports writing a base64-encoded integer to a `Buffer`. Patches implementing decoding or other forms of encoding are welcome!


## Example

```ocaml
let buf = Buffer.create 10 in
Vlq.Base64.encode buf 123456789;
Buffer.contents buf (* "qxmvrH" *)
```

This encoding can be used to generate JavaScript sourcemaps.


## License

ocaml-vlq is MIT licensed, as found in the LICENSE file.
6 changes: 6 additions & 0 deletions src/jbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(jbuild_version 1)

(library (
(name vlq)
(public_name vlq)
))
60 changes: 60 additions & 0 deletions src/vlq.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
(**
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)

(* VLQ (variable-length quantity) encoder
https://en.wikipedia.org/wiki/Variable-length_quantity *)

module type Config = sig
val shift: int
val char_of_digit: int -> char
end

module type S = sig
val encode: Buffer.t -> int -> unit
end

module Make (C: Config) = struct
let vlq_base = 1 lsl C.shift
let vlq_base_mask = vlq_base - 1
let vlq_continuation_bit = vlq_base (* MSB *)

(**
* Converts from a two-complement value to a value where the sign bit is
* placed in the least significant bit. For example, as decimals:
* 1 becomes 2 (10 binary), -1 becomes 3 (11 binary)
* 2 becomes 4 (100 binary), -2 becomes 5 (101 binary)
*)
let vlq_signed_of_int value =
if value < 0 then ((-value) lsl 1) + 1 else (value lsl 1) + 0

(* Write the value to the buffer, as multiple characters as necessary *)
let rec encode_vlq buf vlq =
let digit = vlq land vlq_base_mask in
let vlq = vlq lsr C.shift in
if vlq = 0 then Buffer.add_char buf (C.char_of_digit digit)
else begin
(* set the continuation bit *)
Buffer.add_char buf (C.char_of_digit (digit lor vlq_continuation_bit));
encode_vlq buf vlq
end

(* Encodes `value` as a VLQ and writes it to `buf` *)
let encode buf value =
let vlq = vlq_signed_of_int value in
encode_vlq buf vlq
end

module Base64 = Make (struct
let shift = 5
let base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

(* Convert a number between 0 and 63 to a base64 char *)
let char_of_digit digit =
if 0 <= digit && digit < String.length base64
then base64.[digit]
else failwith (Printf.sprintf "Must be between 0 and 63: %d" digit)
end)
12 changes: 12 additions & 0 deletions src/vlq.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(**
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)

module type S = sig
val encode: Buffer.t -> int -> unit
end

module Base64 : S
12 changes: 12 additions & 0 deletions test/jbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(jbuild_version 1)

(executable (
(name test)
(libraries (vlq oUnit))
))

(alias (
(name runtest)
(deps (test.exe))
(action (run ${<}))
))
33 changes: 33 additions & 0 deletions test/test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(**
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*)

open OUnit2

let cases = [
(* input, output *)
0, "A";
1, "C";
-1, "D";
123, "2H";
123456789, "qxmvrH";
]

let test_encode ctxt =
List.iter (fun (input, expected) ->
let buf = Buffer.create 10 in
Vlq.Base64.encode buf input;
let actual = Buffer.contents buf in
assert_equal ~ctxt ~printer:(fun x -> x)
~msg:(Printf.sprintf "Vql.encode buf %d:" input)
expected actual
) cases

let tests = "vlq" >::: [
"encode" >:: test_encode;
]

let () = run_test_tt_main tests
1 change: 1 addition & 0 deletions vlq.descr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A simple library for encoding variable-length quantities.
18 changes: 18 additions & 0 deletions vlq.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
opam-version: "1.2"
maintainer: "Marshall Roch <[email protected]>"
authors: ["Marshall Roch <[email protected]>"]
homepage: "https://github.com/flowtype/ocaml-vlq"
doc: "https://github.com/flowtype/ocaml-vlq"
license: "MIT"
dev-repo: "https://github.com/flowtype/ocaml-vlq.git"
bug-reports: "https://github.com/flowtype/ocaml-vlq/issues"
tags: []
available: [ ocaml-version >= "4.01.0"]
depends:
[
"jbuilder" {build & >= "1.0+beta7"}
"ounit" {test & >= "2.0.0"}
]
depopts: []
build: [["jbuilder" "build" "-p" name "-j" jobs]]
build-test: [["jbuilder" "runtest" "-p" name "-j" jobs]]

0 comments on commit 99da01b

Please sign in to comment.