-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved handling for double-width characters in track name
- Loading branch information
Showing
6 changed files
with
118 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// util.cpp | ||
// | ||
// Copyright (c) 2022 Kristofer Berggren | ||
// All rights reserved. | ||
// | ||
// namp is distributed under the GPLv2 license, see LICENSE for details. | ||
// | ||
|
||
#include "util.h" | ||
|
||
#include <algorithm> | ||
#include <codecvt> | ||
#include <locale> | ||
|
||
std::string Util::ToString(const std::wstring& p_WStr) | ||
{ | ||
try | ||
{ | ||
return std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>{ }.to_bytes(p_WStr); | ||
} | ||
catch (...) | ||
{ | ||
std::wstring wstr = p_WStr; | ||
wstr.erase(std::remove_if(wstr.begin(), wstr.end(), [](wchar_t wch) { return !isascii(wch); }), wstr.end()); | ||
std::string str = std::string(wstr.begin(), wstr.end()); | ||
return str; | ||
} | ||
} | ||
|
||
std::wstring Util::ToWString(const std::string& p_Str) | ||
{ | ||
try | ||
{ | ||
return std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>{ }.from_bytes(p_Str); | ||
} | ||
catch (...) | ||
{ | ||
std::string str = p_Str; | ||
str.erase(std::remove_if(str.begin(), str.end(), [](unsigned char ch) { return !isascii(ch); }), str.end()); | ||
std::wstring wstr = std::wstring(str.begin(), str.end()); | ||
return wstr; | ||
} | ||
} | ||
|
||
std::wstring Util::TrimPadWString(const std::wstring& p_Str, int p_Len) | ||
{ | ||
p_Len = std::max(p_Len, 0); | ||
std::wstring str = p_Str; | ||
if (WStringWidth(str) > p_Len) | ||
{ | ||
str = str.substr(0, p_Len); | ||
int subLen = p_Len; | ||
while (WStringWidth(str) > p_Len) | ||
{ | ||
str = str.substr(0, --subLen); | ||
} | ||
} | ||
else if (WStringWidth(str) < p_Len) | ||
{ | ||
str = str + std::wstring(p_Len - WStringWidth(str), ' '); | ||
} | ||
return str; | ||
} | ||
|
||
int Util::WStringWidth(const std::wstring& p_WStr) | ||
{ | ||
int width = wcswidth(p_WStr.c_str(), p_WStr.size()); | ||
return (width != -1) ? width : p_WStr.size(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// util.h | ||
// | ||
// Copyright (c) 2022 Kristofer Berggren | ||
// All rights reserved. | ||
// | ||
// namp is distributed under the GPLv2 license, see LICENSE for details. | ||
// | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
|
||
class Util | ||
{ | ||
public: | ||
static std::string ToString(const std::wstring& p_WStr); | ||
static std::wstring ToWString(const std::string& p_Str); | ||
static std::wstring TrimPadWString(const std::wstring& p_Str, int p_Len); | ||
static int WStringWidth(const std::wstring& p_WStr); | ||
}; |