Skip to content

Commit

Permalink
dont replace <sub>, </sub>, <sup> and </sup> in escapeHtmlStuff (#7)
Browse files Browse the repository at this point in the history
* dont replace `<sub>`, `</sub>`, `<sup>` and `</sup>` in escapeHtmlStuff

* keep <sub> et al intact for jasp-stats/jasp-issues#129
  • Loading branch information
JorisGoosen authored Jun 20, 2023
1 parent c63c020 commit 8342595
Showing 1 changed file with 79 additions and 76 deletions.
155 changes: 79 additions & 76 deletions stringutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,72 +19,72 @@ typedef std::vector<stringvec> stringvecvec;
class stringUtils
{
public:
inline static std::string stripRComments(const std::string & rCode)
{
std::stringstream out;

//Fixes https://github.com/jasp-stats/INTERNAL-jasp/issues/72
//Gotta do some rudimentary parsing here... A comment starts with # and ends with newline, but if a # is inside a string then it doesn't start a comment...
//String are started with ' or "

enum class status { R, Comment, SingleStr, DoubleStr };

status curStatus = status::R;

for(size_t r=0; r<rCode.size(); r++)
{
bool pushMe = true;

char kar = rCode[r];

switch(curStatus)
{
case status::R:
switch(kar)
{
case '\'': curStatus = status::SingleStr; break;
case '"': curStatus = status::DoubleStr; break;
case '#':
curStatus = status::Comment;
pushMe = false;
break;
}
break;

case status::Comment:
if(kar == '\n') curStatus = status::R;
else pushMe = false;
break;

case status::SingleStr:
if(kar == '\'' && rCode[r - 1] != '\\')
curStatus = status::R;
break;

case status::DoubleStr:
if(kar == '"' && rCode[r - 1] != '\\')
curStatus = status::R;
break;
}

if(pushMe)
out << kar;
}

return out.str();
}

inline static std::vector<std::string> splitString(const std::string & str, const char sep = ',')
{
std::vector<std::string> vecString;
std::string item;
std::stringstream stringStream(str);

while (std::getline(stringStream, item, sep))
vecString.push_back(item);

return vecString;
}
inline static std::string stripRComments(const std::string & rCode)
{
std::stringstream out;

//Fixes https://github.com/jasp-stats/INTERNAL-jasp/issues/72
//Gotta do some rudimentary parsing here... A comment starts with # and ends with newline, but if a # is inside a string then it doesn't start a comment...
//String are started with ' or "

enum class status { R, Comment, SingleStr, DoubleStr };

status curStatus = status::R;

for(size_t r=0; r<rCode.size(); r++)
{
bool pushMe = true;

char kar = rCode[r];

switch(curStatus)
{
case status::R:
switch(kar)
{
case '\'': curStatus = status::SingleStr; break;
case '"': curStatus = status::DoubleStr; break;
case '#':
curStatus = status::Comment;
pushMe = false;
break;
}
break;

case status::Comment:
if(kar == '\n') curStatus = status::R;
else pushMe = false;
break;

case status::SingleStr:
if(kar == '\'' && rCode[r - 1] != '\\')
curStatus = status::R;
break;

case status::DoubleStr:
if(kar == '"' && rCode[r - 1] != '\\')
curStatus = status::R;
break;
}

if(pushMe)
out << kar;
}

return out.str();
}

inline static std::vector<std::string> splitString(const std::string & str, const char sep = ',')
{
std::vector<std::string> vecString;
std::string item;
std::stringstream stringStream(str);

while (std::getline(stringStream, item, sep))
vecString.push_back(item);

return vecString;
}

inline static std::string toLower(std::string input)
{
Expand All @@ -94,23 +94,26 @@ class stringUtils

inline static std::string replaceBy(std::string input, const std::string & replaceThis, const std::string & withThis)
{
size_t oldLen = replaceThis.size(),
newLen = withThis.size();
size_t oldLen = replaceThis.size();

for(std::string::size_type curPos = input.find_first_of(replaceThis); curPos + oldLen < input.size() && curPos != std::string::npos; curPos = input.find_first_of(replaceThis, curPos))
{
for ( std::string::size_type curPos = input.find(replaceThis)
; curPos + oldLen <= input.size() && curPos != std::string::npos
; curPos = input.find(replaceThis)
)
input.replace(curPos, oldLen, withThis);
curPos += newLen;
}

return input;
}

inline static std::string escapeHtmlStuff(std::string input)
{
input = replaceBy(input, "&", "&amp;");
input = replaceBy(input, "<", "&lt;");
input = replaceBy(input, ">", "&gt;");
input = replaceBy(input, "&", "&amp;" );
input = replaceBy(input, "<", "&lt;" );
input = replaceBy(input, ">", "&gt;" );
input = replaceBy(input, "&lt;sub&gt;", "<sub>" );
input = replaceBy(input, "&lt;/sub&gt;", "</sub>");
input = replaceBy(input, "&lt;sup&gt;", "<sup>" );
input = replaceBy(input, "&lt;/sup&gt;", "</sup>");

return input;
}
Expand Down Expand Up @@ -196,7 +199,7 @@ class stringUtils
}

private:
stringUtils();
stringUtils();
};

#endif // STRINGUTILS_H

0 comments on commit 8342595

Please sign in to comment.