123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- //
- // KMPageRangeTools.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2022/12/19.
- //
- import Cocoa
- class KMPageRangeTools: NSObject {
- class func isValidPagesString(pagesString: String)-> Bool {
- var valid = false
- for ch in pagesString {
- if ch != "0" && ch != "1" && ch != "2" && ch != "3" && ch != "4" && ch != "5" && ch != "6" && ch != "7" && ch != "8" && ch != "9" && ch != "," && ch != "-" {
- valid = false
- break
- } else {
- valid = true
- }
- }
-
- return valid
- }
-
- class func findSelectPage(pageRangeString: String, pageCount: Int) -> ([Int]) {
- if !isValidPagesString(pagesString: pageRangeString) {
- return []
- }
-
- var result: [Int] = []
- let array = pageRangeString.components(separatedBy: ",")
- for string in array {
- if string.isEmpty {
- return []
- } else {
- let pages = string .components(separatedBy: "-")
- if pages.count > 2 {
- return []
- } else if pages.count == 1 {
- let page = pages[0]
- if page.isEmpty || Int(page)! > pageCount || Int(page)! == 0 {
- return []
- } else {
- var hasSame: Bool = false
- for i in result {
- if i == Int(page)! {
- hasSame = true
- return []
- }
- }
- if !hasSame {
- result.append(Int(page)!)
- }
- }
- } else if pages.count == 2 {
- let page1 = pages[0]
- let page2 = pages[1]
- if page1.isEmpty || page2.isEmpty || Int(page1)! >= Int(page2)! || Int(page2)! > pageCount || Int(page1)! == 0 {
- return []
- } else {
- var hasSame: Bool = false
- for i in Int(page1)! ... Int(page2)! {
- for j in result {
- if j == i {
- hasSame = true
- return []
- }
- }
- }
- if !hasSame {
- for i in Int(page1)! ... Int(page2)! {
- result.append(i)
- }
- }
- }
- }
- }
- }
-
- return result
- }
-
- class func newParseSelectedIndexs(selectedIndex: Array<Int>) -> String {
- if (selectedIndex.count == 0) {
- return ""
- }
-
- if (selectedIndex.count == 1) {
- return "\(selectedIndex.first!+1)"
- }
-
- var newDatas: Array<Int> = []
- 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!
- }
- }
|