Permalink
Browse files

ugly #if checks to make it build and run most tests against 2.5.8, 4.…

…6.1, 5.5.3.

Probably just recommend using v5.5 or higher?
  • Loading branch information...
1 parent 57aeb9c commit 601b11bfc7ceabccfb4778b866dc77333d9611a6 Nikolai Lugovoi committed Mar 26, 2007
Showing with 27 additions and 4 deletions.
  1. +18 −0 ext/oregexp.c
  2. +9 −4 test/test_oniguruma.rb
View
@@ -47,20 +47,26 @@ static OnigEncodingType * int2encoding( VALUE v_index ) {
case 15: return ONIG_ENCODING_ISO_8859_15;
case 16: return ONIG_ENCODING_ISO_8859_16;
case 17: return ONIG_ENCODING_UTF8;
+#if ONIGURUMA_VERSION_MAJOR != 2
case 18: return ONIG_ENCODING_UTF16_BE;
case 19: return ONIG_ENCODING_UTF16_LE;
case 20: return ONIG_ENCODING_UTF32_BE;
case 21: return ONIG_ENCODING_UTF32_LE;
+#endif
case 22: return ONIG_ENCODING_EUC_JP;
case 23: return ONIG_ENCODING_EUC_TW;
case 24: return ONIG_ENCODING_EUC_KR;
case 25: return ONIG_ENCODING_EUC_CN;
case 26: return ONIG_ENCODING_SJIS;
/*case 27: return ONIG_ENCODING_KOI8;*/
case 28: return ONIG_ENCODING_KOI8_R;
+#if ONIGURUMA_VERSION_MAJOR == 5
case 29: return ONIG_ENCODING_CP1251;
+#endif
case 30: return ONIG_ENCODING_BIG5;
+#if ONIGURUMA_VERSION_MAJOR != 2
case 31: return ONIG_ENCODING_GB18030;
+#endif
case 32: return ONIG_ENCODING_UNDEF;
}
}
@@ -72,15 +78,19 @@ static OnigSyntaxType * int2syntax( VALUE v_index ) {
if( ! NIL_P(v_index) ) {
index = FIX2INT(v_index);
switch( index ) {
+#if ONIGURUMA_VERSION_MAJOR != 2
case 0: return ONIG_SYNTAX_ASIS;
+#endif
case 1: return ONIG_SYNTAX_POSIX_BASIC;
case 2: return ONIG_SYNTAX_POSIX_EXTENDED;
case 3: return ONIG_SYNTAX_EMACS;
case 4: return ONIG_SYNTAX_GREP;
case 5: return ONIG_SYNTAX_GNU_REGEX;
case 6: return ONIG_SYNTAX_JAVA;
case 7: return ONIG_SYNTAX_PERL;
+#if ONIGURUMA_VERSION_MAJOR != 2
case 8: return ONIG_SYNTAX_PERL_NG;
+#endif
case 9: return ONIG_SYNTAX_RUBY;
case 10: return ONIG_SYNTAX_DEFAULT;
}
@@ -225,6 +235,9 @@ backslash). */
/* scan the replacement text, looking for substitutions (\n) and \escapes. */
#define MAX_GROUP_NAME_LEN 64
+#if ONIGURUMA_VERSION_MAJOR == 2
+#define ONIGENC_MBC_ENC_LEN(e, p) enc_len(e, *(p))
+#endif
static VALUE
oregexp_get_replacement(pat, src_text, repl_text, region)
VALUE pat,
@@ -256,6 +269,10 @@ oregexp_get_replacement(pat, src_text, repl_text, region)
while (replIdx < replacementLength) {
OnigCodePoint c = ONIGENC_MBC_TO_CODE(enc, replacementText+replIdx, replacementEnd);
int c_len =ONIGENC_MBC_ENC_LEN(enc, replacementText+replIdx) ;
+ if( c_len == 0 ) {
+ rb_warn("Strange, for %d enc_len is 0", c);
+ c_len = 1;
+ }
replIdx += c_len;
if ( c != BACKSLASH) {
/* Common case, no substitution, no escaping, */
@@ -627,4 +644,5 @@ void Init_oregexp() {
rb_define_method( cORegexp, "gsub!", oregexp_m_gsub_bang, -1 );
rb_define_method( cORegexp, "sub!", oregexp_m_sub_bang, -1 );
rb_define_method( cORegexp, "scan", oregexp_m_scan, 1 );
+ rb_define_const( mOniguruma, "VERSION", rb_str_new2(onig_version()) );
}
View
@@ -97,8 +97,8 @@ def test_to_s
def test_kcode
reg = Oniguruma::ORegexp.new( "(3.)(.*)(3.)" )
assert_equal( Oniguruma::ENCODING_ASCII, reg.kcode )
- reg = Oniguruma::ORegexp.new( "(3.)(.*)(3.)", '', 'cp1251' )
- assert_equal( Oniguruma::ENCODING_CP1251, reg.kcode )
+ reg = Oniguruma::ORegexp.new( "(3.)(.*)(3.)", '', 'SJIS' )
+ assert_equal( Oniguruma::ENCODING_SJIS, reg.kcode )
end
def test_options
@@ -231,6 +231,8 @@ def test_group_by_name
assert_equal( nil, matches[:inexistent])
end
+# casefolding for full Unicode set is not present in versions prior to 5.
+if Oniguruma::VERSION >= '5.0.0'
def test_utf8_ignore_case
reg = Oniguruma::ORegexp.new( '([а-я])+', :options => Oniguruma::OPTION_IGNORECASE, :encoding => Oniguruma::ENCODING_UTF8 )
matches = reg.match("Text: Ехал Грека Через Реку")
@@ -251,7 +253,8 @@ def test_utf8_gsub2
new_str = reg.gsub("Text: Ехал Грека Через Реку") {|m| m[0]*2 }
assert_equal("Text: ЕЕххаалл ГГррееккаа ЧЧеерреезз РРееккуу", new_str)
end
-
+end
+
def test_sub_compatibility
$x = "a.gif"
assert_equal("b.gif", $x.osub('.*\.([^\.]+)$', 'b.\1'))
@@ -296,12 +299,14 @@ def test_match_compat
def _u16(str)
str.unpack("U*").pack("n*")
end
-
+puts Oniguruma::VERSION
+if Oniguruma::VERSION >= '4.0.0'
def test_utf16_gsub
r = Oniguruma::ORegexp.new( _u16('[aeiou]'), :encoding => Oniguruma::ENCODING_UTF16_BE)
assert_equal( _u16("h*ll*"), r.gsub( _u16("hello"), _u16('*')) )
r = Oniguruma::ORegexp.new( _u16('([aeiou])'), :encoding => Oniguruma::ENCODING_UTF16_BE)
assert_equal( _u16("h<e>\\ll<o>\\"), r.gsub( _u16("hello"), _u16('<\1>\\')) )
end
+end
end

0 comments on commit 601b11b

Please sign in to comment.