Skip to content

Commit 09a445c

Browse files
authored
Merge pull request swiftlang#296 from swiftwasm/main
[pull] swiftwasm from main
2 parents a665689 + cc5fc93 commit 09a445c

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

Sources/Foundation/NSData.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,12 +912,14 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
912912
// anyway.
913913
buffer.baseAddress!.advanced(by: outputIndex).copyMemory(from: &outputBytes, byteCount: 4)
914914
outputIndex += 4
915+
bytesLeft = dataBuffer.count - inputIndex
916+
915917
if lineLength != 0 {
916918
// Add required EOL markers.
917919
currentLineCount += 4
918920
assert(currentLineCount <= lineLength)
919921

920-
if currentLineCount == lineLength {
922+
if currentLineCount == lineLength && bytesLeft > 0 {
921923
buffer[outputIndex] = separatorByte1
922924
outputIndex += 1
923925

@@ -928,7 +930,6 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
928930
currentLineCount = 0
929931
}
930932
}
931-
bytesLeft = dataBuffer.count - inputIndex
932933
}
933934

934935
// Return the number of ASCII bytes written to the buffer

Tests/Foundation/Tests/TestNSData.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ class TestNSData: LoopbackServerTest {
244244
("test_base64EncodedDataWithOptionToInsertCarriageReturnContainsCarriageReturn", test_base64EncodedDataWithOptionToInsertCarriageReturnContainsCarriageReturn),
245245
("test_base64EncodedDataWithOptionToInsertLineFeedsContainsLineFeed", test_base64EncodedDataWithOptionToInsertLineFeedsContainsLineFeed),
246246
("test_base64EncodedDataWithOptionToInsertCarriageReturnAndLineFeedContainsBoth", test_base64EncodedDataWithOptionToInsertCarriageReturnAndLineFeedContainsBoth),
247+
("test_base64EncodeDoesNotAddLineSeparatorsWhenStringFitsInLine", test_base64EncodeDoesNotAddLineSeparatorsWhenStringFitsInLine),
248+
("test_base64EncodeAddsLineSeparatorsWhenStringDoesNotFitInLine", test_base64EncodeAddsLineSeparatorsWhenStringDoesNotFitInLine),
247249
("test_base64EncodedStringGetsEncodedText", test_base64EncodedStringGetsEncodedText),
248250
("test_initializeWithBase64EncodedStringGetsDecodedData", test_initializeWithBase64EncodedStringGetsDecodedData),
249251
("test_base64DecodeWithPadding1", test_base64DecodeWithPadding1),
@@ -814,6 +816,36 @@ class TestNSData: LoopbackServerTest {
814816
XCTAssertEqual(encodedTextResult, encodedText)
815817
}
816818

819+
func test_base64EncodeDoesNotAddLineSeparatorsWhenStringFitsInLine() {
820+
821+
XCTAssertEqual(
822+
Data(repeating: 0, count: 48).base64EncodedString(options: .lineLength64Characters),
823+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
824+
"each 3 byte is converted into 4 characterss. 48 / 3 * 4 <= 64, therefore result should not have line separator."
825+
)
826+
827+
XCTAssertEqual(
828+
Data(repeating: 0, count: 57).base64EncodedString(options: .lineLength76Characters),
829+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
830+
"each 3 byte is converted into 4 characterss. 57 / 3 * 4 <= 76, therefore result should not have line separator."
831+
)
832+
}
833+
834+
func test_base64EncodeAddsLineSeparatorsWhenStringDoesNotFitInLine() {
835+
836+
XCTAssertEqual(
837+
Data(repeating: 0, count: 49).base64EncodedString(options: .lineLength64Characters),
838+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\nAA==",
839+
"each 3 byte is converted into 4 characterss. 49 / 3 * 4 > 64, therefore result should have lines with separator."
840+
)
841+
842+
XCTAssertEqual(
843+
Data(repeating: 0, count: 58).base64EncodedString(options: .lineLength76Characters),
844+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\nAA==",
845+
"each 3 byte is converted into 4 characterss. 58 / 3 * 4 > 76, therefore result should have lines with separator."
846+
)
847+
}
848+
817849
func test_base64EncodedStringGetsEncodedText() {
818850
let plainText = "Revocate animos, maestumque timorem mittite: forsan et haec olim meminisse iuvabit."
819851
let encodedText = "UmV2b2NhdGUgYW5pbW9zLCBtYWVzdHVtcXVlIHRpbW9yZW0gbWl0dGl0ZTogZm9yc2FuIGV0IGhhZWMgb2xpbSBtZW1pbmlzc2UgaXV2YWJpdC4="

0 commit comments

Comments
 (0)