cicu 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +21 -0
- data/.travis.yml +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +52 -0
- data/Rakefile +20 -0
- data/cicu.gemspec +25 -0
- data/ext/cicu/cicu.c +82 -0
- data/ext/cicu/cicu.h +14 -0
- data/ext/cicu/extconf.rb +5 -0
- data/lib/cicu.rb +6 -0
- data/lib/cicu/icubo.so +0 -0
- data/lib/cicu/version.rb +3 -0
- data/spec/cicu_spec.rb +36 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2b8e72cfb8379d5249cf6965b45d6a774c1a4f8b
|
4
|
+
data.tar.gz: 43836e1ef235b269a90a53be442fa6fb90694a86
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e9f16004c700fdf515fb0216a11af2071719fc475d65f76a0f136fa9439ca341ebfa70158db8ab716f6bafc181eea76c031ffe45230b308f956b088d5ad37a68
|
7
|
+
data.tar.gz: 5066582856134b980be1034fb73ca68c9fb40b5a41b95ff73b817aa46feacc87e8387a3539c65ae9de1394ce4b78b094aea1a01ac8bdb8e3e33d38b063984d24
|
data/.gitignore
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
*.bundle
|
4
|
+
.bundle
|
5
|
+
.config
|
6
|
+
.yardoc
|
7
|
+
.DS_Store
|
8
|
+
Gemfile.lock
|
9
|
+
InstalledFiles
|
10
|
+
_yardoc
|
11
|
+
coverage
|
12
|
+
doc/
|
13
|
+
lib/bundler/man
|
14
|
+
pkg
|
15
|
+
rdoc
|
16
|
+
spec/reports
|
17
|
+
test/tmp
|
18
|
+
test/version_tmp
|
19
|
+
tmp
|
20
|
+
.ruby-gemset
|
21
|
+
.ruby-version
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Benjamin Groessing
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Cicu
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/groe/cicu.png?branch=master)](http://travis-ci.org/groe/cicu)
|
4
|
+
|
5
|
+
Custom ICU rulesets for collation in ruby - requires libicu
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'cicu'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install cicu
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Create a ruleset
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
ruleset = Cicu::Ruleset.new("[normalization on] & b < a < c")
|
27
|
+
```
|
28
|
+
|
29
|
+
Sort an array
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
ary = %w( a b c ba bb bc )
|
33
|
+
sorted_ary = ruleset.sort_by { |el| ruleset.sort_key(el) }
|
34
|
+
# => "["b", "bb", "ba", "bc", "a", "c"]"
|
35
|
+
```
|
36
|
+
|
37
|
+
Get the sort key for a string
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
ruleset.sort_key("abc")
|
41
|
+
# => "*\x04)*\x05\x01\a\x01\a"
|
42
|
+
```
|
43
|
+
|
44
|
+
You can store sort keys in a database column to use them in `ORDER BY` queries. Make sure to use a column type that supports binary strings (e.g. MySQLs [VARBINARY](http://dev.mysql.com/doc/refman/5.7/en/binary-varbinary.html)).
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
require 'rake/extensiontask'
|
4
|
+
|
5
|
+
desc "Cicu unit tests"
|
6
|
+
RSpec::Core::RakeTask.new(:test) do |t|
|
7
|
+
t.pattern = "spec/*_spec.rb"
|
8
|
+
t.verbose = true
|
9
|
+
end
|
10
|
+
|
11
|
+
gemspec = Gem::Specification.load('cicu.gemspec')
|
12
|
+
Rake::ExtensionTask.new do |ext|
|
13
|
+
ext.name = 'cicu'
|
14
|
+
ext.source_pattern = "*.{c,h}"
|
15
|
+
ext.ext_dir = 'ext/cicu'
|
16
|
+
ext.lib_dir = 'lib/cicu'
|
17
|
+
ext.gem_spec = gemspec
|
18
|
+
end
|
19
|
+
|
20
|
+
task :default => [:compile, :test]
|
data/cicu.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cicu/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "cicu"
|
8
|
+
gem.version = Cicu::VERSION
|
9
|
+
gem.authors = ["Benjamin Groessing"]
|
10
|
+
gem.email = ["contact@BYTEQ.com"]
|
11
|
+
gem.description = %q{ Custom ICU rulesets for collation in ruby - requires libicu }
|
12
|
+
gem.summary = %q{ Custom ICU rulesets for collation }
|
13
|
+
gem.homepage = "https://github.com/groe/cicu"
|
14
|
+
gem.license = "MIT"
|
15
|
+
|
16
|
+
gem.add_development_dependency "rspec", ">= 2.13.0"
|
17
|
+
gem.add_development_dependency "rake", ">= 1.9.1"
|
18
|
+
gem.add_development_dependency "rake-compiler", ">= 0.8.3"
|
19
|
+
|
20
|
+
gem.files = `git ls-files`.split($/)
|
21
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
22
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
23
|
+
gem.extensions = gem.files.grep(%r{/extconf\.rb$})
|
24
|
+
gem.require_paths = ["lib"]
|
25
|
+
end
|
data/ext/cicu/cicu.c
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
#include "cicu.h"
|
2
|
+
|
3
|
+
#include <unicode/ustring.h>
|
4
|
+
|
5
|
+
#define BUF_SIZE 10000
|
6
|
+
|
7
|
+
static void str_to_utf16(VALUE string, UChar *ustr, int32_t *ulen)
|
8
|
+
{
|
9
|
+
UErrorCode status = U_ZERO_ERROR;
|
10
|
+
string = StringValue(string);
|
11
|
+
u_strFromUTF8(ustr, BUF_SIZE, ulen, RSTRING_PTR(string), RSTRING_LEN(string), &status);
|
12
|
+
if (status == U_INVALID_CHAR_FOUND) ulen = 0;
|
13
|
+
}
|
14
|
+
|
15
|
+
static VALUE ruleset_allocate(VALUE klass)
|
16
|
+
{
|
17
|
+
UCollator *collator = NULL;
|
18
|
+
|
19
|
+
return Data_Wrap_Struct(klass, NULL, ruleset_deallocate, collator);
|
20
|
+
}
|
21
|
+
|
22
|
+
static void ruleset_deallocate(void * collator)
|
23
|
+
{
|
24
|
+
if(collator != NULL) {
|
25
|
+
ucol_close(collator);
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
static VALUE ruleset_initialize(VALUE self, VALUE rules) {
|
30
|
+
UChar uRules[BUF_SIZE];
|
31
|
+
UErrorCode status = U_ZERO_ERROR;
|
32
|
+
int32_t uRulesLen = 0;
|
33
|
+
UCollator *collator;
|
34
|
+
|
35
|
+
if (TYPE(rules) != T_STRING) {
|
36
|
+
rb_raise(rb_eTypeError, "rules must be a string");
|
37
|
+
}
|
38
|
+
if (RSTRING_LEN(rules) > BUF_SIZE) {
|
39
|
+
rb_raise(rb_eArgError, "given ruleset string exceeds buffer size");
|
40
|
+
}
|
41
|
+
|
42
|
+
str_to_utf16(rules, uRules, &uRulesLen);
|
43
|
+
collator = ucol_openRules(uRules, uRulesLen, UCOL_ON, UCOL_DEFAULT_STRENGTH, NULL, &status);
|
44
|
+
|
45
|
+
if (U_SUCCESS(status)) {
|
46
|
+
DATA_PTR(self) = collator;
|
47
|
+
}
|
48
|
+
else {
|
49
|
+
rb_raise(rb_eArgError, "Failed to initialize ICU ruleset");
|
50
|
+
}
|
51
|
+
|
52
|
+
return self;
|
53
|
+
}
|
54
|
+
|
55
|
+
static VALUE ruleset_sort_key(VALUE self, VALUE string) {
|
56
|
+
UCollator *collator;
|
57
|
+
char str[BUF_SIZE];
|
58
|
+
UChar ustr[BUF_SIZE];
|
59
|
+
int32_t len = 0;
|
60
|
+
int32_t uLen = 0;
|
61
|
+
|
62
|
+
if (TYPE(string) != T_STRING) {
|
63
|
+
rb_raise(rb_eTypeError, "sort_key expects a string argument");
|
64
|
+
}
|
65
|
+
if (RSTRING_LEN(string) > BUF_SIZE) {
|
66
|
+
rb_raise(rb_eArgError, "given string for sort_key exceeds buffer size");
|
67
|
+
}
|
68
|
+
|
69
|
+
Data_Get_Struct(self, UCollator, collator);
|
70
|
+
str_to_utf16(string, ustr, &uLen);
|
71
|
+
len = ucol_getSortKey(collator, ustr, uLen, (uint8_t*)str, BUF_SIZE);
|
72
|
+
|
73
|
+
return rb_str_new(str, len-1);
|
74
|
+
}
|
75
|
+
|
76
|
+
void Init_cicu() {
|
77
|
+
VALUE rb_mCicu = rb_define_module("Cicu");
|
78
|
+
VALUE rb_cRuleset = rb_define_class_under(rb_mCicu, "Ruleset", rb_cObject);
|
79
|
+
rb_define_alloc_func(rb_cRuleset, ruleset_allocate);
|
80
|
+
rb_define_method(rb_cRuleset, "initialize", ruleset_initialize, 1);
|
81
|
+
rb_define_method(rb_cRuleset, "sort_key", ruleset_sort_key, 1);
|
82
|
+
}
|
data/ext/cicu/cicu.h
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include <unicode/ucol.h>
|
3
|
+
|
4
|
+
static void str_to_utf16(VALUE, UChar *, int32_t*);
|
5
|
+
|
6
|
+
static VALUE ruleset_allocate(VALUE);
|
7
|
+
|
8
|
+
static void ruleset_deallocate(void *);
|
9
|
+
|
10
|
+
static VALUE ruleset_initialize(VALUE, VALUE);
|
11
|
+
|
12
|
+
static VALUE ruleset_sort_key(VALUE, VALUE);
|
13
|
+
|
14
|
+
void Init_cicu();
|
data/ext/cicu/extconf.rb
ADDED
data/lib/cicu.rb
ADDED
data/lib/cicu/icubo.so
ADDED
Binary file
|
data/lib/cicu/version.rb
ADDED
data/spec/cicu_spec.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'cicu'
|
2
|
+
|
3
|
+
describe Cicu::Ruleset do
|
4
|
+
describe "#new" do
|
5
|
+
it "should raise ArgumentError if rules are invalid" do
|
6
|
+
expect(proc { Cicu::Ruleset.new("?") }).to raise_exception(ArgumentError)
|
7
|
+
end
|
8
|
+
it "should raise TypeError if argument is not a string" do
|
9
|
+
expect(proc { Cicu::Ruleset.new([123]) }).to raise_exception(TypeError)
|
10
|
+
end
|
11
|
+
it "should raise ArgumentError if rules are too long" do
|
12
|
+
rules = "[normalization on] & " + 100_000.times.map { "a" }.join("<")
|
13
|
+
expect(proc { Cicu::Ruleset.new(rules) }).to raise_exception(ArgumentError)
|
14
|
+
end
|
15
|
+
it "should create a ruleset if rules are not too long" do
|
16
|
+
ruleset = Cicu::Ruleset.new("[normalization on] & a < a < a < a < a < a")
|
17
|
+
expect(ruleset.sort_key("123")).to eq("\x14\x16\x18\x01\a\x01\a")
|
18
|
+
end
|
19
|
+
it "should create a ruleset" do
|
20
|
+
ruleset = Cicu::Ruleset.new("[normalization on] & a")
|
21
|
+
expect(ruleset.sort_key("123")).to eq("\x14\x16\x18\x01\a\x01\a")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
describe "#sort_key" do
|
25
|
+
it "should raise TypeErrorif argument is not a string" do
|
26
|
+
ruleset = Cicu::Ruleset.new("[normalization on] & b < a < c")
|
27
|
+
expect(proc { ruleset.sort_key([123]) }).to raise_exception(TypeError)
|
28
|
+
end
|
29
|
+
it "should sort any array according to the rukes" do
|
30
|
+
ary = %w( a b c ba bb bc )
|
31
|
+
ruleset = Cicu::Ruleset.new("[normalization on] & b < a < c")
|
32
|
+
sorted_ary = ary.sort_by { |el| ruleset.sort_key(el) }
|
33
|
+
expect(sorted_ary).to eq(%w( b bb ba bc a c ))
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cicu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Benjamin Groessing
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.13.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.13.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.9.1
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.9.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake-compiler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.8.3
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.8.3
|
55
|
+
description: " Custom ICU rulesets for collation in ruby - requires libicu "
|
56
|
+
email:
|
57
|
+
- contact@BYTEQ.com
|
58
|
+
executables: []
|
59
|
+
extensions:
|
60
|
+
- ext/cicu/extconf.rb
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- cicu.gemspec
|
70
|
+
- ext/cicu/cicu.c
|
71
|
+
- ext/cicu/cicu.h
|
72
|
+
- ext/cicu/extconf.rb
|
73
|
+
- lib/cicu.rb
|
74
|
+
- lib/cicu/icubo.so
|
75
|
+
- lib/cicu/version.rb
|
76
|
+
- spec/cicu_spec.rb
|
77
|
+
homepage: https://github.com/groe/cicu
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.2.5
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Custom ICU rulesets for collation
|
101
|
+
test_files:
|
102
|
+
- spec/cicu_spec.rb
|