substrings corruption?

Hi guys,

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?

–Eric

The function is supposed to truncate lines longer than line_length and add the remainder after a tab on the next line.

so truncating:

This is a test for line truncation.

will create something like this:

This is a test 
    for line truncation.

However, the Substring function seems to lose data after a certain point:

becomes:

The third line got corrupted. :frowning:

Hmm…unless I’m missing something, I think I would just do

function ChopLines(line_length : int, text : String) { 
	if (!enable_line_truncation) { 
		return text; 
	}
	line_length = Mathf.Clamp(line_length, 0, text.Length);	// Sanity enforcement
	return text.Substring(0, line_length)+"\n\t"+text.Substring(line_length, text.Length-line_length);
}

–Eric

Thanks for the code, after reviewing your stuff. I noticed that my original routine had some indexing logic flaws: So here is the fixed routine…

(for multiple lines of input)

function ChopLines(line_length : int, original_text : String) {
    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);
        
        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";
                
                Debug.Log("ChopLines:"+m+" to "+(m+ line_length)+":"+text.length +":"+output_text +"\n");
                
                m = m + line_length;               
            } else {
                // find_index <= m+line_length
                output_text += text.Substring(m,find_index-m);
                output_text += "\n";

                Debug.Log("ChopLines:"+m+" to "+find_index+":"+text.length +":"+output_text +"\n");
                
                m = find_index +1;
            }
         } else {            
            // \n not found
            output_text += text.Substring(m,text.length-m);
            
            Debug.Log("ChopLines: \\n not found:"+m+" to "+find_index+":"+output_text +"\n");            
            break;
         }
    }
    
    

    
    return output_text;

}