Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sdottaka committed Mar 23, 2023
1 parent 7b80c8a commit a4c2cb5
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 182 deletions.
130 changes: 130 additions & 0 deletions Externals/crystaledit/editlib/FindTextHelper.cpp
@@ -0,0 +1,130 @@
#include "StdAfx.h"
#include "FindTextHelper.h"
#include "utils/cregexp.h"
#include "utils/string_util.h"

static const tchar_t *memstr(const tchar_t *str1, size_t str1len, const tchar_t *str2, size_t str2len)
{
ASSERT(str1 && str2 && str2len > 0);
for (const tchar_t *p = str1; p < str1 + str1len; ++p)
{
if (*p == *str2)
{
if (memcmp(p, str2, str2len * sizeof(tchar_t)) == 0)
return p;
}
}
return nullptr;
}

inline tchar_t mytoupper(tchar_t ch)
{
return static_cast<tchar_t>(reinterpret_cast<uintptr_t>(CharUpper(reinterpret_cast<LPTSTR>(ch))));
}

static const tchar_t *memistr(const tchar_t *str1, size_t str1len, const tchar_t *str2, size_t str2len)
{
ASSERT(str1 && str2 && str2len > 0);
for (const tchar_t *p = str1; p < str1 + str1len; ++p)
{
if (mytoupper(*p) == mytoupper(*str2))
{
size_t i;
for (i = 0; i < str2len; ++i)
{
if (mytoupper(p[i]) != mytoupper(str2[i]))
break;
}
if (i == str2len)
return p;
}
}
return nullptr;
}

ptrdiff_t
FindStringHelper (const tchar_t* pszLineBegin, size_t nLineLength, const tchar_t* pszFindWhere, const tchar_t* pszFindWhat, findtext_flags_t dwFlags, int &nLen, RxNode *&rxnode, RxMatchRes *rxmatch)
{
if (dwFlags & FIND_REGEXP)
{
ptrdiff_t pos = -1;

if (rxnode)
RxFree (rxnode);
rxnode = nullptr;
if (pszFindWhat[0] == '^' && pszLineBegin != pszFindWhere)
return pos;
rxnode = RxCompile (pszFindWhat, (dwFlags & FIND_MATCH_CASE) != 0 ? RX_CASE : 0);
if (rxnode && RxExec (rxnode, pszLineBegin, nLineLength, pszFindWhere, rxmatch))
{
pos = rxmatch->Open[0];
ASSERT((rxmatch->Close[0] - rxmatch->Open[0]) < INT_MAX);
nLen = static_cast<int>(rxmatch->Close[0] - rxmatch->Open[0]);
}
return pos;
}
else
{
ASSERT (pszFindWhere != nullptr);
ASSERT (pszFindWhat != nullptr);
int nCur = static_cast<int>(pszFindWhere - pszLineBegin);
int nLength = (int) tc::tcslen (pszFindWhat);
const tchar_t* pszFindWhereOrig = pszFindWhere;
nLen = nLength;
for (;;)
{
const tchar_t* pszPos;
if (dwFlags & FIND_MATCH_CASE)
pszPos = memstr(pszFindWhere, nLineLength - (pszFindWhere - pszLineBegin), pszFindWhat, nLength);
else
pszPos = memistr(pszFindWhere, nLineLength - (pszFindWhere - pszLineBegin), pszFindWhat, nLength);
if (pszPos == nullptr)
return -1;
if ((dwFlags & FIND_WHOLE_WORD) == 0)
return nCur + (int) (pszPos - pszFindWhere);
if (pszPos > pszFindWhereOrig && xisalnum (pszPos[-1]))
{
nCur += (int) (pszPos - pszFindWhere + 1);
pszFindWhere = pszPos + 1;
continue;
}
if (xisalnum (pszPos[nLength]))
{
nCur += (int) (pszPos - pszFindWhere + 1);
pszFindWhere = pszPos + 1;
continue;
}
return nCur + (int) (pszPos - pszFindWhere);
}
}
//~ ASSERT (false); // Unreachable
}

findtext_flags_t ConvertSearchInfosToSearchFlags(const LastSearchInfos* lastSearch)
{
findtext_flags_t dwSearchFlags = 0;
if (lastSearch->m_bMatchCase)
dwSearchFlags |= FIND_MATCH_CASE;
if (lastSearch->m_bWholeWord)
dwSearchFlags |= FIND_WHOLE_WORD;
if (lastSearch->m_bRegExp)
dwSearchFlags |= FIND_REGEXP;
if (lastSearch->m_nDirection == 0)
dwSearchFlags |= FIND_DIRECTION_UP;
if (lastSearch->m_bNoWrap)
dwSearchFlags |= FIND_NO_WRAP;
if (lastSearch->m_bNoClose)
dwSearchFlags |= FIND_NO_CLOSE;
return dwSearchFlags;
}

void ConvertSearchFlagsToLastSearchInfos(LastSearchInfos* lastSearch, findtext_flags_t dwFlags)
{
lastSearch->m_bMatchCase = (dwFlags & FIND_MATCH_CASE) != 0;
lastSearch->m_bWholeWord = (dwFlags & FIND_WHOLE_WORD) != 0;
lastSearch->m_bRegExp = (dwFlags & FIND_REGEXP) != 0;
lastSearch->m_nDirection = (dwFlags & FIND_DIRECTION_UP) == 0;
lastSearch->m_bNoWrap = (dwFlags & FIND_NO_WRAP) != 0;
lastSearch->m_bNoClose = (dwFlags & FIND_NO_CLOSE) != 0;
}

39 changes: 39 additions & 0 deletions Externals/crystaledit/editlib/FindTextHelper.h
@@ -0,0 +1,39 @@
#pragma once

#include "utils/ctchar.h"
#include "utils/cregexp.h"

// CCrystalTextView::FindText() flags
typedef unsigned findtext_flags_t;

enum : findtext_flags_t
{
FIND_MATCH_CASE = 0x0001U,
FIND_WHOLE_WORD = 0x0002U,
FIND_REGEXP = 0x0004U,
FIND_DIRECTION_UP = 0x0010U,
REPLACE_SELECTION = 0x0100U,
FIND_NO_WRAP = 0x200U,
FIND_NO_CLOSE = 0x400U
};

/**
* @brief Infos about the last search settings (saved in registry)
*
* @note Is also used in the replace dialog
*/
struct LastSearchInfos
{
int m_nDirection; // only for search
bool m_bNoWrap;
bool m_bMatchCase;
CString m_sText;
bool m_bWholeWord;
bool m_bRegExp;
bool m_bNoClose;
};

ptrdiff_t FindStringHelper(const tchar_t* pszLineBegin, size_t nLineLength, const tchar_t* pszFindWhere, const tchar_t* pszFindWhat, findtext_flags_t dwFlags, int& nLen, RxNode*& rxnode, RxMatchRes* rxmatch);
void ConvertSearchFlagsToLastSearchInfos(LastSearchInfos* lastSearch, findtext_flags_t dwFlags);
findtext_flags_t ConvertSearchInfosToSearchFlags(const LastSearchInfos* lastSearch);

2 changes: 1 addition & 1 deletion Externals/crystaledit/editlib/ccrystaleditview.cpp
Expand Up @@ -1470,7 +1470,7 @@ OnDropSource (DROPEFFECT de)
}

void CCrystalEditView::
UpdateView (CCrystalTextView * pSource, CUpdateContext * pContext, DWORD dwFlags, int nLineIndex /*= -1*/ )
UpdateView (CCrystalTextView * pSource, CUpdateContext * pContext, updateview_flags_t dwFlags, int nLineIndex /*= -1*/ )
{
CCrystalTextView::UpdateView (pSource, pContext, dwFlags, nLineIndex);

Expand Down
4 changes: 2 additions & 2 deletions Externals/crystaledit/editlib/ccrystaleditview.h
Expand Up @@ -61,7 +61,7 @@ public :

protected:
bool m_bLastReplace;
DWORD m_dwLastReplaceFlags;
findtext_flags_t m_dwLastReplaceFlags;
CEditReplaceDlg *m_pEditReplaceDlg;

protected:
Expand Down Expand Up @@ -124,7 +124,7 @@ public :
void DoDragScroll (const CPoint & point);

virtual bool QueryEditable ();
virtual void UpdateView (CCrystalTextView * pSource, CUpdateContext * pContext, DWORD dwFlags, int nLineIndex = -1) override;
virtual void UpdateView (CCrystalTextView * pSource, CUpdateContext * pContext, updateview_flags_t dwFlags, int nLineIndex = -1) override;

void SaveLastSearch(LastSearchInfos *lastSearch);
bool ReplaceSelection (const tchar_t* pszNewText, size_t cchNewText, DWORD dwFlags, bool bGroupWithPrevious = false);
Expand Down
2 changes: 1 addition & 1 deletion Externals/crystaledit/editlib/ccrystaltextbuffer.h
Expand Up @@ -41,7 +41,7 @@

class CCrystalTextView;

enum LINEFLAGS: unsigned long
enum LINEFLAGS: lineflags_t
{
LF_BOOKMARK_FIRST = 0x00000001UL,
LF_EXECUTION = 0x00010000UL,
Expand Down
2 changes: 1 addition & 1 deletion Externals/crystaledit/editlib/ccrystaltextmarkers.cpp
Expand Up @@ -20,7 +20,7 @@ CCrystalTextMarkers::~CCrystalTextMarkers()
{
}

bool CCrystalTextMarkers::SetMarker(const tchar_t *pKey, const CString& sFindWhat, DWORD dwFlags, enum COLORINDEX nBgColorIndex, bool bUserDefined, bool bVisible)
bool CCrystalTextMarkers::SetMarker(const tchar_t *pKey, const CString& sFindWhat, findtext_flags_t dwFlags, enum COLORINDEX nBgColorIndex, bool bUserDefined, bool bVisible)
{
Marker marker = { sFindWhat, dwFlags, nBgColorIndex, bUserDefined, bVisible };
m_markers.insert_or_assign(pKey, marker);
Expand Down
5 changes: 3 additions & 2 deletions Externals/crystaledit/editlib/ccrystaltextmarkers.h
Expand Up @@ -7,6 +7,7 @@

#include "SyntaxColors.h"
#include "utils/ctchar.h"
#include "FindTextHelper.h"
#include <vector>
#include <map>

Expand All @@ -18,7 +19,7 @@ class CCrystalTextMarkers
struct Marker
{
CString sFindWhat;
DWORD dwFlags;
findtext_flags_t dwFlags;
enum COLORINDEX nBgColorIndex;
bool bUserDefined;
bool bVisible;
Expand All @@ -29,7 +30,7 @@ class CCrystalTextMarkers

void SetGroupName(const tchar_t *name) { m_sGroupName = name; }
CString GetGroupName() const { return m_sGroupName; }
bool SetMarker(const tchar_t *pKey, const CString& sFindWhat, DWORD dwFlags, enum COLORINDEX nBgColorIndex, bool bUserDefined = true, bool bVisible = true);
bool SetMarker(const tchar_t *pKey, const CString& sFindWhat, findtext_flags_t dwFlags, enum COLORINDEX nBgColorIndex, bool bUserDefined = true, bool bVisible = true);
void DeleteMarker(const tchar_t *pKey);
void DeleteAllMarker();
void AddView(CCrystalTextView *pView);
Expand Down

0 comments on commit a4c2cb5

Please sign in to comment.