Strings in Tahiti

From version 6.0 Tahiti is compiled as a Unicode application. Two approaches are used:

  • our own string types defined in TahitiBase for the Tahiti core and multiplatform compatibility,

  • CString for the user interface on the Windows platform.

TahitiBase string types

TahitiBase/TahitiString.h provides three types, all deriving from a std string type:

Tahiti::u8string

UTF-8 string, derived from std::string. The type for core and portable code. Its const char* constructor is explicit; build literals with the TU8("...") macro.

Tahiti::pstring

Platform dependent Unicode string for OS and filesystem APIs. On Windows derived from std::wstring (UTF-16), on Linux from std::string (UTF-8).

Tahiti::astring

The UI string. In Unicode builds (all current builds) it is derived from std::wstring (UTF-16) and matches CString; in non-Unicode builds it was derived from std::string in the user’s OEM code page; on Linux it is std::string (UTF-8).

Related macro:

Tahiti::u8string someText;
someText = TU8("Description of something ...");

Conversions

Convert between the Tahiti::XXXstring types with the constructor of the target type:

Tahiti::u8string str(TU8("My data"));
Tahiti::pstring str2(str);

Build messages with Tahiti::U8StringBuilder().append(...).

User interface

MFC and ATL use CString. As the application is compiled as Unicode, CString is based on wchar_t. In UI classes that are Windows specific we prefer CString for storing data. For input parameters we use LPCTSTR, which is const wchar_t * in a Unicode application.

FAQ

How do I set text from a Tahiti::u8string into a control? Use Tahiti::astring, which in a Unicode build is UTF-16 like CString:

Tahiti::u8string data(TU8("some text in UTF-8"));
Tahiti::astring strForUI(data);
m_ctrl.SetWindowText(strForUI.c_str());

How do I read text from a control into a Tahiti::u8string ? Read it into a CString and convert through Tahiti::astring:

CString strFromUI;
m_ctrl.GetWindowText(strFromUI);

Tahiti::u8string data;
data = Tahiti::astring(CSTRING_TO_ASTRING(strFromUI));