Skip to content

Commit cf2a2b2

Browse files
committed
fix the ${now}/${now,%...} replacement placeholder, and make it work in the regex test dialog as well
part of #470
1 parent 961a776 commit cf2a2b2

File tree

5 files changed

+74
-52
lines changed

5 files changed

+74
-52
lines changed

src/RegexReplaceFormatter.cpp

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// grepWin - regex search and replace for Windows
22

3-
// Copyright (C) 2011, 2015 - Stefan Kueng
3+
// Copyright (C) 2011, 2015, 2024 - Stefan Kueng
44

55
// This program is free software; you can redistribute it and/or
66
// modify it under the terms of the GNU General Public License
@@ -18,3 +18,70 @@
1818
//
1919
#include "stdafx.h"
2020
#include "RegexReplaceFormatter.h"
21+
22+
std::wstring ExpandString(const std::wstring& replaceString)
23+
{
24+
// ${now,formatString}
25+
wchar_t buf[4096] = {};
26+
GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, nullptr, nullptr, buf, _countof(buf));
27+
std::wstring dateStr = buf;
28+
GetTimeFormat(LOCALE_USER_DEFAULT, 0, nullptr, nullptr, buf, _countof(buf));
29+
dateStr += L" - ";
30+
dateStr += buf;
31+
std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
32+
UINT syntaxFlags = boost::regex::normal;
33+
boost::wregex expression = boost::wregex(L"\\$\\{now\\s*,?([^}]*)\\}", syntaxFlags);
34+
boost::match_results<std::wstring::const_iterator> whatC;
35+
boost::match_flag_type matchFlags = boost::match_default | boost::format_all;
36+
auto resultString = replaceString;
37+
SearchReplace(resultString, L"${now}", dateStr);
38+
39+
try
40+
{
41+
auto start = resultString.cbegin();
42+
auto end = resultString.cend();
43+
while (regex_search(start, end, whatC, expression, matchFlags))
44+
{
45+
if (whatC[0].matched)
46+
{
47+
auto fullMatch = whatC.str();
48+
49+
std::wstring formatStr;
50+
if (whatC.size() > 1)
51+
{
52+
formatStr = whatC[1].str();
53+
if (!formatStr.empty())
54+
{
55+
std::wstring formattedDateStr(4096, '\0');
56+
struct tm locTime;
57+
_localtime64_s(&locTime, &now);
58+
try
59+
{
60+
std::wcsftime(&formattedDateStr[0], formattedDateStr.size(), formatStr.c_str(), &locTime);
61+
SearchReplace(resultString, fullMatch, formattedDateStr);
62+
}
63+
catch (const std::exception&)
64+
{
65+
}
66+
}
67+
}
68+
else
69+
{
70+
SearchReplace(resultString, fullMatch, dateStr);
71+
}
72+
}
73+
if (start == whatC[0].second)
74+
{
75+
if (start == end)
76+
break;
77+
++start;
78+
}
79+
else
80+
start = whatC[0].second;
81+
}
82+
}
83+
catch (const std::exception&)
84+
{
85+
}
86+
return resultString;
87+
}

src/RegexReplaceFormatter.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// grepWin - regex search and replace for Windows
22

3-
// Copyright (C) 2011-2012, 2014-2015, 2021, 2023 - Stefan Kueng
3+
// Copyright (C) 2011-2012, 2014-2015, 2021, 2023-2024 - Stefan Kueng
44

55
// This program is free software; you can redistribute it and/or
66
// modify it under the terms of the GNU General Public License
@@ -46,6 +46,9 @@ class NumberReplacer
4646
std::basic_string<CharT> expression;
4747
};
4848

49+
std::wstring ExpandString(const std::wstring &replaceString);
50+
51+
4952
// Iter is the same as the BidirectionalIterator in which `regex_replace` it is used
5053
template<typename CharT, typename Iter = std::basic_string<CharT>::const_iterator>
5154
class RegexReplaceFormatter

src/RegexTestDlg.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// grepWin - regex search and replace for Windows
22

3-
// Copyright (C) 2007-2008, 2011-2013, 2019-2021 - Stefan Kueng
3+
// Copyright (C) 2007-2008, 2011-2013, 2019-2021, 2024 - Stefan Kueng
44

55
// This program is free software; you can redistribute it and/or
66
// modify it under the terms of the GNU General Public License
@@ -214,6 +214,7 @@ void CRegexTestDlg::DoRegex()
214214
if (!bDotMatchesNewline)
215215
rflags |= boost::match_not_dot_newline;
216216

217+
m_replaceText = ExpandString(m_replaceText);
217218
RegexReplaceFormatter<wchar_t> replaceFmt(m_replaceText);
218219
replaceFmt.SetReplacePair(L"${filepath}", L"c:\\grepwintest\\file.txt");
219220
replaceFmt.SetReplacePair(L"${filename}", L"file");

src/SearchDlg.cpp

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4842,51 +4842,3 @@ bool CSearchDlg::CloneWindow()
48424842
return true;
48434843
}
48444844

4845-
std::wstring CSearchDlg::ExpandString(const std::wstring& replaceString)
4846-
{
4847-
// ${now,formatString}
4848-
wchar_t buf[4096] = {};
4849-
GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, nullptr, nullptr, buf, _countof(buf));
4850-
std::wstring dateStr = buf;
4851-
GetTimeFormat(LOCALE_USER_DEFAULT, 0, nullptr, nullptr, buf, _countof(buf));
4852-
dateStr += L" - ";
4853-
dateStr += buf;
4854-
std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
4855-
UINT syntaxFlags = boost::regex::normal;
4856-
boost::wregex expression = boost::wregex(L"\\$\\{now\\s*,?([^}]*)\\}", syntaxFlags);
4857-
boost::match_results<std::wstring::const_iterator> whatC;
4858-
boost::match_flag_type matchFlags = boost::match_default | boost::format_all;
4859-
auto resultString = replaceString;
4860-
try
4861-
{
4862-
while (regex_search(resultString.cbegin(), resultString.cend(), whatC, expression, matchFlags))
4863-
{
4864-
if (whatC[0].matched)
4865-
{
4866-
auto fullMatch = whatC.str();
4867-
4868-
std::wstring formatStr;
4869-
if (whatC.size() > 1)
4870-
{
4871-
formatStr = whatC[1].str();
4872-
if (!formatStr.empty())
4873-
{
4874-
std::wstring formattedDateStr(4096, '\0');
4875-
struct tm locTime;
4876-
_localtime64_s(&locTime, &now);
4877-
std::wcsftime(&formattedDateStr[0], formattedDateStr.size(), formatStr.c_str(), &locTime);
4878-
SearchReplace(resultString, fullMatch, formattedDateStr);
4879-
}
4880-
}
4881-
else
4882-
{
4883-
SearchReplace(resultString, fullMatch, dateStr);
4884-
}
4885-
}
4886-
}
4887-
}
4888-
catch (const std::exception&)
4889-
{
4890-
}
4891-
return resultString;
4892-
}

src/SearchDlg.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ class CSearchDlg : public CDialog
132132
void ShowUpdateAvailable();
133133
static bool IsVersionNewer(const std::wstring& sVer);
134134
bool CloneWindow();
135-
static std::wstring ExpandString(const std::wstring& replaceString);
136135

137136
private:
138137
HWND m_hParent;

0 commit comments

Comments
 (0)