KMDataVersionManager.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // KMDataVersionManager.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by liujiajie on 2023/11/7.
  6. //
  7. import Foundation
  8. let KMHeaderFooterManagerVersion = "KMHeaderFooterManagerVersion"
  9. class KMDataVersionManager: NSObject{
  10. class func updateBatesData() -> Bool {
  11. var defaults = UserDefaults.standard
  12. if (defaults.object(forKey: KMHeaderFooterManagerVersion) != nil) {
  13. defaults.setValue("1", forKey: KMHeaderFooterManagerVersion)
  14. return true
  15. }
  16. return false
  17. }
  18. class func refrshBatesData(bates: KMHeaderFooterObject?) -> KMHeaderFooterObject? {
  19. if bates == nil {
  20. return nil
  21. }
  22. var str: String
  23. if bates?.batesDigits ?? 0 > 0 {
  24. str = String(format: "%@<<%@,%ld>>%@", bates!.batesPrefixString, bates!.startString, bates!.batesDigits, bates!.batesSuffixString)
  25. } else {
  26. str = String(format: "%@<<%@>>%@", bates!.batesPrefixString, bates!.startString, bates!.batesSuffixString)
  27. }
  28. bates!.topLeftString = KMDataVersionManager.newBateString(oldString: bates!.topLeftString, bates: bates!, match: str)
  29. bates!.topCenterString = KMDataVersionManager.newBateString(oldString: bates!.topCenterString, bates: bates!, match: str)
  30. bates!.topRightString = KMDataVersionManager.newBateString(oldString: bates!.topRightString, bates: bates!, match: str)
  31. bates!.bottomLeftString = KMDataVersionManager.newBateString(oldString: bates!.bottomLeftString, bates: bates!, match: str)
  32. bates!.bottomCenterString = KMDataVersionManager.newBateString(oldString: bates!.bottomCenterString, bates: bates!, match: str)
  33. bates!.bottomRightString = KMDataVersionManager.newBateString(oldString: bates!.bottomRightString, bates: bates!, match: str)
  34. return bates
  35. }
  36. class func newBateString(oldString: String, bates: KMHeaderFooterObject, match: String) -> String {
  37. let regex = try! NSRegularExpression(pattern: match, options: [])
  38. let results = regex.matches(in: oldString, options: [], range: NSRange(location: 0, length: oldString.count))
  39. var newString = oldString
  40. for object in results {
  41. let range = object.range
  42. var tString: String
  43. if bates.batesDigits > 0 {
  44. tString = "<<#(bates.batesDigits)#(bates.startString)"
  45. } else {
  46. tString = "<<#(bates.startString)"
  47. }
  48. if bates.batesPrefixString.count > 0 {
  49. tString += "#\(bates.batesPrefixString)"
  50. } else if bates.batesPrefixString.count < 1 && bates.batesSuffixString.count > 0 {
  51. tString += "#"
  52. }
  53. if bates.batesSuffixString.count > 0 {
  54. tString += "#\(bates.batesSuffixString)"
  55. }
  56. tString += ">>"
  57. let resultStr = (oldString as NSString).substring(with: range)
  58. newString = newString.replacingOccurrences(of: resultStr, with: tString)
  59. }
  60. return newString
  61. }
  62. }