cmultibyte 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +20 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/cmultibyte.gemspec +27 -0
- data/ext/Makefile +218 -0
- data/ext/cmultibyte.c +201 -0
- data/ext/extconf.rb +3 -0
- data/lib/cmultibyte/version.rb +3 -0
- metadata +105 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Burke Libbey
|
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,29 @@
|
|
1
|
+
# Cmultibyte
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'cmultibyte'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install cmultibyte
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/cmultibyte.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'cmultibyte/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "cmultibyte"
|
8
|
+
spec.version = Cmultibyte::VERSION
|
9
|
+
spec.authors = ["Burke Libbey"]
|
10
|
+
spec.email = ["burke@libbey.me"]
|
11
|
+
spec.description = %q{ActiveSupport::Multibyte::Unicode.tidy_bytes in C}
|
12
|
+
spec.summary = %q{ActiveSupport::Multibyte::Unicode.tidy_bytes in C.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.extensions = ["ext/extconf.rb"]
|
22
|
+
|
23
|
+
spec.add_runtime_dependency 'activesupport', '~> 3.2.0'
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
26
|
+
spec.add_development_dependency "rake"
|
27
|
+
end
|
data/ext/Makefile
ADDED
@@ -0,0 +1,218 @@
|
|
1
|
+
|
2
|
+
SHELL = /bin/sh
|
3
|
+
|
4
|
+
# V=0 quiet, V=1 verbose. other values don't work.
|
5
|
+
V = 0
|
6
|
+
Q1 = $(V:1=)
|
7
|
+
Q = $(Q1:0=@)
|
8
|
+
n=$(NULLCMD)
|
9
|
+
ECHO1 = $(V:1=@$n)
|
10
|
+
ECHO = $(ECHO1:0=@echo)
|
11
|
+
|
12
|
+
#### Start of system configuration section. ####
|
13
|
+
|
14
|
+
srcdir = .
|
15
|
+
topdir = /opt/boxen/rbenv/versions/1.9.3-p385-perf/include/ruby-1.9.1
|
16
|
+
hdrdir = /opt/boxen/rbenv/versions/1.9.3-p385-perf/include/ruby-1.9.1
|
17
|
+
arch_hdrdir = /opt/boxen/rbenv/versions/1.9.3-p385-perf/include/ruby-1.9.1/$(arch)
|
18
|
+
VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
|
19
|
+
prefix = $(DESTDIR)/opt/boxen/rbenv/versions/1.9.3-p385-perf
|
20
|
+
rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
|
21
|
+
exec_prefix = $(prefix)
|
22
|
+
vendorhdrdir = $(rubyhdrdir)/vendor_ruby
|
23
|
+
sitehdrdir = $(rubyhdrdir)/site_ruby
|
24
|
+
rubyhdrdir = $(includedir)/$(RUBY_BASE_NAME)-$(ruby_version)
|
25
|
+
vendordir = $(rubylibprefix)/vendor_ruby
|
26
|
+
sitedir = $(rubylibprefix)/site_ruby
|
27
|
+
ridir = $(datarootdir)/$(RI_BASE_NAME)
|
28
|
+
mandir = $(datarootdir)/man
|
29
|
+
localedir = $(datarootdir)/locale
|
30
|
+
libdir = $(exec_prefix)/lib
|
31
|
+
psdir = $(docdir)
|
32
|
+
pdfdir = $(docdir)
|
33
|
+
dvidir = $(docdir)
|
34
|
+
htmldir = $(docdir)
|
35
|
+
infodir = $(datarootdir)/info
|
36
|
+
docdir = $(datarootdir)/doc/$(PACKAGE)
|
37
|
+
oldincludedir = $(DESTDIR)/usr/include
|
38
|
+
includedir = $(prefix)/include
|
39
|
+
localstatedir = $(prefix)/var
|
40
|
+
sharedstatedir = $(prefix)/com
|
41
|
+
sysconfdir = $(prefix)/etc
|
42
|
+
datadir = $(datarootdir)
|
43
|
+
datarootdir = $(prefix)/share
|
44
|
+
libexecdir = $(exec_prefix)/libexec
|
45
|
+
sbindir = $(exec_prefix)/sbin
|
46
|
+
bindir = $(exec_prefix)/bin
|
47
|
+
rubylibdir = $(rubylibprefix)/$(ruby_version)
|
48
|
+
archdir = $(rubylibdir)/$(arch)
|
49
|
+
sitelibdir = $(sitedir)/$(ruby_version)
|
50
|
+
sitearchdir = $(sitelibdir)/$(sitearch)
|
51
|
+
vendorlibdir = $(vendordir)/$(ruby_version)
|
52
|
+
vendorarchdir = $(vendorlibdir)/$(sitearch)
|
53
|
+
|
54
|
+
NULLCMD = :
|
55
|
+
|
56
|
+
CC = /usr/bin/cc
|
57
|
+
CXX = g++
|
58
|
+
LIBRUBY = $(LIBRUBY_A)
|
59
|
+
LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
|
60
|
+
LIBRUBYARG_SHARED =
|
61
|
+
LIBRUBYARG_STATIC = -l$(RUBY_SO_NAME)-static
|
62
|
+
empty =
|
63
|
+
OUTFLAG = -o $(empty)
|
64
|
+
COUTFLAG = -o $(empty)
|
65
|
+
|
66
|
+
RUBY_EXTCONF_H =
|
67
|
+
cflags = $(optflags) $(debugflags) $(warnflags)
|
68
|
+
optflags = -O3
|
69
|
+
debugflags = -ggdb
|
70
|
+
warnflags = -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wshorten-64-to-32 -Wimplicit-function-declaration
|
71
|
+
CFLAGS = -fno-common -Wno-error=shorten-64-to-32 -pipe $(ARCH_FLAG)
|
72
|
+
INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir)
|
73
|
+
DEFS =
|
74
|
+
CPPFLAGS = -I'/opt/boxen/rbenv/versions/1.9.3-p385-perf/include' -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE $(DEFS) $(cppflags)
|
75
|
+
CXXFLAGS = $(CFLAGS) $(cxxflags)
|
76
|
+
ldflags = -L. -L'/opt/boxen/rbenv/versions/1.9.3-p385-perf/lib'
|
77
|
+
dldflags = -Wl,-undefined,dynamic_lookup -Wl,-multiply_defined,suppress -Wl,-flat_namespace
|
78
|
+
ARCH_FLAG =
|
79
|
+
DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG)
|
80
|
+
LDSHARED = $(CC) -dynamic -bundle
|
81
|
+
LDSHAREDXX = $(CXX) -dynamic -bundle
|
82
|
+
AR = ar
|
83
|
+
EXEEXT =
|
84
|
+
|
85
|
+
RUBY_BASE_NAME = ruby
|
86
|
+
RUBY_INSTALL_NAME = ruby
|
87
|
+
RUBY_SO_NAME = ruby
|
88
|
+
arch = x86_64-darwin12.2.0
|
89
|
+
sitearch = $(arch)
|
90
|
+
ruby_version = 1.9.1
|
91
|
+
ruby = /opt/boxen/rbenv/versions/1.9.3-p385-perf/bin/ruby
|
92
|
+
RUBY = $(ruby)
|
93
|
+
RM = rm -f
|
94
|
+
RM_RF = $(RUBY) -run -e rm -- -rf
|
95
|
+
RMDIRS = rmdir -p
|
96
|
+
MAKEDIRS = mkdir -p
|
97
|
+
INSTALL = /usr/bin/install -c
|
98
|
+
INSTALL_PROG = $(INSTALL) -m 0755
|
99
|
+
INSTALL_DATA = $(INSTALL) -m 644
|
100
|
+
COPY = cp
|
101
|
+
TOUCH = exit >
|
102
|
+
|
103
|
+
#### End of system configuration section. ####
|
104
|
+
|
105
|
+
preload =
|
106
|
+
|
107
|
+
libpath = . $(libdir)
|
108
|
+
LIBPATH = -L. -L$(libdir)
|
109
|
+
DEFFILE =
|
110
|
+
|
111
|
+
CLEANFILES = mkmf.log
|
112
|
+
DISTCLEANFILES =
|
113
|
+
DISTCLEANDIRS =
|
114
|
+
|
115
|
+
extout =
|
116
|
+
extout_prefix =
|
117
|
+
target_prefix =
|
118
|
+
LOCAL_LIBS =
|
119
|
+
LIBS = -lpthread -ldl -lobjc
|
120
|
+
SRCS = cmultibyte.c
|
121
|
+
OBJS = cmultibyte.o
|
122
|
+
TARGET = cmultibyte
|
123
|
+
DLLIB = $(TARGET).bundle
|
124
|
+
EXTSTATIC =
|
125
|
+
STATIC_LIB =
|
126
|
+
|
127
|
+
BINDIR = $(bindir)
|
128
|
+
RUBYCOMMONDIR = $(sitedir)$(target_prefix)
|
129
|
+
RUBYLIBDIR = $(sitelibdir)$(target_prefix)
|
130
|
+
RUBYARCHDIR = $(sitearchdir)$(target_prefix)
|
131
|
+
HDRDIR = $(rubyhdrdir)/ruby$(target_prefix)
|
132
|
+
ARCHHDRDIR = $(rubyhdrdir)/$(arch)/ruby$(target_prefix)
|
133
|
+
|
134
|
+
TARGET_SO = $(DLLIB)
|
135
|
+
CLEANLIBS = $(TARGET).bundle
|
136
|
+
CLEANOBJS = *.o *.bak
|
137
|
+
|
138
|
+
all: $(DLLIB)
|
139
|
+
static: $(STATIC_LIB)
|
140
|
+
.PHONY: all install static install-so install-rb
|
141
|
+
.PHONY: clean clean-so clean-rb
|
142
|
+
|
143
|
+
clean-static::
|
144
|
+
clean-rb-default::
|
145
|
+
clean-rb::
|
146
|
+
clean-so::
|
147
|
+
clean: clean-so clean-static clean-rb-default clean-rb
|
148
|
+
-$(Q)$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES) .*.time
|
149
|
+
|
150
|
+
distclean-rb-default::
|
151
|
+
distclean-rb::
|
152
|
+
distclean-so::
|
153
|
+
distclean: clean distclean-so distclean-rb-default distclean-rb
|
154
|
+
@-$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
|
155
|
+
@-$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
|
156
|
+
@-$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true
|
157
|
+
|
158
|
+
realclean: distclean
|
159
|
+
install: install-so install-rb
|
160
|
+
|
161
|
+
install-so: $(RUBYARCHDIR)/$(DLLIB)
|
162
|
+
$(RUBYARCHDIR)/$(DLLIB): $(DLLIB)
|
163
|
+
-$(Q)$(MAKEDIRS) $(@D)
|
164
|
+
$(INSTALL_PROG) $(DLLIB) $(@D)
|
165
|
+
clean-static::
|
166
|
+
-$(Q)$(RM) $(STATIC_LIB)
|
167
|
+
install-rb: pre-install-rb install-rb-default
|
168
|
+
install-rb-default: pre-install-rb-default
|
169
|
+
pre-install-rb: Makefile
|
170
|
+
pre-install-rb-default: Makefile
|
171
|
+
pre-install-rb-default:
|
172
|
+
$(ECHO) installing default cmultibyte libraries
|
173
|
+
./.RUBYARCHDIR.time:
|
174
|
+
$(Q) $(MAKEDIRS) $(RUBYARCHDIR)
|
175
|
+
$(Q) $(TOUCH) $@
|
176
|
+
|
177
|
+
site-install: site-install-so site-install-rb
|
178
|
+
site-install-so: install-so
|
179
|
+
site-install-rb: install-rb
|
180
|
+
|
181
|
+
.SUFFIXES: .c .m .cc .mm .cxx .cpp .C .o
|
182
|
+
|
183
|
+
.cc.o:
|
184
|
+
$(ECHO) compiling $(<)
|
185
|
+
$(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
|
186
|
+
|
187
|
+
.mm.o:
|
188
|
+
$(ECHO) compiling $(<)
|
189
|
+
$(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
|
190
|
+
|
191
|
+
.cxx.o:
|
192
|
+
$(ECHO) compiling $(<)
|
193
|
+
$(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
|
194
|
+
|
195
|
+
.cpp.o:
|
196
|
+
$(ECHO) compiling $(<)
|
197
|
+
$(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
|
198
|
+
|
199
|
+
.C.o:
|
200
|
+
$(ECHO) compiling $(<)
|
201
|
+
$(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
|
202
|
+
|
203
|
+
.c.o:
|
204
|
+
$(ECHO) compiling $(<)
|
205
|
+
$(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
|
206
|
+
|
207
|
+
.m.o:
|
208
|
+
$(ECHO) compiling $(<)
|
209
|
+
$(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
|
210
|
+
|
211
|
+
$(DLLIB): $(OBJS) Makefile
|
212
|
+
$(ECHO) linking shared-object $(DLLIB)
|
213
|
+
-$(Q)$(RM) $(@)
|
214
|
+
$(Q) $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
|
215
|
+
|
216
|
+
|
217
|
+
|
218
|
+
$(OBJS): $(hdrdir)/ruby.h $(hdrdir)/ruby/defines.h $(arch_hdrdir)/ruby/config.h
|
data/ext/cmultibyte.c
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
#include <stdint.h>
|
3
|
+
#include <stdio.h>
|
4
|
+
|
5
|
+
VALUE ActiveSupport = Qnil;
|
6
|
+
VALUE Multibyte = Qnil;
|
7
|
+
VALUE Unicode = Qnil;
|
8
|
+
int *cp1252;
|
9
|
+
|
10
|
+
static ID idUnpack;
|
11
|
+
static ID idPack;
|
12
|
+
|
13
|
+
static int inline is_cont(uint8_t byte) { return (byte > 127 && byte < 192); }
|
14
|
+
static int inline is_lead(uint8_t byte) { return (byte > 191 && byte < 245); }
|
15
|
+
static int inline is_unused(uint8_t byte) { return (byte > 240); }
|
16
|
+
static int inline is_restricted(uint8_t byte) { return (byte > 244); }
|
17
|
+
|
18
|
+
static uint8_t *tidy_byte2(uint8_t byte, uint8_t *dst) {
|
19
|
+
int map;
|
20
|
+
VALUE ary;
|
21
|
+
|
22
|
+
if (byte < 160) {
|
23
|
+
map = cp1252[byte];
|
24
|
+
if (map == -1) {
|
25
|
+
map = byte;
|
26
|
+
}
|
27
|
+
ary = rb_ary_new2(1);
|
28
|
+
rb_ary_store(ary, 0, INT2FIX(map));
|
29
|
+
ary = rb_funcall(ary, idPack, 1, rb_str_new2("U"));
|
30
|
+
ary = rb_funcall(ary, idUnpack, 1, rb_str_new2("C*"));
|
31
|
+
for (int i = 0; i < RARRAY_LEN(ary) ; i++) {
|
32
|
+
dst[i] = FIX2INT(RARRAY_PTR(ary)[i]);
|
33
|
+
}
|
34
|
+
return dst + RARRAY_LEN(ary);
|
35
|
+
} else if (byte < 192) {
|
36
|
+
dst[0] = 194;
|
37
|
+
dst[1] = byte;
|
38
|
+
return dst + 2;
|
39
|
+
} else {
|
40
|
+
dst[0] = 195;
|
41
|
+
dst[1] = byte - 64;
|
42
|
+
return dst + 2;
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
VALUE tidy_byte(uint8_t byte) {
|
47
|
+
int map;
|
48
|
+
VALUE ary;
|
49
|
+
|
50
|
+
if (byte < 160) {
|
51
|
+
map = cp1252[byte];
|
52
|
+
if (map == -1) {
|
53
|
+
map = byte;
|
54
|
+
}
|
55
|
+
ary = rb_ary_new2(1);
|
56
|
+
rb_ary_store(ary, 0, INT2FIX(map));
|
57
|
+
ary = rb_funcall(ary, idPack, 1, rb_str_new2("U"));
|
58
|
+
ary = rb_funcall(ary, idUnpack, 1, rb_str_new2("C*"));
|
59
|
+
return ary;
|
60
|
+
} else if (byte < 192) {
|
61
|
+
ary = rb_ary_new2(2);
|
62
|
+
rb_ary_store(ary, 0, INT2FIX(194));
|
63
|
+
rb_ary_store(ary, 1, INT2FIX(byte));
|
64
|
+
return ary;
|
65
|
+
} else {
|
66
|
+
ary = rb_ary_new2(2);
|
67
|
+
rb_ary_store(ary, 0, INT2FIX(195));
|
68
|
+
rb_ary_store(ary, 1, INT2FIX(byte - 64));
|
69
|
+
return ary;
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
VALUE rb_tidy_byte(VALUE self, VALUE byte) {
|
74
|
+
return tidy_byte(FIX2INT(byte));
|
75
|
+
}
|
76
|
+
|
77
|
+
static void cp1252_hash_to_array(VALUE cp1252_hash) {
|
78
|
+
cp1252 = malloc(256 * sizeof(int));
|
79
|
+
VALUE val;
|
80
|
+
for (int i = 0; i < 256; i++) {
|
81
|
+
val = rb_hash_aref(cp1252_hash, INT2FIX(i));
|
82
|
+
if (val == Qnil) {
|
83
|
+
cp1252[i] = -1;
|
84
|
+
} else {
|
85
|
+
cp1252[i] = FIX2INT(val);
|
86
|
+
}
|
87
|
+
}
|
88
|
+
}
|
89
|
+
|
90
|
+
static VALUE tidy_bytes(VALUE string) {
|
91
|
+
int conts_expected = 0;
|
92
|
+
int i, j;
|
93
|
+
uint8_t byte;
|
94
|
+
|
95
|
+
uint8_t *arr = malloc(4 * sizeof(uint8_t) * RSTRING_LEN(string));
|
96
|
+
uint8_t *curr = arr;
|
97
|
+
uint8_t *prev = curr;
|
98
|
+
uint8_t *last_lead = curr;
|
99
|
+
|
100
|
+
int did_write = 0;
|
101
|
+
|
102
|
+
for (i = 0; i < RSTRING_LEN(string) ; i++) {
|
103
|
+
byte = RSTRING_PTR(string)[i];
|
104
|
+
curr[0] = byte;
|
105
|
+
did_write = 0;
|
106
|
+
|
107
|
+
if (is_unused(byte) || is_restricted(byte)) {
|
108
|
+
curr = tidy_byte2(byte, curr);
|
109
|
+
did_write = 1;
|
110
|
+
} else if (is_cont(byte)) {
|
111
|
+
if (conts_expected == 0) {
|
112
|
+
curr = tidy_byte2(byte, curr);
|
113
|
+
did_write = 1;
|
114
|
+
} else {
|
115
|
+
conts_expected--;
|
116
|
+
}
|
117
|
+
} else {
|
118
|
+
if (conts_expected > 0) {
|
119
|
+
int start_index = last_lead-arr;
|
120
|
+
int end_index = curr-arr;
|
121
|
+
int len = end_index - start_index;
|
122
|
+
uint8_t *prev = curr;
|
123
|
+
|
124
|
+
uint8_t temp[len];
|
125
|
+
for (j = 0; j < len; j++) {
|
126
|
+
temp[j] = arr[start_index + j];
|
127
|
+
}
|
128
|
+
|
129
|
+
curr = arr + start_index;
|
130
|
+
for (j = 0; j < len; j++) {
|
131
|
+
curr = tidy_byte2(temp[j], curr);
|
132
|
+
}
|
133
|
+
|
134
|
+
//last_lead += (curr-prev);
|
135
|
+
conts_expected = 0;
|
136
|
+
}
|
137
|
+
if (is_lead(byte)) {
|
138
|
+
if (i == RSTRING_LEN(string) - 1) {
|
139
|
+
curr = tidy_byte2(byte, curr);
|
140
|
+
did_write = 1;
|
141
|
+
} else {
|
142
|
+
if (byte < 224) {
|
143
|
+
conts_expected = 1;
|
144
|
+
} else if (byte < 240) {
|
145
|
+
conts_expected = 2;
|
146
|
+
} else {
|
147
|
+
conts_expected = 3;
|
148
|
+
}
|
149
|
+
last_lead = curr;
|
150
|
+
}
|
151
|
+
}
|
152
|
+
}
|
153
|
+
if (!did_write) {
|
154
|
+
*curr++ = byte;
|
155
|
+
}
|
156
|
+
}
|
157
|
+
VALUE str = rb_str_new((const char *)arr, curr-arr);
|
158
|
+
free(arr);
|
159
|
+
return str;
|
160
|
+
}
|
161
|
+
|
162
|
+
static VALUE force_tidy_bytes(VALUE string) {
|
163
|
+
uint8_t *arr = malloc(4 * sizeof(uint8_t) * RSTRING_LEN(string));
|
164
|
+
uint8_t *curr = arr;
|
165
|
+
for (int i = 0 ; i < RSTRING_LEN(string) ; i++) {
|
166
|
+
curr = tidy_byte2(RSTRING_PTR(string)[i], curr);
|
167
|
+
}
|
168
|
+
VALUE str = rb_str_new((const char *)arr, curr - arr);
|
169
|
+
free(arr);
|
170
|
+
return str;
|
171
|
+
}
|
172
|
+
|
173
|
+
VALUE rb_tidy_bytes(int argc, VALUE *argv, VALUE self) {
|
174
|
+
VALUE string, force;
|
175
|
+
rb_scan_args(argc, argv, "11", &string, &force);
|
176
|
+
if (force != Qfalse && force != Qnil) {
|
177
|
+
return force_tidy_bytes(string);
|
178
|
+
} else {
|
179
|
+
return tidy_bytes(string);
|
180
|
+
}
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
void Init_cmultibyte() {
|
185
|
+
idUnpack = rb_intern("unpack");
|
186
|
+
idPack = rb_intern("pack");
|
187
|
+
|
188
|
+
rb_funcall(rb_cObject, rb_intern("require"), 1, rb_str_new2("active_support/all"));
|
189
|
+
|
190
|
+
ActiveSupport = rb_const_get(rb_cObject, rb_intern("ActiveSupport"));
|
191
|
+
Multibyte = rb_const_get_at(ActiveSupport, rb_intern("Multibyte"));
|
192
|
+
Unicode = rb_const_get_at(Multibyte, rb_intern("Unicode"));
|
193
|
+
|
194
|
+
VALUE database = rb_funcall(Unicode, rb_intern("database"), 0);
|
195
|
+
rb_funcall(database, rb_intern("load"), 0);
|
196
|
+
VALUE cp1252_hash = rb_ivar_get(database, rb_intern("@cp1252"));
|
197
|
+
cp1252_hash_to_array(cp1252_hash);
|
198
|
+
|
199
|
+
rb_define_singleton_method(Unicode, "tidy_byte", rb_tidy_byte, 1);
|
200
|
+
rb_define_singleton_method(Unicode, "tidy_bytes", rb_tidy_bytes, -1);
|
201
|
+
}
|
data/ext/extconf.rb
ADDED
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cmultibyte
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.2
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Burke Libbey
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.2.0
|
20
|
+
none: false
|
21
|
+
name: activesupport
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
requirement: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 3.2.0
|
29
|
+
none: false
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
version_requirements: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ~>
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '1.3'
|
36
|
+
none: false
|
37
|
+
name: bundler
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
requirement: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '1.3'
|
45
|
+
none: false
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
version_requirements: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
none: false
|
53
|
+
name: rake
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
none: false
|
62
|
+
description: ActiveSupport::Multibyte::Unicode.tidy_bytes in C
|
63
|
+
email:
|
64
|
+
- burke@libbey.me
|
65
|
+
executables: []
|
66
|
+
extensions:
|
67
|
+
- ext/extconf.rb
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- cmultibyte.gemspec
|
76
|
+
- ext/Makefile
|
77
|
+
- ext/cmultibyte.c
|
78
|
+
- ext/extconf.rb
|
79
|
+
- lib/cmultibyte/version.rb
|
80
|
+
homepage: ''
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
none: false
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
none: false
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.8.23
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: ActiveSupport::Multibyte::Unicode.tidy_bytes in C.
|
105
|
+
test_files: []
|