about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-02-27 01:22:55 +0000
committerEric Wong <normalperson@yhbt.net>2011-02-27 01:25:53 +0000
commite619319982d51b495a3a627adfab742433c976e4 (patch)
tree88b84c3f97309277de50c717acf4df813f8369b8
parent9c14d0c61ae5c9ed16604d03d70dc3c4765563d0 (diff)
downloadmahoro-e619319982d51b495a3a627adfab742433c976e4.tar.gz
avoid needless double String conversion
No need to convert things twice when once is enough.
-rw-r--r--mahoro.c9
-rwxr-xr-xtest.rb11
2 files changed, 18 insertions, 2 deletions
diff --git a/mahoro.c b/mahoro.c
index 3fd1e61..4b125b0 100644
--- a/mahoro.c
+++ b/mahoro.c
@@ -8,6 +8,9 @@
 #ifndef RSTRING_LEN
 #  define RSTRING_LEN(s)->len
 #endif
+#ifndef RSTRING_PTR
+#  define RSTRING_PTR(s)->ptr
+#endif
 
 struct MagicCookie
 {
@@ -91,8 +94,10 @@ mahoro_buffer(self, input)
         const char *msg;
         magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie;
 
-        if(!(msg = magic_buffer(cookie, StringValuePtr(input),
-                                        RSTRING_LEN(StringValue(input))))) {
+        StringValue(input);
+
+        if(!(msg = magic_buffer(cookie, RSTRING_PTR(input),
+                                        RSTRING_LEN(input)))) {
                 rb_raise(eMahoroError, "failed lookup: %s", magic_error(cookie));
         }
 
diff --git a/test.rb b/test.rb
index 1ffb570..52b3b80 100755
--- a/test.rb
+++ b/test.rb
@@ -32,6 +32,17 @@ class MahoroTestCase < Test::Unit::TestCase
                              @m.buffer(File.read('mahoro.c')))
         end
 
+        def test_buffer_string_convert
+                tmp = File.read('mahoro.c')
+                buf = Struct.new(:to_str).new(tmp)
+                assert_equal('ASCII C program text', @m.buffer(buf))
+        end
+
+        def test_buffer_invalid
+                @m.flags = Mahoro::NONE
+                assert_raises(TypeError) { @m.buffer @m }
+        end
+
         def test_mime_buffer
                 @m.flags = Mahoro::MIME
                 assert({