<?php
/**
 * The Text_Filter_tabs2spaces:: converts tabs into spaces.
 *
 * $Horde: framework/Text_Filter/Filter/tabs2spaces.php,v 1.3.10.6 2006/01/01 21:28:38 jan Exp $
 *
 * Copyright 2004-2006 Chuck Hagenbuch <chuck@horde.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you did not
 * receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  Chuck Hagenbuch <chuck@horde.org>
 * @package Horde_Text
 */
class Text_Filter_tabs2spaces extends Text_Filter {

    /**
     * Filter parameters.
     *
     * @var array
     */
    var $_params = array('tabstop' => 8, 'breakchar' => "\n");

    /**
     * Executes any code necessary before applying the filter patterns.
     *
     * @param string $text  The text before the filtering.
     *
     * @return string  The modified text.
     */
    function preProcess($text)
    {
        $lines = explode($this->_params['breakchar'], $text);
        for ($i = 0; $i < count($lines); $i++) {
            while (($pos = strpos($lines[$i], "\t")) !== false) {
                $n_space = $this->_params['tabstop'] - ($pos % $this->_params['tabstop']);
                $new_str = str_repeat(' ', $n_space);
                $lines[$i] = substr_replace($lines[$i], $new_str, $pos, 1);
            }
        }
        return implode("\n", $lines);
    }

}
