From 00c7a023c7d4f8c322b81d211972932cc50af9de Mon Sep 17 00:00:00 2001 From: arjen Date: Sun, 3 Nov 2002 13:18:57 +0000 Subject: [PATCH] New functions - String::escape() and String::unescape() --- demos/acltest.cpp | 25 ++++++- src/String.h | 17 +++-- src/string.cpp | 217 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 244 insertions(+), 15 deletions(-) diff --git a/demos/acltest.cpp b/demos/acltest.cpp index 9cdaf04..46743a9 100644 --- a/demos/acltest.cpp +++ b/demos/acltest.cpp @@ -5,7 +5,7 @@ *********************** ** FILE NAME : acltest.cpp ** SYSTEM NAME : AXE - Andromeda X-windows Encapsulation -** VERSION NUMBER : $Revision: 1.4 $ +** VERSION NUMBER : $Revision: 1.5 $ ** ** DESCRIPTION : Test routine for non-X classes ** @@ -22,7 +22,10 @@ /***************************** $Log: acltest.cpp,v $ - Revision 1.4 2002-09-26 14:48:46 arjen + Revision 1.5 2002-11-03 13:19:33 arjen + New functions - String::escape() and String::unescape() + + Revision 1.4 2002/09/26 14:48:46 arjen Use the std: namespace for STL objects Revision 1.3 2002/09/02 06:18:12 arjen @@ -37,7 +40,7 @@ *****************************/ -static const char *RCSID = "$Id: acltest.cpp,v 1.4 2002-09-26 14:48:46 arjen Exp $"; +static const char *RCSID = "$Id: acltest.cpp,v 1.5 2002-11-03 13:19:33 arjen Exp $"; #include "String.h" #include "integer.h" @@ -105,6 +108,22 @@ int main() String num(3737); std::cout << num << "\n"; + std::cout << "***********************\nEscape Test\n*********************\n"; + + x = "\a\b\f\n\r\t\v\"'\\\xC0\x02"; + y = x.escape(); + std::cout << "Escaped sequence = " << y << "\n"; + if (y.unescape() == x) + { + std::cout << "Escape sequence converts back into original string.\n"; + } + else + { + std::cout << "Escape sequence does NOT convert back into original string.\n"; + x = y.unescape(); + std::cout << x.escape() << "\n"; + } + std::cout << "***********************\nInteger Test\n*********************\n"; integer a(2000000000), b(2048); integer c; diff --git a/src/String.h b/src/String.h index 4d80d60..2fa0127 100644 --- a/src/String.h +++ b/src/String.h @@ -5,7 +5,7 @@ *********************** ** FILE NAME : String.h ** SYSTEM NAME : Andromeda X-Windows Encapsulation -** VERSION NUMBER : $Revision: 1.2 $ +** VERSION NUMBER : $Revision: 1.3 $ ** ** DESCRIPTION : Character String class definition ** @@ -17,13 +17,16 @@ ******************************** ** ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl ** CREATION DATE : Nov 17, 1995 -** LAST UPDATE : Feb 23, 2001 +** LAST UPDATE : Nov 02, 2002 ** MODIFICATIONS : **************************************************************************/ /***************************** $Log: String.h,v $ - Revision 1.2 2002-09-28 06:45:51 arjen + Revision 1.3 2002-11-03 13:18:57 arjen + New functions - String::escape() and String::unescape() + + Revision 1.2 2002/09/28 06:45:51 arjen New feature: subtring selection by regular expression. Bugfix: use the std: namespace for STL classes istream and ostream @@ -32,7 +35,7 @@ *****************************/ -// static const char RCSID[] = "$Id: String.h,v 1.2 2002-09-28 06:45:51 arjen Exp $"; +// static const char RCSID[] = "$Id: String.h,v 1.3 2002-11-03 13:18:57 arjen Exp $"; #ifndef STRING_H #define STRING_H @@ -277,8 +280,10 @@ public: * Modifiers */ - String upper(void); - String lower(void); + String upper(); // Convert to upper case (ASCII) + String lower(); // Convert to lower case (ASCII) + String escape(); // Insert backslashes to escape special characters + String unescape(); // Remove backslashes from escape codes. /* * Character searching and Pattern matching diff --git a/src/string.cpp b/src/string.cpp index 6a23007..1bcded9 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -5,7 +5,7 @@ *********************** ** FILE NAME : string.cpp ** SYSTEM NAME : Andromeda X-Windows Encapsulation -** VERSION NUMBER : $Revision: 1.2 $ +** VERSION NUMBER : $Revision: 1.3 $ ** ** DESCRIPTION : String class implementation. ** @@ -17,13 +17,16 @@ ******************************** ** ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl ** CREATION DATE : Nov 17, 1997 -** LAST UPDATE : Aug 26, 1999 +** LAST UPDATE : Nov 03, 2002 ** MODIFICATIONS : **************************************************************************/ /***************************** $Log: string.cpp,v $ - Revision 1.2 2002-09-28 06:42:11 arjen + Revision 1.3 2002-11-03 13:18:57 arjen + New functions - String::escape() and String::unescape() + + Revision 1.2 2002/09/28 06:42:11 arjen A few small bug fixes. Revision 1.1 2002/07/25 08:01:27 arjen @@ -31,7 +34,7 @@ *****************************/ -static const char RCSID[] = "$Id: string.cpp,v 1.2 2002-09-28 06:42:11 arjen Exp $"; +static const char RCSID[] = "$Id: string.cpp,v 1.3 2002-11-03 13:18:57 arjen Exp $"; #include #include @@ -390,7 +393,7 @@ char& String::operator[](int i) return p->s[i]; } -String String::upper(void) +String String::upper() { String up; int i; @@ -406,7 +409,7 @@ String String::upper(void) return up; } -String String::lower(void) +String String::lower() { String low; int i; @@ -422,6 +425,208 @@ String String::lower(void) return low; } +String String::escape() +{ + const int BUFSIZE = 500; + + char buffer[BUFSIZE]; + String escaped = ""; + int i; // Index in buffer[] + int j; // Index in *this + + i = 0; + for (j = 0; p->s[j] != '\0'; j++) + { + if (i + 5 > BUFSIZE) + { + escaped += buffer; + i = 0; + } + + switch (p->s[j]) + { + case '\a': + buffer[i++] = '\\'; + buffer[i++] = 'a'; + break; + + case '\b': + buffer[i++] = '\\'; + buffer[i++] = 'b'; + break; + + case '\f': + buffer[i++] = '\\'; + buffer[i++] = 'f'; + break; + + case '\n': + buffer[i++] = '\\'; + buffer[i++] = 'n'; + break; + + case '\r': + buffer[i++] = '\\'; + buffer[i++] = 'r'; + break; + + case '\t': + buffer[i++] = '\\'; + buffer[i++] = 't'; + break; + + case '\v': + buffer[i++] = '\\'; + buffer[i++] = 'v'; + break; + + case '\'': + buffer[i++] = '\\'; + buffer[i++] = '\''; + break; + + case '"': + buffer[i++] = '\\'; + buffer[i++] = '"'; + break; + + case '\\': + buffer[i++] = '\\'; + buffer[i++] = '\\'; + break; + + default: + if (p->s[j] > '\x20' && p->s[j] < '\x7F') + { + buffer[i++] = p->s[j]; + } + else + { + short nibble; + + // Turn into hexadecimal representation + + buffer[i++] = '\\'; + buffer[i++] = 'x'; + nibble = (p->s[j] >> 4) & 0x0f; + buffer[i++] = nibble < 10 ? nibble + '0' : nibble - 10 + 'A'; + nibble = p->s[j] & 0x0f; + buffer[i++] = nibble < 10 ? nibble + '0' : nibble - 10 + 'A'; + } + break; + } + } + + buffer[i] = '\0'; + escaped += buffer; + + return escaped; +} + +String String::unescape() +{ + String unescaped; + char *s, *d; + + unescaped.p->s = new char[strlen(p->s)+1]; + s = p->s; + d = unescaped.p->s; + + while (*s != '\0') + { + if (*s == '\\') + { + s++; + + switch (*s) + { + case 'a': + *d = '\a'; + break; + + case 'b': + *d = '\b'; + break; + + case 'f': + *d = '\f'; + break; + + case 'n': + *d = '\n'; + break; + + case 'r': + *d = '\r'; + break; + + case 't': + *d = '\t'; + break; + + case 'v': + *d = '\v'; + break; + + case '\'': + *d = '\''; + break; + + case '"': + *d = '"'; + break; + + case '\\': + *d = '\\'; + break; + + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + *d = 0; + while (*s >= '0' && *s <= '7') + { + *d *= 8; + *d += *s - '0'; + s++; + } + s--; + break; + + case 'x': + *d = 0; + s++; // Skip the initial 'x' + while (isxdigit(*s)) + { + *d *= 16; + *d += *s > '9' ? toupper(*s) - 'A' + 10 : *s - '0'; + s++; + } + s--; + break; + + default: + *d = *s; + break; + } + } + else + { + *d = *s; + } + s++; + d++; + } + + return unescaped; +} + + // Find the first occurance of 'c' int String::index(char c) -- 2.11.0