Re: Function recommendations for Standard Library?

new topic     » goto parent     » topic index » view thread      » older message » newer message

Here are 4 functions for consideration for inclusion in the standard library.
They are rfind(), rfind_from(), rmatch(), rmatch_from().
They perform the same functions as find(), find_from(), match(), match_from()
but in reverse order. They also have similar error messages.

--Find x as an element of s starting from index start going down to 1
--If start<1 then it is an offset from the end of s
global function rfind_from(object x, sequence s, integer start)
integer len
	len=length(s)
	if (start > len) or (len + start < 1) then
		printf(1,"third argument of rfind_from() is out of bounds (%d)",start)
		?1/0
	end if
	if start < 1 then
		start = len + start
	end if
	for i = start to 1 by -1 do
		if equal(s[i], x) then
			return i
		end if
	end for
	return 0
end function

global function rfind(object x, sequence s)
	return rfind_from(x, s, length(s))
end function

--Try to match x against some slice of s, starting from index start and going
down to 1
--if start<0 then it is an offset from the end of s
global function rmatch_from(sequence x, sequence s, integer start)
integer len,lenx
	len = length(s)
	lenx = length(x)
	if lenx = 0 then
		puts(1, "first argument of rmatch_from() must be a non-empty sequence")
		?1/0
	elsif (start > len) or  (len + start < 1) then
		printf(1, "third argument of rmatch_from is out of bounds (%d)",start)
		?1/0
	end if
	if start < 1 then
		start = len + start
	end if
	if start + lenx - 1 > len then
		start = len - lenx + 1
	end if
	lenx-= 1
	for i=start to 1 by -1 do
		if equal(x, s[i..i + lenx]) then
			return i
		end if
	end for
	return 0
end function

global function rmatch(sequence x, sequence s)
	if length(x)=0 then
		puts(1,"first argument of rmatch_from() must be a non-empty string")
		?1/0
	end if
	return rmatch_from(x, s, length(s))
end function


Larry Miller

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu