Difference between revisions of "User talk:ANSI Soderstrom/Misc useful functions"

From Second Life Wiki
Jump to navigation Jump to search
(Created page with "{{Talk}}")
 
(→‎printf: new section)
Line 1: Line 1:
{{Talk}}
{{Talk}}
== printf ==
Your printf will have a problem if the text being inserted contains "%s". I'd recommend either splitting the input string into a list or copying the output into a new string so that it doesn't get the opportunity to chew on it's own tail. -- '''[[User:Strife_Onizuka|Strife]]''' <sup><small>([[User talk:Strife_Onizuka|talk]]|[[Special:Contributions/Strife_Onizuka|contribs]])</small></sup> 12:10, 14 May 2013 (PDT)
<lsl>
// modify a string in ANSI-C style
// returns a string with all replacements for "modifier".... based on the equivalent positions in the list-content
string printf(string sentence, list insertation) {
    integer j = llGetListLength(insertation);
    if(j) {
        list modifiers = ["%s"];
        list split = llParseString2List(sentence, [], modifiers);
        integer m = llGetListLength(split) - 1;
        integer i = 0;
        integer k = -1;
        do {
            while(!~llListFindList(modifiers, llList2List(split, ++k, k))) {
                if(k == m) {//this is an error!
                    return (string)split;
                }
            }
            //string splitter = llList2String(split, k);
            //if (splitter == "%s")
            split = llListReplaceList(split, llList2List(split, i, i), k, k);
        } while(j > ++i);
        return (string)split;
    }
    return "Error, the insertation list is empty!\n(sentence: " + sentence + ")";
}
</lsl>

Revision as of 12:10, 14 May 2013


printf

Your printf will have a problem if the text being inserted contains "%s". I'd recommend either splitting the input string into a list or copying the output into a new string so that it doesn't get the opportunity to chew on it's own tail. -- Strife (talk|contribs) 12:10, 14 May 2013 (PDT)

<lsl> // modify a string in ANSI-C style // returns a string with all replacements for "modifier".... based on the equivalent positions in the list-content string printf(string sentence, list insertation) {

   integer j = llGetListLength(insertation);
   if(j) {
       list modifiers = ["%s"];
       list split = llParseString2List(sentence, [], modifiers);
       integer m = llGetListLength(split) - 1;
       integer i = 0;
       integer k = -1;
       do {
           while(!~llListFindList(modifiers, llList2List(split, ++k, k))) {
               if(k == m) {//this is an error!
                   return (string)split;
               }
           }
           //string splitter = llList2String(split, k);
           //if (splitter == "%s")
           split = llListReplaceList(split, llList2List(split, i, i), k, k);
       } while(j > ++i);
       return (string)split;
   }
   return "Error, the insertation list is empty!\n(sentence: " + sentence + ")";

} </lsl>