@@ -159,29 +159,27 @@ public let CodeGenerators: [CodeGenerator] = [
159159 CodeGenerator ( " HexGenerator " ) { b in
160160 let hexValues = [ " 0 " , " 1 " , " 2 " , " 3 " , " 4 " , " 5 " , " 6 " , " 7 " , " 8 " , " 9 " , " a " , " b " , " c " , " d " , " e " , " f " , " A " , " B " , " C " , " D " , " E " , " F " ]
161161
162- let Uint8Array = b. createNamedVariable ( forBuiltin: " Uint8Array " )
162+ let uint8ArrayBuiltin = b. createNamedVariable ( forBuiltin: " Uint8Array " )
163163
164164 withEqualProbability ( {
165+ // Generate Uint8Array construction from hex string.
165166 var s = " "
166- for _ in 0 ..< Int . random ( in: 1 ... 10 ) {
167+ for _ in 0 ..< Int . random ( in: 1 ... 40 ) {
167168 s += chooseUniform ( from: hexValues)
168169 s += chooseUniform ( from: hexValues)
169170 }
170171 let hex = b. loadString ( s)
171172
172173 if probability ( 0.5 ) {
173- b. callMethod ( " fromHex " , on: Uint8Array , withArgs: [ hex] )
174+ b. callMethod ( " fromHex " , on: uint8ArrayBuiltin , withArgs: [ hex] )
174175 } else {
175- let target = b. construct ( Uint8Array , withArgs: [ b. loadInt ( Int64 . random ( in: 0 ... 0x100 ) ) ] )
176+ let target = b. construct ( uint8ArrayBuiltin , withArgs: [ b. loadInt ( Int64 . random ( in: 0 ... 0x100 ) ) ] )
176177 b. callMethod ( " setFromHex " , on: target, withArgs: [ hex] )
177178 }
178179 } , {
179- var values = [ Variable] ( )
180- for _ in 0 ..< Int . random ( in: 1 ... 20 ) {
181- values. append ( b. loadInt ( Int64 . random ( in: 0 ... 0xFF ) ) )
182- }
183-
184- let bytes = b. callMethod ( " of " , on: Uint8Array, withArgs: values)
180+ // Generate hex String construction from Uint8Array.
181+ let values = ( 0 ..< Int . random ( in: 1 ... 20 ) ) . map { _ in b. loadInt ( Int64 . random ( in: 0 ... 0xFF ) ) }
182+ let bytes = b. callMethod ( " of " , on: uint8ArrayBuiltin, withArgs: values)
185183 b. callMethod ( " toHex " , on: bytes, withArgs: [ ] )
186184 }
187185 )
@@ -191,11 +189,11 @@ public let CodeGenerators: [CodeGenerator] = [
191189 let base64Alphabet = [ " A " , " B " , " C " , " D " , " E " , " F " , " G " , " H " , " I " , " J " , " K " , " L " , " M " , " N " , " O " , " P " , " Q " , " R " , " S " , " T " , " U " , " V " , " W " , " X " , " Y " , " Z " , " a " , " b " , " c " , " d " , " e " , " f " , " g " , " h " , " i " , " j " , " k " , " l " , " m " , " n " , " o " , " p " , " q " , " r " , " s " , " t " , " u " , " v " , " w " , " x " , " y " , " z " , " 0 " , " 1 " , " 2 " , " 3 " , " 4 " , " 5 " , " 6 " , " 7 " , " 8 " , " 9 " , " + " , " / " ]
192190 let base64URLAlphabet = [ " A " , " B " , " C " , " D " , " E " , " F " , " G " , " H " , " I " , " J " , " K " , " L " , " M " , " N " , " O " , " P " , " Q " , " R " , " S " , " T " , " U " , " V " , " W " , " X " , " Y " , " Z " , " a " , " b " , " c " , " d " , " e " , " f " , " g " , " h " , " i " , " j " , " k " , " l " , " m " , " n " , " o " , " p " , " q " , " r " , " s " , " t " , " u " , " v " , " w " , " x " , " y " , " z " , " 0 " , " 1 " , " 2 " , " 3 " , " 4 " , " 5 " , " 6 " , " 7 " , " 8 " , " 9 " , " - " , " _ " ]
193191
194- let Uint8Array = b. createNamedVariable ( forBuiltin: " Uint8Array " )
192+ let uint8ArrayBuiltin = b. createNamedVariable ( forBuiltin: " Uint8Array " )
195193
196194 withEqualProbability ( {
197195 var options = [ String: Variable] ( )
198- var alphabet = chooseUniform ( from: [ base64Alphabet, base64URLAlphabet] )
196+ let alphabet = chooseUniform ( from: [ base64Alphabet, base64URLAlphabet] )
199197
200198 options [ " alphabet " ] = b. loadString ( ( alphabet == base64Alphabet) ? " base64 " : " base64url " )
201199 options [ " lastChunkHandling " ] = b. loadString (
@@ -209,7 +207,7 @@ public let CodeGenerators: [CodeGenerator] = [
209207 s += chooseUniform ( from: alphabet)
210208 }
211209
212- // extend by 0, 1, or 2 bytes
210+ // Extend by 0, 1, or 2 bytes.
213211 switch ( Int . random ( in: 0 ... 3 ) ) {
214212 case 1 :
215213 s += base64Alphabet [ Int . random ( in: 0 ... 63 ) ]
@@ -232,18 +230,14 @@ public let CodeGenerators: [CodeGenerator] = [
232230
233231 let optionsObject = b. createObject ( with: options)
234232 if probability ( 0.5 ) {
235- b. callMethod ( " fromBase64 " , on: Uint8Array , withArgs: [ base64, optionsObject] )
233+ b. callMethod ( " fromBase64 " , on: uint8ArrayBuiltin , withArgs: [ base64, optionsObject] )
236234 } else {
237- let target = b. construct ( Uint8Array , withArgs: [ b. loadInt ( Int64 . random ( in: 0 ... 0x100 ) ) ] )
235+ let target = b. construct ( uint8ArrayBuiltin , withArgs: [ b. loadInt ( Int64 . random ( in: 0 ... 0x100 ) ) ] )
238236 b. callMethod ( " setFromBase64 " , on: target, withArgs: [ base64, optionsObject] )
239237 }
240238 } , {
241- var values = [ Variable] ( )
242- for _ in 0 ..< Int . random ( in: 1 ... 64 ) {
243- values. append ( b. loadInt ( Int64 . random ( in: 0 ... 0xFF ) ) )
244- }
245-
246- let bytes = b. callMethod ( " of " , on: Uint8Array, withArgs: values)
239+ let values = ( 0 ..< Int . random ( in: 1 ... 64 ) ) . map { _ in b. loadInt ( Int64 . random ( in: 0 ... 0xFF ) ) }
240+ let bytes = b. callMethod ( " of " , on: uint8ArrayBuiltin, withArgs: values)
247241 b. callMethod ( " toBase64 " , on: bytes, withArgs: [ ] )
248242 }
249243 )
0 commit comments