hop-2012/server/ibuffer.ml

85 lines
1.9 KiB
OCaml
Raw Normal View History

2012-03-07 18:23:41 +00:00
(* Copyright 2012 Tony Garnock-Jones <tonygarnockjones@gmail.com>. *)
2012-05-01 21:36:38 +00:00
(* This file is part of Hop. *)
2012-03-07 18:23:41 +00:00
2012-05-01 21:36:38 +00:00
(* Hop is free software: you can redistribute it and/or modify it *)
2012-03-07 18:23:41 +00:00
(* 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. *)
2012-05-01 21:36:38 +00:00
(* Hop is distributed in the hope that it will be useful, but *)
2012-03-07 18:23:41 +00:00
(* 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 *)
2012-05-01 21:36:38 +00:00
(* along with Hop. If not, see <http://www.gnu.org/licenses/>. *)
2012-03-07 18:23:41 +00:00
2012-03-03 23:04:52 +00:00
type t = {
mutable pos: int;
limit: int;
buf: string;
}
let create s ofs len = {
pos = ofs;
limit = ofs + len;
buf = s
}
let of_string s = create s 0 (String.length s)
2012-03-03 23:04:52 +00:00
let sub b ofs len =
if b.pos + ofs + len > b.limit
then
raise End_of_file
else
{ pos = b.pos + ofs;
limit = b.pos + ofs + len;
buf = b.buf }
let remaining b = b.limit - b.pos
let skip_byte b =
if b.pos < b.limit
then b.pos <- b.pos + 1
else raise End_of_file
let skip_ws b =
while b.pos < b.limit && String.get b.buf b.pos <= ' ' do
b.pos <- b.pos + 1
done
let peek_char b =
if b.pos < b.limit
then String.get b.buf b.pos
else raise End_of_file
let peek_byte b = int_of_char (peek_char b)
2012-03-03 23:04:52 +00:00
let next_char b =
if b.pos < b.limit
then
let v = String.get b.buf b.pos in
b.pos <- b.pos + 1;
v
else
raise End_of_file
let next_byte b = int_of_char (next_char b)
let next_chars b n =
if remaining b < n
then
raise End_of_file
else
let dst = String.create n in
String.blit b.buf b.pos dst 0 n;
b.pos <- b.pos + n;
dst
let next_sub b n =
let v = sub b 0 n in
b.pos <- b.pos + n;
v