ReadViewSDK/Sources/RDReaderView/EPUBCore/CFI/RDEPUBCFIPath.swift
shenlei f50495ad91 fix: CFI module — 11 issues from code review
Phase 1 (correctness):
- #1: rawValue changed from stored to computed property, eliminating stale cache risk
- #2: tokenSamples/textMarker unified to UTF-16 offsets, fixing emoji/CJK positioning
- #3: makeOffsetCFI now accepts optional contentPath parameter

Phase 2 (robustness):
- #4: Resolver uses fixed index access instead of last(where:) for manifest/spine steps
- #5: HTML comments and CDATA stripped before regex matching
- #6: Text assertion parser handles backslash escapes (\[ \] \)
- #7: Token matching uses prefix/suffix with length ratio constraints
- #8: makeOffsetRangeCFI validates startOffset <= endOffset

Phase 3 (code quality):
- #9: Shared internal nilIfEmpty extension in RDEPUBCFIUtilities.swift
- #10: RDEPUBCFIMap has markerByPath index dictionary for O(1) lookup
- #11: parseRange unified to use try (not try?) for parent parsing

Review fixes:
- Documented init(rawValue:) parameter is ignored (computed property)
- Fixed escape unescaping to handle \\ → \ correctly
- Lowered token minLength threshold from 4 to 2

Co-Authored-By: Claude <noreply@anthropic.com>
2026-06-18 13:25:17 +08:00

40 lines
1.2 KiB
Swift

import Foundation
public struct RDEPUBCFIPath: Codable, Equatable, Hashable {
public var steps: [RDEPUBCFIStep]
public init(steps: [RDEPUBCFIStep] = []) {
self.steps = steps
}
public func commonPrefix(with other: RDEPUBCFIPath) -> RDEPUBCFIPath {
var prefix: [RDEPUBCFIStep] = []
let upperBound = min(steps.count, other.steps.count)
for index in 0..<upperBound {
guard steps[index] == other.steps[index] else { break }
prefix.append(steps[index])
}
return RDEPUBCFIPath(steps: prefix)
}
public func droppingPrefix(_ prefix: RDEPUBCFIPath) -> RDEPUBCFIPath {
guard prefix.steps.count <= steps.count else { return self }
let candidate = Array(steps.prefix(prefix.steps.count))
guard candidate == prefix.steps else { return self }
return RDEPUBCFIPath(steps: Array(steps.dropFirst(prefix.steps.count)))
}
}
public struct RDEPUBCFIStep: Codable, Equatable, Hashable {
public var index: Int
public var idAssertion: String?
public init(index: Int, idAssertion: String? = nil) {
self.index = index
self.idAssertion = idAssertion?.nilIfEmpty
}
}