<commit_before>//
//  LineRangeCacheable.swift
//
//  CotEditor
//  https://coteditor.com
//
//  Created by 1024jp on 2020-02-11.
//
//  ---------------------------------------------------------------------------
//
//  © 2020 1024jp
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  https://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

import Foundation

protocol LineRangeCacheable: AnyObject {
    
    var string: String { get }
    var lineStartIndexes: IndexSet { get set }
    var firstLineUncoundedIndex: Int { get set }
    
    // This method must be invoked every time when the receiver's `.string` is updated.
    func invalidateLineRanges(from index: Int)
}


extension LineRangeCacheable {
    
    // MARK: Public Methods
    
    /// Return the 1-based line number at the given character index.
    ///
    /// - Parameter index: The character index.
    /// - Returns: The 1-based line number.
    func lineNumber(at index: Int) -> Int {
        
        assert(index <= self.string.length)
        
        self.ensureLineRanges(upTo: index)
        
        return self.lineStartIndexes.count(in: ...index) + 1
    }
    
    
    /// Return the range of the line touched by the given index.
    ///
    /// Because this method count up all the line ranges up to the given index when not cached yet,
    /// there is a large perormance disadbantage when just a single line range is needed.
    ///
    /// - Parameter index: The character index.
    /// - Returns: The characer range of the line.
    func lineRange(at index: Int) -> NSRange {
        
        assert(index <= self.string.length)
        
        self.ensureLineRanges(upTo: index)
        
        let lowerBound = self.lineStartIndexes.integerLessThanOrEqualTo(index) ?? 0
        let upperBound = self.lineStartIndexes.integerGreaterThan(index) ?? self.string.length
        
        return NSRange(location: lowerBound, length: upperBound - lowerBound)
    }<commit_msg>Specify range start in lineNumber(at:)<commit_after>//
//  LineRangeCacheable.swift
//
//  CotEditor
//  https://coteditor.com
//
//  Created by 1024jp on 2020-02-11.
//
//  ---------------------------------------------------------------------------
//
//  © 2020 1024jp
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  https://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

import Foundation

protocol LineRangeCacheable: AnyObject {
    
    var string: String { get }
    var lineStartIndexes: IndexSet { get set }
    var firstLineUncoundedIndex: Int { get set }
    
    // This method must be invoked every time when the receiver's `.string` is updated.
    func invalidateLineRanges(from index: Int)
}


extension LineRangeCacheable {
    
    // MARK: Public Methods
    
    /// Return the 1-based line number at the given character index.
    ///
    /// - Parameter index: The character index.
    /// - Returns: The 1-based line number.
    func lineNumber(at index: Int) -> Int {
        
        assert(index <= self.string.length)
        
        self.ensureLineRanges(upTo: index)
        
        return self.lineStartIndexes.count(in: 0...index) + 1
    }
    
    
    /// Return the range of the line touched by the given index.
    ///
    /// Because this method count up all the line ranges up to the given index when not cached yet,
    /// there is a large perormance disadbantage when just a single line range is needed.
    ///
    /// - Parameter index: The character index.
    /// - Returns: The characer range of the line.
    func lineRange(at index: Int) -> NSRange {
        
        assert(index <= self.string.length)
        
        self.ensureLineRanges(upTo: index)
        
        let lowerBound = self.lineStartIndexes.integerLessThanOrEqualTo(index) ?? 0
        let upperBound = self.lineStartIndexes.integerGreaterThan(index) ?? self.string.length
        
        return NSRange(location: lowerBound, length: upperBound - lowerBound)
    }
{'fraction_non_alphanumeric': 0.12407711238720262, 'fraction_numerical': 0.011689909762100083, 'mean_word_length': 2.1202815099168264, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3618992', 'n_tokens_mistral': 1367, 'n_tokens_neox': 1311, 'n_words': 488}