Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add CString::StripControls to strip controls (Colors, C0) from strings

  • Loading branch information...
commit 10a982b6d3a5ca86095687287effe0854a0de11c 1 parent 40fa544
@ravomavain ravomavain authored
Showing with 65 additions and 0 deletions.
  1. +13 −0 include/znc/ZNCString.h
  2. +52 −0 src/ZNCString.cpp
View
13 include/znc/ZNCString.h
@@ -457,6 +457,19 @@ class CString : public std::string {
* @return The result of the conversion.
*/
CString RightChomp_n(size_type uLen = 1) const;
+ /** Remove controls characters from this string.
+ * Controls characters are color codes, and those in C0 set
+ * See https://en.wikipedia.org/wiki/C0_and_C1_control_codes
+ * @return The result of the conversion.
+ */
+ CString& StripControls();
+ /** Remove controls characters from this string.
+ * Controls characters are color codes, and those in C0 set
+ * See https://en.wikipedia.org/wiki/C0_and_C1_control_codes
+ * This string object isn't modified.
+ * @return The result of the conversion.
+ */
+ CString StripControls_n() const;
private:
protected:
View
52 src/ZNCString.cpp
@@ -1131,6 +1131,58 @@ bool CString::RightChomp(size_type uLen) {
return bRet;
}
+CString CString::StripControls_n() const {
+ CString sRet;
+ const unsigned char *pStart = (const unsigned char*) data();
+ unsigned char ch = *pStart;
+ size_type iLength = length();
+ sRet.reserve(iLength);
+ bool colorCode = false;
+ unsigned int digits = 0;
+ bool comma = false;
+
+ for (unsigned int a = 0; a < iLength; a++, ch = pStart[a]) {
+ // Color code. Format: \x03([0-9]{1,2}(,[0-9]{1,2})?)?
+ if (ch == 0x03) {
+ colorCode = true;
+ digits = 0;
+ comma = false;
+ continue;
+ }
+ if (colorCode) {
+ if (isdigit(ch) && digits < 2) {
+ digits++;
+ continue;
+ }
+ if (ch == ',' && !comma && digits > 0) {
+ comma = true;
+ digits = 0;
+ continue;
+ }
+
+ colorCode = false;
+
+ if (digits == 0 && comma) { // There was a ',' which wasn't followed by digits, we should print it.
+ sRet += ',';
+ }
+ }
+ // CO controls codes
+ if (ch < 0x20 || ch == 0x7F)
+ continue;
+ sRet += ch;
+ }
+ if (colorCode && digits == 0 && comma) {
+ sRet += ',';
+ }
+
+ sRet.reserve(0);
+ return sRet;
+}
+
+CString& CString::StripControls() {
+ return (*this = StripControls_n());
+}
+
//////////////// MCString ////////////////
const MCString MCString::EmptyMap;
Please sign in to comment.
Something went wrong with that request. Please try again.