//-----------------------------------------------------------------------------
// Copyright © 2003 - Philip Howard - All rights reserved
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package	libh/string
// homepage	http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author	Philip Howard
// email	libh at ipal dot org
// homepage	http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

#include "string_lib.h"

__DEFINE_BEGIN__
//-----------------------------------------------------------------------------
// macro	STR_EQ_NOCASE
// purpose	Specify the option flag to disregard case for alphabetics.
//
// macro	STR_EQ_SPACE
// purpose	Specify the option flag to treat whitespace clusters as one.
//
// macro	STR_EQ_NUMERIC
// purpose	Specify the option flag to handle digits like numeric
//-----------------------------------------------------------------------------
#define STR_EQ_NOCASE	1
#define STR_EQ_SPACE	2
#define STR_EQ_NUMERIC	4

__DEFINE_END__

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	str_eq
//
// purpose	Compare two strings for equality.
//
// arguments	1 (const char *) string 1
//		2 (const char *) string 2
//
// returns	(int) 0 : strings are not equal
//		(int) 1 : strings are equal
//-----------------------------------------------------------------------------
#define str_eq(a,b) (str_eq_opt((a),(b),~(size_t)0,0))

//-----------------------------------------------------------------------------
// macro	str_eq_nocase
//
// purpose	Compare two strings for equality disregarding the case of
//		alphabetic characters.
//
// arguments	1 (const char *) string 1
//		2 (const char *) string 2
//
// returns	(int) 0 : strings are not equal
//		(int) 1 : strings are equal
//-----------------------------------------------------------------------------
#define str_eq_nocase(a,b) (str_eq_opt((a),(b),~(size_t)0,STR_EQ_NOCASE))

//-----------------------------------------------------------------------------
// macro	str_eq_space
//
// purpose	Compare two strings for equality with whitespace clusters
//		considered to be one space.
//
// arguments	1 (const char *) string 1
//		2 (const char *) string 2
//
// returns	(int) 0 : strings are not equal
//		(int) 1 : strings are equal
//-----------------------------------------------------------------------------
#define str_eq_space(a,b) (str_eq_opt((a),(b),~(size_t)0,STR_EQ_SPACE))

//-----------------------------------------------------------------------------
// macro	str_eq_nocase_space
//
// purpose	Compare two strings for equality disregarding the case of
//		alphabetic characters and with whitespace clusters considered
//		to be one space.
//
// arguments	1 (const char *) string 1
//		2 (const char *) string 2
//
// returns	(int) 0 : strings are not equal
//		(int) 1 : strings are equal
//-----------------------------------------------------------------------------
#define str_eq_nocase_space(a,b) (str_eq_opt((a),(b),~(size_t)0,STR_EQ_NOCASE|STR_EQ_SPACE))

//-----------------------------------------------------------------------------
// macro	str_eq_numeric
//
// purpose	Compare two strings for equality with numeric digits compared
//		based on their value.
//
// arguments	1 (const char *) string 1
//		2 (const char *) string 2
//
// returns	(int) 0 : strings are not equal
//		(int) 1 : strings are equal
//-----------------------------------------------------------------------------
#define str_eq_numeric(a,b) (str_eq_opt((a),(b),~(size_t)0,STR_EQ_NUMERIC))

//-----------------------------------------------------------------------------
// macro	str_eq_nocase_numeric
//
// purpose	Compare two strings for equality disregarding the case of
//		alphabetic characters and with numeric digits compared based
//		on their value.
//
// arguments	1 (const char *) string 1
//		2 (const char *) string 2
//
// returns	(int) 0 : strings are not equal
//		(int) 1 : strings are equal
//-----------------------------------------------------------------------------
#define str_eq_nocase_numeric(a,b) (str_eq_opt((a),(b),~(size_t)0,STR_EQ_NOCASE|STR_EQ_NUMERIC))

//-----------------------------------------------------------------------------
// macro	str_eq_space_numeric
//
// purpose	Compare two strings for equality with whitespace clusters
//		considered to be one space and with numeric digits compared
//		based on their value.
//
// arguments	1 (const char *) string 1
//		2 (const char *) string 2
//
// returns	(int) 0 : strings are not equal
//		(int) 1 : strings are equal
//-----------------------------------------------------------------------------
#define str_eq_space_numeric(a,b) (str_eq_opt((a),(b),~(size_t)0,STR_EQ_SPACE|STR_EQ_NUMERIC))

//-----------------------------------------------------------------------------
// macro	str_eq_nocase_space_numeric
//
// purpose	Compare two strings for equality disregarding the case of
//		alphabetic characters and with numeric digits compared based
//		on their value with numeric digits compared based on their
//		value.
//
// arguments	1 (const char *) string 1
//		2 (const char *) string 2
//
// returns	(int) 0 : strings are not equal
//		(int) 1 : strings are equal
//-----------------------------------------------------------------------------
#define str_eq_nocase_space_numeric(a,b) (str_eq_opt((a),(b),~(size_t)0,STR_EQ_NOCASE|STR_EQ_SPACE|STR_EQ_NUMERIC))

__FMACRO_END__

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	str_eq_opt
//
// purpose	Compare two strings for equality according to specified
//		options:
//
//		STR_EQ_NOCASE	disregard case for alphabetics
//		STR_EQ_SPACE	treat whitespace clusters as one
//		STR_EQ_NUMERIC	handle digits like numeric
//
// arguments	1 (const char *) string 1
//		2 (const char *) string 2
//		3 (size_t) maximum length to compare, or ~0 to compare full
//		4 (int) options
//
// returns	(int)  0 : string 1 != string 2
//		(int)  1 : string 1 == string 2
//-----------------------------------------------------------------------------
int
str_eq_opt (
    const char *	arg_string1
    ,
    const char *	arg_string2
    ,
    size_t		arg_length
    ,
    int			arg_option
    )
__PROTO_END__
{
    const char *	str1	;
    const char *	str2	;
    size_t		len	;

    str1 = arg_string1 ? arg_string1 : "";
    str2 = arg_string2 ? arg_string2 : "";

    if ( ( len = arg_length ) == ~(size_t)0 ) {
	for (;;) {
	    if ( arg_option | STR_EQ_SPACE && isspace( * str1 ) && isspace( * str2 ) ) {
		do { ++ str1; } while( isspace( * str1 ) );
		do { ++ str2; } while( isspace( * str2 ) );
		continue;
	    }
	    if ( arg_option | STR_EQ_NUMERIC && isdigit( * str1 ) && isdigit( * str2 ) ) {
		while ( * str1 == '0' ) ++ str1;
		while ( * str2 == '0' ) ++ str2;
		continue;
	    }
	    if ( arg_option | STR_EQ_NOCASE ) {
		if ( tolower( * str1 ) != tolower( * str2 ) ) break;
	    }
	    else if ( * str1 != * str2 ) break;
	    ++ str1;
	    ++ str2;
	}
	return ( * str1 == 0 && * str2 == 0 ) ? 1 : 0;
    } else {
	for ( ; len > 0 ; -- len ) {
	    if ( arg_option | STR_EQ_SPACE && isspace( * str1 ) && isspace( * str2 ) ) {
		do { ++ str1; } while( isspace( * str1 ) );
		do { ++ str2; } while( isspace( * str2 ) );
		continue;
	    }
	    if ( arg_option | STR_EQ_NUMERIC && isdigit( * str1 ) && isdigit( * str2 ) ) {
		while ( * str1 == '0' ) ++ str1;
		while ( * str2 == '0' ) ++ str2;
		continue;
	    }
	    if ( arg_option | STR_EQ_NOCASE ) {
		if ( tolower( * str1 ) != tolower( * str2 ) ) break;
	    }
	    else if ( * str1 != * str2 ) break;
	    ++ str1;
	    ++ str2;
	}
	return ( len == 0 || ( * str1 == 0 && * str2 == 0 ) ) ? 1 : 0;
    }
}

