String functions problems on iOS18

On iOS 18 some string functions return incorrect values in some cases.

Found problems on replacingOccurrences() and split() functions, but there may be others. In the results of these functions in some cases a character is left in the result string when it shouldn't.

This did not happen on iOS17 and older versions. I created a very simple Test Project to reproduce the problem. If I run these tests on iOS17 or older the tests succeed. If I run these tests on iOS18 the tests fail.

test_TestStr1() function shows a problem in replacingOccurrences() directly using strings.

test_TestStr2() function shows a problem in split() that seems to happen only when bridging from NSString to String.

import XCTest
final class TestStrings18Tests: XCTestCase {
    
    override func setUpWithError() throws {
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }
    
    override func tearDownWithError() throws {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }
    
    func test_TestStr1()
    {
        let str1 = "_%\u{7}1\u{7}_";
        let str2 = "%\u{7}1\u{7}";
        let str3 = "X";
        
        let str4 = str1.replacingOccurrences(of: str2, with: str3);
        
        //This should be true
        XCTAssertTrue(str4 == "_X_");
    }

    func test_TestStr2()
    {
        let s1 = "TVAR(6)\u{11}201\"Ã\"\u{11}201\"A\"";
        let s2 = s1.components(separatedBy: "\u{11}201");
        let t1 = NSString("TVAR(6)\u{11}201\"Ã\"\u{11}201\"A\"") as String;
        let t2 = t1.components(separatedBy: "\u{11}201");
        
        XCTAssertTrue(s2.count == t2.count);
        let c = s2.count
        
        //This should be True
        XCTAssertTrue(s2[0] == t2[0]);
    }
}

I did see that the second XCTAssertTrue in test_TestStr2() failed. (test_TestStr1() passed though), and would like to confirm that it is a regression on the system side. I've filed a bug report (r.141838826) to track the issue.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

String functions problems on iOS18
 
 
Q