Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function bool IsWhitespace(int c)
- {
- return (c == '\n' || c == '\t' || c == ' ' || c == '\r');
- }
- function str WrapLines(str input, int limit)
- {
- int l = StrLen(input);
- if (l <= limit) return input;
- str o = "";
- int lastWordBoundary = -1;
- str currentLine = "";
- bool lastWhitespace = false;
- for (int i = 0; i < l; i++)
- {
- int c = GetChar(input, i);
- bool ws = IsWhitespace(c);
- if (ws && !lastWhitespace)
- {
- lastWordBoundary = StrLen(currentLine);
- currentLine = StrParam(s:currentLine, c:' ');
- lastWhitespace = true;
- }
- else if (!ws)
- {
- if (StrLen(currentLine)+1 > limit) // string too large, wrap
- {
- if (lastWordBoundary >= 0) // move current word to the next line
- {
- o = StrParam(s:o, c:'\n', s:StrMid(currentLine, 0, lastWordBoundary));
- currentLine = StrMid(currentLine, lastWordBoundary+1, -1);
- }
- else // extremely long word case
- {
- o = StrParam(s:o, c:'\n', s:currentLine);
- currentLine = "";
- }
- lastWordBoundary = -1;
- }
- currentLine = StrParam(s:currentLine, c:c);
- lastWhitespace = false;
- }
- }
- if (StrLen(currentLine) > 0)
- o = StrParam(s:o, c:'\n', s:currentLine);
- return o;
- }
Advertisement
Add Comment
Please, Sign In to add comment