Skip to content

Commit a437b41

Browse files
committed
Support Ruby 1.8 fix GH-1
1 parent a10a1de commit a437b41

8 files changed

Lines changed: 92 additions & 18 deletions

File tree

Rakefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ file "lib/#{NAME}/#{NAME}.so" =>
1212
Dir.chdir("ext/#{NAME}") do
1313
# this does essentially the same thing
1414
# as what RubyGems does
15-
ruby "extconf.rb"
16-
sh "make"
15+
ruby "extconf.rb", *ARGV.grep(/\A--/)
16+
sh "make", *ARGV.grep(/\A(?!--)/)
1717
end
1818
cp "ext/#{NAME}/#{NAME}.so", "lib/#{NAME}"
1919
end

ext/iconv/extconf.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
conf = File.exist?(File.join($srcdir, "config.charset"))
66
conf = with_config("config-charset", enable_config("config-charset", conf))
77

8-
have_func("rb_enc_get", "ruby/encoding.h")
9-
have_func("st_lookup", "ruby/st.h")
8+
unless have_func("rb_enc_get", "ruby/encoding.h") || have_func("vasprintf", "stdio.h")
9+
raise "vasprintf is required for Ruby 1.8"
10+
end
1011
if have_func("iconv", "iconv.h") or
1112
have_library("iconv", "iconv", "iconv.h")
1213
check_signedness("size_t") rescue nil

ext/iconv/iconv.c

Lines changed: 82 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,60 @@
1717
#include <errno.h>
1818
#include <iconv.h>
1919
#include <assert.h>
20-
#ifdef HAVE_RUBY_ST_H
21-
#include "ruby/st.h"
22-
#else /* assume 1.8 */
23-
#include "st.h"
24-
#endif
2520
#ifdef HAVE_RUBY_ENCODING_H
26-
#include "ruby/encoding.h"
21+
/* assume Ruby 1.9 or later */
22+
# include "ruby/st.h"
23+
# include "ruby/encoding.h"
24+
#else
25+
# include "st.h"
26+
# include <stdarg.h>
27+
# define rb_f_notimplement rb_notimplement
28+
# define rb_str_subseq(a, b, c) rb_str_substr(a, b, c)
29+
# define rb_str_new_cstr(a) rb_str_new2(a)
30+
NORETURN(static void rb_sys_fail_str(VALUE msg));
31+
static void
32+
rb_sys_fail_str(VALUE msg)
33+
{
34+
rb_sys_fail(RSTRING_PTR(msg));
35+
}
36+
static VALUE
37+
rb_str_equal(str1, str2)
38+
VALUE str1, str2;
39+
{
40+
if (str1 == str2) return Qtrue;
41+
if (TYPE(str2) != T_STRING) {
42+
if (!rb_respond_to(str2, rb_intern("to_str"))) {
43+
return Qfalse;
44+
}
45+
return rb_equal(str2, str1);
46+
}
47+
if (RSTRING(str1)->len == RSTRING(str2)->len &&
48+
rb_str_cmp(str1, str2) == 0) {
49+
return Qtrue;
50+
}
51+
return Qfalse;
52+
}
53+
void
54+
rb_set_errinfo(VALUE err)
55+
{
56+
extern VALUE ruby_errinfo;
57+
ruby_errinfo = err;
58+
}
59+
#define ENCODING_GET(a) 0
60+
VALUE
61+
rb_sprintf(const char *format, ...)
62+
{
63+
va_list ap;
64+
char *ret;
65+
int len;
66+
67+
va_start(ap, format);
68+
len = vasprintf(&ret, format, ap);
69+
va_end(ap);
70+
if (len == -1) return Qnil;
71+
72+
return rb_str_new(ret, len);
73+
}
2774
#endif
2875

2976
/*
@@ -212,7 +259,9 @@ iconv_create(VALUE to, VALUE from, struct rb_iconv_opt_t *opt, int *idx)
212259
iconv_t cd;
213260
int retry = 0;
214261

262+
#ifdef HAVE_RUBY_ENCODING_H
215263
*idx = rb_enc_find_index(tocode);
264+
#endif
216265

217266
if (toopt) {
218267
toenc = rb_str_plus(to, toopt);
@@ -499,15 +548,19 @@ iconv_convert(iconv_t cd, VALUE str, long start, long length, int toidx, struct
499548
{
500549
if (NIL_P(str)) {
501550
ret = rb_str_new(buffer, outlen);
551+
#ifdef HAVE_RUBY_ENCODING_H
502552
if (toidx >= 0) rb_enc_associate_index(ret, toidx);
553+
#endif
503554
}
504555
else {
505556
if (ret) {
506557
ret = rb_str_buf_cat(ret, instart, tmpstart - instart);
507558
}
508559
else {
509560
ret = rb_str_new(instart, tmpstart - instart);
561+
#ifdef HAVE_RUBY_ENCODING_H
510562
if (toidx >= 0) rb_enc_associate_index(ret, toidx);
563+
#endif
511564
OBJ_INFECT(ret, str);
512565
}
513566
ret = rb_str_buf_cat(ret, buffer, outlen);
@@ -529,7 +582,9 @@ iconv_convert(iconv_t cd, VALUE str, long start, long length, int toidx, struct
529582

530583
if (!ret) {
531584
ret = rb_str_derive(str, instart, inptr - instart);
585+
#ifdef HAVE_RUBY_ENCODING_H
532586
if (toidx >= 0) rb_enc_associate_index(ret, toidx);
587+
#endif
533588
}
534589
else if (inptr > instart) {
535590
rb_str_cat(ret, instart, inptr - instart);
@@ -555,7 +610,9 @@ iconv_convert(iconv_t cd, VALUE str, long start, long length, int toidx, struct
555610

556611
if (!ret) {
557612
ret = rb_str_derive(str, instart, inptr - instart);
613+
#ifdef HAVE_RUBY_ENCODING_H
558614
if (toidx >= 0) rb_enc_associate_index(ret, toidx);
615+
#endif
559616
}
560617
else if (inptr > instart) {
561618
rb_str_cat(ret, instart, inptr - instart);
@@ -673,7 +730,9 @@ iconv_initialize(int argc, VALUE *argv, VALUE self)
673730
iconv_free(check_iconv(self));
674731
DATA_PTR(self) = NULL;
675732
DATA_PTR(self) = (void *)ICONV2VALUE(iconv_create(to, from, &opt, &idx));
733+
#ifdef HAVE_RUBY_ENCODING_H
676734
if (idx >= 0) ENCODING_SET(self, idx);
735+
#endif
677736
return self;
678737
}
679738

@@ -697,7 +756,9 @@ iconv_s_open(int argc, VALUE *argv, VALUE self)
697756
cd = ICONV2VALUE(iconv_create(to, from, &opt, &idx));
698757

699758
self = Data_Wrap_Struct(self, NULL, ICONV_FREE, (void *)cd);
759+
#ifdef HAVE_RUBY_ENCODING_H
700760
if (idx >= 0) ENCODING_SET(self, idx);
761+
#endif
701762

702763
if (rb_block_given_p()) {
703764
return rb_ensure(rb_yield, self, (VALUE(*)())iconv_finish, self);
@@ -838,7 +899,7 @@ iconv_s_list(void)
838899

839900
args[1] = rb_block_given_p() ? 0 : rb_ary_new();
840901
iconvlist(list_iconv, args);
841-
state = *(int *)args;
902+
state = (int)args[0];
842903
if (state) rb_jump_tag(state);
843904
if (args[1]) return args[1];
844905
#elif defined(HAVE___ICONV_FREE_LIST)
@@ -930,27 +991,38 @@ iconv_iconv(int argc, VALUE *argv, VALUE self)
930991

931992
rb_scan_args(argc, argv, "12", &str, &n1, &n2);
932993
if (!NIL_P(str)) {
994+
#ifdef HAVE_RUBY_ENCODING_H
933995
VALUE n = rb_str_length(StringValue(str));
934996
slen = NUM2LONG(n);
997+
#else
998+
slen = RSTRING_LEN(StringValue(str));
999+
#endif
9351000
}
9361001
if (argc != 2 || !RTEST(rb_range_beg_len(n1, &start, &length, slen, 0))) {
9371002
if (NIL_P(n1) || ((start = NUM2LONG(n1)) < 0 ? (start += slen) >= 0 : start < slen)) {
9381003
length = NIL_P(n2) ? -1 : NUM2LONG(n2);
9391004
}
9401005
}
941-
#ifdef HAVE_RUBY_ENCODING_H
9421006
if (start > 0 || length > 0) {
943-
rb_encoding *enc = rb_enc_get(str);
1007+
#ifdef HAVE_RUBY_ENCODING_H
9441008
const char *s = RSTRING_PTR(str), *e = s + RSTRING_LEN(str);
9451009
const char *ps = s;
1010+
rb_encoding *enc = rb_enc_get(str);
9461011
if (start > 0) {
9471012
start = (ps = rb_enc_nth(s, e, start, enc)) - s;
9481013
}
9491014
if (length > 0) {
9501015
length = rb_enc_nth(ps, e, length, enc) - ps;
9511016
}
952-
}
1017+
#else
1018+
if (start > slen) {
1019+
start = slen;
1020+
}
1021+
if (length > slen - start) {
1022+
length = slen - start;
1023+
}
9531024
#endif
1025+
}
9541026

9551027
return iconv_convert(VALUE2ICONV(cd), str, start, length, ENCODING_GET(self), NULL);
9561028
}

iconv.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ Gem::Specification.new do |gem|
1717
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
1818
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
1919
gem.require_paths = ["lib"]
20+
gem.required_ruby_version = '>= 1.9.2'
2021
end

lib/iconv/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class Iconv < Data
2-
VERSION = "1.0.1"
2+
VERSION = "1.0.2"
33
end

test/test_basic.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require_relative "utils.rb"
1+
require File.expand_path("../utils.rb", __FILE__)
22

33
class TestIconv::Basic < TestIconv
44
def test_euc2sjis

test/test_option.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require_relative "utils.rb"
1+
require File.expand_path("../utils.rb", __FILE__)
22

33
class TestIconv::Option < TestIconv
44
def test_ignore_option

test/test_partial.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require_relative "utils.rb"
1+
require File.expand_path("../utils.rb", __FILE__)
22

33
class TestIconv::Partial < TestIconv
44
def test_partial_ascii

0 commit comments

Comments
 (0)