Workaround word boundary issue on Swift.Regex

This commit is contained in:
1024jp 2023-12-27 23:54:19 +09:00
parent 077d5e26e4
commit d81e694413
3 changed files with 10 additions and 1 deletions

View File

@ -9,6 +9,11 @@
- [non-AppStore ver.] Update Sparkle from 2.5.1 to 2.5.2.
### Fixes
- Fix an issue that the application could not open a document containing a specific pattern of letters.
4.7.1 (623)
--------------------------

View File

@ -176,7 +176,7 @@ extension String {
guard !self.isEmpty else { return nil }
let regex = /\b(charset=|encoding=|@charset|encoding:|coding:) *["']? *(?<encoding>[-_a-zA-Z0-9]+)/
let regex = /\b(charset=|encoding=|@charset|encoding:|coding:) *["']? *(?<encoding>[-_a-zA-Z0-9]+)/.wordBoundaryKind(.simple)
guard let ianaCharSetName = try? regex.firstMatch(in: self.prefix(maxLength))?.encoding else { return nil }

View File

@ -154,6 +154,10 @@ final class EncodingDetectionTests: XCTestCase {
XCTAssertEqual("<meta charset=\"utf-8\"/>".scanEncodingDeclaration(upTo: 128, suggestedCFEncodings: [utf8, shiftJISX0213, shiftJIS]),
.utf8)
// Swift.Regex with non-simple word boundaries never returns when the given string contains a specific pattern of letters (2023-12 on Swift 5.9).
XCTAssertNil("タマゴ,1,".scanEncodingDeclaration(upTo: 128, suggestedCFEncodings: []))
XCTAssertNil(try /\ba/.wordBoundaryKind(.simple).firstMatch(in: "タマゴ,1,"))
}