Skip to content
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

Conversation

tomuben
Copy link
Collaborator

@tomuben tomuben commented Oct 25, 2024


void replaceTrailingEscapeWhitespaces(std::string & s) {
if (s.size() > 0) {
const size_t lastIdx = s.find_last_not_of(" \t\v\f");
Copy link
Collaborator

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;
Copy link
Collaborator

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);
Copy link
Collaborator

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 );
Copy link
Collaborator

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) {
Copy link
Collaborator

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

@tomuben tomuben force-pushed the refactoring/990_implement_escape_sequence_for_jar_option_with_trailing_whitespaces branch from 0020462 to 506db77 Compare October 28, 2024 14:37
Comment on lines 14 to 16
if (value != "") {
m_jvmOptions.push_back("-Dexasol.scriptclass=" + trimmedValue);
m_jvmOptions.push_back("-Dexasol.scriptclass=" + value);
}
Copy link
Member

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);

Copy link
Member

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.

Copy link
Collaborator

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

Comment on lines 11 to 13
uint32_t retVal(0);
while (pos >= 0 && s[pos--] == '\\') retVal++;
return retVal;
Copy link
Member

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.

Suggested change
uint32_t retVal(0);
while (pos >= 0 && s[pos--] == '\\') retVal++;
return retVal;
// including <algorithm> is required
return std::count (s.cbegin(), s.cend(), '\\');

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like

Copy link
Collaborator

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

Comment on lines 16 to 77
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
Copy link
Member

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

Copy link
Collaborator

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

@tomuben tomuben merged commit cf43d08 into master Oct 29, 2024
24 checks passed
@tomuben tomuben deleted the refactoring/990_implement_escape_sequence_for_jar_option_with_trailing_whitespaces branch October 29, 2024 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants