778899
778899

Reputation: 339

std::find case insensitive checking

bool is_executable_file(std::wstring file) {
    std::vector<std::wstring> d = get_splitpath(file);
    // d [ 3 ] is extension of file ie .exe
    std::vector<std::wstring> ext = { L".exe", L".dll", L".cmd", L".msi" };
    if ( std::find(ext.begin() , ext.end() , d [ 3 ]) != ext.end() ) {
        return true;
    }
    // do further checks
    return false;
}

In the above how can I get std::find to do a case insensitive check so I don't need to add all combinations ie .exe and .EXE are the same?

Or another way to check a file extension against a list of extensions ignoring case in both the extension and list of extensions?

Upvotes: 1

Views: 3100

Answers (1)

Killzone Kid
Killzone Kid

Reputation: 6240

You could probably do with std::equal and std::tolower to check both wstrings

#include <iostream>
#include <string>
#include <algorithm>
#include <cwctype>
#include <locale>

int main()
{
    std::wstring wstr1 = L"dll", wstr2 = L"DLL";

    auto icompare = [](wchar_t const &c1, wchar_t const &c2)
    { 
        return std::tolower(c1, std::locale()) == std::tolower(c2, std::locale());
    };

    if (std::equal(wstr1.begin(), wstr1.end(), wstr2.begin(), wstr2.end(), icompare))
        std::cout << "equal" << std::endl;
    else
        std::cout << "notEqual" << std::endl;

    return 0;
}

For example: https://ideone.com/I3zukI

Upvotes: 1

Related Questions