- String: format operator % like in python
- String: assign fstream objects to read and write a file
- String: convert to and from std::sstream and std::string
-- regex: throw an exception when contructing a regex returns an error
- date: test parser for invalid dates and syntax errors
- date: Parser for stream input
friend bool operator == (const regex &r, const String &s);
};
-class StringException
-{
- String message;
-
-public:
-
- StringException(String m)
- {
- message = m;
- }
-
- String what()
- {
- return message;
- }
-};
-
class SuperString
{
std::vector<String> _ss;
String join(const String &separator);
};
+/*
+ * Exception classes
+ */
+
+
+class StringException
+{
+ String message;
+
+public:
+
+ StringException(String m)
+ {
+ message = m;
+ }
+
+ String what()
+ {
+ return message;
+ }
+};
+
+#define ERROR_MSG_SIZE 200
+
+class RegexException
+{
+ String message;
+
+public:
+
+ RegexException(int errcode, regex_t *preg)
+ {
+ char errbuf[ERROR_MSG_SIZE];
+
+ regerror(errcode, preg, errbuf, ERROR_MSG_SIZE);
+ message = errbuf;
+ }
+
+ String what()
+ {
+ return message;
+ }
+};
+
#endif /* STRING_H */
#include <ctype.h>
#include "String.h"
+
// Constructors and destructors for the regex class
regex::regex(const String ®)
original = reg;
int error = regcomp (&expression, reg.p->s, REG_EXTENDED);
- //TODO: Handle error
+ if (error != 0)
+ {
+ throw RegexException(error, &expression);
+ }
}
regex::regex(const char *reg)
original = reg;
int error = regcomp (&expression, reg, REG_EXTENDED);
- //TODO: Handle error
+ if (error != 0)
+ {
+ throw RegexException(error, &expression);
+ }
}
regex::regex(const regex & reg)
original = reg.original;
int error = regcomp (&expression, reg.original, REG_EXTENDED);
- //TODO: Handle error
+ if (error != 0)
+ {
+ throw RegexException(error, &expression);
+ }
}
regex::~regex()
original = x.original;
int error = regcomp (&expression, x.original, REG_EXTENDED);
+ if (error != 0)
+ {
+ throw RegexException(error, &expression);
+ }
return *this;
}
assert(c == 'd');
// Out of bounds
+ bool exception_caught = false;
+
try
{
c = s1[7];
catch (StringException se)
{
std::cout << "String exception: " << se.what() << "\n";
+ exception_caught = true;
}
- //assert(c == 'a');
+ assert(exception_caught);
s1[2] = 'Z';
std::cout << "After \"abcde\"[2] = 'Z' : " << s1 << "\n";
assert(s1 == "abZde");
+ exception_caught = false;
try
{
s1[5] = 'X';
catch (StringException se)
{
std::cout << "String exception: " << se.what() << "\n";
+ exception_caught = true;
}
std::cout << "After \"abcde\"[5] = 'X' : " << s1 << "\n";
assert(s1 == "abZde");
+ assert(exception_caught);
return 0;
}
int main()
{
- // TODO: test regex coinstructors
+ // TODO: test regex constructors
// A simple regular expression
regex nr("[0-9]+");
std::cout << "The matching part of \"" << s2 << "\" is \"" << match << "\"\n";
assert(match == "123");
+ // Test a regex with an error
+
+ bool exception_caught = false;
+
+ try
+ {
+ regex wrong_nr("[0-9+");
+ std::cout << "No regex error detected.\n";
+ }
+ catch (RegexException rexc)
+ {
+ std::cout << "Regex exception: " << rexc.what() << "\n";
+ exception_caught = true;
+ }
+
+ assert(exception_caught);
+
return 0;
}
Regular expression matching.
The matching part of "abd123def" is "123"
+Regex exception: Unmatched [ or [^
PASS string_regex (exit status: 0)