Skip to content

Commit d22a66f

Browse files
WasmParser: Avoid using indices.contains in ByteStream
The idiomatic way is semantically correct, but it's not the most efficient way.
1 parent 66fa6c5 commit d22a66f

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Sources/WasmParser/Stream/ByteStream.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public final class StaticByteStream: ByteStream {
1616

1717
@discardableResult
1818
public func consumeAny() throws -> UInt8 {
19-
guard bytes.indices.contains(currentIndex) else {
19+
guard currentIndex < self.bytes.endIndex else {
2020
throw StreamError<Element>.unexpectedEnd(expected: nil)
2121
}
2222

@@ -27,7 +27,7 @@ public final class StaticByteStream: ByteStream {
2727

2828
@discardableResult
2929
public func consume(_ expected: Set<UInt8>) throws -> UInt8 {
30-
guard bytes.indices.contains(currentIndex) else {
30+
guard currentIndex < self.bytes.endIndex else {
3131
throw StreamError<Element>.unexpectedEnd(expected: Set(expected))
3232
}
3333

@@ -44,7 +44,7 @@ public final class StaticByteStream: ByteStream {
4444
guard count > 0 else { return [] }
4545
let updatedIndex = currentIndex + count
4646

47-
guard bytes.indices.contains(updatedIndex - 1) else {
47+
guard updatedIndex - 1 < bytes.endIndex else {
4848
throw StreamError<Element>.unexpectedEnd(expected: nil)
4949
}
5050

@@ -54,7 +54,7 @@ public final class StaticByteStream: ByteStream {
5454
}
5555

5656
public func peek() -> UInt8? {
57-
guard bytes.indices.contains(currentIndex) else {
57+
guard currentIndex < self.bytes.endIndex else {
5858
return nil
5959
}
6060
return bytes[currentIndex]

0 commit comments

Comments
 (0)