Skip to content

Commit

Permalink
Merge pull request #7 from BrettRToomey/byte-order-mark
Browse files Browse the repository at this point in the history
Byte order mark bug fix
  • Loading branch information
BrettRToomey authored Mar 12, 2017
2 parents dd40e76 + 77ffdb3 commit 029406f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Sources/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,25 @@ extension XMLParser {

public static func parse(_ bytes: Bytes) throws -> BML {
var parser = XMLParser(scanner: Scanner(bytes))


parser.trimByteOrderMark()

guard let root = try parser.extractTag() else {
throw Error.malformedXML("Expected root element.")
}

return root
}

mutating func trimByteOrderMark() {
if
scanner.peek() == 0xEF,
scanner.peek(aheadBy: 1) == 0xBB,
scanner.peek(aheadBy: 2) == 0xBF
{
scanner.pop(3)
}
}
}

extension XMLParser {
Expand Down
10 changes: 10 additions & 0 deletions Tests/BMLTests/BMLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class BMLTests: XCTestCase {
("testArrayBasic", testArrayBasic),
("testObjectBasic", testObjectBasic),
("testObjectUTF8", testObjectUTF8),
("testUTF8ByteOrderMark", testUTF8ByteOrderMark),
("testObjectEmbedded", testObjectEmbedded),
("testSelfClosing", testSelfClosing),
("testSelfClosingWithAttributes", testSelfClosingWithAttributes),
Expand Down Expand Up @@ -93,6 +94,15 @@ class BMLTests: XCTestCase {
}
}

func testUTF8ByteOrderMark() {
do {
let result = try XMLParser.parse("\u{FEFF}<Book></Book>")
result.expect(BML(name: "Book"))
} catch {
XCTFail("Parser failed: \(error)")
}
}

func testObjectEmbedded() {
do {
let result = try XMLParser.parse(
Expand Down

0 comments on commit 029406f

Please sign in to comment.