Permalink
Browse files

Make Unichars compile and run on Ruby 1.9.

I'm not sure why you would want to use Unichars on 1.9 because 1.9's encoding
support is good enough and certainly a lot faster than Unichars.

Tip of the hat to: crishoj.
  • Loading branch information...
1 parent 2bc1506 commit 674e96ba5d4e0648de8708428330200d426f687a @Manfred committed Jul 26, 2009
Showing with 27 additions and 12 deletions.
  1. +1 −0 .gitignore
  2. +14 −6 ext/glib/glib.c
  3. +10 −4 lib/chars.rb
  4. +1 −1 test/profile/memory.rb
  5. +1 −1 test/test_helper.rb
View
@@ -5,5 +5,6 @@ Makefile
*.dll
pkg
doc
+html
.DS_Store
*.log
View
@@ -1,6 +1,14 @@
#include <ruby.h>
#include <glib.h>
+#ifndef RSTRING_LEN
+#define RSTRING_LEN(string) RSTRING(string)->len
+#endif
+
+#ifndef RSTRING_PTR
+#define RSTRING_PTR(string) RSTRING(string)->ptr
+#endif
+
/*
* call-seq:
* utf8_size(string)
@@ -14,7 +22,7 @@ static VALUE utf8_size(VALUE self, VALUE string)
VALUE result;
Check_Type(string, T_STRING);
- result = ULONG2NUM(g_utf8_strlen(StringValuePtr(string), RSTRING(string)->len));
+ result = ULONG2NUM(g_utf8_strlen(StringValuePtr(string), RSTRING_LEN(string)));
return result;
}
@@ -33,7 +41,7 @@ static VALUE utf8_upcase(VALUE self, VALUE string)
gchar *temp;
Check_Type(string, T_STRING);
- temp = g_utf8_strup(StringValuePtr(string), RSTRING(string)->len);
+ temp = g_utf8_strup(StringValuePtr(string), RSTRING_LEN(string));
result = rb_str_new2(temp);
free(temp);
@@ -54,7 +62,7 @@ static VALUE utf8_downcase(VALUE self, VALUE string)
gchar *temp;
Check_Type(string, T_STRING);
- temp = g_utf8_strdown(StringValuePtr(string), RSTRING(string)->len);
+ temp = g_utf8_strdown(StringValuePtr(string), RSTRING_LEN(string));
result = rb_str_new2(temp);
free(temp);
@@ -75,7 +83,7 @@ static VALUE utf8_reverse(VALUE self, VALUE string)
gchar *temp;
Check_Type(string, T_STRING);
- temp = g_utf8_strreverse(StringValuePtr(string), RSTRING(string)->len);
+ temp = g_utf8_strreverse(StringValuePtr(string), RSTRING_LEN(string));
result = rb_str_new2(temp);
free(temp);
@@ -114,10 +122,10 @@ static VALUE utf8_normalize(VALUE self, VALUE string, VALUE form)
} else if (ID2SYM(rb_intern("kc")) == form) {
mode = G_NORMALIZE_NFKC;
} else {
- rb_raise(rb_eArgError, "%s is not a valid normalization form, options are: :d, :kd, :c, or :kc", RSTRING(rb_inspect(form))->ptr);
+ rb_raise(rb_eArgError, "%s is not a valid normalization form, options are: :d, :kd, :c, or :kc", RSTRING_PTR(rb_inspect(form)));
}
- temp = g_utf8_normalize(StringValuePtr(string), RSTRING(string)->len, mode);
+ temp = g_utf8_normalize(StringValuePtr(string), RSTRING_LEN(string), mode);
result = rb_str_new2(temp);
free(temp);
View
@@ -9,9 +9,15 @@ class Chars
alias to_s wrapped_string
alias to_str wrapped_string
- # Creates a new Chars instance by wrapping _string_.
- def initialize(string)
- @wrapped_string = string
+ if 'string'.respond_to?(:force_encoding)
+ # Creates a new Chars instance by wrapping _string_.
+ def initialize(string)
+ @wrapped_string = string.dup.force_encoding(Encoding::UTF_8)
+ end
+ else
+ def initialize(string) #:nodoc:
+ @wrapped_string = string.dup
+ end
end
# Forward all undefined methods to the wrapped string.
@@ -42,7 +48,7 @@ def respond_to?(method, include_private=false)
def <=>(other)
@wrapped_string <=> other.to_s
end
-
+
# Returns a new Chars object containing the _other_ object concatenated to the string.
#
# Example:
View
@@ -3,7 +3,7 @@
$:.unshift(File.expand_path('../../../ext/glib', __FILE__))
$:.unshift(File.expand_path('../../../lib', __FILE__))
-$KCODE = 'UTF8'
+$KCODE = 'UTF8' unless 'string'.respond_to?(:encoding)
require 'unichars'
View
@@ -3,7 +3,7 @@
$:.unshift(File.expand_path('../../ext/glib', __FILE__))
$:.unshift(File.expand_path('../../lib', __FILE__))
-$KCODE = 'UTF8'
+$KCODE = 'UTF8' unless 'string'.respond_to?(:encoding)
require 'rubygems' rescue LoadError
require 'test/spec'

0 comments on commit 674e96b

Please sign in to comment.