From 4092585ab3eea9de6c2a92024d3ab5ec3bfc4785 Mon Sep 17 00:00:00 2001 From: rinsuki <428rinsuki+git@gmail.com> Date: Sun, 2 Feb 2025 12:19:55 +0900 Subject: [PATCH] Fix issues around Xcode 16, and rename to parse(...) from init(...) We shouldn't depends to pointer is still valid after withUnsafe~'s callback is end. but `convenience init` will not allow us to call self.init inside of withUnsafe~'s callback, this is why we need to rename to parse(...) from init(...) --- Sources/Document.swift | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/Sources/Document.swift b/Sources/Document.swift index 299150f..5289470 100644 --- a/Sources/Document.swift +++ b/Sources/Document.swift @@ -74,11 +74,15 @@ open class XMLDocument { - returns: An `XMLDocument` with the contents of the specified XML string. */ - public convenience init(string: String, encoding: String.Encoding = String.Encoding.utf8) throws { + public class func parse(string: String, encoding: String.Encoding = String.Encoding.utf8) throws -> Self { guard let cChars = string.cString(using: encoding) else { throw XMLError.invalidData } - try self.init(cChars: cChars) + return try Self.parse(cChars: cChars) + } + @available(*, unavailable, renamed: "parse") + public convenience init(string: String, encoding: String.Encoding = String.Encoding.utf8) throws { + preconditionFailure() } /** @@ -90,9 +94,14 @@ open class XMLDocument { - returns: An `XMLDocument` with the contents of the specified XML string. */ + public class func parse(data: Data) throws -> Self { + try data.withUnsafeBytes { + try Self(buffer: $0.bindMemory(to: Int8.self)) + } + } + @available(*, unavailable, renamed: "parse") public convenience init(data: Data) throws { - let buffer = data.withUnsafeBytes { $0.bindMemory(to: Int8.self) } - try self.init(buffer: buffer) + preconditionFailure() } /** @@ -104,11 +113,14 @@ open class XMLDocument { - returns: An `XMLDocument` with the contents of the specified XML string. */ - public convenience init(cChars: [CChar]) throws { - let buffer = cChars.withUnsafeBufferPointer { buffer in - UnsafeBufferPointer(rebasing: buffer[0.. Self { + return try cChars.withUnsafeBytes { + return try Self(buffer: $0.bindMemory(to: Int8.self)) } - try self.init(buffer: buffer) + } + @available(*, unavailable, renamed: "parse") + public convenience init(cChars: [CChar]) throws { + preconditionFailure() } /** @@ -121,7 +133,7 @@ open class XMLDocument { - returns: An `XMLDocument` with the contents of the specified XML string. */ - public convenience init(buffer: UnsafeBufferPointer) throws { + public convenience required init(buffer: UnsafeBufferPointer) throws { let options = Int32(XML_PARSE_NOWARNING.rawValue | XML_PARSE_NOERROR.rawValue | XML_PARSE_RECOVER.rawValue) try self.init(buffer: buffer, options: options) }