From 5cf194306608877a3e24e44eeff59bf4c5150c77 Mon Sep 17 00:00:00 2001 From: Arjen Baart Date: Sat, 12 Sep 2020 10:32:38 +0200 Subject: [PATCH] String: Implement String != regex operator --- TODO | 1 - src/String.h | 5 ++++- src/regex.cpp | 11 +++++++++++ test/string_regex.cpp | 3 +++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/TODO b/TODO index e3ebf5a..1b87c40 100644 --- 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 diff --git a/src/String.h b/src/String.h index bd8a354..d83c0a0 100644 --- a/src/String.h +++ b/src/String.h @@ -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 diff --git a/src/regex.cpp b/src/regex.cpp index 3f7a675..397f3c0 100644 --- a/src/regex.cpp +++ b/src/regex.cpp @@ -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; diff --git a/test/string_regex.cpp b/test/string_regex.cpp index 4723ad1..1f5e57d 100644 --- a/test/string_regex.cpp +++ b/test/string_regex.cpp @@ -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; -- 2.20.1