enc_canonize(char_u *enc)
{
    char_u	*r;
    char_u	*p, *s;
    int		i;

    if (STRCMP(enc, "default") == 0)
    {
#ifdef MSWIN
	// Use the system encoding, the default is always utf-8.
	r = enc_locale();
#else
	// Use the default encoding as it's found by set_init_1().
	r = get_encoding_default();
#endif
	if (r == NULL)
	    r = (char_u *)ENC_DFLT;
	return vim_strsave(r);
    }

    // copy "enc" to allocated memory, with room for two '-'
    r = alloc(STRLEN(enc) + 3);
    if (r != NULL)
    {
	// Make it all lower case and replace '_' with '-'.
	p = r;
	for (s = enc; *s != NUL; ++s)
	{
	    if (*s == '_')
		*p++ = '-';
	    else
		*p++ = TOLOWER_ASC(*s);
	}
	*p = NUL;

	// Skip "2byte-" and "8bit-".
	p = enc_skip(r);

	// Change "microsoft-cp" to "cp".  Used in some spell files.
	if (STRNCMP(p, "microsoft-cp", 12) == 0)
	    STRMOVE(p, p + 10);

	// "iso8859" -> "iso-8859"
	if (STRNCMP(p, "iso8859", 7) == 0)
	{
	    STRMOVE(p + 4, p + 3);
	    p[3] = '-';
	}

	// "iso-8859n" -> "iso-8859-n"
	if (STRNCMP(p, "iso-8859", 8) == 0 && isdigit(p[8]))
	{
	    STRMOVE(p + 9, p + 8);
	    p[8] = '-';
	}

	// "latin-N" -> "latinN"
	if (STRNCMP(p, "latin-", 6) == 0)
	    STRMOVE(p + 5, p + 6);

	if (enc_canon_search(p) >= 0)
	{
	    // canonical name can be used unmodified
	    if (p != r)
		STRMOVE(r, p);
	}
	else if ((i = enc_alias_search(p)) >= 0)
	{
	    // alias recognized, get canonical name
	    vim_free(r);
	    r = vim_strsave((char_u *)enc_canon_table[i].name);
	}
    }
    return r;
}
enc_canonize(char_u *enc)
{
    char_u	*r;
    char_u	*p, *s;
    int		i;

    if (STRCMP(enc, "default") == 0)
    {
#ifdef MSWIN
	 
	r = enc_locale();
#else
	 
	r = get_encoding_default();
#endif
	if (r == NULL)
	    r = (char_u *)ENC_DFLT;
	return vim_strsave(r);
    }

     
    r = alloc(STRLEN(enc) + 3);
    if (r != NULL)
    {
	 
	p = r;
	for (s = enc; *s != NUL; ++s)
	{
	    if (*s == '_')
		*p++ = '-';
	    else
		*p++ = TOLOWER_ASC(*s);
	}
	*p = NUL;

	 
	p = enc_skip(r);

	 
	if (STRNCMP(p, "microsoft-cp", 12) == 0)
	    STRMOVE(p, p + 10);

	 
	if (STRNCMP(p, "iso8859", 7) == 0)
	{
	    STRMOVE(p + 4, p + 3);
	    p[3] = '-';
	}

	 
	if (STRNCMP(p, "iso-8859", 8) == 0 && isdigit(p[8]))
	{
	    STRMOVE(p + 9, p + 8);
	    p[8] = '-';
	}

	 
	if (STRNCMP(p, "latin-", 6) == 0)
	    STRMOVE(p + 5, p + 6);

	if (enc_canon_search(p) >= 0)
	{
	     
	    if (p != r)
		STRMOVE(r, p);
	}
	else if ((i = enc_alias_search(p)) >= 0)
	{
	     
	    vim_free(r);
	    r = vim_strsave((char_u *)enc_canon_table[i].name);
	}
    }
    return r;
}