Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-11530] Cannot locate backslash index in String #53931

Open
swift-ci opened this issue Sep 26, 2019 · 2 comments
Open

[SR-11530] Cannot locate backslash index in String #53931

swift-ci opened this issue Sep 26, 2019 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-11530
Radar None
Original Reporter weifelin (JIRA User)
Type Bug
Environment
  • Swift 5
  • Xcode 11
  • macOS Catalina
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: f6bf70def3bb430a43111f22b335d5c0

Issue Description:

When a computed string containing back slash in it, for example, a html page as string, and when trying to get the index of that backslash, using:

htmlString.firstIndex(of: #"\"#)
// or
htmlString.firstIndex(of: "\\")

Both cases return nil. Expect to return the index of the backslash

@belkadan
Copy link
Contributor

I see no reason why this wouldn't work. Can you include the contents of htmlString in your example?

@swift-ci
Copy link
Collaborator Author

Comment by Weifeng Lin (JIRA)

Thanks replying me.

The htmlString is generated by the following function in WKNavigationDelegate (used with swiftUI)

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("in didFinish")
        let url = webView.url?.absoluteString
        if url == "https://example.com"{
            webView.evaluateJavaScript("(function(){return window.document.body.innerHTML})();") { (data, error) in
                let code = getCode(htmlString: data as! String) // This is where I deal with htmlString, implementation below
            }
        }
}


func getCode(htmlString: String?) -> String {
        if htmlString == nil || htmlString!.isEmpty {
            return ""
        }
        let html = htmlString!
        /*
          other codes
        */
        let quote = Character("\"")
        let slash = Character(#"\"#)
        let key = "MYKEY"
        let keyIndex = html.range(of: key, options: .backwards)?.lowerBound
        var subHTML = html[keyIndex!...]
        let starting = subHTML.index(after: subHTML.firstIndex(of: quote)!) // This will work
        subHTML = subHTML[starting...]
        /* other codes */
        let ending = subHTML.firstIndex(of: slash) // This will return nil. The htmlString is not "null", nor nil

        // if I find the index of the character right after the backslash, then I'll able to locate the index of slash.(What I'm doing now)
       
       /* other codes */
    }

Please keep me updated, thanks

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants