// // KMNTools.swift // PDF Reader Pro // // Created by 丁林圭 on 2024/10/24. // import Cocoa class KMNTools: NSObject { class func indexpathsToIndexs(indexpaths: Set) -> IndexSet { var indexs = IndexSet() for indexPath in indexpaths { indexs.insert(indexPath.item) } return indexs } class func indexsToIndexpaths(indexs: IndexSet) -> Set { var indexpaths = Set() for index in indexs { indexpaths.insert(IndexPath(item: index, section: 0)) } return indexpaths } class func newParseSelectedIndexs(selectedIndex: Array) -> String { if (selectedIndex.count == 0) { return "" } if (selectedIndex.count == 1) { return "\(selectedIndex.first!+1)" } var newDatas: Array = [] for i in selectedIndex { newDatas.append(i) } /// 排序 /// 根据id进行排序(升序) newDatas.sort(){$0 < $1} var a: Int = 0 var b: Int = 0 var result: String? = nil for i in newDatas { if (result == nil) { a = i b = i result = "" } else { if (i == b+1) { b = i if (i == newDatas.last) { result!.append(String(format: "%d-%d", a+1,b+1)) } } else { if (a == b) { result!.append(String(format: "%d,", a+1)) } else { result!.append(String(format: "%d-%d,", a+1,b+1)) } a = i b = i if (i == newDatas.last) { result!.append(String(format: "%d", a+1)) } } } } return result! } @objc class func parseIndexPathsSet(indexSets: Set) -> String { var indexSet = IndexSet() for indeIndexPath in indexSets { indexSet.insert(indeIndexPath.item) } return self.parseIndexs(indexs: indexSet.sorted()) } @objc class func parseIndexSet(indexSet: IndexSet) -> String { return self.parseIndexs(indexs: indexSet.sorted()) } @objc class func parseIndexs(indexs: [Int]) -> String { if (indexs.isEmpty) { return "" } if (indexs.count == 1) { return "\(indexs.first!+1)" } var sortArray: [Int] = [] for i in indexs { sortArray.append(i) } /// 排序 (升序) sortArray.sort(){$0 < $1} var a: Int = 0 var b: Int = 0 var result: String? for i in sortArray { if (result == nil) { a = i b = i result = "" continue } if (i == b+1) { b = i if (i == sortArray.last) { result?.append(String(format: "%d-%d", a+1,b+1)) } } else { if (a == b) { result?.append(String(format: "%d,", a+1)) } else { result?.append(String(format: "%d-%d,", a+1,b+1)) } a = i b = i if (i == sortArray.last) { result?.append(String(format: "%d", a+1)) } } } return result ?? "" } class func isValidateEmail(_ email: String) -> Bool { let emailRegex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" let emailTest = NSPredicate(format: "SELF MATCHES %@", emailRegex) return emailTest.evaluate(with: email) } class func judgeEmailURL(_ urlString: String) -> String { var modifiedURLString = urlString if !modifiedURLString.hasPrefix("mailto:") { modifiedURLString = "mailto:" + modifiedURLString } if modifiedURLString == "mailto:" { modifiedURLString = "" } return modifiedURLString } class func judgeWebURL(_ urlString: String) -> String { var modifiedURLString = urlString modifiedURLString = modifiedURLString.replacingOccurrences(of: "\n", with: "") modifiedURLString = modifiedURLString.replacingOccurrences(of: " ", with: "") if !modifiedURLString.hasPrefix("http://") && !modifiedURLString.hasPrefix("https://") { if modifiedURLString.hasPrefix("smb://") { } else { modifiedURLString = "https://" + modifiedURLString } } if modifiedURLString.hasSuffix(".com") == false { modifiedURLString = modifiedURLString + ".com" } if modifiedURLString == "https://.com" { modifiedURLString = "" } return modifiedURLString } }