On Wednesday 03 July 2002 4:34 pm, Jean-Marc Lasgouttes wrote:
> >>>>> "Togan" == Togan Muftuoglu <[EMAIL PROTECTED]> writes:
>
> Togan> Well it depends as I am using the patched version of Lyx. (the
> Togan> very patch Mike was discussing regarding making Lyx 1.2.0 work
> Togan> again). So somehow what Mike did has to be incorporated into
> Togan> LyX. Without Mike's patch I did not had the menu entries at
> Togan> all. --
>
> Lars, could you have a look at the first patch sent by mike? I do not
> want to have these compare_ascii_no_case everywhere. Most of these are
> consistency tests to see whether current lyxlex token is what we think
> it should be. What about replacing them with
>
> assert(lex.tokenis("foo"))
>
> where tokenis would be something like
>
> bool LyxLex::tokenis(string const & tok)
> {
>         return compare_ascii_no_case(GetString(), tok) == 0;
> }
>
> Or we could remove these checks, as they are used a bit on a random
> basis, and I am not sure how useful they actually are.
>
> JMarc

or we could replace our home grown compare_no_case with a "proper", 
locale-sensitive one. See attached.

Angus
/**
   The following is taken straight out of Appendix A of Meyers (2001).
   In turn, he re-printed the solution proposed by Matt Austern in a column
   in the May 200 C++ Report.

   Scott Meyers, 2001, Effective STL: 50 specific ways to improve your use
   of the standard template library, Addison-Wesley.
 */

#include <algorithm>
#include <functional>
#include <locale>
#include <string>

using std::string;

struct lt_str_2: public std::binary_function<string, string, bool> {
	struct lt_char {
		char const * tab;

		lt_char(char const * t) : tab(t) {}

		bool operator()(char x, char y) const {
			return tab[x - CHAR_MIN] < tab[y - CHAR_MIN];
		}
	};

	char tab[CHAR_MAX - CHAR_MIN + 1];

	lt_str_2(const std::locale & L = std::locale::classic()) {
		std::ctype<char> const & ct =
			std::use_facet<std::ctype<char> >(L);

		for (int i = CHAR_MIN; i <= CHAR_MAX; ++i)
			tab[i - CHAR_MIN] = static_cast<char>(i);

		ct.toupper(tab, tab + (CHAR_MAX - CHAR_MIN + 1));
	}

	bool operator()(string const & x, string const & y) const {
		return std::lexicographical_compare(x.begin(), x.end(),
						    y.begin(), y.end(),
						    lt_char(tab));
	}
};


int compare_no_case(string const & lhs, string const & rhs)
{
	lt_str_2 const cmp = lt_str_2(std::locale());

	if (cmp(lhs, rhs))
		return -1;
	if (cmp(rhs, lhs))
		return 1;
	return 0;
}


namespace {

char const * const langs[] = {
        "Afrikaans",
        "Américain",
        "Arabe",
        "Autrichien",
        "Bahasa",
        "Basque",
        "Portugais (Brésil)",
        "Breton",
        "Anglais Britannique",
        "Bulgarian",
        "Canadien",
        "Français Canadien",
        "Catalan",
        "Croate",
        "Tchèque",
        "Danois",
        "Néerlandais",
        "Anglais",
        "Espéranto",
        "Estonien",
        "Finnois",
        "Français (GUTenberg)",
        "Français",
        "Galicien",
        "Allemand",
        "Grec",
        "Hébreu",
        "Irlandais",
        "Italien",
        "Bas-Sorbe",
        "Magyar",
        "Allemand (nouvelle orthographe)",
        "Norvégien",
        "Polonais",
        "Portugais",
        "Roumain",
        "Russe",
        "Écossais",
        "Serbe",
        "Serbo-Croate",
        "Slovaque",
        "Slovène",
        "Espagnol",
        "Suédois",
        "Thaï",
        "Turc",
        "Ukrainien",
        "Haut-Sorbe",
        "Gallois"};

size_t const langs_size = sizeof(langs) / sizeof(char *);

} // namespace anon


#include <iostream>
#include <vector>

using std::vector;

int main () 
{
	std::locale::global(std::locale("fr_FR.ISO8859-1"));

	std::cout << "The global locale is " << std::locale().name()
		  << std::endl;

	vector<string> sorted(langs, langs+langs_size);
	std::sort(sorted.begin(), sorted.end(), lt_str_2(std::locale()));

	for (int i = 0; i < langs_size; ++i) {
		std::cout << i << "\t" << sorted[i] << std::endl;
	}

	return 0;
	
}

Reply via email to