@@ -351,16 +351,8 @@ extension Base64 {
351
351
Self . withUnsafeEncodingTablesAsBufferPointers ( options: options) { ( e0, e1) throws ( Never) -> Void in
352
352
let to = input. count / 3 * 3
353
353
var outIndex = 0
354
- for index in stride ( from: 0 , to: to, by: 3 ) {
355
- let i1 = input [ index]
356
- let i2 = input [ index &+ 1 ]
357
- let i3 = input [ index &+ 2 ]
358
- buffer [ outIndex] = e0 [ Int ( i1) ]
359
- buffer [ outIndex &+ 1 ] = e1 [ Int ( ( ( i1 & 0x03 ) &<< 4 ) | ( ( i2 &>> 4 ) & 0x0F ) ) ]
360
- buffer [ outIndex &+ 2 ] = e1 [ Int ( ( ( i2 & 0x0F ) &<< 2 ) | ( ( i3 &>> 6 ) & 0x03 ) ) ]
361
- buffer [ outIndex &+ 3 ] = e1 [ Int ( i3) ]
362
- outIndex += 4
363
- }
354
+
355
+ self . loopEncode ( e0, e1, input: input, from: 0 , to: to, output: buffer, outIndex: & outIndex)
364
356
365
357
if to < input. count {
366
358
let index = to
@@ -435,37 +427,35 @@ extension Base64 {
435
427
436
428
Self . withUnsafeEncodingTablesAsBufferPointers ( options: options) { e0, e1 in
437
429
var outIndex = 0
438
- for lineInputIndex in stride ( from: 0 , to: lines * lineLength, by: lineLength) {
439
- for index in stride ( from: lineInputIndex, to: lineInputIndex + lineLength, by: 3 ) {
440
- let i1 = input [ index]
441
- let i2 = input [ index + 1 ]
442
- let i3 = input [ index + 2 ]
443
- buffer [ outIndex] = e0 [ Int ( i1) ]
444
- buffer [ outIndex + 1 ] = e1 [ Int ( ( ( i1 & 0x03 ) << 4 ) | ( ( i2 >> 4 ) & 0x0F ) ) ]
445
- buffer [ outIndex + 2 ] = e1 [ Int ( ( ( i2 & 0x0F ) << 2 ) | ( ( i3 >> 6 ) & 0x03 ) ) ]
446
- buffer [ outIndex + 3 ] = e1 [ Int ( i3) ]
447
- outIndex += 4
448
- }
449
430
431
+ // first full line
432
+ if input. count >= lineLength {
433
+ self . loopEncode ( e0, e1, input: input, from: 0 , to: lineLength, output: buffer, outIndex: & outIndex)
434
+ }
435
+
436
+ // following full lines
437
+ for lineInputIndex in stride ( from: lineLength, to: lines * lineLength, by: lineLength) {
450
438
buffer [ outIndex] = separatorByte1
451
439
outIndex += 1
452
440
if let separatorByte2 {
453
441
buffer [ outIndex] = separatorByte2
454
442
outIndex += 1
455
443
}
444
+
445
+ self . loopEncode ( e0, e1, input: input, from: lineInputIndex, to: lineInputIndex + lineLength, output: buffer, outIndex: & outIndex)
456
446
}
457
447
458
- let to = input. count / 3 * 3
459
- for index in stride ( from: lines * lineLength, to: to, by: 3 ) {
460
- let i1 = input [ index]
461
- let i2 = input [ index + 1 ]
462
- let i3 = input [ index + 2 ]
463
- buffer [ outIndex] = e0 [ Int ( i1) ]
464
- buffer [ outIndex + 1 ] = e1 [ Int ( ( ( i1 & 0x03 ) << 4 ) | ( ( i2 >> 4 ) & 0x0F ) ) ]
465
- buffer [ outIndex + 2 ] = e1 [ Int ( ( ( i2 & 0x0F ) << 2 ) | ( ( i3 >> 6 ) & 0x03 ) ) ]
466
- buffer [ outIndex + 3 ] = e1 [ Int ( i3) ]
467
- outIndex += 4
448
+ // last line beginning
449
+ if lines > 0 && lines * lineLength < input. count {
450
+ buffer [ outIndex] = separatorByte1
451
+ outIndex += 1
452
+ if let separatorByte2 {
453
+ buffer [ outIndex] = separatorByte2
454
+ outIndex += 1
455
+ }
468
456
}
457
+ let to = input. count / 3 * 3
458
+ self . loopEncode ( e0, e1, input: input, from: lines * lineLength, to: to, output: buffer, outIndex: & outIndex)
469
459
470
460
if to < input. count {
471
461
let index = to
@@ -504,6 +494,27 @@ extension Base64 {
504
494
}
505
495
}
506
496
497
+ private static func loopEncode(
498
+ _ e0: UnsafeBufferPointer < UInt8 > ,
499
+ _ e1: UnsafeBufferPointer < UInt8 > ,
500
+ input: UnsafeBufferPointer < UInt8 > ,
501
+ from: Int ,
502
+ to: Int ,
503
+ output: UnsafeMutableBufferPointer < UInt8 > ,
504
+ outIndex: inout Int
505
+ ) {
506
+ for index in stride ( from: from, to: to, by: 3 ) {
507
+ let i1 = input [ index]
508
+ let i2 = input [ index + 1 ]
509
+ let i3 = input [ index + 2 ]
510
+ output [ outIndex] = e0 [ Int ( i1) ]
511
+ output [ outIndex + 1 ] = e1 [ Int ( ( ( i1 & 0x03 ) << 4 ) | ( ( i2 >> 4 ) & 0x0F ) ) ]
512
+ output [ outIndex + 2 ] = e1 [ Int ( ( ( i2 & 0x0F ) << 2 ) | ( ( i3 >> 6 ) & 0x03 ) ) ]
513
+ output [ outIndex + 3 ] = e1 [ Int ( i3) ]
514
+ outIndex += 4
515
+ }
516
+ }
517
+
507
518
static func encodeComputeCapacity( bytes: Int , options: Data . Base64EncodingOptions ) -> Int {
508
519
let capacityWithoutBreaks = ( ( bytes + 2 ) / 3 ) * 4
509
520
0 commit comments