Skip to content

Commit 9535863

Browse files
committed
Factor out some code in an util module
1 parent ba6038e commit 9535863

File tree

4 files changed

+43
-36
lines changed

4 files changed

+43
-36
lines changed

_oasis

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Plugins: META (0.4), StdFiles (0.4)
1212
Library "cosmetrics"
1313
Path: src/
1414
Modules: Cosmetrics
15-
InternalModules: Cosmetrics_html, Cosmetrics_opam
15+
InternalModules: Cosmetrics_html, Cosmetrics_opam, Cosmetrics_utils
1616
BuildDepends: git (>= 1.7.1), zip, nocrypto, cohttp,
1717
ocamlgraph, calendar,
1818
opam-lib.client (>= 1.2 && < 1.3), str, bytes, unix

src/Cosmetrics.ml

+1-35
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
open Lwt
33
open CalendarLib
4+
open Cosmetrics_utils
45

56
module Store = Git_unix.FS
67
module G = Git_unix.Sync.Make(Store)
@@ -22,41 +23,6 @@ let read_tree_exn t sha =
2223
fail(Failure "Cosmetrics.read_tree_exn: not a tree value")
2324

2425

25-
module String = struct
26-
include String
27-
28-
let starting ~w:prefix s =
29-
if String.length prefix <= String.length s then
30-
try
31-
for i = 0 to String.length prefix - 1 do
32-
if unsafe_get prefix i <> unsafe_get s i then raise Exit
33-
done;
34-
true
35-
with Exit -> false
36-
else false
37-
38-
let ending ~w:postfix s =
39-
let ofs = String.length s - String.length postfix in
40-
if ofs >= 0 then
41-
try
42-
for i = 0 to String.length postfix - 1 do
43-
if unsafe_get postfix i <> unsafe_get s (ofs + i) then raise Exit
44-
done;
45-
true
46-
with Exit -> false
47-
else false
48-
end
49-
50-
module List = struct
51-
include List
52-
53-
let rec remove_consecutive_duplicates equal = function
54-
| ([] | [_]) as l -> l
55-
| x :: ((y :: _) as tl) ->
56-
if equal x y then remove_consecutive_duplicates equal tl
57-
else x :: remove_consecutive_duplicates equal tl
58-
end
59-
6026
(* Simple cache module. *)
6127
module Cache = struct
6228
type 'a t = { log: string -> unit Lwt.t;

src/Cosmetrics_utils.ml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module String = struct
2+
include String
3+
4+
let starting ~w:prefix s =
5+
if String.length prefix <= String.length s then
6+
try
7+
for i = 0 to String.length prefix - 1 do
8+
if unsafe_get prefix i <> unsafe_get s i then raise Exit
9+
done;
10+
true
11+
with Exit -> false
12+
else false
13+
14+
let ending ~w:postfix s =
15+
let ofs = String.length s - String.length postfix in
16+
if ofs >= 0 then
17+
try
18+
for i = 0 to String.length postfix - 1 do
19+
if unsafe_get postfix i <> unsafe_get s (ofs + i) then raise Exit
20+
done;
21+
true
22+
with Exit -> false
23+
else false
24+
end
25+
26+
module List = struct
27+
include List
28+
29+
let rec remove_consecutive_duplicates equal = function
30+
| ([] | [_]) as l -> l
31+
| x :: ((y :: _) as tl) ->
32+
if equal x y then remove_consecutive_duplicates equal tl
33+
else x :: remove_consecutive_duplicates equal tl
34+
35+
let rec filter_map f = function
36+
| [] -> []
37+
| x :: tl -> match f x with
38+
| Some y -> y :: filter_map f tl
39+
| None -> filter_map f tl
40+
end

src/main.ml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ open CalendarLib
33
module C = Cosmetrics
44
module T = C.Timeseries
55
module H = Cosmetrics_html
6+
open Cosmetrics_utils
67

78
let is_finite x = neg_infinity < x && x < infinity
89

0 commit comments

Comments
 (0)