aboutsummaryrefslogtreecommitdiff
path: root/src/cell.ml
blob: dc5dcdc6069adac5f697039e8ada556b18438f10 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
(*
This file is part of licht.

licht is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

licht is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with licht.  If not, see <http://www.gnu.org/licenses/>.
*)

type t = (int * int) * (bool * bool)

let u = UTF8.from_utf8string

let from_string (fixed_x, x_name) (fixed_y, y) =

  let x = ref 0 in
  String.iter (function
  | 'a'..'z' as c -> x:= (!x * 26) + ((int_of_char c) - 96)
  | 'A'..'Z' as c -> x:= (!x * 26) + ((int_of_char c) - 64)
  | _ -> ()
  ) x_name;
  (!x, y), (fixed_x, fixed_y)

let to_hname x = begin
  let rec extract acc value =
    if value > 0 then (
      let value' = value - 1 in
      let rem = value' mod 26 in
      let quot = (value' - rem) / 26
      in (extract[@tailcall]) ((char_of_int (65 + rem))::acc) quot
    ) else (
      acc
    )
  in
  let res = extract [] x
  and buff = UTF8.Buffer.create 4 in
  List.iter (fun c -> UTF8.Buffer.add_char buff c) res;
  UTF8.Buffer.contents buff
end

let to_string ((x, y), (fixed_x, fixed_y)) =
  let buff = UTF8.Buffer.create 2 in

  if fixed_x then UTF8.Buffer.add_char buff '$';
  UTF8.Buffer.add_string buff (to_hname x);
  if fixed_y then UTF8.Buffer.add_char buff '$';
  UTF8.Buffer.add_string buff @@ u @@ string_of_int y;
  UTF8.Buffer.contents buff

let to_buffer buff ((x, y), (fixed_x, fixed_y)) = begin
  if fixed_x then UTF8.Buffer.add_char buff '$';
  UTF8.Buffer.add_string buff (to_hname x);
  if fixed_y then UTF8.Buffer.add_char buff '$';
  UTF8.Buffer.add_string buff @@ u @@ string_of_int y
end

let to_string t =
  let buff = UTF8.Buffer.create 2 in
  to_buffer buff t;
  UTF8.Buffer.contents buff

let to_pair = Pervasives.fst

module Set = (struct
  include Set.Make(struct
    type t = (int * int)
    let compare = Pervasives.compare
  end)

  let show_int_tuple b t = Tools.Tuple2.printb
    (fun b x -> UTF8.Buffer.add_string b @@u(string_of_int x))
    (fun b x -> UTF8.Buffer.add_string b @@u(string_of_int x))
    b t

  let printb buff =
    iter (fun x -> to_buffer buff (x, (false,false)); UTF8.Buffer.add_char buff ' ')

end)