opencc-rb 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 36111587c88ebb5bad5627397aec98d5a5100863ba55c6da2decf5c6740e0008
4
- data.tar.gz: 58d1302032b5612973a48d13df89ca86bdfefb619e6b70261fa587ac137c5680
3
+ metadata.gz: d68ddb8428b8039e56ea9784495cf60534822013694fdda4c1e94815169fb614
4
+ data.tar.gz: d3bb445106db7e37f3347f263bc2bb083ee4eaa11f8336dcb5ca36263ba8a3e5
5
5
  SHA512:
6
- metadata.gz: a589ef598d17e5506844398be9c91e0b67053cbcfa5bad8731a926a0c016f849ca5ebac405921c98ed08b76d29704b4033f37394e477e3c3f79104d8c429826e
7
- data.tar.gz: 89dc71b414e3515009836a0ee5f3940289cb794a610a6ecca0ed3a5b4580a94e122d0db71c63d2f60f83e46d69b22520cc203eb54370ba05479792b3a958a89a
6
+ metadata.gz: b04c2067f6dad9761b3d27113e1dae9760b085734208735a5983046c0fa1d52a98179eb285eaa09b0ff8730bc41b5539223af021840878fada02b73cd2aa7500
7
+ data.tar.gz: 219a38feecfc811789f86e35390c3b2fe394d9d03b1ae114b17458e7a36d38674b2c34582c8037347ebf55440ede174cb0c25deaedc49b7091892a3c1dff00ac
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- opencc-rb (1.0.5)
4
+ opencc-rb (1.0.6)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -21,4 +21,4 @@ DEPENDENCIES
21
21
  rake-compiler
22
22
 
23
23
  BUNDLED WITH
24
- 2.2.16
24
+ 2.2.22
data/README.md CHANGED
@@ -92,14 +92,11 @@ OpenCC.jp2t("芸術 欠航 飲料缶") # => "藝術 缺航 飲料罐"
92
92
  OpenCC.tw2t("想到自己一緊張就口吃,我就沒胃口吃飯") # => "想到自己一緊張就口吃,我就沒胃口喫飯"
93
93
 
94
94
  # Or
95
- converter = OpenCC[:s2t]
96
- converter = OpenCC.new(:s2t)
95
+ # You *must* call close when you're finished
97
96
  converter = OpenCC::Converter.new(:s2t)
98
97
  converter.convert("汉字") # => '漢字'
99
98
  converter.close # => true
100
- converter.closed? # => true
101
99
  converter.close # => false
102
- converter.convert("汉字") # => nil
103
100
  ```
104
101
 
105
102
  ## Development
data/ext/opencc/opencc.c CHANGED
@@ -1,38 +1,89 @@
1
1
  #include "opencc.h"
2
2
 
3
- VALUE rb_mOpenCC;
4
- VALUE rb_mOpenCC_Mixin;
3
+ #define OPENCC_DEFAULT_CONFIG_SIMP_TO_TRAD "s2t.json"
5
4
 
6
- static VALUE rb_opencc_open(VALUE self, VALUE rb_cfg) {
7
- const char *cfg = OPENCC_DEFAULT_CONFIG_SIMP_TO_TRAD;
8
- opencc_t ptr;
5
+ VALUE mOpenCC;
6
+ VALUE cConverter;
9
7
 
10
- if (TYPE(rb_cfg) == T_STRING && RSTRING_LEN(rb_cfg) > 0) {
11
- cfg = RSTRING_PTR(rb_cfg);
8
+ typedef struct {
9
+ opencc_t opencc;
10
+ } opencc_converter_t;
11
+
12
+ static void opencc_converter_t_dfree(void* _ptr) {
13
+ opencc_converter_t* ptr = (opencc_converter_t*)_ptr;
14
+ if (ptr->opencc != NULL && ptr->opencc != (opencc_t) - 1) {
15
+ opencc_close(ptr->opencc);
16
+ }
17
+ free(ptr);
18
+ }
19
+
20
+ static size_t opencc_converter_t_dsize(const void* _ptr) {
21
+ return sizeof(opencc_converter_t);
22
+ }
23
+
24
+ static const rb_data_type_t opencc_converter_data_type = {
25
+ .wrap_struct_name = "opencc converter object",
26
+ .function = {
27
+ .dfree = opencc_converter_t_dfree,
28
+ .dsize = opencc_converter_t_dsize,
29
+ },
30
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
31
+ };
32
+
33
+ VALUE opencc_converter_t_alloc(VALUE self) {
34
+ opencc_converter_t *ptr = malloc(sizeof(opencc_converter_t));
35
+
36
+ return TypedData_Wrap_Struct(self, &opencc_converter_data_type, ptr);
37
+ }
38
+
39
+ static VALUE opencc_converter_t_initialize(int argc, VALUE* argv, VALUE self) {
40
+ opencc_converter_t *ptr;
41
+ const char *cfg = NULL;
42
+
43
+ if (argv[0] != Qnil) {
44
+ switch (TYPE(argv[0])) {
45
+ case T_SYMBOL:
46
+ cfg = RSTRING_PTR(rb_sym_to_s(argv[0]));
47
+ break;
48
+ case T_STRING:
49
+ cfg = RSTRING_PTR(argv[0]);
50
+ break;
51
+ }
12
52
  }
13
53
 
14
- ptr = opencc_open(cfg);
54
+ TypedData_Get_Struct(self, opencc_converter_t, &opencc_converter_data_type, ptr);
55
+
56
+ ptr->opencc = opencc_open(cfg);
15
57
 
16
58
  // On error the return value will be (opencc_t) -1.
17
- if (ptr == (opencc_t) - 1) {
18
- return Qnil;
19
- } else {
20
- return LONG2FIX((long)ptr);
59
+ if (ptr->opencc == (opencc_t) - 1) {
60
+ free(ptr);
61
+ rb_raise(rb_eException, "%s", "(opencc_open) failed to allocate instance of opencc");
21
62
  }
63
+ return self;
22
64
  }
23
65
 
24
- static VALUE rb_opencc_convert(VALUE self, VALUE rb_opencc, VALUE rb_str) {
25
- opencc_t ptr = (opencc_t) FIX2LONG(rb_opencc);
26
- char * buff = opencc_convert_utf8(ptr, RSTRING_PTR(rb_str), RSTRING_LEN(rb_str));
27
- VALUE converted = rb_utf8_str_new_cstr(buff);
28
- opencc_convert_utf8_free(buff);
29
- return converted;
66
+ static VALUE opencc_converter_t_convert(VALUE self, VALUE input) {
67
+ opencc_converter_t *ptr;
68
+ TypedData_Get_Struct(self, opencc_converter_t, &opencc_converter_data_type, ptr);
69
+
70
+ if (ptr->opencc != NULL) {
71
+ char * buff = opencc_convert_utf8(ptr->opencc, RSTRING_PTR(input), RSTRING_LEN(input));
72
+ VALUE result = rb_utf8_str_new_cstr(buff);
73
+ opencc_convert_utf8_free(buff);
74
+ return result;
75
+ } else {
76
+ rb_warn("%s", "opencc has been closed");
77
+ return Qnil;
78
+ }
30
79
  }
31
80
 
32
- static VALUE rb_opencc_close(VALUE self, VALUE rb_opencc) {
33
- opencc_t ptr = (opencc_t) FIX2LONG(rb_opencc);
81
+ static VALUE opencc_converter_t_close(VALUE self) {
82
+ opencc_converter_t *ptr;
83
+ TypedData_Get_Struct(self, opencc_converter_t, &opencc_converter_data_type, ptr);
34
84
 
35
- if (opencc_close(ptr) == 0) {
85
+ if (ptr->opencc != NULL && opencc_close(ptr->opencc) == 0) {
86
+ ptr->opencc = NULL;
36
87
  return Qtrue;
37
88
  } else {
38
89
  return Qfalse;
@@ -40,10 +91,11 @@ static VALUE rb_opencc_close(VALUE self, VALUE rb_opencc) {
40
91
  }
41
92
 
42
93
  void Init_opencc(void) {
43
- rb_mOpenCC = rb_define_module("OpenCC");
44
- rb_mOpenCC_Mixin = rb_define_module_under(rb_mOpenCC, "Mixin");
94
+ mOpenCC = rb_define_module("OpenCC");
95
+ cConverter = rb_define_class_under(mOpenCC, "Converter", rb_cObject);
45
96
 
46
- rb_define_private_method(rb_mOpenCC_Mixin, "opencc_open", rb_opencc_open, 1);
47
- rb_define_private_method(rb_mOpenCC_Mixin, "opencc_close", rb_opencc_close, 1);
48
- rb_define_private_method(rb_mOpenCC_Mixin, "opencc_convert", rb_opencc_convert, 2);
97
+ rb_define_alloc_func(cConverter, opencc_converter_t_alloc);
98
+ rb_define_method(cConverter, "initialize", opencc_converter_t_initialize, -1);
99
+ rb_define_method(cConverter, "convert", opencc_converter_t_convert, 1);
100
+ rb_define_method(cConverter, "close", opencc_converter_t_close, 0);
49
101
  }
data/ext/opencc/opencc.h CHANGED
@@ -4,8 +4,4 @@
4
4
  #include <ruby.h>
5
5
  #include <opencc/opencc.h>
6
6
 
7
- static VALUE rb_opencc_open(VALUE self, VALUE rb_cfg);
8
- static VALUE rb_opencc_convert(VALUE self, VALUE rb_occid, VALUE rb_str);
9
- static VALUE rb_opencc_close(VALUE self, VALUE rb_occid);
10
-
11
7
  #endif /* RUBY_OPENCC_H */
data/lib/opencc.rb CHANGED
@@ -1,28 +1,31 @@
1
1
  require "opencc/version"
2
2
  require "opencc/opencc"
3
- require "opencc/converter"
4
3
 
5
4
  module OpenCC
6
5
  CONFIGS = %w[ s2t t2s s2tw tw2s s2hk hk2s s2twp tw2sp hk2t t2hk t2jp jp2t tw2t ]
7
- DEFAULT_CONFIG = "s2t"
8
6
 
9
7
  class << self
10
8
  def with(config, &block)
11
- Converter.with(config, &block)
12
- end
13
-
14
- def [](config)
15
- config && new(config)
16
- end
17
-
18
- def new(config = nil)
19
- Converter.new(config)
9
+ converter = Converter.new(config)
10
+
11
+ begin
12
+ if block.arity == 0
13
+ converter.instance_eval(&block)
14
+ else
15
+ yield converter
16
+ end
17
+ ensure
18
+ converter && converter.close
19
+ end
20
20
  end
21
21
 
22
22
  CONFIGS.each do |config|
23
23
  define_method :"#{config}" do |input|
24
- with(config) do |converter|
24
+ begin
25
+ converter = Converter.new(config)
25
26
  converter.convert(input)
27
+ ensure
28
+ converter && converter.close
26
29
  end
27
30
  end
28
31
  end
@@ -1,3 +1,3 @@
1
1
  module OpenCC
2
- VERSION = "1.0.5"
2
+ VERSION = "1.0.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opencc-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - zsj
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-04 00:00:00.000000000 Z
11
+ date: 2021-08-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This gem is for conversions between Traditional Chinese, Simplified Chinese
14
14
  and Japanese Kanji (Shinjitai). It supports character-level and phrase-level conversion,
@@ -35,7 +35,6 @@ files:
35
35
  - ext/opencc/opencc.c
36
36
  - ext/opencc/opencc.h
37
37
  - lib/opencc.rb
38
- - lib/opencc/converter.rb
39
38
  - lib/opencc/version.rb
40
39
  - opencc.gemspec
41
40
  homepage: https://github.com/songjiz/opencc-rb
@@ -59,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
58
  - !ruby/object:Gem::Version
60
59
  version: '0'
61
60
  requirements: []
62
- rubygems_version: 3.2.15
61
+ rubygems_version: 3.2.22
63
62
  signing_key:
64
63
  specification_version: 4
65
64
  summary: This gem is for conversions between Traditional Chinese, Simplified Chinese
@@ -1,70 +0,0 @@
1
- module OpenCC
2
- class Converter
3
- include OpenCC::Mixin
4
-
5
- class << self
6
- def with(config, &block)
7
- converter = new(config)
8
- if block.arity == 0
9
- converter.instance_eval(&block)
10
- else
11
- yield converter
12
- end
13
- ensure
14
- converter.close
15
- end
16
- end
17
-
18
- attr_reader :config
19
-
20
- # *<tt>:config</tt> - The config file name without .json suffix, default "s2t"
21
- def initialize(config = nil)
22
- @config = (config || OpenCC::DEFAULT_CONFIG).to_s
23
-
24
- if !OpenCC::CONFIGS.include?(@config)
25
- raise ArgumentError, "Unsupported configuration name #{@config.inspect}, expected one of #{OpenCC::CONFIGS.join(', ')}"
26
- end
27
-
28
- @mutex = Mutex.new
29
- @closed = false
30
- end
31
-
32
- def convert(input)
33
- synchronize do
34
- return if closed?
35
-
36
- @__opencc__ ||= opencc_open(config_file_name)
37
-
38
- if @__opencc__.nil?
39
- raise RuntimeError, "OpenCC failed to load (#{config_file_name})"
40
- end
41
-
42
- opencc_convert(@__opencc__, input.force_encoding(Encoding::UTF_8))
43
- end
44
- end
45
-
46
- def close
47
- synchronize do
48
- return false if closed?
49
- return false if @__opencc__.nil?
50
- return false if !opencc_close(@__opencc__)
51
-
52
- @__opencc__ = nil
53
- @closed = true
54
- end
55
- end
56
-
57
- def closed?
58
- @closed
59
- end
60
-
61
- private
62
- def synchronize(&block)
63
- @mutex.synchronize(&block)
64
- end
65
-
66
- def config_file_name
67
- "#{@config}.json"
68
- end
69
- end
70
- end