diff options
author | Pascal Rigaux <pixel@mandriva.com> | 2001-07-24 16:53:54 +0000 |
---|---|---|
committer | Pascal Rigaux <pixel@mandriva.com> | 2001-07-24 16:53:54 +0000 |
commit | 5bef71a0c86613f95e154d08b5f8f0cc23226e27 (patch) | |
tree | 57150f236c56d435fbf76b092eab0a78007169c4 /MDK/Common/String.pm | |
parent | d7cea7bcbafb212013a3638ee3e76d63e9ef18cc (diff) | |
download | perl-MDK-Common-5bef71a0c86613f95e154d08b5f8f0cc23226e27.tar perl-MDK-Common-5bef71a0c86613f95e154d08b5f8f0cc23226e27.tar.gz perl-MDK-Common-5bef71a0c86613f95e154d08b5f8f0cc23226e27.tar.bz2 perl-MDK-Common-5bef71a0c86613f95e154d08b5f8f0cc23226e27.tar.xz perl-MDK-Common-5bef71a0c86613f95e154d08b5f8f0cc23226e27.zip |
initial commit
Diffstat (limited to 'MDK/Common/String.pm')
-rw-r--r-- | MDK/Common/String.pm | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/MDK/Common/String.pm b/MDK/Common/String.pm new file mode 100644 index 0000000..74b8e2b --- /dev/null +++ b/MDK/Common/String.pm @@ -0,0 +1,90 @@ +package MDK::Common::String; + +use vars qw(@ISA %EXPORT_TAGS @EXPORT_OK); +@ISA = qw(Exporter); +@EXPORT_OK = qw(bestMatchSentence formatList formatError formatTimeRaw formatLines formatAlaTeX warp_text); +%EXPORT_TAGS = (all => [ @EXPORT_OK ]); + + +# count the number of character that match +sub bestMatchSentence { + + my $best = -1; + my $bestSentence; + my @s = split /\W+/, shift; + foreach (@_) { + my $count = 0; + foreach my $e (@s) { + $count+= length ($e) if /^$e$/; + $count+= length ($e) if /^$e$/i; + $count+= length ($e) if /$e/; + $count+= length ($e) if /$e/i; + } + $best = $count, $bestSentence = $_ if $count > $best; + } + wantarray ? ($bestSentence, $best) : $bestSentence; +} + + +sub formatList { + my $nb = shift; + join(", ", @_ <= $nb ? @_ : (@_[0..$nb-1], '...')); +} +sub formatError { + my ($err) = @_; + $err =~ s/ at .*?$/\./ if !$::testing; + $err; +} +sub formatTimeRaw { + my ($s, $m, $h) = gmtime($_[0]); + sprintf "%d:%02d:%02d", $h, $m, $s; +} +sub formatLines { + my ($t, $tmp); + foreach (split "\n", $_[0]) { + if (/^\s/) { + $t .= "$tmp\n"; + $tmp = $_; + } else { + $tmp = ($tmp ? "$tmp " : ($t && "\n") . $tmp) . $_; + } + } + "$t$tmp\n"; +} +sub formatAlaTeX { + my ($t, $tmp); + foreach (split "\n", $_[0]) { + if (/^$/) { + $t .= ($t && "\n") . $tmp; + $tmp = ''; + } else { + $tmp = ($tmp && "$tmp ") . (/^\s*(.*?)\s*$/)[0]; + } + } + $t . ($t && $tmp && "\n") . $tmp; +} + + + + +sub warp_text { + my ($text, $width) = @_; + $width ||= 80; + + my @l; + foreach (split "\n", $text) { + my $t = ''; + foreach (split /\s+/, $_) { + if (length "$t $_" > $width) { + push @l, $t; + $t = $_; + } else { + $t = "$t $_"; + } + } + push @l, $t; + } + @l; +} + +1; |