//-----------------------------------------------------------------------------
// 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"

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	str_cmp_prefix
//
// purpose	Compare two strings to determine if string one is an exact
//		prefix of string two.
//
// arguments	1 (const char *) string one (prefix candidate)
//		2 (const char *) string two
//
// returns	(char *) pointer to end of prefix in string two
//		(char *) NULL if not a prefix or error
//
// note		If the returned pointer points to a termination character the
//		the two strings are identical.
//-----------------------------------------------------------------------------
char *
str_cmp_prefix (
    const char *	arg_str_one
    ,
    const char *	arg_str_two
    )
__PROTO_END__
{
    char *	str_one		;
    char *	str_two		;

    //-- Validate argument string pointers.
    if ( ! arg_str_one || ! arg_str_two ) return NULL;

    //-- Yes, this strips off the const attribute.
    * ( (const char * *) & str_one ) = arg_str_one;
    * ( (const char * *) & str_two ) = arg_str_two;

    //-- Search for difference point.
    while ( * str_one && * str_one == * str_two ) {
	++ str_one;
	++ str_two;
    }

    //-- Return NULL if string 1 is not a prefix
    //-- else return pointer in string 2 where different.
    return ( ! * str_one ) ? NULL : str_two;
}

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	str_cmp_prefix_nocase
//
// purpose	Compare two strings to determine if string one is an exact
//		prefix of string two, without considering alpha case.
//
// arguments	1 (const char *) string one (prefix candidate)
//		2 (const char *) string two
//
// returns	(char *) pointer to end of prefix in string two
//		(char *) NULL if not a prefix or error
//
// note		If the returned pointer points to a termination character the
//		the two strings are equivalent.
//-----------------------------------------------------------------------------
char *
str_cmp_prefix_nocase (
    const char *	arg_str_one
    ,
    const char *	arg_str_two
    )
__PROTO_END__
{
    char *	str_one		;
    char *	str_two		;

    //-- Validate argument string pointers.
    if ( ! arg_str_one || ! arg_str_two ) return NULL;

    //-- Yes, this strips off the const attribute.
    * ( (const char * *) & str_one ) = arg_str_one;
    * ( (const char * *) & str_two ) = arg_str_two;

    //-- Search for difference point.
    while ( * str_one && tolower( * str_one ) == tolower( * str_two ) ) {
	++ str_one;
	++ str_two;
    }

    //-- Return NULL if string 1 is not a prefix
    //-- else return pointer in string 2 where different.
    return ( ! * str_one ) ? NULL : str_two;
}

