shithub: mc

Download patch

ref: 9a778738a652738ed3877477a37359d7d490c853
parent: 287133959e79176c450af23efe31f0ac3b695679
author: Ori Bernstein <[email protected]>
date: Wed Jan 8 05:52:34 EST 2014

Add support for 'skipto'

--- a/bio.myr
+++ b/bio.myr
@@ -51,6 +51,7 @@
 	/* delimited read; returns freshly allocated buffer. */
 	const readln	: (f : file#	-> std.option(byte[:]))
 	const readto	: (f : file#, delim : byte[:]	-> std.option(byte[:]))
+	const skipto	: (f : file#, delim : byte[:]	-> bool)
 
 	/* formatted i/o */
 	const put	: (f : file#, fmt : byte[:], args : ... -> std.size)
@@ -375,7 +376,7 @@
 			if f.rbuf[i] == delim[0]
 				for j = 0; j < delim.len; j++
 					if f.rbuf[i + j] != delim[j]
-						goto nextiter
+						goto nextiterread
 					;;
 				;;
 				ret = readinto(f, ret, i - f.rstart)
@@ -382,7 +383,7 @@
 				f.rstart += delim.len
 				-> `std.Some ret
 			;;
-:nextiter
+:nextiterread
 		;;
 		ret = readinto(f, ret, i - f.rstart)
 	;;
@@ -393,6 +394,32 @@
 const readln = {f
 	-> readto(f, "\n")
 }
+
+const skipto = {f, delim
+	var ret : byte[:]
+	var i, j
+
+	ret = [][:]
+	while true
+		if !ensureread(f, delim.len)
+			-> ret.len > 0
+		;;
+		for i = f.rstart; i < f.rend; i++
+			if f.rbuf[i] == delim[0]
+				for j = 0; j < delim.len; j++
+					if f.rbuf[i + j] != delim[j]
+						goto nextiterskip
+					;;
+				;;
+				f.rstart += delim.len
+				-> true
+			;;
+:nextiterskip
+		;;
+	;;
+	std.die("unreachable")
+}
+
 
 /*
 Same as std.put, but buffered. Returns the number of bytes written.