String: Implement String != regex operator
authorArjen Baart <arjen@andromeda.nl>
Sat, 12 Sep 2020 08:32:38 +0000 (10:32 +0200)
committerArjen Baart <arjen@andromeda.nl>
Sat, 12 Sep 2020 08:32:38 +0000 (10:32 +0200)
TODO
src/String.h
src/regex.cpp
test/string_regex.cpp

diff --git a/TODO b/TODO
index e3ebf5a..1b87c40 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,7 +1,6 @@
 Things to do:
 =============
 
-- String: Implement String != regex operator
 - String: index and rindex methods with String argument
 - String: format operator % like in python
 - String: assign fstream objects to read and write a file
index bd8a354..d83c0a0 100644 (file)
@@ -312,7 +312,8 @@ public:
  
    friend bool operator == (const String &s, const regex &r);
    friend bool operator == (const regex &r, const String &s);
-   // TODO: The != operator
+   friend bool operator != (const String &s, const regex &r);
+   friend bool operator != (const regex &r, const String &s);
 
    SuperString split(const String &separator);
 
@@ -390,6 +391,8 @@ public:
 
    friend bool operator == (const String &s, const regex &r);
    friend bool operator == (const regex &r, const String &s);
+   friend bool operator != (const String &s, const regex &r);
+   friend bool operator != (const regex &r, const String &s);
 };
 
 class SuperString
index 3f7a675..397f3c0 100644 (file)
@@ -96,11 +96,22 @@ bool operator == (const String &s, const regex &r)
 {
    return regexec(&r.expression, s.p->s, 0, 0, 0) == 0;
 }
+
 bool operator == (const regex &r, const String &s)
 {
    return regexec(&r.expression, s.p->s, 0, 0, 0) == 0;
 }
 
+bool operator != (const String &s, const regex &r)
+{
+   return regexec(&r.expression, s.p->s, 0, 0, 0) != 0;
+}
+
+bool operator != (const regex &r, const String &s)
+{
+   return regexec(&r.expression, s.p->s, 0, 0, 0) != 0;
+}
+
 substring String::operator()(const regex &r)
 {
    substring sub;
index 4723ad1..1f5e57d 100644 (file)
@@ -24,6 +24,9 @@ int main()
    assert((s1 == nr) == false);
    assert((s2 == nr) == true);
 
+   assert((s1 != nr) == true);
+   assert((s2 != nr) == false);
+
    // Select a substring with a regex
 
    String match;