jewalky

Untitled

Mar 19th, 2017
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.49 KB | None | 0 0
  1.  
  2. function bool IsWhitespace(int c)
  3. {
  4.     return (c == '\n' || c == '\t' || c == ' ' || c == '\r');
  5. }
  6.  
  7. function str WrapLines(str input, int limit)
  8. {
  9.     int l = StrLen(input);
  10.     if (l <= limit) return input;
  11.  
  12.     str o = "";
  13.     int lastWordBoundary = -1;
  14.     str currentLine = "";
  15.     bool lastWhitespace = false;
  16.     for (int i = 0; i < l; i++)
  17.     {
  18.         int c = GetChar(input, i);
  19.         bool ws = IsWhitespace(c);
  20.         if (ws && !lastWhitespace)
  21.         {
  22.             lastWordBoundary = StrLen(currentLine);
  23.             currentLine = StrParam(s:currentLine, c:' ');
  24.             lastWhitespace = true;
  25.         }
  26.         else if (!ws)
  27.         {
  28.             if (StrLen(currentLine)+1 > limit) // string too large, wrap
  29.             {
  30.                 if (lastWordBoundary >= 0) // move current word to the next line
  31.                 {
  32.                     o = StrParam(s:o, c:'\n', s:StrMid(currentLine, 0, lastWordBoundary));
  33.                     currentLine = StrMid(currentLine, lastWordBoundary+1, -1);
  34.                 }
  35.                 else // extremely long word case
  36.                 {
  37.                     o = StrParam(s:o, c:'\n', s:currentLine);
  38.                     currentLine = "";
  39.                 }
  40.                 lastWordBoundary = -1;
  41.             }
  42.             currentLine = StrParam(s:currentLine, c:c);
  43.             lastWhitespace = false;
  44.         }
  45.     }
  46.  
  47.     if (StrLen(currentLine) > 0)
  48.         o = StrParam(s:o, c:'\n', s:currentLine);
  49.  
  50.     return o;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment