Difference between revisions of "SplitLine"

From Second Life Wiki
Jump to navigation Jump to search
(Add SplitLine function example)
 
m (<lsl> tag to <source>)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{LSL Header}} __NOTOC__
{{LSL Header}} __NOTOC__
<div id="box">
<div id="box">{{#if:
{{#vardefine:p_source_desc|source string}}
{{#vardefine:p_source_desc|source string}}
{{#vardefine:p_separator_desc|split separator string}}
{{#vardefine:p_separator_desc|split separator string}}
{{#vardefine:p_stride_desc|split stride}}
{{#vardefine:p_stride_desc|split stride}}
== Function: [[string]] SplitLine([[string]] {{LSL Param|source}},[[string]] {{LSL Param|separator}},[[integer]] {{LSL Param|stride}}); ==
}}
== Function: [[string]] SplitLine([[string]] {{LSL Param|source}}, [[string]] {{LSL Param|separator}}, [[integer]] {{LSL Param|stride}}); ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
Returns a [[String|string]] split by inserted cr/lf at '''{{LSL Param|separator}}''' positions to portions of at least '''{{LSL Param|stride}}''' length.<br/>
Returns a [[String|string]] split by inserted 'new line' escape code at '''{{LSL Param|separator}}''' positions to portions of at least '''{{LSL Param|stride}}''' length.<br/>
In other words, a line can be split up to several lines at positions where '''{{LSL Param|separator}}''' string is found, each line with a length of at least '''{{LSL Param|stride}}''' characters.
In other words, a line can be split up to several lines at positions where '''{{LSL Param|separator}}''' string is found, each line with a length of at least '''{{LSL Param|stride}}''' characters.
{|
{|
Line 17: Line 18:
== Specification ==
== Specification ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
<pre>
<source lang="lsl2">
string SplitLine(string _source, string _separator, integer _stride)
string SplitLine(string _source, string _separator, integer _stride)
{
{
Line 37: Line 38:
     return _source;
     return _source;
}
}
</pre>
</source>
</div></div>
</div></div>


Line 44: Line 45:
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
Trivial example to listen to any chat from the object owner for '''{{LSL Param|source}}''' values and respond lines split at space (" ") positions using 10 characters stride value.
Trivial example to listen to any chat from the object owner for '''{{LSL Param|source}}''' values and respond lines split at space (" ") positions using 10 characters stride value.
<pre>
<source lang="lsl2">
// Insert code of SplitLine user function here
 
default
default
{
{
Line 58: Line 61:
     }
     }
}
}
</pre>
</source>
</div></div>
</div></div>


{{LSLC|Examples|SplitLine}}
{{LSLC|Examples|SplitLine}}

Latest revision as of 17:06, 24 January 2015

Function: string SplitLine(string source, string separator, integer stride);

Returns a string split by inserted 'new line' escape code at separator positions to portions of at least stride length.
In other words, a line can be split up to several lines at positions where separator string is found, each line with a length of at least stride characters.

• string source source string
• string separator split separator string
• integer stride split stride

Specification

string SplitLine(string _source, string _separator, integer _stride)
{
    integer offset= _stride;
    integer sourceLen = llStringLength(_source);
    integer separatorLen = llStringLength(_separator);

    while (sourceLen > _stride)
    {
	integer split = llSubStringIndex(llGetSubString(_source, offset, -1), _separator);
	if (split != -1)
        {
	    _source = llInsertString(_source, offset + split + separatorLen, "\n");
	    sourceLen = llStringLength(_source) - offset;
	    offset += split + _stride + 2;
	}
        else sourceLen = -1;
    }
    return _source;	
}

Example

Trivial example to listen to any chat from the object owner for source values and respond lines split at space (" ") positions using 10 characters stride value.

// Insert code of SplitLine user function here

default
{
    state_entry()
    {
        llOwnerSay("Enter text");
	llListen(0, "", llGetOwner(), "");
    }
	
    listen(integer _chan, string _str, key _id, string _msg)
    {
        llOwnerSay(_msg + "\nsplit to:\n" + SplitLine(_msg, " ", 10));
    }
}