package TextUtil;
use strict;

use vars qw (@ISA @EXPORT_OK);
use Exporter;
@ISA = ('Exporter');
@EXPORT_OK = qw( DeleteSpaceFromBegEnd GetMaxLength DeleteUnwantedChar IndexArray DeleteHtmlTag );
use CGI qw/:standard :html3/;

# Author: Gail Binkley
# Date  : 1 December 1999

# This package includes routines to manipulate text

use Carp;

#############################################################################
sub DeleteSpaceFromBegEnd {
#############################################################################
# Remove spaces from the beginning and ending of text string
#
# Usage:  &DeleteSpaceFromBegEnd(\$string);

    my ($stringref) = @_;

    $$stringref =~ s/^\s+//;
    $$stringref =~ s/\s+$//;

}


#############################################################################
sub GetMaxLength {
#############################################################################
# Compares the length of 2 strings
#
# Usage:  &GetMaxLength(\$string, $\length);

    my ( $stringref, $lengthref ) = @_;
	
    if (length($$stringref) > $$lengthref) {
	$$lengthref = length($$stringref);
   }

}

#############################################################################
sub DeleteUnwantedChar {
#############################################################################
# Remove spaces from the beginning and ending of text string
# Remove Carriage returns, tabs, new lines and form feeds
#
# Usage:  &DeleteUnwantedChar(\$string);

    my ($stringref) = @_;

    $$stringref =~ s/[\t\n\r\f]/ /g;
    $$stringref =~ s/ +/ /g;
    $$stringref =~ s/^\s+//;
    $$stringref =~ s/\s+$//;
}

#################################################################################################
sub IndexArray{
#################################################################################################
# This subroutine takes an array, and a hash (both by reference), and populates the hash such that
# the array contents hash to the position within the array where they fall.  I do this so often,
# that this seems like a good thing to have prewritten.  The array contents are uppercased.

# Usage : &IndexArray(\@array, \%colHash);

    my ($arrayRef, $colHashRef) = @_;

    my $column;

    my $i = 0;

    foreach $column (@{$arrayRef}){

	$$colHashRef{uc($column)} = $i;
	$i++;

    }
    
}

#############################################################################
sub DeleteHtmlTag {
#############################################################################
# Remove HTML like tags 
#
# Usage:  &DeleteHtmlTag(\$string);

    my ($stringref) = @_;

    $$stringref =~ s/<[^<>]+>//g;

}

