[perl-Gtk3] Do not use length() in overrides where number of bytes is wanted



commit e5d74fba47aa148c05b0555985eae297d2158dac
Author: Torsten SchÃnfeld <kaffeetisch gmx de>
Date:   Sat Jan 26 22:13:31 2013 +0100

    Do not use length() in overrides where number of bytes is wanted
    
    length() returns the number of characters.

 lib/Gtk3.pm          |   18 +++++++++---------
 t/overrides.t        |   29 ++++++++++++++++++++++-------
 t/zz-GtkTextBuffer.t |    4 +++-
 3 files changed, 34 insertions(+), 17 deletions(-)
---
diff --git a/lib/Gtk3.pm b/lib/Gtk3.pm
index d299e24..59ed88c 100644
--- a/lib/Gtk3.pm
+++ b/lib/Gtk3.pm
@@ -515,14 +515,14 @@ sub Gtk3::Builder::add_objects_from_string {
   my $ref = _rest_to_ref (\ rest);
   return Glib::Object::Introspection->invoke (
     $_GTK_BASENAME, 'Builder', 'add_objects_from_string',
-    $builder, $string, length $string, $ref);
+    $builder, $string, -1, $ref); # wants length in bytes
 }
 
 sub Gtk3::Builder::add_from_string {
   my ($builder, $string) = @_;
   return Glib::Object::Introspection->invoke (
     $_GTK_BASENAME, 'Builder', 'add_from_string',
-    $builder, $string, length $string);
+    $builder, $string, -1); # wants length in bytes
 }
 
 # Copied from Gtk2.pm
@@ -741,7 +741,7 @@ sub Gtk3::Dialog::set_alternative_button_order {
 sub Gtk3::Editable::insert_text {
   return Glib::Object::Introspection->invoke (
     $_GTK_BASENAME, 'Editable', 'insert_text',
-    @_ == 4 ? @_ : (@_[0,1], length $_[1], $_[2]));
+    @_ == 4 ? @_ : (@_[0,1], -1, $_[2])); # wants length in bytes
 }
 
 sub Gtk3::FileChooserDialog::new {
@@ -950,25 +950,25 @@ sub Gtk3::TextBuffer::create_tag {
 sub Gtk3::TextBuffer::insert {
   return Glib::Object::Introspection->invoke (
     $_GTK_BASENAME, 'TextBuffer', 'insert',
-    @_ == 4 ? @_ : (@_[0,1,2], length $_[2]));
+    @_ == 4 ? @_ : (@_[0,1,2], -1)); # wants length in bytes
 }
 
 sub Gtk3::TextBuffer::insert_at_cursor {
   return Glib::Object::Introspection->invoke (
     $_GTK_BASENAME, 'TextBuffer', 'insert_at_cursor',
-    @_ == 3 ? @_ : (@_[0,1], length $_[1]));
+    @_ == 3 ? @_ : (@_[0,1], -1)); # wants length in bytes
 }
 
 sub Gtk3::TextBuffer::insert_interactive {
   return Glib::Object::Introspection->invoke (
     $_GTK_BASENAME, 'TextBuffer', 'insert_interactive',
-    @_ == 5 ? @_ : (@_[0,1,2], length $_[2], $_[3]));
+    @_ == 5 ? @_ : (@_[0,1,2], -1, $_[3])); # wants length in bytes
 }
 
 sub Gtk3::TextBuffer::insert_interactive_at_cursor {
   return Glib::Object::Introspection->invoke (
     $_GTK_BASENAME, 'TextBuffer', 'insert_interactive_at_cursor',
-    @_ == 4 ? @_ : (@_[0,1], length $_[1], $_[2]));
+    @_ == 4 ? @_ : (@_[0,1], -1, $_[2])); # wants length in bytes
 }
 
 sub Gtk3::TextBuffer::insert_with_tags {
@@ -1000,7 +1000,7 @@ sub Gtk3::TextBuffer::insert_with_tags_by_name {
 sub Gtk3::TextBuffer::set_text {
   return Glib::Object::Introspection->invoke (
     $_GTK_BASENAME, 'TextBuffer', 'set_text',
-    @_ == 3 ? @_ : (@_[0,1], length $_[1]));
+    @_ == 3 ? @_ : (@_[0,1], -1)); # wants length in bytes
 }
 
 sub Gtk3::TreeModel::get {
@@ -1089,7 +1089,7 @@ sub Gtk3::UIManager::add_ui_from_string {
   my ($manager, $string) = @_;
   return Glib::Object::Introspection->invoke (
     $_GTK_BASENAME, 'UIManager', 'add_ui_from_string',
-    $manager, $string, length $string);
+    $manager, $string, -1); # wants length in bytes
 }
 
 sub Gtk3::VBox::new {
diff --git a/t/overrides.t b/t/overrides.t
index 4900137..6aa4a4f 100644
--- a/t/overrides.t
+++ b/t/overrides.t
@@ -4,8 +4,10 @@ BEGIN { require './t/inc/setup.pl' };
 
 use strict;
 use warnings;
+use utf8;
+use Encode;
 
-plan tests => 123;
+plan tests => 125;
 
 # Gtk3::CHECK_VERSION and check_version
 {
@@ -97,16 +99,29 @@ plan tests => 123;
 {
   my $entry = Gtk3::Entry->new;
   my $orig_text = 'aeiou';
+  my $orig_text_chars = length ($orig_text);
+  my $orig_text_bytes = length (Encode::encode_utf8 ($orig_text));
   $entry->set_text ($orig_text);
-  my ($new_text, $pos) = ('0123456789', length $orig_text);
+  my ($new_text, $pos) = ('0123456789', $orig_text_chars);
+  my $new_text_chars = length ($new_text);
+  my $new_text_bytes = length (Encode::encode_utf8 ($new_text));
   is ($entry->insert_text ($new_text, $pos),
-      $pos + length $new_text);
+      $pos + $new_text_chars);
   $pos = 0;
-  is ($entry->insert_text ($new_text, length $new_text, $pos),
-      $pos + length $new_text);
+  is ($entry->insert_text ($new_text, $new_text_bytes, $pos),
+      $pos + $new_text_chars);
   is ($entry->get_text, $new_text . $orig_text . $new_text);
 }
 
+# Gtk3::Editable::insert_text and length issues
+{
+  my $entry = Gtk3::Entry->new;
+  my ($text, $pos) = ('0123456789â', 0);
+  is ($entry->insert_text ($text, $pos),
+      $pos + length ($text));
+  is ($entry->get_text, $text);
+}
+
 # GtkEditable.insert-text signal
 SKIP: {
   skip 'Need generic signal marshaller', 5
@@ -118,9 +133,9 @@ SKIP: {
 
   my ($my_text, $my_pos) = ('123', 2);
   $entry->signal_connect ('insert-text' => sub {
-    my ($entry, $new_text, $new_text_length, $position, $data) = @_;
+    my ($entry, $new_text, $new_text_bytes, $position, $data) = @_;
     is ($new_text, $my_text);
-    is ($new_text_length, length $my_text);
+    is ($new_text_bytes, length (Encode::encode_utf8 ($my_text)));
     is ($position, $my_pos);
     # Disregard $position and move the text to the end.
     return length $entry->get_text;
diff --git a/t/zz-GtkTextBuffer.t b/t/zz-GtkTextBuffer.t
index 291b208..78d46f0 100644
--- a/t/zz-GtkTextBuffer.t
+++ b/t/zz-GtkTextBuffer.t
@@ -6,6 +6,7 @@ BEGIN { require './t/inc/setup.pl' }
 
 use strict;
 use warnings;
+use utf8;
 use Glib qw/TRUE FALSE/;
 
 plan tests => 38;
@@ -24,7 +25,8 @@ isa_ok($buffer -> get_end_iter(), "Gtk3::TextIter");
 
 $buffer -> set_modified(FALSE);
 
-my $text = "Lore ipsem dolor.  I think that is misspelled.\n";
+# Use one multi-byte character to test length handling.
+my $text = "Lore ipsem dolorâ  I think that is misspelled.\n";
 my $start = sub { $buffer -> get_start_iter() };
 my $end = sub { $buffer -> get_end_iter() };
 my $bounds = sub { $buffer -> get_bounds() };



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]