简体   繁体   中英

C++ regex with char and wchar_t?

I have a const char and a const wchar_t. My function below works with the char. What's the simplest/most efficient way to write a function that can easily handle both char and wchar_t?

    const char* asciiChar = "this is an ascii string";
    const wchar_t* unicodeChar = L"this is a unicode string";

    std::string replaceSubstring(const char* find, const char* asciiChar, const char* replace)
    {
        std::string const text(str);
        std::regex const reg(find);
        std::string const newStr = std::regex_replace(text, reg, replace);
        return newStr;
    }

For this reason exactly, regex is a typedef of basic_regex<char> , much like string is a typedef of basic_string<char> . Knowing this, you can get away with a single template:

template<typename CharType>
std::basic_string<CharType>
  replaceSubstring(const CharType* find, const CharType* str, const CharType* rep)
{
    std::basic_string<CharType> text(str);
    std::basic_regex<CharType> reg(find);
    return std::regex_replace(text, reg, rep);
}

This correctly handles both char pointers and wchar_t pointers, and returns the correct type of string. You may want to accept const std::basic_string<CharType>& parameters instead, too.

you can use another function to overload the function:

    std::wstring replaceSubstring(const wchar_t* find, const wchar_t* asciiChar, const wchar_t* replace)
    {
        std::wstring const text(str);
        std::wregex const reg(find);
        std::wstring const newStr = std::wregex_replace(text, reg, replace);
        return newStr;
    }

You can use template for this, like below,

template<typename CharT>
const CharT* replaceSubstring(const CharT* find, const CharT* str, const CharT* replace);

template<> const char* replaceSubstring<char>(const char* find, const char* str, const char* replace) 
{
    std::string const text(str);
    std::regex const reg(find);

    std::string swap_str(replace);

    return std::regex_replace(text, reg, swap_str).c_str();
}

template<> const wchar_t* replaceSubstring<wchar_t>(const wchar_t* find, const wchar_t* str, const wchar_t* replace) 
{   
    std::wstring const text(str);
    std::wregex const reg(find);

    std::wstring swap_str(replace);

    return std::regex_replace(text, reg, swap_str).c_str();
}

Also, overloading can be an other option.

You may get good advice from below link.

Is it possible to have a C++ method accept either const char* and const wchar_t* as a parameter without overloading the method?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM