forked from ocaml/dune
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ocaml#9941 from ocaml/ps/rr/refactor__optimize__pa…
…th_reach_ refactor: optimize [Path.reach]
- Loading branch information
Showing
4 changed files
with
165 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
type t = | ||
{ mutable buf : Bytes.t | ||
; mutable pos : int | ||
} | ||
|
||
let[@inline] create capacity = { buf = Bytes.create capacity; pos = 0 } | ||
|
||
let[@inline] build_exact_exn t = | ||
if not (Int.equal t.pos (Bytes.length t.buf)) | ||
then Code_error.raise "Stdune.String_builder.build_exact_exn: buffer not full" []; | ||
let result = Bytes.unsafe_to_string t.buf in | ||
(* Ensure that [t.buf] doesn't get used again. *) | ||
t.buf <- Bytes.empty; | ||
t.pos <- 0; | ||
result | ||
;; | ||
|
||
let[@inline] add_char t c = | ||
Bytes.set t.buf t.pos c; | ||
t.pos <- t.pos + 1 | ||
;; | ||
|
||
let[@inline] add_substring t src ~pos ~len = | ||
Bytes.blit_string ~src ~src_pos:pos ~dst:t.buf ~dst_pos:t.pos ~len; | ||
t.pos <- t.pos + len | ||
;; | ||
|
||
let[@inline] add_string t src = add_substring t src ~pos:0 ~len:(String.length src) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
(** A buffer for efficiently building strings. *) | ||
type t | ||
|
||
(** Creates a [t] with the given capacity. *) | ||
val create : int -> t | ||
|
||
(** Adds a [char] to the end of the buffer. *) | ||
val add_char : t -> char -> unit | ||
|
||
(** Adds a [string] to the end of the buffer. *) | ||
val add_string : t -> string -> unit | ||
|
||
(** Adds a substring to the end of the buffer. *) | ||
val add_substring : t -> string -> pos:int -> len:int -> unit | ||
|
||
(** Returns the built string. Raises if the buffer is not full. Subsequent calls to | ||
[add_char] or [add_string] will also raise. *) | ||
val build_exact_exn : t -> string |