KMPageRangeTools.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //
  2. // KMPageRangeTools.swift
  3. // PDF Master
  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. private class func parseSelectedIndexs(selectedIndex: Array<Int>) -> String {
  77. if (selectedIndex.count <= 0) {
  78. return ""
  79. }
  80. if (selectedIndex.count == 1) {
  81. return "\((selectedIndex.first)!)"
  82. }
  83. if (selectedIndex.count == 2) {
  84. return "\((selectedIndex.first)!),\((selectedIndex.last)!)"
  85. }
  86. // 元素大于2个
  87. var string: String = ""
  88. var index = 0
  89. var prePage = 0
  90. var count = 0
  91. var cubeStart = NSNotFound
  92. var cubeEnd = NSNotFound
  93. for i in selectedIndex {
  94. if index == 0 {
  95. string.append(String(i))
  96. } else {
  97. if i - prePage == 1 {
  98. if count == 0 {
  99. cubeStart = prePage
  100. }
  101. count += 1
  102. } else {
  103. count = 0
  104. }
  105. // 最后一个元素
  106. if (index == selectedIndex.count-1) {
  107. if cubeStart != NSNotFound && cubeEnd != NSNotFound { /// 有效块
  108. if cubeStart == selectedIndex.first {
  109. string.removeFirst()
  110. } else {
  111. string.append(",")
  112. }
  113. if index == selectedIndex.count-1 {
  114. if count == 0 {
  115. string.append(String(cubeStart))
  116. string.append("-")
  117. string.append(String(cubeEnd))
  118. string.append(",")
  119. string.append(String(i))
  120. } else {
  121. string.append(String(cubeStart))
  122. string.append("-")
  123. string.append(String(i))
  124. }
  125. } else {
  126. string.append(String(cubeStart))
  127. string.append("-")
  128. string.append(String(cubeEnd))
  129. }
  130. } else {
  131. if prePage == selectedIndex.first {
  132. } else {
  133. string.append(",")
  134. string.append(String(prePage))
  135. }
  136. cubeStart = NSNotFound
  137. cubeEnd = NSNotFound
  138. }
  139. break
  140. }
  141. // 不是最后一个元素
  142. if count == 0 { /// 块结束
  143. if cubeStart != NSNotFound && cubeEnd != NSNotFound { /// 有效块
  144. if cubeStart == selectedIndex.first {
  145. string.removeFirst()
  146. } else {
  147. string.append(",")
  148. }
  149. if index == selectedIndex.count-1 {
  150. if count == 0 {
  151. string.append(String(cubeStart))
  152. string.append("-")
  153. string.append(String(cubeEnd))
  154. string.append(",")
  155. string.append(String(i))
  156. } else {
  157. string.append(String(cubeStart))
  158. string.append("-")
  159. string.append(String(i))
  160. }
  161. } else {
  162. string.append(String(cubeStart))
  163. string.append("-")
  164. string.append(String(cubeEnd))
  165. }
  166. } else {
  167. if prePage == selectedIndex.first {
  168. // string.append(",")
  169. } else {
  170. string.append(",")
  171. string.append(String(prePage))
  172. }
  173. cubeStart = NSNotFound
  174. cubeEnd = NSNotFound
  175. }
  176. } else if count >= 2 { /// 可成块
  177. cubeEnd = i
  178. }
  179. }
  180. prePage = i
  181. index += 1
  182. }
  183. return string
  184. }
  185. class func newParseSelectedIndexs(selectedIndex: Array<Int>) -> String {
  186. if (selectedIndex.count == 0) {
  187. return ""
  188. }
  189. if (selectedIndex.count == 1) {
  190. return "\(selectedIndex.first!+1)"
  191. }
  192. var newDatas: Array<Int> = []
  193. for i in selectedIndex {
  194. newDatas.append(i)
  195. }
  196. /// 排序
  197. /// 根据id进行排序(升序)
  198. newDatas.sort(){$0 < $1}
  199. var a: Int = 0
  200. var b: Int = 0
  201. var result: String? = nil
  202. for i in newDatas {
  203. if (result == nil) {
  204. a = i
  205. b = i
  206. result = ""
  207. } else {
  208. if (i == b+1) {
  209. b = i
  210. if (i == newDatas.last) {
  211. result!.append(String(format: "%d-%d", a+1,b+1))
  212. }
  213. } else {
  214. if (a == b) {
  215. result!.append(String(format: "%d,", a+1))
  216. } else {
  217. result!.append(String(format: "%d-%d,", a+1,b+1))
  218. }
  219. a = i
  220. b = i
  221. if (i == newDatas.last) {
  222. result!.append(String(format: "%d", a+1))
  223. }
  224. }
  225. }
  226. }
  227. return result!
  228. }
  229. }