I’ve been having trouble with Unity’s implementation of Substring recently
(which seems to be using the windows spec here: http://msdn2.microsoft.com/en-us/library/aka44szs.aspx )
It appears as if this code is allowing the original string to be corrupted:
Any ideas?
function ChopLines(line_length : int, original_text : String) {
// looks like the Unity javascript string functions are a buggy mess!!
var output_text : String = "";
var text : String = "";
if (!enable_line_truncation) {
return original_text;
}
text += original_text;
Debug.Log("ChopLines: input:"+ original_text +"\n");
for (var m = 0; m < text.length; ) {
// hack to deal with Substring destroying text string
text = "";
text += original_text;
// copy first line
var find_index = text.IndexOf("\n", m);
Debug.Log("ChopLines:"+m+" to "+find_index+" out of:"+text.length+"\n");
if ((find_index >= 0) (find_index < text.length)) {
if (find_index > (m+line_length)) {
// insert a \n\t
// Substring(from,to) actually uses: substr(start,length)
output_text += text.Substring(m,line_length);
output_text += "\n\t";
output_text += text.Substring(m+line_length,find_index-(m+line_length));
output_text += "\n";
} else {
// find_index <= m+line_length
output_text += text.Substring(m,find_index-m);
output_text += "\n";
}
//Debug.Log("ChopLines: input:"+original_text);
Debug.Log("ChopLines: text:"+text);
Debug.Log("ChopLines:"+m+" to "+find_index+":"+output_text +"\n");
m += find_index +1;
} else {
// \n not found
output_text += text.Substring(m,text.length-m);
Debug.Log("ChopLines: text:"+text);
Debug.Log("ChopLines: \\n not found:"+m+" to "+find_index+":"+output_text +"\n");
break;
}
}
return output_text;
}
Unity uses .Net rather than any specific Javascript functions; it’s the same in C# and Boo. I haven’t noticed any bugs or corruption in string handling. What is that function supposed to do?