:i	static char str_trim_sccs_id[] = "%W% %G%";
#include "V_M_UC.h"
#include "str.h"

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Trim away any of the chars in p from both ends of s.
*/
Str * str_lrtrimsp(s,p,m)
	Str  *s;	/* String to trim */
	char *p;	/* Chars to trim */
	char *m;	/* Decription, for debugmessages */
{	char *x;
	if (s->l > 0) str_rtrimsp(s,p,m);
	if (s->l > 0) str_ltrimsp(s,p,m);
	return s;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Trim away any of the chars in p from the end of s.
*/
Str * str_rtrimsp(s,p,m)
	Str  *s;	/* String to trim */
	char *p;	/* Chars to trim */
	char *m;	/* Decription, for debugmessages */
{	char *x;
	int   c, d;
	while (s->l > 0 && (c = s->v[s->l-1])) {
		for (x=p; (d = *x); x++) {
			if (c == d) {
				s->v[--s->l] = 0;
				goto next;
			}
		}
next:	if (c != d) return s;
	}
	return s;
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Trim away any of the chars in p from the start of s.
*/
Str * str_ltrimsp(s,p,m)
	Str  *s;	/* String to trim */
	char *p;	/* Chars to trim */
	char *m;	/* Decription, for debugmessages */
{	char *v, *x, *z;
	int   c, d, i, l;
	if ((l = s->l) < 1) return s;
	v = s->v;
	z = v + l;
	while (v < z) {		/* Test chars of s one at a time */
		c = *v;
		for (x=p; (d = *x); x++) {	/* Run thru trimmabale chars */
			if (c == d) goto next;	/* If this one trimmable, go on to next s char */
		}
		if (v > s->v) {			/* Found end of trimmable chars */
			l = z - v;;			/* New length */
			for (i=0; i<l; i++)	/* Copy new val to start of s */
				s->v[i] = v[i];
			s->v[s->l = l] = 0;	/* Nul-terminate it to new length */
		}
		return s;
next:	v++;		/* Advance to next char in s */
	}
	s->v[s->l = 0] = 0;	/* All of s is trimmable */
	return s;
}
