/*
 *  NekoML Standard Library
 *  Copyright (c)2005 Motion-Twin
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License or the LICENSE file for more details.
 */

type t

neko("
	 regexp_matched_pos = $loader.loadprim('regexp@regexp_matched_pos',2);
");

var build : string -> t = neko("$loader.loadprim('regexp@regexp_new',1)");
var find : t -> string -> int -> int -> bool = neko("$loader.loadprim('regexp@regexp_match',4)");
var matched : t -> int -> string = neko("$loader.loadprim('regexp@regexp_matched',2)");

function matched_pos( r : t, n : int ) : (int , int) {
	neko("
		var s = regexp_matched_pos(r,n);
		$array(s.pos,s.len);
	");
}

function split( r, str ) {
	function rec loop(pos,len) {
		if !find  r str pos len then
			[String.sub str pos len]
		else {
			var ppos, plen = matched_pos r 0;
			var tot = ppos - pos + plen;
			String.sub str pos (ppos - pos) :: loop (pos + tot) (len - tot)
		}
	}
	loop 0 String.length(str)
}