(* $Id$
 * ----------------------------------------------------------------------
 * PXP: The polymorphic XML parser for Objective Caml.
 * Copyright by Gerd Stolpmann. See LICENSE for details.
 *)

open Lexing

type lexbuf = Lexing.lexbuf

(* We are now again using the functions from Lexing. They are ok since
 * O'Caml 3.05. In O'Caml 3.04 and earlier versions, the Lexing module
 * had serious performance problems for very large tokens.
 *
 * Note that the type of lexbuf has changed between O'Caml 3.06 and 3.07.
 * (New components lex_mem, lex_start_p, lex_curr_p.)
 *)

let from_function = Lexing.from_function

let from_channel = Lexing.from_channel

let from_string = Lexing.from_string

let lexeme = Lexing.lexeme
let lexeme_char = Lexing.lexeme_char
let lexeme_start = Lexing.lexeme_start
let lexeme_end = Lexing.lexeme_end

let lexeme_len lexbuf = 
  lexbuf.lex_curr_pos - lexbuf.lex_start_pos

let from_bytes_inplace s =
  (* avoids copying s *)
  let lb = from_string "" in
  { lb with
      lex_buffer = s;
      lex_buffer_len = Bytes.length s
  }
;;

let sub_lexeme lexbuf k l =
  (* = String.sub (Lexing.lexeme lexbuf) k l *)
  (* In recent versions of O'Caml (3.06+X), there are already definitions 
   * of sub_lexeme. These have the same effect, but don't protect against
   * improper usage.
   *)
  let lexeme_len = lexbuf.lex_curr_pos - lexbuf.lex_start_pos in
  if (k < 0 || k > lexeme_len || l < 0 || k+l > lexeme_len) then
    invalid_arg "sub_lexeme";
  let s = Bytes.create l in
  Bytes.unsafe_blit 
    lexbuf.lex_buffer (lexbuf.lex_start_pos + k) s 0 l;
  Bytes.unsafe_to_string s
;;

