[sword-devel] swlocale question

Karl Kleinpaste karl at kleinpaste.org
Fri Sep 4 05:29:52 MST 2009


Slightly improved version.

	int ncmp[3] = { 100, 5, 2 };	// fixed data

	if (sys_locale) {
		SWBuf locale;
		StringList localelist = LocaleMgr::getSystemLocaleMgr()->getAvailableLocales();
		StringList::iterator it;

		// length-limited match
		for (int i = 0; i < 3; ++i) {
			for (it = localelist.begin(); it != localelist.end(); ++it) {
				locale = *it;
				if (!strncmp(sys_locale, locale.c_str(), ncmp[i])) {
					LocaleMgr::getSystemLocaleMgr()->
					    setDefaultLocaleName(locale.c_str());
					return g_strdup(locale.c_str());
				}
			}
		}

	}

The improvement is the elimination of the 1st pass using just strcmp(),
instead leaving it as a 3-pass test using only strncmp(), but with the
1st pass employing an absurdly long potential match length, having the
contextual effect of using strcmp().

I just like smaller code, especially when one section nearly duplicates
another.



More information about the sword-devel mailing list