Skip to content

Commit

Permalink
Make Unichars compile and run on Ruby 1.9.
Browse files Browse the repository at this point in the history
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
Manfred committed Jul 26, 2009
1 parent 2bc1506 commit 674e96b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ Makefile
*.dll
pkg
doc
html
.DS_Store
*.log
20 changes: 14 additions & 6 deletions ext/glib/glib.c
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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;
}
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
14 changes: 10 additions & 4 deletions lib/chars.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion test/profile/memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 674e96b

Please sign in to comment.