Skip to content

Commit

Permalink
fix 4887, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Jun 5, 2017
1 parent 56c7ea3 commit c6f3e88
Show file tree
Hide file tree
Showing 21 changed files with 136 additions and 124 deletions.
1 change: 1 addition & 0 deletions far/RegExp.cpp
Expand Up @@ -37,6 +37,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "RegExp.hpp"

WARNING_DISABLE_GCC("-Wpragmas")
WARNING_DISABLE_GCC("-Wimplicit-fallthrough")

#ifdef RE_DEBUG
Expand Down
6 changes: 6 additions & 0 deletions far/changelog
@@ -1,3 +1,9 @@
drkns 05.06.2017 13:26:32 +0200 - build 4973

1. Уточнение 4887.

2. Рефакторинг.

svs 30.05.2017 23:13:19 +0300 - build 4972

1. SQLite 3.19.2
Expand Down
2 changes: 1 addition & 1 deletion far/cmdline.cpp
Expand Up @@ -880,7 +880,7 @@ void CommandLine::ShowViewEditHistory()

void CommandLine::SetPromptSize(int NewSize)
{
PromptSize = NewSize? Clamp(NewSize, 5, 95) : DEFAULT_CMDLINE_WIDTH;
PromptSize = NewSize? std::clamp(NewSize, 5, 95) : DEFAULT_CMDLINE_WIDTH;
}

class execution_context: noncopyable, public i_execution_context
Expand Down
159 changes: 80 additions & 79 deletions far/colormix.cpp
Expand Up @@ -52,102 +52,103 @@ WORD FarColorToConsoleColor(const FarColor& Color)
static COLORREF LastTrueColors[2] = {};
static FARCOLORFLAGS LastFlags = 0;
static WORD Result = 0;
if (Color.BackgroundColor != LastTrueColors[0] || Color.ForegroundColor != LastTrueColors[1] || (Color.Flags & FCF_4BITMASK) != (LastFlags & FCF_4BITMASK))

if (Color.BackgroundColor == LastTrueColors[0] && Color.ForegroundColor == LastTrueColors[1] && (Color.Flags & FCF_4BITMASK) == (LastFlags & FCF_4BITMASK))
return Result;

LastFlags = Color.Flags;

static BYTE IndexColors[2] = {};

const struct
{
const COLORREF Color;
const rgba RGBA;
FARCOLORFLAGS Flags;
COLORREF* LastColor;
BYTE* IndexColor;
}
data[] =
{
{Color.BackgroundColor, Color.BackgroundRGBA, FCF_BG_4BIT, &LastTrueColors[0], &IndexColors[0]},
{Color.ForegroundColor, Color.ForegroundRGBA, FCF_FG_4BIT, &LastTrueColors[1], &IndexColors[1]}
};

enum console_mask
{
BlueMask = bit(0),
GreenMask = bit(1),
RedMask = bit(2),
IntensityMask = bit(3),
};

for (auto& i: data)
{
LastFlags = Color.Flags;
if (i.Color == *i.LastColor)
continue;

static BYTE IndexColors[2] = {};
const struct
*i.LastColor = i.Color;
if(Color.Flags & i.Flags)
{
COLORREF Color;
rgba RGBA;
FARCOLORFLAGS Flags;
COLORREF* LastColor;
BYTE* IndexColor;
*i.IndexColor = i.Color & ConsoleMask;
continue;
}
data[2] =

int R = i.RGBA.r;
int G = i.RGBA.g;
int B = i.RGBA.b;

// special case, silver color:
if (InRange(160, R, 223) && InRange(160, G, 223) && InRange(160, B, 223))
{
{Color.BackgroundColor, Color.BackgroundRGBA, FCF_BG_4BIT, &LastTrueColors[0], &IndexColors[0]},
{Color.ForegroundColor, Color.ForegroundRGBA, FCF_FG_4BIT, &LastTrueColors[1], &IndexColors[1]}
};
*i.IndexColor = RedMask | GreenMask | BlueMask;
continue;
}

enum console_mask
int* p[] = { &R, &G, &B };
size_t IntenseCount = 0;
for (auto& component : p)
{
BlueMask = bit(0),
GreenMask = bit(1),
RedMask = bit(2),
IntensityMask = bit(3),
};
if(InRange(0, *component, 63))
{
*component = 0;
}
else if(InRange(64, *component, 191))
{
*component = 128;
}
else if(InRange(192, *component, 255))
{
*component = 255;
++IntenseCount;
}
}

for (auto& i: data)
// eliminate mixed intensity
if(IntenseCount > 0 && IntenseCount < 3)
{
if(i.Color != *i.LastColor)
for(auto& component: p)
{
*i.LastColor = i.Color;
if(Color.Flags & i.Flags)
{
*i.IndexColor = i.Color & ConsoleMask;
}
else
if(*component == 128)
{
int R = i.RGBA.r;
int G = i.RGBA.g;
int B = i.RGBA.b;

// special case, silver color:
if (InRange(160, R, 223) && InRange(160, G, 223) && InRange(160, B, 223))
{
*i.IndexColor = RedMask|GreenMask|BlueMask;
}
else
{
int* p[] = { &R, &G, &B };
size_t IntenseCount = 0;
for (auto& component : p)
{
if(InRange(0, *component, 63))
{
*component = 0;
}
else if(InRange(64, *component, 191))
{
*component = 128;
}
else if(InRange(192, *component, 255))
{
*component = 255;
++IntenseCount;
}
}

// eliminate mixed intensity
if(IntenseCount > 0 && IntenseCount < 3)
{
for(auto& component: p)
{
if(*component == 128)
{
*component = IntenseCount==1? 0 : 255;
}
}
}

const auto& ToMask = [](size_t component, console_mask mask) { return component ? mask : 0; };
*i.IndexColor = ToMask(R, RedMask) | ToMask(G, GreenMask) | ToMask(B, BlueMask) | ToMask(IntenseCount, IntensityMask);
}
*component = IntenseCount == 1? 0 : 255;
}
}
}

if (COLORVALUE(data[0].Color) != COLORVALUE(data[1].Color) && IndexColors[0] == IndexColors[1])
{
// oops, unreadable
IndexColors[0]&IntensityMask? IndexColors[0]&=~IntensityMask : IndexColors[1]|=IntensityMask;
}
Result = (IndexColors[0] << ConsoleBgShift) | (IndexColors[1] << ConsoleFgShift);
const auto& ToMask = [](size_t component, console_mask mask) { return component? mask : 0; };
*i.IndexColor = ToMask(R, RedMask) | ToMask(G, GreenMask) | ToMask(B, BlueMask) | ToMask(IntenseCount, IntensityMask);
}

return (WORD)(Result | ((WORD)(Color.Flags & ConsoleExtraMask)));
if (COLORVALUE(data[0].Color) != COLORVALUE(data[1].Color) && IndexColors[0] == IndexColors[1])
{
// oops, unreadable
IndexColors[0] & IntensityMask? IndexColors[0] &= ~IntensityMask : IndexColors[1] |= IntensityMask;
}

Result = (IndexColors[0] << ConsoleBgShift) | (IndexColors[1] << ConsoleFgShift) | (Color.Flags & ConsoleExtraMask);

return Result;
}

FarColor ConsoleColorToFarColor(WORD Color)
Expand Down
15 changes: 0 additions & 15 deletions far/common/algorithm.hpp
Expand Up @@ -227,19 +227,4 @@ std::enable_if_t<!detail::has_find_t<container>::value, bool> contains(const con
return std::find(std::cbegin(Container), End, Element) != End;
}


// TODO: std::clamp once it's clear how to detect its presence
template<typename type, typename compare>
constexpr const type& Clamp(const type& Value, const type& Low, const type& High, compare Compare)
{
return assert(!Compare(High, Low)),
Compare(Value, Low)? Low : Compare(High, Value)? High : Value;
}

template<typename type>
constexpr const type& Clamp(const type& Value, const type& Low, const type& High)
{
return Clamp(Value, Low, High, std::less<>());
}

#endif // ALGORITHM_HPP_BBD588C0_4752_46B2_AAB9_65450622FFF0
2 changes: 1 addition & 1 deletion far/config.cpp
Expand Up @@ -1587,7 +1587,7 @@ Options::Options():
ViOpt.MaxLineSize.SetValidator([](long long Value)
{
return Value?
Clamp(Value, static_cast<long long>(ViewerOptions::eMinLineSize), static_cast<long long>(ViewerOptions::eMaxLineSize)) :
std::clamp(Value, static_cast<long long>(ViewerOptions::eMinLineSize), static_cast<long long>(ViewerOptions::eMaxLineSize)) :
static_cast<long long>(ViewerOptions::eDefLineSize);
});

Expand Down
28 changes: 23 additions & 5 deletions far/cpp.hpp
Expand Up @@ -76,7 +76,7 @@ using std::wmemchr;

#endif

#if COMPILER == C_GCC && !defined(__cpp_lib_nonmember_container_access)
#if !defined _MSC_VER && !defined __cpp_lib_nonmember_container_access
namespace std
{
template <class C>
Expand Down Expand Up @@ -139,7 +139,7 @@ namespace std
}
#endif

#if COMPILER == C_GCC && !defined(__cpp_lib_invoke)
#if !defined _MSC_VER && !defined __cpp_lib_invoke
namespace std
{
template<typename... args>
Expand All @@ -150,7 +150,7 @@ namespace std
}
#endif

#if (COMPILER == C_CL && _MSC_VER < 1910) || (COMPILER != C_CL && !defined(__cpp_lib_apply))
#if (!defined _MSC_VER && !defined __cpp_lib_apply) || (defined _MSC_VER && _MSC_VER < 1910)
namespace std
{
namespace detail
Expand All @@ -170,7 +170,7 @@ namespace std
}
#endif

#if COMPILER == C_GCC && !defined(__cpp_lib_uncaught_exceptions)
#if !defined _MSC_VER && !defined __cpp_lib_uncaught_exceptions
namespace __cxxabiv1
{
extern "C" struct __cxa_eh_globals* __cxa_get_globals() noexcept;
Expand All @@ -185,7 +185,25 @@ namespace std
}
#endif

#if (COMPILER == C_CL && _MSC_VER < 1910) || (COMPILER != C_CL && __cpp_static_assert < 201411)
#if !defined _MSC_VER && !defined __cpp_lib_clamp
namespace std
{
template<typename type, typename compare>
constexpr const type& clamp(const type& Value, const type& Low, const type& High, compare Compare)
{
assert(!Compare(High, Low));
return Compare(Value, Low)? Low : Compare(High, Value)? High : Value;
}

template<typename type>
constexpr const type& clamp(const type& Value, const type& Low, const type& High)
{
return clamp(Value, Low, High, std::less<>());
}
}
#endif

#if (!defined _MSC_VER && __cpp_static_assert < 201411) || (defined _MSC_VER && _MSC_VER < 1910)
#define DETAIL_GET_MACRO(_1, _2, NAME, ...) NAME
#define DETAIL_STATIC_ASSERT_2(expression, message) static_assert(expression, message)
#define DETAIL_STATIC_ASSERT_1(expression) DETAIL_STATIC_ASSERT_2(expression, #expression)
Expand Down
2 changes: 1 addition & 1 deletion far/delete.cpp
Expand Up @@ -440,7 +440,7 @@ ShellDelete::ShellDelete(panel_ptr SrcPanel, bool Wipe):
string tText;
auto mDBttn = Wipe? lng::MDeleteWipe : Global->Opt->DeleteToRecycleBin? lng::MDeleteRecycle : lng::MDelete;
bool bHilite = Global->Opt->DelOpt.HighlightSelected;
const auto mshow = Clamp(static_cast<int>(Global->Opt->DelOpt.ShowSelected), 1, ScrY / 2);
const auto mshow = std::min(std::max((int)Global->Opt->DelOpt.ShowSelected, 1), ScrY / 2);

std::vector<string> items{ strDeleteFilesMsg };

Expand Down
4 changes: 2 additions & 2 deletions far/editcontrol.cpp
Expand Up @@ -116,7 +116,7 @@ void EditControl::SetMenuPos(VMenu2& menu)
{
int MaxHeight = std::min(Global->Opt->Dialogs.CBoxMaxHeight.Get(), static_cast<long long>(menu.size())) + 1;

const auto NewX2 = Clamp(static_cast<int>(m_X2), m_X1 + 20, ScrX - 2);
const auto NewX2 = std::max(std::min(ScrX - 2, static_cast<int>(m_X2)), m_X1 + 20);

if((ScrY-m_Y1<MaxHeight && m_Y1>ScrY/2) || MenuUp)
{
Expand Down Expand Up @@ -791,7 +791,7 @@ bool EditControl::ProcessMouse(const MOUSE_EVENT_RECORD *MouseEvent)
{
SelectionStart=m_CurPos;
}
Select(std::min(SelectionStart, m_CurPos), Clamp(m_CurPos, SelectionStart, m_Str.size()));
Select(std::min(SelectionStart, m_CurPos), std::min(m_Str.size(), std::max(SelectionStart, m_CurPos)));
Show();
}
}
Expand Down
2 changes: 1 addition & 1 deletion far/editor.cpp
Expand Up @@ -6769,7 +6769,7 @@ void Editor::EditorShowMsg(const string& Title,const string& Msg, const string&
const auto strMsg = concat(Msg, L' ', Name);
if (Percent!=-1)
{
const size_t Length = Clamp(static_cast<int>(strMsg.size()), 40, ScrX - 1 - 10);
const size_t Length = std::max(std::min(ScrX - 1 - 10, static_cast<int>(strMsg.size())), 40);
strProgress = make_progressbar(Length, Percent, true, true);
}

Expand Down
1 change: 0 additions & 1 deletion far/elevation.cpp
Expand Up @@ -47,7 +47,6 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "notification.hpp"
#include "scrbuf.hpp"
#include "synchro.hpp"
#include "strmix.hpp"
#include "manager.hpp"
#include "pipe.hpp"
#include "console.hpp"
Expand Down
2 changes: 1 addition & 1 deletion far/filelist.cpp
Expand Up @@ -431,7 +431,7 @@ void FileList::ToEnd()

void FileList::MoveCursor(int offset)
{
m_CurFile = Clamp(m_CurFile + offset, 0, static_cast<int>(m_ListData.size() - 1));
m_CurFile = std::clamp(m_CurFile + offset, 0, static_cast<int>(m_ListData.size() - 1));
}

void FileList::MoveCursorAndShow(int offset)
Expand Down
4 changes: 2 additions & 2 deletions far/filepanels.cpp
Expand Up @@ -246,8 +246,8 @@ void FilePanels::SetPanelPositions(bool LeftFullScreen, bool RightFullScreen) co
if (Global->Opt->WidthDecrement > (ScrX/2-10))
Global->Opt->WidthDecrement=(ScrX/2-10);

Global->Opt->LeftHeightDecrement = Clamp(Global->Opt->LeftHeightDecrement.Get(), 0ll, ScrY - 7ll);
Global->Opt->RightHeightDecrement = Clamp(Global->Opt->RightHeightDecrement.Get(), 0ll, ScrY - 7ll);
Global->Opt->LeftHeightDecrement = std::max(0ll, std::min(Global->Opt->LeftHeightDecrement.Get(), ScrY - 7ll));
Global->Opt->RightHeightDecrement = std::max(0ll, std::min(Global->Opt->RightHeightDecrement.Get(), ScrY - 7ll));

const auto Left = LeftPanel();
const auto Right = RightPanel();
Expand Down
4 changes: 2 additions & 2 deletions far/grabber.cpp
Expand Up @@ -722,8 +722,8 @@ bool Grabber::ProcessMouse(const MOUSE_EVENT_RECORD *MouseEvent)
ResetArea = false;
}

GArea.Current.X = Clamp(IntKeyState.MouseX, SHORT(0), ScrX);
GArea.Current.Y = Clamp(IntKeyState.MouseY, SHORT(0), ScrY);
GArea.Current.X = std::clamp(IntKeyState.MouseX, SHORT(0), ScrX);
GArea.Current.Y = std::clamp(IntKeyState.MouseY, SHORT(0), ScrY);

if (MouseEvent->dwEventFlags == MOUSE_MOVED)
{
Expand Down
4 changes: 2 additions & 2 deletions far/help.cpp
Expand Up @@ -545,7 +545,7 @@ bool Help::ReadHelp(const string& Mask)
string strDescription,strKeyName;
while (Global->CtrlObject->Macro.GetMacroKeyInfo(strMacroArea,MI,strKeyName,strDescription))
{
SizeKeyName = Clamp(strKeyName.size(), SizeKeyName, static_cast<size_t>(MaxLength) / 2);
SizeKeyName = std::min(std::max(SizeKeyName, strKeyName.size()), static_cast<size_t>(MaxLength) / 2);
MI++;
}
MI=0;
Expand Down Expand Up @@ -1130,7 +1130,7 @@ void Help::OutString(const wchar_t *Str)

void Help::CorrectPosition() const
{
StackData->CurX = Clamp(StackData->CurX, 0, CanvasWidth() - 1);
StackData->CurX = std::clamp(StackData->CurX, 0, CanvasWidth() - 1);

if (StackData->CurY > BodyHeight() - 1)
{
Expand Down

0 comments on commit c6f3e88

Please sign in to comment.