mirror of
				https://github.com/OpenMW/openmw.git
				synced 2025-11-04 07:56:40 +00:00 
			
		
		
		
	There is no change in behaviour since we were using the C locale. The locale-aware tolower is much slower than the locale-unaware one. At least on Linux/GCC it calls dynamic_cast's, and is overall slower by an order of magnitude.
		
			
				
	
	
		
			75 lines
		
	
	
	
		
			1.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			75 lines
		
	
	
	
		
			1.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
#ifndef MISC_STRINGOPS_H
 | 
						|
#define MISC_STRINGOPS_H
 | 
						|
 | 
						|
#include <cctype>
 | 
						|
#include <string>
 | 
						|
#include <algorithm>
 | 
						|
 | 
						|
namespace Misc
 | 
						|
{
 | 
						|
class StringUtils
 | 
						|
{
 | 
						|
    struct ci
 | 
						|
    {
 | 
						|
        bool operator()(char x, char y) const {
 | 
						|
            return tolower(x) < tolower(y);
 | 
						|
        }
 | 
						|
    };
 | 
						|
 | 
						|
public:
 | 
						|
    static bool ciLess(const std::string &x, const std::string &y) {
 | 
						|
        return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), ci());
 | 
						|
    }
 | 
						|
 | 
						|
    static bool ciEqual(const std::string &x, const std::string &y) {
 | 
						|
        if (x.size() != y.size()) {
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        std::string::const_iterator xit = x.begin();
 | 
						|
        std::string::const_iterator yit = y.begin();
 | 
						|
        for (; xit != x.end(); ++xit, ++yit) {
 | 
						|
            if (tolower(*xit) != tolower(*yit)) {
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
        }
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
 | 
						|
    static int ciCompareLen(const std::string &x, const std::string &y, size_t len)
 | 
						|
    {
 | 
						|
        std::string::const_iterator xit = x.begin();
 | 
						|
        std::string::const_iterator yit = y.begin();
 | 
						|
        for(;xit != x.end() && yit != y.end() && len > 0;++xit,++yit,--len)
 | 
						|
        {
 | 
						|
            int res = *xit - *yit;
 | 
						|
            if(res != 0 && tolower(*xit) != tolower(*yit))
 | 
						|
                return (res > 0) ? 1 : -1;
 | 
						|
        }
 | 
						|
        if(len > 0)
 | 
						|
        {
 | 
						|
            if(xit != x.end())
 | 
						|
                return 1;
 | 
						|
            if(yit != y.end())
 | 
						|
                return -1;
 | 
						|
        }
 | 
						|
        return 0;
 | 
						|
    }
 | 
						|
 | 
						|
    /// Transforms input string to lower case w/o copy
 | 
						|
    static std::string &toLower(std::string &inout) {
 | 
						|
        for (unsigned int i=0; i<inout.size(); ++i)
 | 
						|
            inout[i] = tolower(inout[i]);
 | 
						|
        return inout;
 | 
						|
    }
 | 
						|
 | 
						|
    /// Returns lower case copy of input string
 | 
						|
    static std::string lowerCase(const std::string &in)
 | 
						|
    {
 | 
						|
        std::string out = in;
 | 
						|
        return toLower(out);
 | 
						|
    }
 | 
						|
};
 | 
						|
 | 
						|
}
 | 
						|
 | 
						|
#endif
 |