//mine
proc/ReplaceAll_With_(var/txt="",var/target="",var/replacement="")
var/endOftxt = lentext(txt)
var/endOftarget = lentext(target)
var/i = endOftxt - endOftarget
while(i>1)
if(copytext(txt,i, i+endOftarget)==target)
txt = copytext(txt,1,i) + replacement + copytext(txt,i+endOftarget,0)
i--
return txt
//deadron's
dd_replaceText(text, search_string, replacement_string)
var/list/textList = dd_text2List(text, search_string)
return dd_list2text(textList, replacement_string)
which calls...
dd_text2List(text, separator)
var/textlength = lentext(text)
var/separatorlength = lentext(separator)
var/list/textList = new /list()
var/searchPosition = 1
var/findPosition = 1
var/buggyText
while (1) // Loop forever.
findPosition = findText(text, separator, searchPosition, 0)
buggyText = copytext(text, searchPosition, findPosition) // Everything from searchPosition to findPosition goes into a list element.
textList += "[buggyText]" // Working around weird problem where "text" != "text" after this copytext().
searchPosition = findPosition + separatorlength // Skip over separator.
if (findPosition == 0) // Didn't find anything at end of string so stop here.
return textList
else
if (searchPosition > textlength) // Found separator at very end of string.
textList += "" // So add empty element.
return textList
Then deadron's function calls...
dd_list2text(list/the_list, separator)
var/total = the_list.len
if (total == 0) // (deadron's comment) Nothing to work with.
return
var/newText = "[the_list[1]]" // (deadron's comment) Treats any object/number as text also.
var/count
for (count = 2, count <= total, count++)
if (separator) newText += separator
newText += "[the_list[count]]"
return newText
I'm really baffled. My function is tiny! How is it so much slower? Is the minus operation really that slow? Or is it because I'm using copytext()?
Note to thread moderator: I placed this in design philosophy because it seemed like a design question (e.g. I don't have a problem with my code because it works, I just wanted to make it better).