//
//  LKFString.swift
//  lkfc
//
//  Created by Alexander Holzer on 03.04.24.
//  Copyright © 2024 Alexander Holzer. All rights reserved.
//

import Foundation


class LKFString {
    static public func stringFrom (string: String, range:(Int?,Int?)) -> String {
        let start: String.Index
        
        let len = string.count
        
        guard let from = range.0 else {
            guard let to = range.1, to < len else {
                return string
            }
            let end = string.index(string.startIndex, offsetBy: to)
            return String(string[..<end])
        }
        
        if len < from {
            return ""
        }
        
        if from == 0 {
            start = string.startIndex
        }
        else {
            start = string.index(string.startIndex, offsetBy: from)
        }
        
        guard let to = range.1, to < len else {
            return String(string[start...])
        }
        
        let end = string.index(string.startIndex, offsetBy: to)
        return String(string[start..<end])
    }

}

extension String {
    func substr(from: Int? = nil, to: Int? = nil) -> String? {
        return LKFString.stringFrom(string: self, range: (from, to))
    }
    
    func substr(from: Int, length: Int) -> String? {
        return LKFString.stringFrom(string: self, range: (from, from + length))
    }
}
