//-----------------------------------------------------------------------------
// Copyright © 2005 - 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.
//-----------------------------------------------------------------------------

__DEFINE_BEGIN__
//-----------------------------------------------------------------------------
// struct	strpe_s
// typedef	strpe_t, strpe_p
//
// purpose	Define a struct containing a string pointer and end pointer.
//-----------------------------------------------------------------------------
struct strpe_s {
    char *	ptr				;
    char *	end				;
};
typedef struct strpe_s		strpe_t		;
typedef struct strpe_s *	strpe_p		;

__DEFINE_END__

__FMACRO_BEGIN__
//-----------------------------------------------------------------------------
// macro	strpe
//
// purpose	Create a strpe value consisting of a string pointer and end
//		pointer.
//
// arguments	1 (char *) string pointer
//		2 (char *) string end pointer
//
// note		This macro is "multiple evaluation safe".  All expressions
//		passed as arguments will only be evaluated once.
//-----------------------------------------------------------------------------
#define strpe(p,e) ({								\
	struct strpe_s libh__spe;						\
	libh__spe.ptr = (p);							\
	libh__spe.end = (e);							\
	libh__spe;								\
})

//-----------------------------------------------------------------------------
// macro	strpe_null
//
// purpose	Make a null value strpe.
//-----------------------------------------------------------------------------
#define strpe_null strpe((NULL),(NULL))

//-----------------------------------------------------------------------------
// macro	strpe_ptr
//
// purpose	Extract the string pointer from a strpe value.
//-----------------------------------------------------------------------------
#define strpe_ptr(s) ((s).ptr)

//-----------------------------------------------------------------------------
// macro	strpe_end
//
// purpose	Extract the string end pointer from a strpe value.
//-----------------------------------------------------------------------------
#define strpe_end(s) ((s).end)

//-----------------------------------------------------------------------------
// macro	strpe_len
//
// purpose	Extract the string length from a strpe value.
//
// note		This macro is "multiple evaluation safe".  All expressions
//		passed as arguments will only be evaluated once.
//-----------------------------------------------------------------------------
#define strpe_len(s) ({								\
	struct strpe_s libh__spe;						\
	libh__spe = (s);							\
	libh__spe.end - libh__spe.ptr;						\
})

//-----------------------------------------------------------------------------
// macro	strpe_strpl
//
// purpose	Convert a strpe value to a strpl value.
//
// note		This macro is "multiple evaluation safe".  All expressions
//		passed as arguments will only be evaluated once.
//-----------------------------------------------------------------------------
#define strpe_strpl(s) ({							\
	struct strpe_s libh__strpe;						\
	struct strpl_s libh__strpl;						\
	libh__strpl.ptr = libh__strpe.ptr;					\
	libh__strpl.len = libh__strpe.end - libh__strpe.ptr;			\
	libh__strpl;								\
})

__FMACRO_END__

