added Buffer.sub & Buffer.nth

git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@6203 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
master
Basile Starynkevitch 2004-04-14 11:26:21 +00:00
parent 4041c97364
commit 4281c5216b
2 changed files with 26 additions and 0 deletions

View File

@ -27,6 +27,22 @@ let create n =
let contents b = String.sub b.buffer 0 b.position
let sub b ofs len =
if ofs < 0 || len < 0 || ofs > b.position - len
then invalid_arg "Buffer.sub"
else begin
let r = String.create len in
String.blit b.buffer ofs r 0 len;
r
end
;;
let nth b ofs =
if ofs < 0 || ofs >= b.position then
invalid_arg "Buffer.nth"
else String.get b.buffer ofs
;;
let length b = b.position
let clear b = b.position <- 0

View File

@ -40,6 +40,16 @@ val contents : t -> string
(** Return a copy of the current contents of the buffer.
The buffer itself is unchanged. *)
val sub : t -> int -> int -> string
(** [Buffer.sub b off len] returns (a copy of) the substring of the
current contents of the buffer [b] starting at offset [off] of length
[len] bytes. May raise {!Invalid_arg} if out of bounds request. The
buffer itself is unaffected. *)
val nth : t -> int -> char
(** get the n-th character of the buffer. Raise {!Invalid_arg} if
index out of bounds *)
val length : t -> int
(** Return the number of characters currently contained in the buffer. *)