123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- //
- // KMPageRangeTools.swift
- // PDF Master
- //
- // 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
- }
- private class func parseSelectedIndexs(selectedIndex: Array<Int>) -> String {
- if (selectedIndex.count <= 0) {
- return ""
- }
- if (selectedIndex.count == 1) {
- return "\((selectedIndex.first)!)"
- }
- if (selectedIndex.count == 2) {
- return "\((selectedIndex.first)!),\((selectedIndex.last)!)"
- }
-
- // 元素大于2个
- var string: String = ""
- var index = 0
- var prePage = 0
- var count = 0
- var cubeStart = NSNotFound
- var cubeEnd = NSNotFound
- for i in selectedIndex {
- if index == 0 {
- string.append(String(i))
- } else {
- if i - prePage == 1 {
- if count == 0 {
- cubeStart = prePage
- }
-
- count += 1
- } else {
- count = 0
- }
-
- // 最后一个元素
- if (index == selectedIndex.count-1) {
- if cubeStart != NSNotFound && cubeEnd != NSNotFound { /// 有效块
- if cubeStart == selectedIndex.first {
- string.removeFirst()
- } else {
- string.append(",")
- }
-
- if index == selectedIndex.count-1 {
- if count == 0 {
- string.append(String(cubeStart))
- string.append("-")
- string.append(String(cubeEnd))
-
- string.append(",")
- string.append(String(i))
- } else {
- string.append(String(cubeStart))
- string.append("-")
- string.append(String(i))
- }
- } else {
- string.append(String(cubeStart))
- string.append("-")
- string.append(String(cubeEnd))
- }
- } else {
- if prePage == selectedIndex.first {
-
- } else {
- string.append(",")
- string.append(String(prePage))
- }
-
- cubeStart = NSNotFound
- cubeEnd = NSNotFound
- }
- break
- }
-
- // 不是最后一个元素
- if count == 0 { /// 块结束
- if cubeStart != NSNotFound && cubeEnd != NSNotFound { /// 有效块
- if cubeStart == selectedIndex.first {
- string.removeFirst()
- } else {
- string.append(",")
- }
-
-
- if index == selectedIndex.count-1 {
- if count == 0 {
- string.append(String(cubeStart))
- string.append("-")
- string.append(String(cubeEnd))
-
- string.append(",")
- string.append(String(i))
- } else {
- string.append(String(cubeStart))
- string.append("-")
- string.append(String(i))
- }
- } else {
- string.append(String(cubeStart))
- string.append("-")
- string.append(String(cubeEnd))
- }
- } else {
- if prePage == selectedIndex.first {
- // string.append(",")
- } else {
- string.append(",")
- string.append(String(prePage))
- }
-
- cubeStart = NSNotFound
- cubeEnd = NSNotFound
- }
- } else if count >= 2 { /// 可成块
- cubeEnd = i
- }
- }
-
- prePage = i
- index += 1
- }
- return string
- }
-
- 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!
- }
- }
|