<commit_before>//
//  StringExtensions.swift
//  ULima
//
//  Created by <NAME> on 2/4/15.
//  Copyright (c) 2015 is.oto.pe. All rights reserved.
//

import Foundation

public extension String {
    
    public var localized: String {
        return NSLocalizedString(self, comment: "")
    }
    
    public var length: Int {
        return self.count
    }
    
    public func substring(range: Range<Int>) -> String {
        let startIndex = self.index(self.startIndex, offsetBy: range.lowerBound)
        let endIndex = self.index(startIndex, offsetBy: range.upperBound - range.lowerBound)
        return String(self[startIndex..<endIndex])
    }
    
    public func substring(start: Int) -> String {
        return substring(start: start, end: self.count)
    }
    
    public func substring(end: Int) -> String {
        return substring(start: 0, end: end)
    }
    
    public func substring(start: Int, end: Int) -> String {
        let startIndex = self.index(self.startIndex, offsetBy: start)
        let endIndex = self.index(self.startIndex, offsetBy: end)
        return String(self[startIndex..<endIndex])
    }
    
    public func substring(start: Index) -> String {
        return String(self[start..<self.endIndex])
    }
    
    public func substring(end: Index) -> String {
        return String(self[startIndex..<end])
    }
    
    public func substring(start: Index, end: Index) -> String {
        return String(self[start..<end])
    }
    
    public func substring(start: String, end: String) -> String? {
        if let startRange = range(of: start) {
            let newString = self.substring(start: startRange.upperBound)
            if let endRange = newString.range(of: end) {
                return newString.substring(end: endRange.lowerBound)
            }
        }
        return nil
    }
    
    public func substring(start: String) -> String? {
        if let startRange = range(of: start) {
            return substring(start: startRange.upperBound)
        }
        return nil
    }
    
    public func substring(end: String) -> String? {
        if let endRange = range(of: end) {
            return substring(end: endRange.lowerBound)
        }
        return nil
    }
    
    public func trimmed() -> String {
        return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)

    }
    
//    public func trimmed() -> String {
//        return self.trimmed
//    }
    
    
    @available(*, deprecated: 2.0, message: "Use cleaned() instead", renamed: "clean(minSize:)")
    public func clean(minSize: Int = 1) -> String? {
        let trimmedSelf = trimmed()
        return (trimmedSelf.count >= minSize ? trimmedSelf : nil)
    }
    
    public func cleaned(minSize: Int = 1) -> String? {
        let trimmedSelf = trimmed()
        return (trimmedSelf.count >= minSize ? trimmedSelf : nil)
    }
    
    public mutating func capitalizeFirst() {
        self.uppercaseFirst()
    }
    
    public mutating func uppercaseFirst() {
        guard self.count > 0 else { return }
        self.replaceSubrange(startIndex...startIndex, with: String(self[startIndex]).uppercased())
    }
    
    public mutating func lowercaseFirst() {
        guard self.count > 0 else { return }
        self.replaceSubrange(startIndex...startIndex, with: String(self[startIndex]).lowercased())
    }
    
    public var capitalizedFirst: String {
        return self.uppercasedFirst()
    }
    
    public func uppercasedFirst() -> String {
        guard self.count > 0 else { return self }
        let first = self.substring(end: 1)
        let rest = self.substring(start: 1)
        return first.uppercased()+rest
    }
    
    public func lowercasedFirst() -> String {
        guard self.count > 0 else { return self }
        let first = self.substring(end: 1)
        let rest = self.substring(start: 1)
        return first.lowercased()+rest
    }
    
    public func indexOf(_ target: String) -> Int? {
        let range = self.range(of: target)
        if let range = range {
            return self.distance(from: self.startIndex, to: range.lowerBound)
        } else {
            return nil
        }
    }
    
    public func indexOf(_ target: String, startIndex: Int) -> Int? {
        let startRange = self.index(self.startIndex, offsetBy: startIndex)
        let range = self.range(of: target, options: NSString.CompareOptions.literal, range: Range<String.Index>(startRange..<self.endIndex))
        if let range = range {
            return self.distance(from: self.startIndex, to: range.lowerBound)
        } else {
            return nil
        }
    }
    
    public func lastIndexOf(_ target: String) -> Int? {
        var index: Int?
        var stepIndex = self.indexOf(target) ?? -1
        while stepIndex > -1 {
            index = stepIndex
            if stepIndex + target.count < self.count {
                stepIndex = indexOf(target, startIndex: stepIndex + target.count) ?? -1
            } else {
                stepIndex = -1
            }<commit_msg>Compatibility with Swift 4.2<commit_after>//
//  StringExtensions.swift
//  ULima
//
//  Created by <NAME> on 2/4/15.
//  Copyright (c) 2015 is.oto.pe. All rights reserved.
//

import Foundation

public extension String {
    
    public var localized: String {
        return NSLocalizedString(self, comment: "")
    }
    
    public var length: Int {
        return self.count
    }
    
    public func substring(range: Range<Int>) -> String {
        let startIndex = self.index(self.startIndex, offsetBy: range.lowerBound)
        let endIndex = self.index(startIndex, offsetBy: range.upperBound - range.lowerBound)
        return String(self[startIndex..<endIndex])
    }
    
    public func substring(start: Int) -> String {
        return substring(start: start, end: self.count)
    }
    
    public func substring(end: Int) -> String {
        return substring(start: 0, end: end)
    }
    
    public func substring(start: Int, end: Int) -> String {
        let startIndex = self.index(self.startIndex, offsetBy: start)
        let endIndex = self.index(self.startIndex, offsetBy: end)
        return String(self[startIndex..<endIndex])
    }
    
    public func substring(start: Index) -> String {
        return String(self[start..<self.endIndex])
    }
    
    public func substring(end: Index) -> String {
        return String(self[startIndex..<end])
    }
    
    public func substring(start: Index, end: Index) -> String {
        return String(self[start..<end])
    }
    
    public func substring(start: String, end: String) -> String? {
        if let startRange = range(of: start) {
            let newString = self.substring(start: startRange.upperBound)
            if let endRange = newString.range(of: end) {
                return newString.substring(end: endRange.lowerBound)
            }
        }
        return nil
    }
    
    public func substring(start: String) -> String? {
        if let startRange = range(of: start) {
            return substring(start: startRange.upperBound)
        }
        return nil
    }
    
    public func substring(end: String) -> String? {
        if let endRange = range(of: end) {
            return substring(end: endRange.lowerBound)
        }
        return nil
    }
    
    public func trimmed() -> String {
        return self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)

    }
    
//    public func trimmed() -> String {
//        return self.trimmed
//    }
    
    
    @available(*, deprecated: 2.0, message: "Use cleaned() instead", renamed: "clean(minSize:)")
    public func clean(minSize: Int = 1) -> String? {
        let trimmedSelf = trimmed()
        return (trimmedSelf.count >= minSize ? trimmedSelf : nil)
    }
    
    public func cleaned(minSize: Int = 1) -> String? {
        let trimmedSelf = trimmed()
        return (trimmedSelf.count >= minSize ? trimmedSelf : nil)
    }
    
    public mutating func capitalizeFirst() {
        self.uppercaseFirst()
    }
    
    public mutating func uppercaseFirst() {
        guard self.count > 0 else { return }
        self.replaceSubrange(startIndex...startIndex, with: String(self[startIndex]).uppercased())
    }
    
    public mutating func lowercaseFirst() {
        guard self.count > 0 else { return }
        self.replaceSubrange(startIndex...startIndex, with: String(self[startIndex]).lowercased())
    }
    
    public var capitalizedFirst: String {
        return self.uppercasedFirst()
    }
    
    public func uppercasedFirst() -> String {
        guard self.count > 0 else { return self }
        let first = self.substring(end: 1)
        let rest = self.substring(start: 1)
        return first.uppercased()+rest
    }
    
    public func lowercasedFirst() -> String {
        guard self.count > 0 else { return self }
        let first = self.substring(end: 1)
        let rest = self.substring(start: 1)
        return first.lowercased()+rest
    }
    
    public func indexOf(_ target: String) -> Int? {
        let range = self.range(of: target)
        if let range = range {
            return self.distance(from: self.startIndex, to: range.lowerBound)
        } else {
            return nil
        }
    }
    
    public func indexOf(_ target: String, startIndex: Int) -> Int? {
        let startRange = self.index(self.startIndex, offsetBy: startIndex)
        let range = self.range(of: target, options: NSString.CompareOptions.literal, range: startRange..<self.endIndex)
        if let range = range {
            return self.distance(from: self.startIndex, to: range.lowerBound)
        } else {
            return nil
        }
    }
    
    public func lastIndexOf(_ target: String) -> Int? {
        var index: Int?
        var stepIndex = self.indexOf(target) ?? -1
        while stepIndex > -1 {
            index = stepIndex
            if stepIndex + target.count < self.count {
                stepIndex = indexOf(target, startIndex: stepIndex + target.count) ?? -1
            } else {
                stepIndex = -1
            }
{'fraction_non_alphanumeric': 0.10873440285204991, 'fraction_numerical': 0.005149534561299267, 'mean_word_length': 2.0940563725490198, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6486593', 'n_tokens_mistral': 2953, 'n_tokens_neox': 2877, 'n_words': 763}