2 string wordwrap(string s, float l)
5 local float lleft, i, j, wlen;
10 for (i = 0;i < strlen(s);i++)
12 if (substring(s, i, 2) == "\\n")
18 else if (substring(s, i, 1) == " ")
28 for (j = i;j < strlen(s);j++)
30 c = substring(s, j, 1);
35 // we need to keep this tempstring alive even if substring is
36 // called repeatedly, so call strcat even though we're not
46 t = strcat(t, substring(s, i, wlen));
67 for (i = 0;i < strlen(s);i++)
69 c = substring(s, i, 1);
73 else if (c == "\\" && substring(s, i + 1, 1) == "n")
78 word = strcat(word, c);
83 // a space may add some whitespace to the output, and flushes the word buffer
86 if (lleft < strlen(word) + 1)
94 // otherwise just add a space if there's already text in
104 // if this is a double space, add the space
108 else if (dowhat == 1)
114 // we need to keep these tempstrings alive even if substring is
115 // called repeatedly, so call strcat even though we're not doing
137 for(i = 0; i <= strlen(s); ++i)
139 if(i != strlen(s) && substring(s, i, 1) != " ")
141 // we need to keep this tempstring alive even if substring is
142 // called repeatedly, so call strcat even though we're not doing
148 word = substring(s, startidx, i - startidx);
153 t = strcat(t, "\n\n");
156 else if(!l || (strlen(word) < lleft))
164 lleft = lleft - strlen(word);
168 t = strcat(t, "\n", word);
169 lleft = l - strlen(word);