From 6fbc0be40c38734f33c728ce70c609c6308ba354 Mon Sep 17 00:00:00 2001 From: Jason Evans Date: Sun, 16 Aug 2020 17:39:32 -0700 Subject: [PATCH] XXX Implement Text.Cursor.next_opt . --- bootstrap/src/basis/text.ml | 39 ++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/bootstrap/src/basis/text.ml b/bootstrap/src/basis/text.ml index ec5151f70..7be9d7474 100644 --- a/bootstrap/src/basis/text.ml +++ b/bootstrap/src/basis/text.ml @@ -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 @@ -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 @@ -102,10 +107,8 @@ module Excerpt = struct let apos t = t.apos -(*XXX let string t = t.string -*) end include T (* XXX Remove? @@ -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 @@ -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