dmonckton Contributor
Joined: 09 Aug 2002 Posts: 117 Location: Lewes, U.K.
|
Posted: Mon Dec 12, 2005 1:55 pm Post subject: Parsing Functions |
|
|
Hi
May recent project called for a lot of work with parsed strings, so I wrote these functions, comments welcome.....
Code: |
#define function,parseCount,subParse,parseIns
rem ***********************************************************
rem * FUNCTION:
REM * @parseIns(string1,pos1,string2)
REM *
REM * DATE:
REM * 121205A
REM *
rem * DSCRPTN:
rem * returns a parsed string consisting of string1 with string2
rem * inserted at parse pos1. If pos1 is omitted or zero unmodified
rem * string1 is returned. If pos1 is greater than the length
rem * of string1 then string2 is inserted at the end of string1
rem *
rem * RETURNS:
rem * substring
rem *
rem ***********************************************************
:parseIns
if @null(%2)@zero(%2)
exit %1
end
if @equal(%2,1)
exit %3
end
if @greater(%2,@parseCount(%1))
%a = %1|%3
exit %a
end
%a = @subParse(%1,1,@pred(%2))
%a = %a|%3
exit %a
rem ***********************************************************
rem ***********************************************************
rem * FUNCTION:
REM * @subParse(string,pos1,pos2)
REM *
REM * DATE:
REM * 211105A
REM *
rem * DSCRPTN:
rem * returns a parsed substring from pos1 to pos2
rem * if pos1 is ommitted or zero unmodified string is returned
rem * if pos2 is zero, omitted or less than pos1, single field
rem * is returned
rem *
rem * RETURNS:
rem * substring
rem *
rem ***********************************************************
:subParse
if @null(%2)@zero(%2)
exit %1
end
if @equal(%2,1)
%a =
else
%a =
%i = 0
%l = @diff(%2,1)
repeat
%a = %a;
%i = @succ(%i)
until @equal(%i,%l)
end
if @null(%3)@zero(%3)
%l = %2
else
%l = %3
end
%i = %2
if @zero(@diff(%l,%i))@greater(%i,%l)
parse %a"%c",%1
else
parse %a"%c",%1
repeat
%a = %a;
parse %a"%b",%1
%c = %c|%b
%i = @succ(%i)
until @equal(%i,%l)
end
exit %c
rem ***********************************************************
rem ***********************************************************
rem * FUNCTION:
REM * @parseCount(string)
REM *
REM * DATE:
REM * 211105A
REM *
rem * DSCRPTN:
rem * counts the number of parsed sections in a string
rem * if 1 is returned then string did not contain any
rem * parsed sections or it was empty
rem *
rem * RETURNS:
rem * count
rem *
rem ***********************************************************
:parseCount
%a = ;
%c = 1
repeat
parse %a"%b",%1
if @null(%b)
exit %c
end
%c = @succ(%c)
%a = %a;
until
exit
rem ***********************************************************
|
Thanks
David.M |
|