clocale 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0005bbb9736e656c500a2e9ba42b921782c028c6
4
+ data.tar.gz: 01dfd8111e5ffac5511264d75b3451d8847bb5e4
5
+ SHA512:
6
+ metadata.gz: 4b62693d42061a404998e93de6d5101358139bb611e1f4c85d9cd47986b129e57f178dadd62482eb5c24d6f82c7acd8e2ec6c0496b4fb3d17141d8481b754573
7
+ data.tar.gz: 9d796fcf11c827a93ac174d20b0baacc847c89c9393d14fe921fe84d47da91a14d539b6883e1f63ee1e8da82198abd7f164621a98f92f54a130d6a6e8db0aa93
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Claudio Bley
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,10 @@
1
+ About
2
+ ------
3
+
4
+ This Ruby extension provides access to the C library's `setlocale`, `strxfrm`
5
+ and `strcoll` functions which are inherently useful for proper sorting
6
+ (collation) of strings.
7
+
8
+ This extension is quite similar to https://github.com/seanohalpin/ffi-locale but
9
+ does not use FFI because the `LC_*` constants are not stable across different C
10
+ libraries.
@@ -0,0 +1,16 @@
1
+ require 'rake/extensiontask'
2
+ require 'rubygems/tasks'
3
+
4
+ Gem::Tasks.new
5
+
6
+ Rake::ExtensionTask.new 'clocale' do |ext|
7
+ ext.lib_dir = 'lib/clocale'
8
+ end
9
+
10
+ # simple sanity check:
11
+
12
+ task test: %w[compile] do
13
+ ruby '-Ilib', '-rclocale', '-e', 'p CLocale.setlocale(CLocale::LC_ALL, "")'
14
+ end
15
+
16
+ task default: :test
@@ -0,0 +1,19 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = 'clocale'
3
+ spec.version = '0.0.1'
4
+ spec.authors = ['Claudio Bley']
5
+ spec.email = ['claudio.bley@gmail.com']
6
+ spec.summary = "A Ruby gem that wraps C locale functions."
7
+ spec.homepage = 'https://github.com/avdv'
8
+ spec.license = 'MIT'
9
+
10
+ spec.extensions = %w[ext/clocale/extconf.rb]
11
+
12
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
13
+ f.match(%r{^.gitignore})
14
+ end
15
+
16
+ spec.add_development_dependency 'rake'
17
+ spec.add_development_dependency 'rake-compiler'
18
+ spec.add_development_dependency 'rubygems-tasks'
19
+ end
@@ -0,0 +1,106 @@
1
+ #include <ruby.h>
2
+ #include <locale.h>
3
+ #include <string.h>
4
+
5
+ static VALUE
6
+ lc_setlocale(VALUE self, VALUE category, VALUE locale) {
7
+ const char* loc = NULL;
8
+ int cat = FIX2INT(category);
9
+
10
+ if (!NIL_P(locale)) {
11
+ loc = StringValueCStr(locale);
12
+ }
13
+ char* ret = setlocale(cat, loc);
14
+
15
+ if (ret == NULL) {
16
+ rb_raise(rb_eRuntimeError, "error calling setlocale(%d, \"%s\")", cat, loc);
17
+ } else {
18
+ return rb_str_new2(ret);
19
+ }
20
+ }
21
+
22
+ static VALUE
23
+ lc_strcoll(VALUE self, VALUE s1, VALUE s2) {
24
+ int ret = strcoll(StringValueCStr(s1), StringValueCStr(s2));
25
+
26
+ // clamp ret to range [-1; 1]
27
+ if (ret < 0) ret = -1;
28
+ if (ret > 0) ret = 1;
29
+
30
+ return INT2FIX(ret);
31
+ }
32
+
33
+ static VALUE
34
+ lc_strxfrm(VALUE self, VALUE str) {
35
+ VALUE ret;
36
+ char* c_str = StringValueCStr(str);
37
+ size_t size = 0;
38
+ char* buf = NULL;
39
+
40
+ for (;;) {
41
+ size_t needed = strxfrm(buf, c_str, size);
42
+
43
+ if (needed == 0) {
44
+ ret = str;
45
+ break;
46
+ } else if (needed < size) {
47
+ ret = rb_str_new2(buf);
48
+ break;
49
+ } else {
50
+ size = needed + 1;
51
+ buf = realloc(buf, size);
52
+
53
+ if (buf == NULL) rb_raise(rb_eNoMemError, "could not allocate %u bytes", size);
54
+ }
55
+ }
56
+
57
+ free(buf);
58
+
59
+ return ret;
60
+ }
61
+
62
+ /*
63
+ * Initialize the extension by defining the CLocale module.
64
+ */
65
+
66
+ void
67
+ Init_clocale(void) {
68
+ VALUE module = rb_define_module("CLocale");
69
+
70
+ #define constant(LC) rb_define_const(module, #LC, INT2FIX(LC))
71
+
72
+ /* categories defined by POSIX */
73
+ constant(LC_ALL);
74
+ constant(LC_COLLATE);
75
+ constant(LC_CTYPE);
76
+ constant(LC_MESSAGES);
77
+ constant(LC_MONETARY);
78
+ constant(LC_NUMERIC);
79
+ constant(LC_TIME);
80
+
81
+ /* GNU extensions */
82
+ # ifdef LC_ADDRESS
83
+ constant(LC_ADDRESS);
84
+ # endif
85
+ # ifdef LC_IDENTIFICATION
86
+ constant(LC_IDENTIFICATION);
87
+ # endif
88
+ # ifdef LC_MEASUREMENT
89
+ constant(LC_MEASUREMENT);
90
+ # endif
91
+ # ifdef LC_NAME
92
+ constant(LC_NAME);
93
+ # endif
94
+ # ifdef LC_PAPER
95
+ constant(LC_PAPER);
96
+ # endif
97
+ # ifdef LC_TELEPHONE
98
+ constant(LC_TELEPHONE);
99
+ # endif
100
+
101
+ #undef constant
102
+
103
+ rb_define_module_function(module, "setlocale", lc_setlocale, 2);
104
+ rb_define_module_function(module, "strcoll", lc_strcoll, 2);
105
+ rb_define_module_function(module, "strxfrm", lc_strxfrm, 1);
106
+ }
@@ -0,0 +1,13 @@
1
+ require 'mkmf'
2
+
3
+ def check_functions
4
+ %w[setlocale strcoll strxfrm].each do |func|
5
+ abort "missing function `#{ func }``" unless have_func func
6
+ end
7
+ end
8
+
9
+ abort 'missing `locale.h`' unless have_header 'locale.h'
10
+
11
+ check_functions
12
+
13
+ create_makefile 'clocale/clocale'
@@ -0,0 +1,2 @@
1
+
2
+ require 'clocale/clocale'
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: clocale
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Claudio Bley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake-compiler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubygems-tasks
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - claudio.bley@gmail.com
58
+ executables: []
59
+ extensions:
60
+ - ext/clocale/extconf.rb
61
+ extra_rdoc_files: []
62
+ files:
63
+ - Gemfile
64
+ - LICENSE
65
+ - README.md
66
+ - Rakefile
67
+ - clocale.gemspec
68
+ - ext/clocale/clocale.c
69
+ - ext/clocale/extconf.rb
70
+ - lib/clocale.rb
71
+ homepage: https://github.com/avdv
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.6.13
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: A Ruby gem that wraps C locale functions.
95
+ test_files: []