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

Add regular expression (regex) support to LineText2 protocol's delimiter #706

Merged
merged 1 commit into from
Apr 25, 2016
Merged

Add regular expression (regex) support to LineText2 protocol's delimiter #706

merged 1 commit into from
Apr 25, 2016

Conversation

taranda
Copy link
Contributor

@taranda taranda commented Mar 28, 2016

This mod allows the use of a regular expression as the end-of-line delimiter in the LineText2 protocol. The set_delimiter method will accept a regular expression and set the end-of-line delimiter appropriately. Anything passed to the set_delimiter method other than a regular expression will be converted to a string. A new test to capture the use of a regular express was added.

def set_delimiter delim
@lt2_delimiter = delim.to_s
if delim.class == Regexp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The case block may be cleaner:

@lt2_delimiter = case delim
when Regexp
   delim
else
  delim.to_s
end

@taranda
Copy link
Contributor Author

taranda commented Mar 28, 2016

Thanks for the recommendation. I modified the commit with your code recommendation.

@taranda
Copy link
Contributor Author

taranda commented Mar 28, 2016

Somehow I captured thedrow's commit in this pull request. Let me know and I will delete this pull request and submit a new one.

@sodabrew
Copy link
Member

Since you're submitting your commit from your master branch - let's assume that you have a remote called upstream - try this:

$ git checkout master
$ git fetch upstream
$ git rebase upstream/master
$ git push -f origin master

To check on your remotes (this is how mine look):

$ git remote -v
origin  git@github.com:sodabrew/eventmachine (fetch)
origin  git@github.com:sodabrew/eventmachine (push)
upstream    git@github.com:eventmachine/eventmachine.git (fetch)
upstream    git@github.com:eventmachine/eventmachine.git (push)

To add a remote:

$ git remote add upstream git@github.com:eventmachine/eventmachine.git

@sodabrew
Copy link
Member

You also do not need to submit new pull requests. A PR matches with a source/dest branch. You can add and remove commits on your branch and the PR will update to reflect whatever is on your branch.

@taranda
Copy link
Contributor Author

taranda commented Mar 28, 2016

I can try reverting and re-commiting.

I am not sure what to do here. Unfortunately, I do not have much experience with pull requests.

@taranda
Copy link
Contributor Author

taranda commented Mar 28, 2016

I think the cleanest solution may be to close this pull request and submit a new one. Thoughts?

@sodabrew
Copy link
Member

I supplied the necessary commands above, happy to help explain further if it's not working.

@sodabrew
Copy link
Member

Now that you've added a revert commit that we do not want to have on this branch, you'll need to do an interactive rebase:

$ git rebase -i upstream/master

When you're inside the editor window, delete the line with the README commit and delete the line with your Revert README commit. Leave only the line with your Line Protocol commit. Then save and exit and let git do the work of adjusting the commits on your local branch.

Force-push your adjusted branch to GitHub:

$ git push -f origin master

@taranda
Copy link
Contributor Author

taranda commented Mar 28, 2016

I reset back to before the revert commits. I think it is fixed now. When you merge, you may want to make sure that README.md is not negatively affected. I did not modify it, but it showed modified on my forked master branch when I reset back to my original commit. This appears to have worked and thank you for your help. I use git everyday, but I learned something new today.

@sodabrew
Copy link
Member

You got it! Well done, let's let the tests execute...

@@ -57,6 +57,7 @@ def receive_data data
while remaining_data.length > 0
if @lt2_mode == :lines
if ix = remaining_data.index( @lt2_delimiter )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does String#index behave correctly with a Regex argument?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does according to ruby's documentation and the test.

@taranda
Copy link
Contributor Author

taranda commented Apr 1, 2016

I have refactored the code. The new code is not a elegant to read, but I believe it will be much more efficient in execution. The only additional computation cost for the string case is a handful of extra conditional operations and some string assignments. There are no extra searches required.

In the regular expression case, the Regex is only evaluated once and further searches are done with strings which will be much more efficient. This re-factor has many more lines of code, but I think the improvement in execution speed is worth it.

Note: I have not done any benchmark testings, but I assume string searches are more expensive than conditionals and string searches.

Please let me know if you have any questions.

@lt2_linebuffer << remaining_data[0...ix]
ln = @lt2_linebuffer.join
@lt2_linebuffer.clear
if @lt2_delimiter == "\n"
ln.chomp!
end
receive_line ln
remaining_data = remaining_data[(ix+@lt2_delimiter.length)..-1]
remaining_data = remaining_data[ix+delimiter_string.length..-1]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest adding back the parens for clarity.

@sodabrew sodabrew added this to the v1.2.1 milestone Apr 8, 2016
…ter. The set_delimiter method will accept a regular expression and set the end-of-line delimiter appropriately. Anything passed to the set_delimiter method other than a regular expression will be converted to a string.
@taranda
Copy link
Contributor Author

taranda commented Apr 18, 2016

Thanks for the suggestions. The pull request has been updated.

@sodabrew sodabrew merged commit 539ae89 into eventmachine:master Apr 25, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants