-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#990:Implemented escape sequence for jar option with trailing whitespaces #472
#990:Implemented escape sequence for jar option with trailing whitespaces #472
Conversation
|
||
void replaceTrailingEscapeWhitespaces(std::string & s) { | ||
if (s.size() > 0) { | ||
const size_t lastIdx = s.find_last_not_of(" \t\v\f"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
trim right
if (s.size() > 0) { | ||
const size_t lastIdx = s.find_last_not_of(" \t\v\f"); | ||
if (lastIdx != std::string::npos) { | ||
size_t rtrimIdx = lastIdx + 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
first whitespace after last not whitespace
if (nBackslashes > 0) { | ||
s = s.erase(pos-nBackslashes, (nBackslashes>>1) ); | ||
} | ||
rtrimIdx = pos + 1 - (nBackslashes>>1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extract nBackslashes>>1 into variable with name halfBackslasches or so
else { | ||
//<replacement> does belong to an escape sequence | ||
//Delete half of the backslashes because they belong to the escape sequences + 1 of the <replacement> | ||
s = s.erase(pos-nBackslashes, (nBackslashes>>1)+1 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -21,7 +21,7 @@ inline void ltrim(std::string &s) { | |||
} | |||
|
|||
inline void rtrim(std::string &s) { | |||
s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { | |||
s.erase(std::find_if(s.rbegin(), s.rend(), [&](unsigned char ch) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to capture
0020462
to
506db77
Compare
if (value != "") { | ||
m_jvmOptions.push_back("-Dexasol.scriptclass=" + trimmedValue); | ||
m_jvmOptions.push_back("-Dexasol.scriptclass=" + value); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if value.empty() {
return
}
m_jvmOptions.push_back("-Dexasol.scriptclass=" + value);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally, it is easier to read if you add preconditions with an early return at the top and include the default logic in the default flow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I knew I another point
uint32_t retVal(0); | ||
while (pos >= 0 && s[pos--] == '\\') retVal++; | ||
return retVal; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think something along the lines below should do too.
uint32_t retVal(0); | |
while (pos >= 0 && s[pos--] == '\\') retVal++; | |
return retVal; | |
// including <algorithm> is required | |
return std::count (s.cbegin(), s.cend(), '\\'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, but does this work, because we need to stop, when we encounter now backslash
inline size_t replaceOnlyBackslashSequencesButKeepChar(std::string & s, size_t backslashStartIdx, size_t nBackslashes) { | ||
const uint32_t nHalfBackslashes = (nBackslashes>>1); | ||
if (nHalfBackslashes > 0) { | ||
s = s.erase(backslashStartIdx, nHalfBackslashes ); | ||
} | ||
const size_t newBackslashEndIdx = backslashStartIdx + nHalfBackslashes; | ||
return newBackslashEndIdx + 1; //+1 because of the last None-Whitespace character we need to keep | ||
} | ||
|
||
inline size_t replaceBackslashSequencesAndWhitespaceSequence(std::string & s, size_t backslashStartIdx, | ||
size_t nBackslashes, const char* replacement) { | ||
const uint32_t nHalfBackslashes = (nBackslashes>>1); | ||
s = s.erase(backslashStartIdx, nHalfBackslashes+1 ); //Delete also backslash of whitespace escape sequence | ||
const size_t newBackslashEndIdx = backslashStartIdx + nHalfBackslashes; | ||
s = s.replace(newBackslashEndIdx, 1, replacement); | ||
return newBackslashEndIdx + 1; //+1 because of the replaced whitespace character | ||
} | ||
|
||
inline size_t replaceCharAtPositionAndBackslashes(std::string & s, size_t pos, const char* replacement) { | ||
const uint32_t nBackslashes = countBackslashesBackwards(s, pos-1); | ||
|
||
const size_t backslashStartIdx = pos-nBackslashes; | ||
if(nBackslashes % 2 == 0) { | ||
return replaceOnlyBackslashSequencesButKeepChar(s, backslashStartIdx, nBackslashes); | ||
} | ||
else { | ||
return replaceBackslashSequencesAndWhitespaceSequence(s, backslashStartIdx, nBackslashes, replacement); | ||
} | ||
} | ||
|
||
void replaceTrailingEscapeWhitespaces(std::string & s) { | ||
if (s.size() > 0) { | ||
const size_t lastNoneWhitespaceIdx = s.find_last_not_of(" \t\v\f"); | ||
if (lastNoneWhitespaceIdx != std::string::npos) { | ||
size_t firstWhitespaceAfterNoneWhitespaceIdx = lastNoneWhitespaceIdx + 1; | ||
if (s.size() > 1) { | ||
if(s[lastNoneWhitespaceIdx] == 't') { | ||
firstWhitespaceAfterNoneWhitespaceIdx = replaceCharAtPositionAndBackslashes(s, lastNoneWhitespaceIdx, "\t"); | ||
} else if (s[lastNoneWhitespaceIdx] == '\\' && s[lastNoneWhitespaceIdx+1] == ' ') { | ||
firstWhitespaceAfterNoneWhitespaceIdx = replaceCharAtPositionAndBackslashes(s, lastNoneWhitespaceIdx+1, " "); | ||
} else if (s[lastNoneWhitespaceIdx] == 'f') { | ||
firstWhitespaceAfterNoneWhitespaceIdx = replaceCharAtPositionAndBackslashes(s, lastNoneWhitespaceIdx, "\f"); | ||
} else if (s[lastNoneWhitespaceIdx] == 'v') { | ||
firstWhitespaceAfterNoneWhitespaceIdx = replaceCharAtPositionAndBackslashes(s, lastNoneWhitespaceIdx, "\v"); | ||
} | ||
} | ||
if (firstWhitespaceAfterNoneWhitespaceIdx != std::string::npos && | ||
firstWhitespaceAfterNoneWhitespaceIdx < s.size()) { | ||
s = s.substr(0, firstWhitespaceAfterNoneWhitespaceIdx); //Right Trim the string | ||
} | ||
} else { | ||
s = ""; | ||
} | ||
} | ||
} | ||
|
||
} //namespace StringOps | ||
|
||
|
||
} //namespace JavaScriptOptions | ||
|
||
} //namespace SWIGVMContainers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider using std::transform
, std::replace
, std::replace_if
, ... for replacing substrings.
https://en.cppreference.com/w/cpp/algorithm/transform
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think, this doesn't apply here, because we replace either a single position or we replace a double backslasches with a single backslasches, by removing half of the backslasches
related to exasol/script-languages-release#990