soundx 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 06e4808727b131b50f1d37e805eb4660e3d0291437588ed2f77bc221691ba57b
4
+ data.tar.gz: e030973ecc248204ac6f7b6c4f09c73bd38fe69cf9b2bd4c8a94e34aa00ecd47
5
+ SHA512:
6
+ metadata.gz: c61cc1f4bfcf617bc34fc2562c703a6e2475756a5b53767ca8b8c3878f67d4c6a89e3c41819444345fe16e853ce398ae4dae0eadbbdef1817bad5999dea844f8
7
+ data.tar.gz: 7a04204d898953dd287b54331bce9d13862d5930519526857344b2fe6eda345842d084feac90a9a02d32441a1a95c257f8cf311e81fb498081939b0cebdc1a47
@@ -0,0 +1,4 @@
1
+ require 'mkmf'
2
+
3
+ dir_config('soundx')
4
+ create_makefile('soundx')
@@ -0,0 +1,127 @@
1
+ #include <stdlib.h>
2
+ #include <ruby.h>
3
+ #include <ctype.h>
4
+
5
+ static VALUE m_soundx;
6
+
7
+
8
+ /*
9
+ Mapping from wikipedia:
10
+
11
+ b, f, p, v => 1
12
+ c, g, j, k, q, s, x, z => 2
13
+ d, t => 3
14
+ l => 4
15
+ m, n => 5
16
+ r => 6
17
+ */
18
+
19
+ /*
20
+ Pre-computed mapping of a-z to the above
21
+ table.
22
+ */
23
+ static const char mapping[128] = {
24
+ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
25
+ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
26
+ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
27
+ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
28
+ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
29
+ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
30
+ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
31
+ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
32
+ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
33
+ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
34
+ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
35
+ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
36
+ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE,
37
+ 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, '0',
38
+ '1', '2', '3', '0', '1', '2', '0',
39
+ '0', '2', '2', '4', '5', '5', '0',
40
+ '1', '2', '6', '2', '3', '0', '1',
41
+ '0', '2', '0', '2', 0xFE, 0xFE, 0xFE,
42
+ 0xFE, 0xFE
43
+ };
44
+
45
+ static VALUE
46
+ rb_soundx(int argc, VALUE* argv, VALUE self)
47
+ {
48
+ VALUE input;
49
+ const unsigned char* src;
50
+ int written = 0;
51
+ size_t srclen;
52
+ char *dest = malloc(5);
53
+
54
+ if (!dest) {
55
+ rb_raise(rb_eNoMemError, "malloc failed");
56
+ }
57
+
58
+ rb_scan_args(argc, argv, "1", &input);
59
+
60
+ if (TYPE(input) != T_STRING) {
61
+ rb_raise(rb_eTypeError, "expected a String");
62
+ }
63
+
64
+ srclen = RSTRING_LEN(input);
65
+ src = (const unsigned char*) StringValueCStr(input);
66
+
67
+ dest[0] = toupper(src[0]);
68
+
69
+ for(size_t i = 1; i <= srclen; i++) {
70
+ if (written >= 3) {
71
+ break;
72
+ }
73
+
74
+ if ('\0' == src[i]) {
75
+ break;
76
+ }
77
+
78
+ if (src[i] > 128) {
79
+ free(dest);
80
+ rb_raise(rb_eArgError, "non-ASCII character found");
81
+ break;
82
+ }
83
+
84
+ unsigned char current = tolower(src[i]);
85
+
86
+ char match = mapping[ current ];
87
+ if (0xFE == (int) match) {
88
+ continue;
89
+ }
90
+
91
+ // Skip if previous character is the same
92
+ if (dest[written] == match) {
93
+ continue;
94
+ }
95
+
96
+ // We landed on a vowel-like character,
97
+ // so skip to the next char
98
+ if ('0' == match) {
99
+ continue;
100
+ }
101
+
102
+ dest[written+1] = mapping[ current ];
103
+
104
+ written++;
105
+ }
106
+
107
+ // Pad at most three '0' characters
108
+ // to handle really short names
109
+ if (written < 3) {
110
+ for( int i = (3-written); i > 0; i--) {
111
+ dest[written+i] = '0';
112
+ }
113
+ }
114
+
115
+ dest[4] = '\0';
116
+
117
+ VALUE rbString = rb_str_new_cstr(dest);
118
+ free(dest);
119
+
120
+ return rbString;
121
+ }
122
+ void
123
+ Init_soundx() {
124
+ m_soundx = rb_define_module("SoundX");
125
+
126
+ rb_define_module_function(m_soundx, "encode", rb_soundx, -1);
127
+ }
@@ -0,0 +1,3 @@
1
+ module SoundX
2
+ VERSION = '1.0.2'
3
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: soundx
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - James Cook
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake-compiler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest-line
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.11'
55
+ description:
56
+ email:
57
+ executables: []
58
+ extensions:
59
+ - ext/soundx/extconf.rb
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ext/soundx/extconf.rb
63
+ - ext/soundx/soundx.c
64
+ - lib/soundx/version.rb
65
+ homepage: https://github.com/jamescook/soundx
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">"
76
+ - !ruby/object:Gem::Version
77
+ version: '2.1'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubygems_version: 3.0.3
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Soundex library written in C
88
+ test_files: []