From 0df94a5457ed8cb150e587946c6131fedd722374 Mon Sep 17 00:00:00 2001 From: arjen Date: Sat, 28 Sep 2002 06:45:51 +0000 Subject: [PATCH] New feature: subtring selection by regular expression. Bugfix: use the std: namespace for STL classes istream and ostream --- src/String.h | 32 ++++++++++++++++++++++++++------ src/regex.cpp | 29 ++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/String.h b/src/String.h index a5f9bf7..4d80d60 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.1 $ +** VERSION NUMBER : $Revision: 1.2 $ ** ** DESCRIPTION : Character String class definition ** @@ -23,18 +23,22 @@ /***************************** $Log: String.h,v $ - Revision 1.1 2002-07-25 08:01:26 arjen + 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 + + Revision 1.1 2002/07/25 08:01:26 arjen First checkin, AXE release 0.2 *****************************/ -// static const char RCSID[] = "$Id: String.h,v 1.1 2002-07-25 08:01:26 arjen Exp $"; +// static const char RCSID[] = "$Id: String.h,v 1.2 2002-09-28 06:45:51 arjen Exp $"; #ifndef STRING_H #define STRING_H #include -#include +#include #include #include @@ -180,16 +184,30 @@ public: /* * Substring selection + * + * A substring can be selected by a start index and a length or + * by matching a regular expression. + * + * Selecting a substring by regular expression, combined with + * the lvalue semantics of the substring class is a particularly + * powerful concept. Possible uses are for example: + * + * (assuming String S, M; regex R;) + * M = S(R); -> Returns matching part of S into M. + * S(R) = "replacement"; -> replace matching part of S. + * S(R) = ""; -> Removes matching part from S. + * S(R) == S; -> true if and only if all of S matches R exactly. */ substring operator()(int start, int len); + substring operator()(const regex &r); /* * Input and output */ - friend ostream& operator<<(ostream &, const String &); - friend istream& operator>>(istream &, String &); + friend std::ostream& operator<<(std::ostream &, const String &); + friend std::istream& operator>>(std::istream &, String &); /* * String comparison tests @@ -333,6 +351,8 @@ public: class regex { + friend class String; + regex_t expression; public: diff --git a/src/regex.cpp b/src/regex.cpp index e3b4311..025ff40 100644 --- a/src/regex.cpp +++ b/src/regex.cpp @@ -5,7 +5,7 @@ *********************** ** FILE NAME : regex.cpp ** SYSTEM NAME : Andromeda X-Windows Encapsulation -** VERSION NUMBER : $Revision: 1.1 $ +** VERSION NUMBER : $Revision: 1.2 $ ** ** DESCRIPTION : regex class implementation. ** @@ -23,12 +23,16 @@ /***************************** $Log: regex.cpp,v $ - Revision 1.1 2002-07-25 08:01:27 arjen + 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 + + Revision 1.1 2002/07/25 08:01:27 arjen First checkin, AXE release 0.2 *****************************/ -static const char RCSID[] = "$Id: regex.cpp,v 1.1 2002-07-25 08:01:27 arjen Exp $"; +static const char RCSID[] = "$Id: regex.cpp,v 1.2 2002-09-28 06:45:51 arjen Exp $"; #include #include @@ -60,3 +64,22 @@ 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; + regmatch_t match; + + sub.str = this; + sub.start = 0; + sub.len = 0; + + if (regexec(&r.expression, p->s, 1, &match, 0) == 0) + { + sub.start = match.rm_so; + sub.len = match.rm_eo - match.rm_so; + } + + return sub; +} + -- 2.11.0