Moved the first character extraction function to StringUtil.

This commit is contained in:
Leon Styhre 2021-01-23 16:25:53 +01:00
parent 0dfe52586c
commit 8173ccda80
4 changed files with 28 additions and 31 deletions

View file

@ -92,20 +92,11 @@ GuiGamelistOptions::GuiGamelistOptions(
ViewController::FAVORITE_CHAR))
isFavorite = true;
if (mFavoritesSorting && file->getFavorite() && isFavorite) {
// Get the first character of the game name (which could be a Unicode character).
if (mFavoritesSorting && file->getFavorite() && isFavorite)
mCurrentFirstCharacter = ViewController::FAVORITE_CHAR;
}
else {
unsigned char checkCharType = file->getSortName().front();
if (checkCharType <= 0x7F) // Normal ASCII character.
mCurrentFirstCharacter = toupper(file->getSortName().front());
else if (checkCharType >= 0xF0) // Four-byte Unicode character.
mCurrentFirstCharacter = file->getSortName().substr(0, 4);
else if (checkCharType >= 0xE0) // Three-byte Unicode character.
mCurrentFirstCharacter = file->getSortName().substr(0, 3);
else if (checkCharType >= 0xC0) // Two-byte Unicode character.
mCurrentFirstCharacter = file->getSortName().substr(0, 2);
}
else
mCurrentFirstCharacter = Utils::String::getFirstCharacter(file->getSortName());
}
mJumpToLetterList = std::make_shared<LetterList>(mWindow, getHelpStyle(),

View file

@ -478,28 +478,15 @@ void ISimpleGameListView::generateFirstLetterIndex(const std::vector<FileData*>&
// Build the index.
for (auto it = files.begin(); it != files.end(); it++) {
if ((*it)->getType() == FOLDER && (*it)->getFavorite() &&
favoritesSorting && !onlyFavorites) {
favoritesSorting && !onlyFavorites)
hasFavorites = true;
}
else if ((*it)->getType() == FOLDER && foldersOnTop && !onlyFolders) {
else if ((*it)->getType() == FOLDER && foldersOnTop && !onlyFolders)
hasFolders = true;
}
else if ((*it)->getType() == GAME && (*it)->getFavorite() &&
favoritesSorting && !onlyFavorites) {
favoritesSorting && !onlyFavorites)
hasFavorites = true;
}
else {
unsigned char checkCharType = (*it)->getSortName().front();
if (checkCharType <= 0x7F) // Normal ASCII character.
firstChar = toupper((*it)->getSortName().front());
else if (checkCharType >= 0xF0) // Four-byte Unicode character.
firstChar = (*it)->getSortName().substr(0, 4);
else if (checkCharType >= 0xE0) // Three-byte Unicode character.
firstChar = (*it)->getSortName().substr(0, 3);
else if (checkCharType >= 0xC0) // Two-byte Unicode character.
firstChar = (*it)->getSortName().substr(0, 2);
mFirstLetterIndex.push_back(firstChar);
}
else
mFirstLetterIndex.push_back(Utils::String::getFirstCharacter((*it)->getSortName()));
}
// Sort and make each entry unique.

View file

@ -91,6 +91,23 @@ namespace Utils
return result;
}
std::string getFirstCharacter(const std::string& _string, bool _toUpper)
{
std::string firstChar;
unsigned char checkCharType = _string.front();
if (checkCharType <= 0x7F) // Normal UTF-8 ASCII character.
(_toUpper) ? firstChar = toupper(_string.front()) : firstChar = _string.front();
else if (checkCharType >= 0xF0) // Four-byte Unicode character.
firstChar = _string.substr(0, 4);
else if (checkCharType >= 0xE0) // Three-byte Unicode character.
firstChar = _string.substr(0, 3);
else if (checkCharType >= 0xC0) // Two-byte Unicode character.
firstChar = _string.substr(0, 2);
return firstChar;
}
size_t nextCursor(const std::string& _string, const size_t _cursor)
{
size_t result = _cursor;

View file

@ -19,6 +19,8 @@ namespace Utils
{
unsigned int chars2Unicode(const std::string& _string, size_t& _cursor);
std::string unicode2Chars(const unsigned int _unicode);
// Return the first character, which could be normal ASCII or 2, 3 or 4 byte Unicode.
std::string getFirstCharacter(const std::string& _string, bool _toUpper = true);
size_t nextCursor(const std::string& _string, const size_t _cursor);
size_t prevCursor(const std::string& _string, const size_t _cursor);
size_t moveCursor(const std::string& _string, const size_t _cursor, const int _amount);