123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- //
- // KMDataVersionManager.swift
- // PDF Reader Pro
- //
- // Created by liujiajie on 2023/11/7.
- //
- import Foundation
- let KMHeaderFooterManagerVersion = "KMHeaderFooterManagerVersion"
- class KMDataVersionManager: NSObject{
- class func updateBatesData() -> Bool {
- var defaults = UserDefaults.standard
- if (defaults.object(forKey: KMHeaderFooterManagerVersion) != nil) {
- defaults.setValue("1", forKey: KMHeaderFooterManagerVersion)
- return true
- }
- return false
- }
-
- class func refrshBatesData(bates: KMHeaderFooterObject?) -> KMHeaderFooterObject? {
- if bates == nil {
- return nil
- }
- var str: String
- if bates?.batesDigits ?? 0 > 0 {
- str = String(format: "%@<<%@,%ld>>%@", bates!.batesPrefixString, bates!.startString, bates!.batesDigits, bates!.batesSuffixString)
- } else {
- str = String(format: "%@<<%@>>%@", bates!.batesPrefixString, bates!.startString, bates!.batesSuffixString)
- }
- bates!.topLeftString = KMDataVersionManager.newBateString(oldString: bates!.topLeftString, bates: bates!, match: str)
- bates!.topCenterString = KMDataVersionManager.newBateString(oldString: bates!.topCenterString, bates: bates!, match: str)
- bates!.topRightString = KMDataVersionManager.newBateString(oldString: bates!.topRightString, bates: bates!, match: str)
- bates!.bottomLeftString = KMDataVersionManager.newBateString(oldString: bates!.bottomLeftString, bates: bates!, match: str)
- bates!.bottomCenterString = KMDataVersionManager.newBateString(oldString: bates!.bottomCenterString, bates: bates!, match: str)
- bates!.bottomRightString = KMDataVersionManager.newBateString(oldString: bates!.bottomRightString, bates: bates!, match: str)
- return bates
- }
- class func newBateString(oldString: String, bates: KMHeaderFooterObject, match: String) -> String {
- let regex = try! NSRegularExpression(pattern: match, options: [])
- let results = regex.matches(in: oldString, options: [], range: NSRange(location: 0, length: oldString.count))
- var newString = oldString
- for object in results {
- let range = object.range
- var tString: String
- if bates.batesDigits > 0 {
- tString = "<<#(bates.batesDigits)#(bates.startString)"
- } else {
- tString = "<<#(bates.startString)"
- }
- if bates.batesPrefixString.count > 0 {
- tString += "#\(bates.batesPrefixString)"
- } else if bates.batesPrefixString.count < 1 && bates.batesSuffixString.count > 0 {
- tString += "#"
- }
- if bates.batesSuffixString.count > 0 {
- tString += "#\(bates.batesSuffixString)"
- }
- tString += ">>"
- let resultStr = (oldString as NSString).substring(with: range)
- newString = newString.replacingOccurrences(of: resultStr, with: tString)
- }
- return newString
- }
- }
|