ID:195048
 
One-liner snippets from my tiny texthandling library.
proc
replacetext(t,t2,t3)return copytext(t,1,findtext(t,t2))+t3+copytext(t,findtext(t,t2)+length(t2),0)
deletetext(t,t2) return copytext(t,1,findtext(t,t2))+copytext(t,findtext(t,t2)+length(t2),0)
inserttext(t,t2,n) return copytext(t,1,n>0?n+1 : length(t)+1)+t2+copytext(t,n>0?n+1 : length(t)+1,0)

mob/verb
HelloMars()src<<replacetext("Hello world!","world","Mars")
//Replaces "world" with "Mars"
HelloWorld()src<<deletetext("Hello stuff in the world!","stuff in the ")
//Outputs "Hello world"
HelloBLAHWorld()src<<inserttext("HelloWorld","BLAH",5)
//Inserts "BLAH" after "Hello", which is 5 characters long.
Kaiochao wrote:
One-liner snippets from my tiny texthandling library.
> proc
> replacetext(t,t2,t3)return copytext(t,1,findtext(t,t2))+t3+copytext(t,findtext(t,t2)+length(t2),0)
> deletetext(t,t2) return copytext(t,1,findtext(t,t2))+copytext(t,findtext(t,t2)+length(t2),0)
> inserttext(t,t2,n) return copytext(t,1,n>0?n+1 : length(t)+1)+t2+copytext(t,n>0?n+1 : length(t)+1,0)
>


Not only do these procs leave much to be desired in the way of iterations (replacetext() and deletetext() would only replace/delete the first instance of t2), they don't work very well, and don't offer much in the way of differentiating between case-sensitivity (you only allow for case-insensitive operations).

The following breaks your first two procs:
client/verb/Test()
// Hello worldMarso world:
src << replacetext("Hello world", "Earth", "Mars")

// Hello worldlo world
src << deletetext("Hello world", "Mars")


A more proper replacetext() function would look like this:
proc
replacetext(haystack, needle, replace)
var
needleLen = length(needle)
replaceLen = length(replace)
pos = findtext(haystack, needle)
while(pos)
haystack = copytext(haystack, 1, pos) + \
replace + copytext(haystack, pos+needleLen)
pos = findtext(haystack, needle, pos+replaceLen)
return haystack