             let textContainer = self.textContainer
             else { return assertionFailure() }
         
-        let selectedLineRanges = (self.rangesForUserTextChange ?? self.selectedRanges)
-            .map { (self.string as NSString).lineRange(for: $0.rangeValue) }
+        let string = self.string as NSString
+        let selectedRanges = (self.rangesForUserTextChange ?? self.selectedRanges).map { $0.rangeValue }
         
         // get glyph range of which line number should be drawn
         // -> Requires additionalLayout to obtain glyphRange for markedText. (2018-12 macOS 10.14 SDK)

        
        // count up lines until the interested area
        let firstIndex = layoutManager.characterIndexForGlyph(at: glyphRangeToDraw.location)
        var lineNumber = (self.string as NSString).lineNumber(at: firstIndex)
        
        // enumerate visible line numbers
        var glyphIndex = glyphRangeToDraw.location
        while glyphIndex < glyphRangeToDraw.upperBound {  // process logical lines
            let characterIndex = layoutManager.characterIndexForGlyph(at: glyphIndex)
            let lineRange = self.string.lineRange(at: characterIndex)
            let lineGlyphRange = layoutManager.glyphRange(forCharacterRange: lineRange, actualCharacterRange: nil)
            let isSelected = selectedLineRanges.contains { $0.intersection(lineRange) != nil }
            glyphIndex = lineGlyphRange.upperBound
            
            var wrappedLineGlyphIndex = max(lineGlyphRange.location, glyphRangeToDraw.lowerBound)

            (layoutRect.minY...layoutRect.maxY).overlaps(extraLineRect.minY...extraLineRect.maxY)
            else { return }
        
        let lastLineNumber = (lineNumber > 1) ? lineNumber : string.lineNumber(at: string.length)
        let isSelected = (selectedRanges.last?.location == string.length)
        
        body(.new(lastLineNumber, isSelected), extraLineRect)
    }
