#include <ncbi.h>
#include <gishlib.h>

/*
	str_tr(s, to, from, default)

	Translate characters in s[] using the rules of from, to, and default.
	The character in 'from' is translated to the corresponding character
	in 'to'.  If 'to' is NULL or shorter than 'from', the default character
	is used.
*/

void LIBCALL
str_tr(sarg, toarg, fromarg, defalt)
	CharPtr sarg, toarg, fromarg;
	int	defalt;
{
	unsigned char	xray[1<<CHAR_BIT];
	register UcharPtr	s = (UcharPtr)sarg,
						to = (UcharPtr)toarg, from = (UcharPtr)fromarg;
	register unsigned char	ch, tch;
	register int	i;

	if (from == NULL)
		return;

	for (i = 0; i < DIM(xray); ++i)
		xray[i] = i;

	tch = defalt;
	while ((ch = *from++) != '\0') {
		if (to != NULL)
			xray[ch] = tch;
		else {
			xray[ch] = tch = *to++;
			if (tch == '\0') {
				to = NULL;
				xray[ch] = tch = defalt;
			}
		}
	}

	while ((ch = *s) != '\0')
		*s++ = xray[ch];

	return;
}
