Skip to content

Commit

Permalink
Show code point values for surrogate pairs in editor status line
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Jun 12, 2021
1 parent 9134c2a commit 956e9de
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
5 changes: 5 additions & 0 deletions far/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
--------------------------------------------------------------------------------
drkns 12.06.2021 12:39:43 +0100 - build 5820

1. Show code point values for surrogate pairs in editor status line.

--------------------------------------------------------------------------------
drkns 12.06.2021 02:00:20 +0100 - build 5819

Expand Down
2 changes: 1 addition & 1 deletion far/char_width.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ namespace
return codepoint_width::ambiguous;
}

const auto IsBMP = Codepoint < 0x10000;
const auto IsBMP = Codepoint <= std::numeric_limits<wchar_t>::max();

if (IsBMP)
{
Expand Down
27 changes: 18 additions & 9 deletions far/fileedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2213,11 +2213,13 @@ string FileEditor::GetTitle() const
return strLocalTitle;
}

static std::pair<string, size_t> char_code(std::optional<wchar_t> const& Char, int const Codebase)
static std::pair<string, size_t> char_code(std::optional<unsigned> const& Char, int const Codebase)
{
const auto process = [&](const auto& Format, string_view const Max)
{
return std::pair{ Char.has_value()? format(Format, static_cast<unsigned>(*Char)) : L""s, Max.size() };
auto Result = std::pair{ Char.has_value()? format(Format, *Char) : L""s, Max.size() };
Result.second = std::max(Result.first.size(), Result.second);
return Result;
};

switch (Codebase)
Expand All @@ -2234,20 +2236,24 @@ static std::pair<string, size_t> char_code(std::optional<wchar_t> const& Char, i
}
}

static std::pair<string, size_t> ansi_char_code(std::optional<wchar_t> const& Char, int const Codebase, uintptr_t const Codepage)
static std::pair<string, size_t> ansi_char_code(std::optional<unsigned> const& Char, int const Codebase, uintptr_t const Codepage)
{
const auto process = [&](const auto& Format, string_view const Max)
{
std::optional<unsigned> CharCode;

char Buffer;
encoding::error_position ErrorPosition;
if (Char.has_value() && encoding::get_bytes(Codepage, { &*Char, 1 }, { &Buffer, 1 }, &ErrorPosition) == 1 && !ErrorPosition)
if (Char.has_value() && *Char <= std::numeric_limits<wchar_t>::max())
{
const unsigned AnsiCode = Buffer;
if (AnsiCode != *Char)
const auto Ch = static_cast<wchar_t>(*Char);
if (encoding::get_bytes(Codepage, { &Ch, 1 }, { &Buffer, 1 }, &ErrorPosition) == 1 && !ErrorPosition)
{
CharCode = AnsiCode;
const unsigned AnsiCode = Buffer;
if (AnsiCode != *Char)
{
CharCode = AnsiCode;
}
}
}

Expand Down Expand Up @@ -2284,8 +2290,11 @@ void FileEditor::ShowStatus() const
string CharCode;

{
std::optional<wchar_t> Char;
if (CurPos < Str.size())
std::optional<unsigned> Char;

if (CurPos + 1 < Str.size() && is_valid_surrogate_pair(Str[CurPos], Str[CurPos + 1]))
Char = encoding::utf16::extract_codepoint(Str[CurPos], Str[CurPos + 1]);
else if (CurPos < Str.size())
Char = Str[CurPos];

auto [UnicodeStr, UnicodeSize] = char_code(Char, m_editor->EdOpt.CharCodeBase);
Expand Down
2 changes: 1 addition & 1 deletion far/vbuild.m4
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5819
5820

0 comments on commit 956e9de

Please sign in to comment.