Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
1.8 KiB

  1. package Text::Tabs;
  2. require Exporter;
  3. @ISA = (Exporter);
  4. @EXPORT = qw(expand unexpand $tabstop);
  5. use vars qw($VERSION $tabstop $debug);
  6. $VERSION = 98.112801;
  7. use strict;
  8. BEGIN {
  9. $tabstop = 8;
  10. $debug = 0;
  11. }
  12. sub expand
  13. {
  14. my (@l) = @_;
  15. for $_ (@l) {
  16. 1 while s/(^|\n)([^\t\n]*)(\t+)/
  17. $1. $2 . (" " x
  18. ($tabstop * length($3)
  19. - (length($2) % $tabstop)))
  20. /sex;
  21. }
  22. return @l if wantarray;
  23. return $l[0];
  24. }
  25. sub unexpand
  26. {
  27. my (@l) = @_;
  28. my @e;
  29. my $x;
  30. my $line;
  31. my @lines;
  32. my $lastbit;
  33. for $x (@l) {
  34. @lines = split("\n", $x, -1);
  35. for $line (@lines) {
  36. $line = expand($line);
  37. @e = split(/(.{$tabstop})/,$line,-1);
  38. $lastbit = pop(@e);
  39. $lastbit = '' unless defined $lastbit;
  40. $lastbit = "\t"
  41. if $lastbit eq " "x$tabstop;
  42. for $_ (@e) {
  43. if ($debug) {
  44. my $x = $_;
  45. $x =~ s/\t/^I\t/gs;
  46. print "sub on '$x'\n";
  47. }
  48. s/ +$/\t/;
  49. }
  50. $line = join('',@e, $lastbit);
  51. }
  52. $x = join("\n", @lines);
  53. }
  54. return @l if wantarray;
  55. return $l[0];
  56. }
  57. 1;
  58. __END__
  59. =head1 NAME
  60. Text::Tabs -- expand and unexpand tabs per the unix expand(1) and unexpand(1)
  61. =head1 SYNOPSIS
  62. use Text::Tabs;
  63. $tabstop = 4;
  64. @lines_without_tabs = expand(@lines_with_tabs);
  65. @lines_with_tabs = unexpand(@lines_without_tabs);
  66. =head1 DESCRIPTION
  67. Text::Tabs does about what the unix utilities expand(1) and unexpand(1)
  68. do. Given a line with tabs in it, expand will replace the tabs with
  69. the appropriate number of spaces. Given a line with or without tabs in
  70. it, unexpand will add tabs when it can save bytes by doing so. Invisible
  71. compression with plain ascii!
  72. =head1 BUGS
  73. expand doesn't handle newlines very quickly -- do not feed it an
  74. entire document in one string. Instead feed it an array of lines.
  75. =head1 AUTHOR
  76. David Muir Sharnoff <[email protected]>