Skip to content

Instantly share code, notes, and snippets.

@benolee
Last active April 13, 2023 23:47
Show Gist options
  • Save benolee/e82686c8a3498c005562c0d8e085ac7d to your computer and use it in GitHub Desktop.
Save benolee/e82686c8a3498c005562c0d8e085ac7d to your computer and use it in GitHub Desktop.

RbStringIndices

What is this?

rb_string_indices is a rust native extension for ruby String that returns all indices for matches of a given substring.

How does it work?

It's using a ruby and rust interop library called rutie.

>> require 'rb_string_indices'
>> RbStringIndices.rb_string_indices('zomg wot a cool toy', 'o')
#=> [1, 6, 12, 13, 17]

Are there tests?

Not really. Just one example.

$ bundle
$ rake test

** Invoke test (first_time)
** Execute test
Run options: --seed 17448

# Running:

.

Finished in 0.000680s, 1469.9397 runs/s, 1469.9397 assertions/s.

1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

Ok

Just a fun toy project.

[package]
name = "rb_string_indices"
version = "0.1.0"
authors = ["Ben Holley <[email protected]>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rutie = {version="0.0.3"}
[lib]
name = "rb_string_indices"
crate_type = ["cdylib"]
# frozen_string_literal: true
source 'https://rubygems.org'
gemspec
gem 'rake', '~> 13.0'
gem 'minitest', '~> 5.0'
gem 'pry'
gem 'pry-doc'
gem 'rutie', '~> 0.0.3'
# lib/rb_string_indices.rb
# frozen_string_literal: true
require_relative "rb_string_indices/version"
require 'rutie'
class RbStringIndices
Rutie.new(:rb_string_indices).init 'Init_rb_string_indices', __dir__
end
# lib/rb_string_indices/version.rb
# frozen_string_literal: true
class RbStringIndices
VERSION = "0.1.0"
end
The MIT License (MIT)
Copyright (c) 2021 Ben Holley
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
# frozen_string_literal: true
require "bundler/gem_tasks"
require "rake/testtask"
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/*_test.rb"]
end
task default: :test
# frozen_string_literal: true
require_relative 'lib/rb_string_indices/version'
Gem::Specification.new do |s|
s.name = 'rb_string_indices'
s.version = RbStringIndices::VERSION
s.authors = ['Ben Holley']
s.email = ['[email protected]']
s.summary = 'rb_string_indices: String#indices'
s.description = 'rb_string_indices is a String method that returns all indices for a given substring'
s.homepage = 'https://github.com/benolee/rb_string_indices'
s.license = 'MIT'
s.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
#s.metadata['allowed_push_host'] = 'http://mygemserver.example.test'
s.metadata['homepage_uri'] = s.homepage
s.metadata['source_code_uri'] = s.homepage
s.metadata['changelog_uri'] = s.homepage
s.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
end
s.bindir = 'exe'
s.executables = s.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
s.require_paths = ['lib']
s.add_dependency 'rutie', '~> 0.0.3'
end
// src/lib.rs
#[macro_use]
extern crate rutie;
use rutie::{Fixnum, Array, Class, Object, RString, VM};
class!(RbStringIndices);
methods!(
RbStringIndices,
_rtself,
fn pub_indices(input: RString, substr: RString) -> Array {
let rb_string_input = input.unwrap();
let rb_string_substr = substr.unwrap();
let rb_string_input_as_string = rb_string_input.to_string();
let rb_string_substr_as_string = rb_string_substr.to_string();
let matches: Vec<_> = rb_string_input_as_string.match_indices(&rb_string_substr_as_string).collect();
let mut array = Array::new();
for (start, part) in matches {
let index = Fixnum::new(start as i64);
array.push(index);
}
array
}
);
#[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn Init_rb_string_indices() {
Class::new("RbStringIndices", None).define(|klass| {
klass.def_self("rb_string_indices", pub_indices);
});
}
# test/rb_string_indices_test.rb
# frozen_string_literal: true
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "rb_string_indices"
require "minitest/autorun"
class RbStringIndicesTest < Minitest::Test
def test_it_gives_indices
assert_equal [1, 6, 12, 13, 17], RbStringIndices.rb_string_indices('zomg wot a cool toy', 'o')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment