Add text_transform example

This commit is contained in:
Godfrey Chan 2017-04-20 12:01:48 -07:00
parent d09c796ea3
commit bd225ddca9
11 changed files with 142 additions and 3 deletions

View File

@ -3,7 +3,7 @@ cache:
- '%HOMEDRIVE%%HOMEPATH%\.multirust -> .appveyor.yml'
environment:
EXAMPLES: duration calculator console membership turbo_blank
EXAMPLES: duration calculator console membership text_transform turbo_blank
VERBOSE: true
RUST_BACKTRACE: 1
matrix:

View File

@ -11,7 +11,7 @@ include = [
[workspace]
members = ["examples/calculator", "examples/console", "examples/duration", "examples/membership", "examples/turbo_blank"]
members = ["examples/calculator", "examples/console", "examples/duration", "examples/membership", "examples/text_transform", "examples/turbo_blank"]
[dependencies]
libc = "0.2.0"

View File

@ -3,7 +3,7 @@ task :test do
sh "bundle exec rake"
end
examples = ENV["EXAMPLES"] || "duration calculator console membership turbo_blank"
examples = ENV["EXAMPLES"] || "duration calculator console membership text_transform turbo_blank"
sh "bash ./examples/runner default #{examples}"
end

View File

@ -0,0 +1,11 @@
[package]
name = "text_transform"
version = "0.1.0"
authors = ["Godfrey Chan <godfrey@tilde.io>"]
[lib]
crate-type = ["cdylib"]
[dependencies.helix_runtime]
path = "../.."

View File

@ -0,0 +1,5 @@
source 'https://rubygems.org'
gem 'helix_runtime', path: '../../ruby'
gem 'rake', '~> 10.0'
gem 'rspec', '~> 3.4'

View File

@ -0,0 +1,37 @@
PATH
remote: ../../ruby
specs:
helix_runtime (0.5.0.alpha.2)
rake (>= 10.0)
thor (~> 0.19.4)
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.3)
rake (10.5.0)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
thor (0.19.4)
PLATFORMS
ruby
DEPENDENCIES
helix_runtime!
rake (~> 10.0)
rspec (~> 3.4)
BUNDLED WITH
1.14.4

View File

@ -0,0 +1,27 @@
require 'bundler/setup'
require 'helix_runtime/build_task'
require 'rspec/core/rake_task'
# For Windows
$stdout.sync = true
HelixRuntime::BuildTask.new("text_transform") do |t|
t.build_root = File.expand_path("../..", __dir__)
t.helix_lib_dir = File.expand_path("../../ruby/windows_build", __dir__)
t.pre_build = -> {
Dir.chdir("../../ruby") do
puts "\n\nBuilding helix runtime\n\n"
Bundler.with_clean_env do
sh "bundle exec rake compile:native"
end
puts "\nRuntime built\n\n"
end
}
end
RSpec::Core::RakeTask.new(:spec) do |t|
t.verbose = false
end
task :spec => :build
task :default => :spec

View File

@ -0,0 +1,2 @@
require 'helix_runtime'
require 'text_transform/native'

View File

@ -0,0 +1,2 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'text_transform'

View File

@ -0,0 +1,11 @@
require "spec_helper"
describe "TextTransform" do
it "can widen text" do
expect(TextTransform.widen("Hello Aaron (@tenderlove)!")).to eq("  ")
end
it "can narrowen text" do
expect(TextTransform.narrowen("  ")).to eq("Hello Aaron (@tenderlove)!")
end
end

View File

@ -0,0 +1,44 @@
#[macro_use]
extern crate helix_runtime;
declare_types! {
class TextTransform {
def widen(text: String) -> String {
text.chars().map(|char| {
match char {
' ' => '\u{3000}', '!' => '', '"' => '', '#' => '', '$' => '', '%' => '', '&' => '', '\'' => '',
'(' => '', ')' => '', '*' => '', '+' => '', ',' => '', '-' => '', '.' => '', '/' => '',
'0' => '', '1' => '', '2' => '', '3' => '', '4' => '', '5' => '', '6' => '', '7' => '',
'8' => '', '9' => '', ':' => '', ';' => '', '<' => '', '=' => '', '>' => '', '?' => '',
'@' => '', 'A' => '', 'B' => '', 'C' => '', 'D' => '', 'E' => '', 'F' => '', 'G' => '',
'H' => '', 'I' => '', 'J' => '', 'K' => '', 'L' => '', 'M' => '', 'N' => '', 'O' => '',
'P' => '', 'Q' => '', 'R' => '', 'S' => '', 'T' => '', 'U' => '', 'V' => '', 'W' => '',
'X' => '', 'Y' => '', 'Z' => '', '[' => '', '\\' => '', ']' => '', '^' => '', '_' => '_',
'`' => '', 'a' => '', 'b' => '', 'c' => '', 'd' => '', 'e' => '', 'f' => '', 'g' => '',
'h' => '', 'i' => '', 'j' => '', 'k' => '', 'l' => '', 'm' => '', 'n' => '', 'o' => '',
'p' => '', 'q' => '', 'r' => '', 's' => '', 't' => '', 'u' => '', 'v' => '', 'w' => '',
'x' => '', 'y' => '', 'z' => '', '{' => '', '|' => '', '}' => '', '~' => '', _ => char,
}
}).collect()
}
def narrowen(text: String) -> String {
text.chars().map(|char| {
match char {
'\u{3000}' => ' ', '' => '!', '' => '"', '' => '#', '' => '$', '' => '%', '' => '&', '' => '\'',
'' => '(', '' => ')', '' => '*', '' => '+', '' => ',', '' => '-', '' => '.', '' => '/',
'' => '0', '' => '1', '' => '2', '' => '3', '' => '4', '' => '5', '' => '6', '' => '7',
'' => '8', '' => '9', '' => ':', '' => ';', '' => '<', '' => '=', '' => '>', '' => '?',
'' => '@', '' => 'A', '' => 'B', '' => 'C', '' => 'D', '' => 'E', '' => 'F', '' => 'G',
'' => 'H', '' => 'I', '' => 'J', '' => 'K', '' => 'L', '' => 'M', '' => 'N', '' => 'O',
'' => 'P', '' => 'Q', '' => 'R', '' => 'S', '' => 'T', '' => 'U', '' => 'V', '' => 'W',
'' => 'X', '' => 'Y', '' => 'Z', '' => '[', '' => '\\', '' => ']', '' => '^', '_' => '_',
'' => '`', '' => 'a', '' => 'b', '' => 'c', '' => 'd', '' => 'e', '' => 'f', '' => 'g',
'' => 'h', '' => 'i', '' => 'j', '' => 'k', '' => 'l', '' => 'm', '' => 'n', '' => 'o',
'' => 'p', '' => 'q', '' => 'r', '' => 's', '' => 't', '' => 'u', '' => 'v', '' => 'w',
'' => 'x', '' => 'y', '' => 'z', '' => '{', '' => '|', '' => '}', '' => '~', _ => char,
}
}).collect()
}
}
}