Skip to content

Commit

Permalink
fallback to ascii copy when utf8 conversion fails
Browse files Browse the repository at this point in the history
  • Loading branch information
d99kris committed Sep 19, 2021
1 parent 0a29898 commit 52a9de1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Project
cmake_minimum_required(VERSION 3.16 FATAL_ERROR) # 3.1 is ok, but is 3.16 needed for proper version string
project(nchat VERSION 2.08 LANGUAGES CXX)
project(nchat VERSION 2.09 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
include(CheckCXXSourceCompiles)
set(NCHAT_PROJECT_VERSION ${PROJECT_VERSION})
Expand Down
27 changes: 25 additions & 2 deletions lib/ncutil/src/strutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <unistd.h>

#include "emoji.h"
#include "log.h"

void StrUtil::DeleteToMatch(std::wstring& p_Str, const int p_StartPos, const wchar_t p_EndChar)
{
Expand Down Expand Up @@ -194,12 +195,34 @@ std::wstring StrUtil::ToLower(const std::wstring& p_WStr)

std::string StrUtil::ToString(const std::wstring& p_WStr)
{
return std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>{}.to_bytes(p_WStr);
try
{
return std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>{}.to_bytes(p_WStr);
}
catch (...)
{
LOG_WARNING("failed to convert from utf-16");
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 StrUtil::ToWString(const std::string& p_Str)
{
return std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>{}.from_bytes(p_Str);
try
{
return std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t>{}.from_bytes(p_Str);
}
catch (...)
{
LOG_WARNING("failed to convert from utf-8");
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 StrUtil::TrimPadWString(const std::wstring& p_Str, int p_Len)
Expand Down
2 changes: 1 addition & 1 deletion src/nchat.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.\" DO NOT MODIFY THIS FILE! It was generated by help2man.
.TH NCHAT "1" "September 2021" "nchat v2.08" "User Commands"
.TH NCHAT "1" "September 2021" "nchat v2.09" "User Commands"
.SH NAME
nchat \- ncurses chat
.SH SYNOPSIS
Expand Down

0 comments on commit 52a9de1

Please sign in to comment.