A part of the project includes something similar to a scrolling 'stock ticker', where a larger string "scrolls across" a fixed width output string.
Using C++ 11 on Linux, the concept is clear when using latin characters. Something like this:
std::string inputString, outputString;
for (int inIdx = 0; inIdx < inputString.size(); inIdx++)
{
// shift output one character left
for (int i = 0; i < mOutputTextWidth - 1; i++)
outputString[i] = outputString[i+1];
// Append character to end of output
if (inIdx < inputString.size())
outputString[mTextWidth] = inputString.at(inIdx);
sleep(1);
}
You would get something like:
[ ]
[ H]
[ HE]
[ HEL]
[ HELLO]
[ HELLO ]
[ HELLO ]
[ HELLO ]
I need to make this work for UTF-8 non-latin characters. From what I've read, it is a complex subject. In particular std::string::at
or []
returns a char, which breaks on long UTF-8 characters.
In C++ what's the right way of doing this?
Eg. Japanese
[ ]
[ こ]
[ こん]
[ こんば]
[ こんばん]
[ こんばんは]
[ こんばんは ]
[ こんばんは ]
(I know the glyph widths will vary by language, that's ok. I just can't figure out how to manipulate UTF-8 strings)