Snippets

orbitzN Unicode Hello WOrld

Created by orbitzN
let lines ?encoding (src : [`Channel of in_channel | `String of string]) =
  let rec loop d buf acc =
    match Uutf.decode d with
      | `Uchar u ->
        begin match Uchar.to_int u with
          | 0x000A ->
            let line = Buffer.contents buf in
            Buffer.clear buf;
            loop d buf (line :: acc)
          | _ ->
            Uutf.Buffer.add_utf_8 buf u; loop d buf acc
        end
      | `End ->
        List.rev (Buffer.contents buf :: acc)
      | `Malformed _ ->
        Uutf.Buffer.add_utf_8 buf Uutf.u_rep; loop d buf acc
      | `Await ->
        assert false
  in
  let nln = `Readline (Uchar.of_int 0x000A) in
  loop (Uutf.decoder ~nln ?encoding src) (Buffer.create 512) []

let () =
  CCListLabels.iteri
    ~f:(fun _ -> print_endline)
    (lines (`String "Hello world, Jo\xc3\xa3o"))

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.