Clearer
Clearer

Reputation: 2306

How do I check if a std::string, containing utf8 text, starts with an uppercase letter in Windows?

I'm a complete novice as a Windows programmer and find it very hard to read Microsoft's documentation with all their weird custom types.

As far as I can tell, I need to use IsCharUpper, but I can't find any documentation that tells me how to use a std::string (containing utf8 text) and get a TCHAR representing the first character of the std::string.

The std::string is utf8 and standardising on something else is not an option. Only supporting some scripts is not an option either.

Upvotes: 0

Views: 133

Answers (2)

Chris Becke
Chris Becke

Reputation: 36101

TCHAR is an alias in the Windows API and will represent a CHAR or a WCHAR depending on whether you have built your application for Unicode or not.

MultiByteToWideChar is the API you are looking for, it will take your utf8 input string and will fill in a WCHAR array. If your application is built for Unicode you will be able to pass WCHARs directly to IsCharUpper. If not, you can call IsCharUpperW which will take a WCHAR regardless of build configuration.

Something like this:

WCHAR out[2]={0};
int written = MultiByteToWideChar( 
  CP_UTF8, // Your input string is UTF8.
  0, // no flags apply
  your_string.c_str(), // the std::string you want to test
  your_string.length(), // can't pass 1 as the characters are variable size
  out, // Put the character here.
  1 // we want 1 character converted.
);
bool isUpper = false;
if(written == 1)
  isUpper = IsCharUpper(out[0]);

It is my recollection that this function doesn't need to write out a zero terminator when given an exact number of characters to write so this should work.

Upvotes: 2

AndersK
AndersK

Reputation: 36102

You could try something like this

#include <locale>
#include <codecvt>


bool isUpper(const std::string& utf8Str)
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
    return IsUpperCase(myconv.from_bytes(utf8Str)[0]);
}

Upvotes: 2

Related Questions