Advertisement
ivandrofly

Balance lines

Sep 19th, 2023
1,054
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. private static string Balance(string text, int singleLineMaxLength = 42)
  2.     {
  3.         // the quick grown fox jumps over a lazy dog! the quick grown fox jumps over a lazy dog! the quick grown fox jumps over a lazy dog!
  4.         // note: the text won't have any tag!
  5.         // find first white-space going backward (ignore tags)
  6.         // find first white-space going forward  (ignore tags)
  7.         var len = text.Length;
  8.         var sb = new StringBuilder();
  9.         var from = 0;
  10.  
  11.         var lineCount = len / singleLineMaxLength;
  12.  
  13.         // calculate the amount of line required to fit all the text basing on the maximum single line length
  14.         // var lineCount = Math.Round(len / Convert.ToDouble(singleLineMaxLength), MidpointRounding.AwayFromZero);
  15.  
  16.         const char whiteSpace = ' ';
  17.         for (var i = 0; i < lineCount; i++)
  18.         {
  19.             // 1. calculate the pivot position (where the 'pivot' will be place to start looking for back/forth white-space index).
  20.             // 2. (x) + 1 : if the pivot is in a position that is not white-space we want to jump once
  21.             // so after we start looking to find it with 'prePivotWhiteSpace--'
  22.             // but after adding the find for white-space in front it's not requited anymore
  23.             // 3. (i + 1) is to move 'singleLineMaxLength' to the next position of same length to handle second line.
  24.             var pivot = singleLineMaxLength * (i + 1) + 1;
  25.  
  26.             // find first whitespace before pivot
  27.             var prePivotWhiteSpace = pivot;
  28.             while (prePivotWhiteSpace > 0 && text[prePivotWhiteSpace] != whiteSpace) prePivotWhiteSpace--;
  29.  
  30.             // find first white-space after pivot
  31.             var postPivotWhiteSpace = pivot;
  32.             while (postPivotWhiteSpace < len && text[postPivotWhiteSpace] != whiteSpace) postPivotWhiteSpace++;
  33.  
  34.             // get closest point to pivot/avg position
  35.             var splitPosition = Math.Abs(pivot - prePivotWhiteSpace) < Math.Abs(pivot - postPivotWhiteSpace)
  36.                 ? prePivotWhiteSpace
  37.                 : postPivotWhiteSpace;
  38.  
  39.             // store text
  40.             // note: this will require trimming the end to remove last line if +2 line were appended
  41.             sb.AppendLine(text.Substring(from, splitPosition - from));
  42.  
  43.             // calculate next from
  44.             from = splitPosition + 1;
  45.         }
  46.  
  47.         // handle the remaining if any
  48.         if (from < len)
  49.         {
  50.             sb.Append(text[from..]);
  51.         }
  52.  
  53.         return sb.ToString();
  54.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement