//-----------------------------------------------------------------------------
// 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	mem_cmp
// alias	mem_compare
//
// purpose	Compare two strings, which are specified by pointer and length,
//		returning the collation order difference as -1, 0, or 1.  If
//		one string ends before a difference is found, then the shorter
//		string is considered to be lower in sequence than the longer
//		string.
//
// arguments	1 (const char *) pointer to string one
//		2 (size_t) length of string one
//		3 (const char *) pointer to string two
//		4 (size_t) length of string two
//
// returns	(int) -1 : string one  < string two
//		(int)  0 : string one == string two
//		(int)  1 : string one  > string two
//-----------------------------------------------------------------------------
#define mem_compare mem_cmp
int
mem_cmp (
    const char *	arg_one_ptr
    ,
    size_t		arg_one_len
    ,
    const char *	arg_two_ptr
    ,
    size_t		arg_two_len
    )
__PROTO_END__
{
    if ( ! arg_one_ptr ) arg_one_len = 0;
    if ( ! arg_two_ptr ) arg_two_len = 0;

    while ( arg_one_len && arg_two_len ) {
	int ch_one;
	int ch_two;
	ch_one = * (const unsigned char *) arg_one_ptr;
	ch_two = * (const unsigned char *) arg_two_ptr;
	if ( ch_one != ch_two ) {
	    return ( ch_one < ch_two ) ? -1 : 1;
	}
	++ arg_one_ptr;
	++ arg_two_ptr;
	-- arg_one_len;
	-- arg_two_len;
    }

    return arg_one_len ? 1 : arg_two_len ? -1 : 0;
}

