/// <summary> /// Warps one long line to sevaral short lines (<=len), additional spaces are preserved /// </summary> /// <param name="line"></param> /// <param name="len"></param> /// <returns></returns> private List <string> WrapLine(string line, int len) { if (string.IsNullOrEmpty(line) || line.Length <= len) { return(new List <string>() { line }); } List <string> newLines = new List <string>(); // char lastChar = '\0'; while (line.Length >= len) { int pos = -1; for (int i = len - 1; i > 0; i--) { DelimiterItem delim = _delimiters.Find(d => d.Char == line[i]); if (delim != null) { WrapMode wrapMode = delim.WrapMode; if (wrapMode == WrapMode.Both) { char charBefore = line[i - 1]; wrapMode = charBefore == ' ' ? WrapMode.Before : WrapMode.After; } pos = wrapMode == WrapMode.Before ? i : i + 1; break; } } if (pos == -1) { pos = len - 1; } if (newLines.Count == 0) { // keep the indentation in the first line line = line.TrimEnd(); } else { // no indentation in wrapped lines line = line.Trim(); } newLines.Add(line.Substring(0, pos)); line = line.Substring(pos).Trim(); } if (line.Length > 0) { newLines.Add(line); } return(newLines); }
/// <summary> /// Warps one long line to sevaral short lines (<=len), additional spaces are preserved /// </summary> /// <param name="line"></param> /// <param name="len"></param> /// <returns></returns> private List <string> WrapLine(string line, int len) { if (string.IsNullOrEmpty(line) || line.Length <= len) { return(new List <string>() { line }); } List <string> newLines = new List <string>(); while (line.Length >= len) { int pos = -1; for (int i = len - 1; i > 0; i--) { //Debug.WriteLine(line[i]); DelimiterItem delim = _delimiters.Find(d => d.Char == line[i]); if (delim != null) { pos = delim.WrapBefore ? i : i + 1; //Debug.WriteLine($"delim {line[i]}, pos={pos}"); break; } } if (pos == -1) { pos = len - 1; } //Debug.WriteLine($"newline '{line}'"); //Debug.WriteLine($"pos={pos}, sub='{line.Substring(0, pos)}'"); newLines.Add(line.Substring(0, pos)); line = line.Substring(pos); //Debug.WriteLine($"line='{line}'"); } if (line.Length > 0) { newLines.Add(line); } return(newLines); }