👻
Xorcist
Blazing-fast-cross-platform-monkey-patch-free string XOR. Yes, that means JRuby too.
Usage
require 'xorcist'
a, b = 'a', 'b'
Xorcist.xor(a, b) # => "\u0003"
a # => 'a'
Xorcist.xor!(a, b) # => "\u0003"
a # => "\u0003"
You can include Xorcist
to expose its methods:
require 'xorcist'
include Xorcist
a, b = 'a', 'b'
xor(a, b)
xor!(a, b)
Refinements on String
are also available:
require 'xorcist'
require 'xorcist/refinements'
using Xorcist::Refinements
a, b = 'a', 'b'
a.xor(b)
a.xor!(b)
You can also monkey patch String
if you're into that:
require 'xorcist'
require 'xorcist/string_methods'
String.include(Xorcist::StringMethods)
Encodings
Note the precise encoding xor
's output is undefined in the v1.0.x releases. In MRI xor!
will return a string using your default encoding. With JRuby, xor!
will return an ASCII-8BIT string.
Benchmarks
Disclaimer: these are run from my craptop. Run 'em yourself with bin/benchmark
. You'll need to have benchmark-ips
installed.
MRI 2.2.1
Calculating -------------------------------------
ruby 26.378k i/100ms
xorcist 73.291k i/100ms
-------------------------------------------------
ruby 376.513k (± 0.5%) i/s - 1.899M
xorcist 2.150M (± 3.9%) i/s - 10.774M
JRuby 1.7.19
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
Calculating -------------------------------------
ruby 31.795k i/100ms
xorcist 118.084k i/100ms
-------------------------------------------------
ruby 978.078k (±11.2%) i/s - 4.801M
xorcist 3.897M (±14.1%) i/s - 19.012M
Installation
Add this line to your application's Gemfile:
gem 'xorcist'
And then execute:
$ bundle
Or install it yourself as:
$ gem install xorcist
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/fny/xorcist. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Special Thanks To...
- Steve Sloan for his fast_xor gem from which most of the tests are borrowed
- James Coglan for his guide on writing a JRuby Java extension
- Thomas Leitner for a faster pure Ruby XOR implementation that's used in the benchmark
- To all contributers!
🍻