shithub: mc

ref: b5bfee96de8a7fae9bdda140f630bea0b5997737
dir: /lib/escfmt/escre.myr/

View raw version
use std

pkg escfmt =
	type escre = byte[:]

	const re	: (s : byte[:] -> escre) 
;;

const __init__ = {
	var s = ("" : escre)
	std.fmtinstall(std.typeof(s), refmt)
}

const re = {s
	-> (s : escre)
}

const refmt = {sb, ap, args
	var s : byte[:]

	s = std.vanext(ap)
	for c : std.bychar(s)
		match c
		| '|':	std.sbputs(sb, "\\|")
		| '*':	std.sbputs(sb, "\\*")
		| '+':	std.sbputs(sb, "\\+")
		| '?':	std.sbputs(sb, "\\?")
		| '[':	std.sbputs(sb, "\\[")
		| ']':	std.sbputs(sb, "\\]")
		| '^':	std.sbputs(sb, "\\^")
		| '$':	std.sbputs(sb, "\\$")
		| '.':	std.sbputs(sb, "\\.")
		| '\\':	std.sbputs(sb, "\\.")
		| '(':	std.sbputs(sb, "\\(")
		| ')':	std.sbputs(sb, "\\)")
		| _:	std.sbputc(sb, c)
		;;
	;;
}