ID:2185380
 
Redundant
Applies to:DM Language
Status: Redundant

This feature has already been implemented, or is already achievable with existing methods.
The addition of a swaptext & swaptextEx.

This is a existing method I could conjure up, seems rather inefficient?

proc/swap(haystack, string1, string2, start=1, end=0)
haystack = replacetext(haystack,string1,"?[string2].",start,end)
haystack = replacetext(haystack,string2,string1,start,end)
haystack = replacetext(haystack,"?[string1].",string2,start,end)
return haystack

mob/Login()
var str = "A cat was killed by the family mouse."
src << swap(str,"cat","mouse")
Lummox JR resolved issue (Redundant)
Since this can be done in soft code, and I don't really see any need for it, I think we'd best leave it here. There isn't a function like this in most languages; for BYOND I can't imagine a useful purpose for it.
Version in one replacetext(), though this one will break if either 'one' or 'two' have any regex special characters in them. Potentially has less issues with interference though, eg if you use yours to swap "foo" and "bar" in the string "foo?bar.foo", you'll get "barbarbar" instead of "bar?foo.bar"

/proc/get_other_swap(x)
if(x == g_one)
return g_two
else
return g_one

/var/g_one
/var/g_two
/proc/swap(text, one, two, start=1, end=0)
g_one = one
g_two = two
return replacetext(text, regex("[one]|[two]", "g"), /proc/get_other_swap, start, end)


Closures or literal semi-procs would be nice if at all feasible, though - they'd simplify this swap() significantly; perhaps a syntax like the following?
/proc/swap(text, a, b, start=1, end=0)
return replacetext(text, regex("[a]|[b]", "g"), proc(x){ return x==a ? b : a }, start, end)


All depends on the compiler and how procs are represented internally to BYOND, I guess.
That's much better.

I never really messed with regex, would be nice to have a better demo/example available to us slower members :P

Also, /proc/get_other_swap in replacetext() ?????

That syntax looks both beautiful and frightening, I ran it and it's called but idk how it's given a argument and exactly how it replaces it via that.

You're a wizard.
The replacetext() proc, when given a regular expression, can take a proc reference as its replacement argument. (I think, but I don't recall with certainty, that text2path() can be used too if you want to specify the proc at runtime.) The arguments to the proc are the matched text, followed by any captured groups. The return value is used as the replacement.