shithub: mc

Download patch

ref: efc139e255850d6300d5ef750cfe92d593efc4e8
parent: 60c4147b74bfe21062e6663dfc7bf1ec306e5601
author: Ori Bernstein <[email protected]>
date: Tue Jan 28 16:20:53 EST 2014

Remove duplicated code.

    Readto and skipto were more or less identical in structure,
    with the one difference that one filled a buffer and the other
    did not.

    This unifies them.

--- a/bio.myr
+++ b/bio.myr
@@ -32,6 +32,8 @@
 	const read	: (f : file#, dst : byte[:]	-> std.option(byte[:]))
 	const flush	: (f : file# -> bool)
 
+	/* seeking */
+
 	/* single unit operations */
 	const putb	: (f : file#, b : byte	-> std.size)
 	const putc	: (f : file#, c : char	-> std.size)
@@ -359,6 +361,17 @@
   	bio.readto(f, ',')	-> "bar\n"
 */
 const readto = {f, delim
+	-> readdelim(f, delim, false)
+}
+
+const skipto = {f, delim
+	match readdelim(f, delim, true)
+	| `std.Some ret:	-> true
+	| `std.None:	-> false
+	;;
+}
+
+const readdelim = {f, delim, drop
 	var ret
 	var i, j
 
@@ -365,7 +378,9 @@
 	ret = [][:]
 	while true
 		if !ensureread(f, delim.len)
-			ret = readinto(f, ret, f.rend - f.rstart)
+			if !drop
+				ret = readinto(f, ret, f.rend - f.rstart)
+			;;
 			if ret.len > 0
 				-> `std.Some ret
 			else
@@ -376,45 +391,22 @@
 			if f.rbuf[i] == delim[0]
 				for j = 0; j < delim.len; j++
 					if f.rbuf[i + j] != delim[j]
-						goto nextiter1
+						goto nextiter
 					;;
 				;;
-				ret = readinto(f, ret, i - f.rstart)
+				if !drop
+					ret = readinto(f, ret, i - f.rstart)
+				;;
 				f.rstart += delim.len
 				-> `std.Some ret
 			;;
-/* compiler bug: labels are global */
-:nextiter1
+:nextiter
 		;;
-		ret = readinto(f, ret, i - f.rstart)
-	;;
-	std.die("unreachable")
-}
-
-const skipto = {f, delim
-	var i, j
-
-	while true
-		/* clear the buffer and signal EOF */
-		if !ensureread(f, delim.len)
-			f.rstart = 0
-			f.rend = 0
-			-> false
+		if !drop
+			ret = readinto(f, ret, f.rend - f.rstart)
 		;;
-		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 nextiter2
-					;;
-				;;
-			;;
-			f.rstart += delim.len
-			-> false
-		;;
-/* compiler bug: labels are global */
-:nextiter2
 	;;
+	std.die("unreachable")
 }
 
 /* Same as readto, but the delimiter is always a '\n' */