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

Allow . to match any character, including line separators. #11

Closed
hanishabsigh opened this issue Mar 15, 2015 · 13 comments
Closed

Allow . to match any character, including line separators. #11

hanishabsigh opened this issue Mar 15, 2015 · 13 comments

Comments

@hanishabsigh
Copy link

First...thank you for this library. I was experiencing an issue in which a large incoming event wasn't being passed to the appropriate on: handler. No error was thrown. After some investigation, I narrowed it down to regex operations that weren't outputting valid json, thus leading the NSJSONSerialization to fail. Here is an example of the regex, although it happens a few times within the library:

var mutMessage = RegexMutable(stringMessage)
let messageGroups = mutMessage["(\\d*)\\/?(\\w*)?,?(\\d*)?(\\[.*\\])?"].groups()

The end of the regex uses a . to match any character, but by default it does not match line separators. If the intent is to include them, SwiftRegex lets us set the option NSRegularExpressionOptions.DotMatchesLineSeparators. I attempted a pull request to do the following but it wasn't working for me:

var mutMessage = RegexMutable(stringMessage)
let messageGroups = mutMessage["(\\d*)\\/?(\\w*)?,?(\\d*)?(\\[.*\\])?",NSRegularExpressionOptions.DotMatchesLineSeparators].groups()

I must be missing something, I thought that subscript would work but it crashes for me. The following did work:

var mutMessage = RegexMutable(stringMessage)
var swiftRegex = SwiftRegex(target: mutMessage, pattern: "(\\d*)\\/?(\\w*)?,?(\\d*)?(.*)?", options: NSRegularExpressionOptions.DotMatchesLineSeparators);
let messageGroups = swiftRegex.groups()

Also, it could be helpful to include a warning when the event parsing fails. Thanks!

@nuclearace
Copy link
Member

@hanishabsigh can you provide a message that fails to get parsed. You can uncomment https://github.com/socketio/socket.io-client-swift/blob/master/SwiftIO/SocketEngine.swift#L409 to see messages before they get sent to the socket.io-client parser.

@nuclearace nuclearace added the bug label Mar 15, 2015
@nuclearace
Copy link
Member

I'm actually finding a few problems with regex. Mostly due to the differences between NSString and String.

@hanishabsigh
Copy link
Author

My object that fails is very large, here is an example I was able to track down:

The following fails:

"42[\"auto\",{\"message_of_the_date\":\"line one\nline two\"}]"

The following succeeds:

"42[\"auto\",{\"message_of_the_date\":\"line one line two\"}]"

The line break in the message of the day causes the parsing of the groups to break.

@hanishabsigh
Copy link
Author

@nuclearace I just noticed you made some parsing changes, this was tested prior to those changes, thanks

@nuclearace
Copy link
Member

@hanishabsigh It seems to work for me on the latest. Please note #13 though. Since it can cause the regex to fail sometimes. Which is why I changed some things.

@hanishabsigh
Copy link
Author

@nuclearace This still isn't working for me, incoming messages can't even get passed the following line:

let messageGroups = stringMessage["(\\d*)\\/?(\\w*)?,?(\\d*)?\\[\"(.*?)\",?(.*?)?\\]$"].groups()

Replacing line 224 with the following worked:

let messageGroups = stringMessage["(\\d*)\\/?(\\w*)?,?(\\d*)?\\[\"(.*?)\",?(.*?)?\\]$", NSRegularExpressionOptions.DotMatchesLineSeparators].groups()

nuclearace added a commit that referenced this issue Mar 16, 2015
@nuclearace
Copy link
Member

@hanishabsigh try after 44e2781

@hanishabsigh
Copy link
Author

@nuclearace Checking against the latest commit isn't working, something about the changes in SwiftRegex. So far from what I can tell regex.firstMatchInString in the groups method isn't returning anything. Still investigating. Thanks

@nuclearace
Copy link
Member

This client is able to get this for me:

socket.emit("jsonTest", {foo: "string\ntest test"})

@hanishabsigh
Copy link
Author

Ok, fixed it by changing the following:

final var targetRange: NSRange {
    return NSRange(location: 0,length: countElements(target))
}

To this:

final var targetRange: NSRange {
    return NSRange(location: 0,length: target.utf16Count)
}

I think the error I was experiencing was happening because my json had variable-width Unicode characters. Found someone having the same issues with swift on StackOverflow: http://stackoverflow.com/questions/25882503/how-can-i-use-nsregularexpression-on-swift-strings-with-variable-width-unicode-c

nuclearace added a commit that referenced this issue Mar 18, 2015
@nuclearace
Copy link
Member

@hanishabsigh so does c67e7a3 fix it

@hanishabsigh
Copy link
Author

@nuclearace Yes, that commit fixes it, thanks.

@Katafalkas
Copy link

screen shot 2015-03-19 at 13 12 34
screen shot 2015-03-19 at 13 13 00

From Xcode 6.3 beta 3 release notes:
>`utf16Count` is removed from String. Instead use count on the UTF16 view of the String. (17627758)

@hanishabsigh @nuclearace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants