Skip to content

Commit 22ec7df

Browse files
authored
Merge pull request swiftlang#1687 from spevans/pr_sr_8689
2 parents f0ad449 + 05d5f07 commit 22ec7df

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

Foundation/NSString.swift

+9-2
Original file line numberDiff line numberDiff line change
@@ -1311,8 +1311,15 @@ open class NSMutableString : NSString {
13111311
}
13121312

13131313
let start = _storage.utf16.startIndex
1314-
let min = _storage.utf16.index(start, offsetBy: range.location).samePosition(in: _storage)!
1315-
let max = _storage.utf16.index(start, offsetBy: range.location + range.length).samePosition(in: _storage)!
1314+
let min = _storage.utf16.index(start, offsetBy: range.location).samePosition(in: _storage) ?? {
1315+
let characterRange = _storage.rangeOfComposedCharacterSequence(at: range.location)
1316+
return _storage.utf16.index(start, offsetBy: characterRange.location)
1317+
}()
1318+
1319+
let max = _storage.utf16.index(start, offsetBy: range.location + range.length).samePosition(in: _storage) ?? {
1320+
let characterRange = _storage.rangeOfComposedCharacterSequence(at: range.location + range.length)
1321+
return _storage.utf16.index(start, offsetBy: characterRange.location)
1322+
}()
13161323
_storage.replaceSubrange(min..<max, with: aString)
13171324
}
13181325

TestFoundation/TestNSString.swift

+32
Original file line numberDiff line numberDiff line change
@@ -1289,5 +1289,37 @@ extension TestNSString {
12891289

12901290
let replaceSuffixWithMultibyte = testString.replacingOccurrences(of: testSuffix, with: testReplacementEmoji)
12911291
XCTAssertEqual(replaceSuffixWithMultibyte, testPrefix + testEmoji + testReplacementEmoji)
1292+
1293+
let str1 = "Hello\r\nworld."
1294+
XCTAssertEqual(str1.replacingOccurrences(of: "\n", with: " "), "Hello\r world.")
1295+
XCTAssertEqual(str1.replacingOccurrences(of: "\r", with: " "), "Hello \nworld.")
1296+
XCTAssertEqual(str1.replacingOccurrences(of: "\r\n", with: " "), "Hello world.")
1297+
XCTAssertEqual(str1.replacingOccurrences(of: "\r\n", with: "\n\r"), "Hello\n\rworld.")
1298+
XCTAssertEqual(str1.replacingOccurrences(of: "\r\n", with: "\r\n"), "Hello\r\nworld.")
1299+
XCTAssertEqual(str1.replacingOccurrences(of: "\n\r", with: " "), "Hello\r\nworld.")
1300+
1301+
let str2 = "Hello\n\rworld."
1302+
XCTAssertEqual(str2.replacingOccurrences(of: "\n", with: " "), "Hello \rworld.")
1303+
XCTAssertEqual(str2.replacingOccurrences(of: "\r", with: " "), "Hello\n world.")
1304+
XCTAssertEqual(str2.replacingOccurrences(of: "\r\n", with: " "), "Hello\n\rworld.")
1305+
XCTAssertEqual(str2.replacingOccurrences(of: "\n\r", with: " "), "Hello world.")
1306+
XCTAssertEqual(str2.replacingOccurrences(of: "\n\r", with: "\r\n"), "Hello\r\nworld.")
1307+
XCTAssertEqual(str2.replacingOccurrences(of: "\n\r", with: "\n\r"), "Hello\n\rworld.")
1308+
1309+
let str3 = "Hello\n\nworld."
1310+
XCTAssertEqual(str3.replacingOccurrences(of: "\n", with: " "), "Hello world.")
1311+
XCTAssertEqual(str3.replacingOccurrences(of: "\r", with: " "), "Hello\n\nworld.")
1312+
XCTAssertEqual(str3.replacingOccurrences(of: "\r\n", with: " "), "Hello\n\nworld.")
1313+
XCTAssertEqual(str3.replacingOccurrences(of: "\r\n", with: "\n\r"), "Hello\n\nworld.")
1314+
XCTAssertEqual(str3.replacingOccurrences(of: "\r\n", with: "\r\n"), "Hello\n\nworld.")
1315+
XCTAssertEqual(str3.replacingOccurrences(of: "\n\r", with: " "), "Hello\n\nworld.")
1316+
1317+
let str4 = "Hello\r\rworld."
1318+
XCTAssertEqual(str4.replacingOccurrences(of: "\n", with: " "), "Hello\r\rworld.")
1319+
XCTAssertEqual(str4.replacingOccurrences(of: "\r", with: " "), "Hello world.")
1320+
XCTAssertEqual(str4.replacingOccurrences(of: "\r\n", with: " "), "Hello\r\rworld.")
1321+
XCTAssertEqual(str4.replacingOccurrences(of: "\r\n", with: "\n\r"), "Hello\r\rworld.")
1322+
XCTAssertEqual(str4.replacingOccurrences(of: "\r\n", with: "\r\n"), "Hello\r\rworld.")
1323+
XCTAssertEqual(str4.replacingOccurrences(of: "\n\r", with: " "), "Hello\r\rworld.")
12921324
}
12931325
}

0 commit comments

Comments
 (0)