Skip to content

Commit

Permalink
XXX Implement Text.Cursor.next_opt .
Browse files Browse the repository at this point in the history
  • Loading branch information
jasone committed Aug 21, 2020
1 parent b862f00 commit 6fbc0be
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions bootstrap/src/basis/text.ml
Expand Up @@ -18,12 +18,10 @@ module Pos = struct
let init ~line ~col =
{line; col}

(*
let succ cp t =
match cp with
| cp when Codepoint.(cp = nl) -> {line=Uns.succ t.line; col=0}
| _ -> {t with col=Uns.succ t.col}
*)

let line t =
t.line
Expand Down Expand Up @@ -52,6 +50,13 @@ module Apos = struct
let init ~bindex ~cindex ~pos =
{bindex; cindex; pos}

let succ cp t =
{
bindex=Codepoint.Utf8.(length (of_codepoint cp));
cindex=Uns.succ t.cindex;
pos=Pos.succ cp t.pos;
}

let bindex t =
t.bindex

Expand Down Expand Up @@ -102,10 +107,8 @@ module Excerpt = struct
let apos t =
t.apos

(*XXX
let string t =
t.string
*)
end
include T
(* XXX Remove?
Expand Down Expand Up @@ -198,11 +201,6 @@ module Cursor = struct
let container t =
t.text

(*XXX
let apos t =
t.apos
*)

let pos t =
Apos.pos t.apos

Expand All @@ -224,8 +222,27 @@ module Cursor = struct
end in
fn (force text)

let next_opt _t =
None (* XXX *)
let rec next_opt t =
match String.Cursor.(
t.scursor < String.Cursor.tl (String.Cursor.container t.scursor)) with
| true -> begin
let cp, scursor' = String.Cursor.next t.scursor in
let apos' = Apos.succ cp t.apos in
Some (cp, {t with apos=apos'; scursor=scursor'})
end
| false -> begin
match Ordset.Cursor.(t.ecursor < tl t.text.excerpts) with
| true -> begin
let excerpt', ecursor' = Ordset.Cursor.next t.ecursor in
let scursor' = String.Cursor.hd (Excerpt.string excerpt') in
next_opt {t with ecursor=ecursor'; scursor=scursor'}
end
| false -> begin
match Lazy.force (t.text.extend) with
| None -> None
| Some text' -> next_opt {t with text=text'}
end
end

let rget_opt t =
match next_opt t with
Expand Down

0 comments on commit 6fbc0be

Please sign in to comment.