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.

Hi,

I had not tested on iOS 18.2 yet. test_TestStr1() works on iOS 18.2, but on a slightly more complex case it does not.

Bellow is another test with a new function test_TestStr3() that works on iOS 17 but not on iOS 18.2. The results of the tests I made are for the 3 functions bellow are: iOS 17 - All 3 functions succeed iOS 18.1.1 - All 3 functions fail iOS 18.2 - test_TestStr1() passes the tests but the other 2 fail.

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()
    {
        //For testing that bug in iOS18 still exists
        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]);
    }
    
    func test_TestStr3()
    {
        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_€");
    }
}

Thanks for providing the new test case and more details. I've added the information to the report I filed (r.141838826).

Since you don't have the access to my report, I’d suggest that you file your own feedback report so you can track the issue and request a status update, if needed.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Hi,

Do you have any update on this?

If I file a feedback report in Apple Feedback Assistant, how do I link it to this post or to your report?

There's no update that I can share unfortunately.

If you haven't yet, I'd suggest that you file your own report against this issue, as mentioned above. Because you represent the developer community and have a concrete use case, your report may attract more attention from the relevant team. Also, as the originator, you can get notified of the status change on your report, and follow up with it if that matters to you.

When filing your report, you can link to this forum post by adding the post link to your description. Folks can then click the link to see the conversation.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

String functions problems on iOS18
 
 
Q