Skip to content

Commit

Permalink
Added string encoding method for html/xml/json stringsy
Browse files Browse the repository at this point in the history
  • Loading branch information
grodansparadis committed Mar 4, 2024
1 parent 07f2615 commit 777fc2a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/vscp/common/vscphelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,34 @@ vscp_parseISOCombined(struct tm *ptm, std::string &dt)
return true;
}

///////////////////////////////////////////////////////////////////////////////
// vscp_safe_encode_str
//
//

std::string
vscp_safe_encode_str(const std::string &str)
{
std::string retstr = "";

for (auto it=str.cbegin(); it!=str.cend(); ++it) {
if ('\\' == *it) {
retstr += "\\";
}
else if ('\"' == *it) {
retstr += "\"";
}
else if (*it < 32) {
retstr += vscp_str_format("\\x%x", *it);
}
else {
retstr += *it;
}
}

return retstr;
}

///////////////////////////////////////////////////////////////////////////////
// vscp_toXMLEscape
//
Expand Down
13 changes: 13 additions & 0 deletions src/vscp/common/vscphelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,19 @@ vscp_str_after(const std::string &str, char c)
return vscp_str_right(str, pos);
}

/*!
Saifly encode a standard string to be encoded in
HTML, XML or JSON
" (double quote)
\ (backslash)
all control characters like \n, \t
@param str Standard C++ string to encode
@return Encoded string.
*/
std::string
vscp_safe_encode_str(const std::string &str);

/*!
Check if a string is a number
@param strNumber String to check
Expand Down

0 comments on commit 777fc2a

Please sign in to comment.