Default locale to C instead of UTF-8 because UTF-8 isnt functional on all systems; Bugfixes, things should work on SDF now

This commit is contained in:
John Sennesael 2022-03-24 21:36:41 -05:00
parent a5c7eccae6
commit 8d5bc9e873
3 changed files with 29 additions and 5 deletions

View File

@ -367,8 +367,15 @@ int main(int argc, char* argv[])
// Set locale to make unicode input work properly.
const char* langEnv = std::getenv("LANG");
const std::string sysLocale{langEnv ? langEnv : "en_US.utf8"};
std::locale::global(std::locale(sysLocale));
const std::string sysLocale{langEnv ? langEnv : "C"};
try
{
std::locale::global(std::locale(sysLocale));
} catch (const std::exception& e)
{
std::cerr << "Could not set locale to: " << sysLocale << " : " << e.what() << std::endl;
return 5;
}
std::vector<std::string> arguments(argv, argv + argc);
Settings settings = parseArgs(arguments);

View File

@ -22,7 +22,6 @@
#include <vector>
using StrVector = std::vector<std::string>;
enum class Justification { CENTER, FILL, LEFT, RIGHT };
struct Settings

View File

@ -95,6 +95,10 @@ std::vector<WordScore> calculateWordScores(
WordScore findHighestScore(const std::vector<WordScore>& vScores)
{
if (vScores.empty())
{
throw std::runtime_error("Can't find the highest score without scores.");
}
WordScore highest = vScores.front();
for (auto& score: vScores)
{
@ -134,7 +138,13 @@ std::vector<std::wstring> injectSpace(
std::reverse(vWords.begin(), vWords.end());
}
if (vWords.empty()) return vWords;
const auto wordScores = calculateWordScores(vWords);
if (wordScores.empty())
{
// This happens for instance if there's only 1 word.
throw std::runtime_error("No scores could be produced.");
}
auto winner = findHighestScore(wordScores).iterator;
for (auto it = vWords.begin(); it != vWords.end(); ++it)
@ -177,7 +187,7 @@ std::vector<std::wstring> justifyFill(
// Insert spaces until line width matches desired width.
for (auto it = parsedLines.begin(); it != parsedLines.end(); ++it)
{
std::wstring line = *it;
std::wstring line = StringRemoveDuplicateWhitespace(*it);
std::wstring nextLine = L"NONE";
auto itNext = it + 1;
if (itNext != parsedLines.end())
@ -228,7 +238,15 @@ std::vector<std::wstring> justifyFill(
auto lastWordIt = --vWordsWithoutLastWord.end();
const auto lastWord = *lastWordIt;
vWordsWithoutLastWord.erase(lastWordIt);
vWordsWithoutLastWord = injectSpace(vWordsWithoutLastWord);
try
{
vWordsWithoutLastWord = injectSpace(vWordsWithoutLastWord);
}
catch(const std::exception&)
{
std::wcout << L"Error while processing line: \"" << line << L"\"" << std::endl;
std::wcout << L"nWords = " << vWordsWithoutLastWord.size() << std::endl;
}
vWords = vWordsWithoutLastWord;
vWords.emplace_back(lastWord);
line = StringJoin<wchar_t>(vWords, L" ");