1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
sub unicode_trim { my ( $unicode, $len ) = @_; return '' if $len <= 0; my $gcs = Unicode::GCString->new( $unicode ); my $pos = $gcs->pos; $gcs->pos( 0 ); my $cols = 0; my $gc; while ( defined( $gc = $gcs->next ) ) { if ( $len < ( $cols += $gc->columns ) ) { my $ret = $gcs->substr( 0, $gcs->pos - 1 ); $gcs->pos( $pos ); return $ret->as_string; } } $gcs->pos( $pos ); return $gcs->as_string; } sub unicode_sprintf { my ( $unicode, $avail_width, $right_justify ) = @_; my $gcs = Unicode::GCString->new( $unicode ); my $colwidth = $gcs->columns; if ( $colwidth > $avail_width ) { my $pos = $gcs->pos; $gcs->pos( 0 ); my $cols = 0; my $gc; while ( defined( $gc = $gcs->next ) ) { if ( $avail_width < ( $cols += $gc->columns ) ) { my $ret = $gcs->substr( 0, $gcs->pos - 1 ); $gcs->pos( $pos ); return $ret->as_string; } } } elsif ( $colwidth < $avail_width ) { if ( $right_justify ) { $unicode = " " x ( $avail_width - $colwidth ) . $unicode; } else { $unicode = $unicode . " " x ( $avail_width - $colwidth ); } } return $unicode; }
2 ** 2