package forester

  1. Overview
  2. Docs
A tool for tending mathematical forests

Install

Dune Dependency

Authors

Maintainers

Sources

5.0.tar.gz
md5=24f4aed96a8b8af33aba13fba66f1b37
sha512=d36b896aca11858bb4a00fc704c16cc27a1f197bdb3e479d6132fd70f70d67d7158096285cb0b6fb00db14417f0f822cc27fe65d82f0971e42378fd8271ce573

doc/src/forester.prelude/String_util.ml.html

Source file String_util.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(*
 * SPDX-FileCopyrightText: 2024 The Forester Project Contributors
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *)

open Bwd

(* Based on cmap_utf_8 from https://erratique.ch/software/uucp/doc/Uucp/Case/index.html#caseexamples *)
let title_case_word s =
  let did_uppercase = ref false in
  let rec loop buf s i max =
    if i > max then Buffer.contents buf
    else
      let dec = String.get_utf_8_uchar s i in
      let u = Uchar.utf_decode_uchar dec in
      let should_ignore = Uucp.Case.is_case_ignorable u || not (Uucp.Case.is_cased u) in
      let () =
        match should_ignore || !did_uppercase with
        | true ->
          Buffer.add_utf_8_uchar buf u
        | false ->
          did_uppercase := true;
          match Uucp.Case.Map.to_upper u with
          | `Self -> Buffer.add_utf_8_uchar buf u
          | `Uchars us -> List.iter (Buffer.add_utf_8_uchar buf) us
      in
      loop buf s (i + Uchar.utf_decode_length dec) max
  in
  let buf = Buffer.create @@ String.length s * 2 in
  loop buf s 0 @@ String.length s - 1

let sentence_case str =
  let words = String.split_on_char ' ' str in
  String.concat " " @@ List.mapi (fun i word -> if i = 0 then title_case_word word else word) words

let trim_newlines str =
  let rec process_lines lines =
    match lines with
    | [] -> []
    | "" :: lines -> process_lines lines
    | _ -> lines
  in
  let lines = String.split_on_char '\n' str in
  String.concat "\n" @@ List.rev @@ process_lines @@ List.rev @@ process_lines lines

let trim_trailing_whitespace str =
  let rec process_chars rstr =
    match rstr with
    | '\n' :: rstr -> process_chars rstr
    | ' ' :: rstr -> process_chars rstr
    | '\t' :: rstr -> process_chars rstr
    | _ -> List.rev rstr
  in
  let n = String.length str in
  let chars = List.rev @@ List.init n (String.get str) in
  String.of_seq @@ List.to_seq @@ process_chars @@ chars

let explode str =
  List.init (String.length str) (String.get str)

let implode chars =
  String.init (List.length chars) (List.nth chars)

let implode_bwd chars =
  implode (Bwd.to_list chars)
OCaml

Innovation. Community. Security.