@@ -1772,71 +1772,50 @@ extension MainViewController {
17721772 }
17731773
17741774 func wrapText( _ text: String , maxLineLength: Int ) -> String {
1775- return text
1776- var lines : [ String ] = [ ]
1777- var currentLine = " "
1778-
1779- let words = text. components ( separatedBy: . whitespacesAndNewlines)
1780- for word in words {
1781- if word. count > maxLineLength {
1782- var wordToProcess = word
1783- while !wordToProcess. isEmpty {
1784- let spaceCount = currentLine. isEmpty ? 0 : 1
1785- let availableSpace = maxLineLength - ( currentLine. count + spaceCount)
1786-
1787- if availableSpace <= 0 {
1788- if !currentLine. isEmpty {
1789- lines. append ( currentLine)
1790- currentLine = " "
1791- }
1792- continue
1793- }
1775+ guard maxLineLength > 0 else {
1776+ return text
1777+ }
17941778
1795- let takeCount = min ( wordToProcess. count, availableSpace)
1796- if takeCount <= 0 {
1797- if !currentLine. isEmpty {
1798- lines. append ( currentLine)
1799- currentLine = " "
1800- }
1801- continue
1802- }
1779+ var result : [ String ] = [ ]
1780+ let lines = text. components ( separatedBy: . newlines)
18031781
1804- let index = wordToProcess. index ( wordToProcess. startIndex, offsetBy: takeCount)
1805- let substring = wordToProcess [ ..< index]
1782+ for line in lines {
1783+ var currentLine = " "
1784+ let words = line. components ( separatedBy: . whitespaces)
18061785
1807- if currentLine. isEmpty {
1808- currentLine = String ( substring)
1809- } else {
1810- currentLine += " " + substring
1786+ for word in words {
1787+ // Handles words that are longer than a single line.
1788+ if word. count > maxLineLength {
1789+ if !currentLine. isEmpty {
1790+ result. append ( currentLine)
1791+ currentLine = " "
18111792 }
18121793
1813- wordToProcess = String ( wordToProcess [ index ... ] )
1814-
1815- if currentLine . count >= maxLineLength {
1816- lines . append ( currentLine )
1817- currentLine = " "
1794+ var wordToSplit = word
1795+ while !wordToSplit . isEmpty {
1796+ let splitIndex = wordToSplit . index ( wordToSplit . startIndex , offsetBy : min ( maxLineLength, wordToSplit . count ) )
1797+ result . append ( String ( wordToSplit [ ..< splitIndex ] ) )
1798+ wordToSplit = String ( wordToSplit [ splitIndex ... ] )
18181799 }
1819- }
1820- } else {
1821- let spaceNeeded = currentLine. isEmpty ? 0 : 1
1822- if currentLine. count + spaceNeeded + word. count > maxLineLength {
1823- lines. append ( currentLine)
1824- currentLine = word
18251800 } else {
1801+ // The word fits on the line.
18261802 if currentLine. isEmpty {
18271803 currentLine = word
1828- } else {
1804+ } else if currentLine . count + word . count + 1 <= maxLineLength {
18291805 currentLine += " " + word
1806+ } else {
1807+ result. append ( currentLine)
1808+ currentLine = word
18301809 }
18311810 }
18321811 }
1833- }
18341812
1835- if !currentLine. isEmpty {
1836- lines. append ( currentLine)
1813+ if !currentLine. isEmpty {
1814+ result. append ( currentLine)
1815+ }
18371816 }
18381817
1839- return lines . joined ( separator: " \r \n " )
1818+ return result . joined ( separator: " \r \n " )
18401819 }
18411820
18421821 func formatPillText( line1: String , time: TimeInterval , line2: String ? = nil ) -> String {
0 commit comments