Skip to content

Instantly share code, notes, and snippets.

Adding UTF-X decoding support to the stdlib

Since 4.06 we have UTF-X encoding support via the Buffer module. The following is a proposal to add UTF-X decoding (and bytes encoding) support with the following goals:

  1. Provide a low-level, allocation-free codec API in bytes. This API provides all the information needed to implement loops for making   higher-level UTF-X codec APIs (e.g. Uutf's folding functions) operating on bytes and string values. It's not geared towards the end-user
@dbuenzli
dbuenzli / bigbytes.ml
Last active October 21, 2023 01:24
Bigarray mmap RW
(* SPDX-License-Identifier: CC0-1.0 *)
let bigbytes_of_file ?(trunc = false) ?length access file =
let module Bigarray = Stdlib.Bigarray (* OCaml < 5 install woes *) in
let flags, shared = match access with
| `R -> Unix.[O_RDONLY], false
| `RW -> Unix.(O_CREAT :: O_RDWR :: if trunc then [O_TRUNC] else []), true
in
let fd = Unix.openfile file flags 0o644 in
let finally () = try Unix.close fd with Unix.Unix_error _ -> () in
@dbuenzli
dbuenzli / sdl-install.sh
Created May 18, 2014 23:19
SDL install script
#!/bin/sh -ex
V=2.0.3
curl -OL http://www.libsdl.org/release/SDL2-${V}.tar.gz
tar -zxvf SDL2-${V}.tar.gz
cd SDL2-${V}
sudo apt-get install build-essential mercurial make cmake autoconf automake \
libtool libasound2-dev libpulse-dev libaudio-dev libx11-dev libxext-dev \
libxrandr-dev libxcursor-dev libxi-dev libxinerama-dev libxxf86vm-dev \
libxss-dev libgl1-mesa-dev libesd0-dev libdbus-1-dev libudev-dev \
@dbuenzli
dbuenzli / work_queue.ml
Created October 30, 2022 13:08
Webworker work queue
(*---------------------------------------------------------------------------
Copyright (c) 2022 The brr programmers. All rights reserved.
Distributed under the ISC license, see terms at the end of the file.
---------------------------------------------------------------------------*)
open Brr
open Brr_webworkers
open Brr_io
module type WORK = sig
@dbuenzli
dbuenzli / buffer_add_json.ml
Created October 11, 2022 13:32
Buffer.add_json function
(* Simple Buffer.add_json function. *)
type json =
[ `Null | `Bool of bool | `Float of float | `String of string
| `Array of json list | `Object of (string * json) list ]
let rec buffer_add_json b = function
| `Null -> Buffer.add_string b "null"
| `Bool bool -> Buffer.add_string b (if bool then "true" else "false")
| `Float f -> Buffer.add_string b (Printf.sprintf "%.16g" f)
@dbuenzli
dbuenzli / ocamlib.md
Last active August 22, 2022 07:38
OCaml compiler support for library linking
@dbuenzli
dbuenzli / ocaml-emacs.md
Last active August 11, 2022 00:39
Emacs setup for ocaml

Install the following opam packages:

opam install caml-mode merlin ocp-indent

Tweak your .emacs file with some or all of the following:

; shift tab to complete
(global-set-key (kbd "S-<tab>") 'company-complete)
@dbuenzli
dbuenzli / gen.ml
Last active June 11, 2022 18:13
OCaml simple generators
(*---------------------------------------------------------------------------
Copyright (c) 2015 Daniel C. Bünzli. All rights reserved.
Distributed under the BSD3 license, see license at the end of the file.
%%NAME%% release %%VERSION%%
---------------------------------------------------------------------------*)
(* Simple generators according to:
Kiselyov, Peyton-Jones, Sabry
Lazy v. Yield: Incremental, Linear Pretty-printing
@dbuenzli
dbuenzli / trojansource.ml
Last active November 8, 2021 01:07
Trojan source for OCaml
(* OCaml Port of fig. 5-6 of https://trojansource.codes/trojan-source.pdf *)
let access_level = "user"
let main () =
if access_level <> "user‮⁦ (* Check if admin *)⁩⁦" then
Printf.printf "You are an admin.\n"
let () = main ()
@dbuenzli
dbuenzli / uunf_data.ml
Created September 17, 2021 13:04
Uunf data
(*---------------------------------------------------------------------------
Copyright (c) 2015 The uunf programmers. All rights reserved.
Distributed under the ISC license, see terms at the end of the file.
---------------------------------------------------------------------------*)
(* WARNING do not edit. This file was automatically generated. *)
open Uunf_tmapbool;;
let v000 = snil