@@ -22,22 +22,29 @@ public struct Verifier {
22
22
23
23
/// Flag to check for alignment if true
24
24
fileprivate let _checkAlignment : Bool
25
- /// Capacity of the current buffer
26
- fileprivate var _capacity : Int
27
- /// Current ApparentSize
28
- fileprivate var _apparentSize : UOffset = 0
29
- /// Amount of tables present within a buffer
30
- fileprivate var _tableCount = 0
31
-
32
- /// Capacity of the buffer
33
- internal var capacity : Int { _capacity }
34
- /// Current reached depth within the buffer
35
- internal var _depth = 0
25
+ /// Storage for all changing values within the verifier
26
+ private let storage : Storage
36
27
/// Current verifiable ByteBuffer
37
28
internal var _buffer : ByteBuffer
38
29
/// Options for verification
39
30
internal let _options : VerifierOptions
40
31
32
+ /// Current stored capacity within the verifier
33
+ var capacity : Int {
34
+ storage. capacity
35
+ }
36
+
37
+ /// Current depth of verifier
38
+ var depth : Int {
39
+ storage. depth
40
+ }
41
+
42
+ /// Current table count
43
+ var tableCount : Int {
44
+ storage. tableCount
45
+ }
46
+
47
+
41
48
/// Initializer for the verifier
42
49
/// - Parameters:
43
50
/// - buffer: Bytebuffer that is required to be verified
@@ -54,23 +61,23 @@ public struct Verifier {
54
61
}
55
62
56
63
_buffer = buffer
57
- _capacity = buffer. capacity
58
64
_checkAlignment = checkAlignment
59
65
_options = options
66
+ storage = Storage ( capacity: buffer. capacity)
60
67
}
61
68
62
69
/// Resets the verifier to initial state
63
- public mutating func reset( ) {
64
- _depth = 0
65
- _tableCount = 0
70
+ public func reset( ) {
71
+ storage . depth = 0
72
+ storage . tableCount = 0
66
73
}
67
74
68
75
/// Checks if the value of type `T` is aligned properly in the buffer
69
76
/// - Parameters:
70
77
/// - position: Current position
71
78
/// - type: Type of value to check
72
79
/// - Throws: `missAlignedPointer` if the pointer is not aligned properly
73
- public mutating func isAligned< T> ( position: Int , type: T . Type ) throws {
80
+ public func isAligned< T> ( position: Int , type: T . Type ) throws {
74
81
75
82
/// If check alignment is false this mutating function doesnt continue
76
83
if !_checkAlignment { return }
@@ -94,13 +101,13 @@ public struct Verifier {
94
101
/// - Throws: `outOfBounds` if the value is out of the bounds of the buffer
95
102
/// and `apparentSizeTooLarge` if the apparent size is bigger than the one specified
96
103
/// in `VerifierOptions`
97
- public mutating func rangeInBuffer( position: Int , size: Int ) throws {
104
+ public func rangeInBuffer( position: Int , size: Int ) throws {
98
105
let end = UInt ( clamping: ( position &+ size) . magnitude)
99
106
if end > _buffer. capacity {
100
- throw FlatbuffersErrors . outOfBounds ( position: end, end: capacity)
107
+ throw FlatbuffersErrors . outOfBounds ( position: end, end: storage . capacity)
101
108
}
102
- _apparentSize = _apparentSize &+ UInt32 ( size)
103
- if _apparentSize > _options. _maxApparentSize {
109
+ storage . apparentSize = storage . apparentSize &+ UInt32 ( size)
110
+ if storage . apparentSize > _options. _maxApparentSize {
104
111
throw FlatbuffersErrors . apparentSizeTooLarge
105
112
}
106
113
}
@@ -111,7 +118,7 @@ public struct Verifier {
111
118
/// - position: Current readable position
112
119
/// - type: Type of value to check
113
120
/// - Throws: FlatbuffersErrors
114
- public mutating func inBuffer< T> ( position: Int , of type: T . Type ) throws {
121
+ public func inBuffer< T> ( position: Int , of type: T . Type ) throws {
115
122
try isAligned ( position: position, type: type)
116
123
try rangeInBuffer ( position: position, size: MemoryLayout< T> . size)
117
124
}
@@ -131,15 +138,15 @@ public struct Verifier {
131
138
type: VOffset . self)
132
139
try rangeInBuffer ( position: vtablePosition, size: length)
133
140
134
- _tableCount += 1
141
+ storage . tableCount += 1
135
142
136
- if _tableCount > _options. _maxTableCount {
143
+ if storage . tableCount > _options. _maxTableCount {
137
144
throw FlatbuffersErrors . maximumTables
138
145
}
139
146
140
- _depth += 1
147
+ storage . depth += 1
141
148
142
- if _depth > _options. _maxDepth {
149
+ if storage . depth > _options. _maxDepth {
143
150
throw FlatbuffersErrors . maximumDepth
144
151
}
145
152
@@ -154,7 +161,7 @@ public struct Verifier {
154
161
/// - Parameter position: Current position to be read
155
162
/// - Throws: `inBuffer` errors
156
163
/// - Returns: a value of type `T` usually a `VTable` or a table offset
157
- internal mutating func getValue< T> ( at position: Int ) throws -> T {
164
+ internal func getValue< T> ( at position: Int ) throws -> T {
158
165
try inBuffer ( position: position, of: T . self)
159
166
return _buffer. read ( def: T . self, position: position)
160
167
}
@@ -165,7 +172,7 @@ public struct Verifier {
165
172
/// - Throws: `inBuffer` errors & `signedOffsetOutOfBounds`
166
173
/// - Returns: Current readable position for a field
167
174
@inline ( __always)
168
- internal mutating func derefOffset( position: Int ) throws -> Int {
175
+ internal func derefOffset( position: Int ) throws -> Int {
169
176
try inBuffer ( position: position, of: Int32 . self)
170
177
171
178
let offset = _buffer. read ( def: Int32 . self, position: position)
@@ -197,14 +204,14 @@ public struct Verifier {
197
204
}
198
205
199
206
/// finishes the current iteration of verification on an object
200
- internal mutating func finish( ) {
201
- _depth -= 1
207
+ internal func finish( ) {
208
+ storage . depth -= 1
202
209
}
203
210
204
211
@inline ( __always)
205
- mutating func verify( id: String ) throws {
212
+ func verify( id: String ) throws {
206
213
let size = MemoryLayout< Int32> . size
207
- guard _capacity >= ( size * 2 ) else {
214
+ guard storage . capacity >= ( size * 2 ) else {
208
215
throw FlatbuffersErrors . bufferDoesntContainID
209
216
}
210
217
let str = _buffer. readString ( at: size, count: size)
@@ -214,4 +221,18 @@ public struct Verifier {
214
221
throw FlatbuffersErrors . bufferIdDidntMatchPassedId
215
222
}
216
223
224
+ final private class Storage {
225
+ /// Current ApparentSize
226
+ fileprivate var apparentSize : UOffset = 0
227
+ /// Amount of tables present within a buffer
228
+ fileprivate var tableCount = 0
229
+ /// Capacity of the current buffer
230
+ fileprivate let capacity : Int
231
+ /// Current reached depth within the buffer
232
+ fileprivate var depth = 0
233
+
234
+ init ( capacity: Int ) {
235
+ self . capacity = capacity
236
+ }
237
+ }
217
238
}
0 commit comments