KMPageRangeTools.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // KMPageRangeTools.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/19.
  6. //
  7. import Cocoa
  8. class KMPageRangeTools: NSObject {
  9. class func isValidPagesString(pagesString: String)-> Bool {
  10. var valid = false
  11. for ch in pagesString {
  12. if ch != "0" && ch != "1" && ch != "2" && ch != "3" && ch != "4" && ch != "5" && ch != "6" && ch != "7" && ch != "8" && ch != "9" && ch != "," && ch != "-" {
  13. valid = false
  14. break
  15. } else {
  16. valid = true
  17. }
  18. }
  19. return valid
  20. }
  21. class func findSelectPage(pageRangeString: String, pageCount: Int) -> ([Int]) {
  22. if !isValidPagesString(pagesString: pageRangeString) {
  23. return []
  24. }
  25. var result: [Int] = []
  26. let array = pageRangeString.components(separatedBy: ",")
  27. for string in array {
  28. if string.isEmpty {
  29. return []
  30. } else {
  31. let pages = string .components(separatedBy: "-")
  32. if pages.count > 2 {
  33. return []
  34. } else if pages.count == 1 {
  35. let page = pages[0]
  36. if page.isEmpty || Int(page)! > pageCount || Int(page)! == 0 {
  37. return []
  38. } else {
  39. var hasSame: Bool = false
  40. for i in result {
  41. if i == Int(page)! {
  42. hasSame = true
  43. return []
  44. }
  45. }
  46. if !hasSame {
  47. result.append(Int(page)!)
  48. }
  49. }
  50. } else if pages.count == 2 {
  51. let page1 = pages[0]
  52. let page2 = pages[1]
  53. if page1.isEmpty || page2.isEmpty || Int(page1)! >= Int(page2)! || Int(page2)! > pageCount || Int(page1)! == 0 {
  54. return []
  55. } else {
  56. var hasSame: Bool = false
  57. for i in Int(page1)! ... Int(page2)! {
  58. for j in result {
  59. if j == i {
  60. hasSame = true
  61. return []
  62. }
  63. }
  64. }
  65. if !hasSame {
  66. for i in Int(page1)! ... Int(page2)! {
  67. result.append(i)
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. return result
  75. }
  76. class func newParseSelectedIndexs(selectedIndex: Array<Int>) -> String {
  77. if (selectedIndex.count == 0) {
  78. return ""
  79. }
  80. if (selectedIndex.count == 1) {
  81. return "\(selectedIndex.first!+1)"
  82. }
  83. var newDatas: Array<Int> = []
  84. for i in selectedIndex {
  85. newDatas.append(i)
  86. }
  87. /// 排序
  88. /// 根据id进行排序(升序)
  89. newDatas.sort(){$0 < $1}
  90. var a: Int = 0
  91. var b: Int = 0
  92. var result: String? = nil
  93. for i in newDatas {
  94. if (result == nil) {
  95. a = i
  96. b = i
  97. result = ""
  98. } else {
  99. if (i == b+1) {
  100. b = i
  101. if (i == newDatas.last) {
  102. result!.append(String(format: "%d-%d", a+1,b+1))
  103. }
  104. } else {
  105. if (a == b) {
  106. result!.append(String(format: "%d,", a+1))
  107. } else {
  108. result!.append(String(format: "%d-%d,", a+1,b+1))
  109. }
  110. a = i
  111. b = i
  112. if (i == newDatas.last) {
  113. result!.append(String(format: "%d", a+1))
  114. }
  115. }
  116. }
  117. }
  118. return result!
  119. }
  120. }