[dev] Patch: plain.php (take 2)

Marc Jauvin marc at register4less.com
Thu May 29 11:42:18 PDT 2003


Quoting Marc Jauvin <marc at register4less.com>:

> OK, here is a "smarter" way of fixing tab expansion.
> Please, let me know if this is not acceptable.

This version should be used instead. It fixes a problem when the "\t" appears as
the first character on a line.


--
Marc Jauvin
450-441-5458
http://register4less.com
-------------- next part --------------
Index: plain.php
===================================================================
RCS file: /repository/imp/lib/MIME/Viewer/plain.php,v
retrieving revision 1.21
diff -u -r1.21 plain.php
--- plain.php   16 May 2003 15:26:13 -0000      1.21
+++ plain.php   29 May 2003 18:39:06 -0000
@@ -84,8 +84,11 @@
         // Filter bad language.
         $text = IMP::filterText($text);

+        // Expand Tabs with respect to tab stops
+        $tabstop = 8; // FIXME: we could add that as a user preference
+        $text = $this->smartExpandTabs($text,$tabstop);
+
         // Wordwrap.
-        $text = str_replace("\t", '        ', $text);
         $text = str_replace('  ', ' &nbsp;', $text);
         $text = str_replace("\n ", "\n&nbsp;", $text);
         if ($text{0} == ' ') {
@@ -154,4 +157,25 @@
         }
     }

+    /**
+     * Return the text after tab expansion
+     *
+     * @access public
+     *
+     * @param string $text  The text to expand
+     *
+     * @return string  the text after tab expansion
+     */
+    function smartExpandTabs($text, $tabstop=8)
+    {
+        $lines = explode("\n", $text);
+        for ($i = 0; $i < sizeof($lines); $i++) {
+            while( ($pos=strpos($lines[$i], "\t")) !== false) {
+                $n_space = $tabstop - ($pos % $tabstop);
+                $new_str = str_repeat(" ", $n_space);
+                $lines[$i] = substr_replace($lines[$i], $new_str, $pos, 1);
+            }
+        }
+        return implode("\n", $lines);
+    }
 }


More information about the dev mailing list