Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Properties | Methods | Protected Attributes | Related Functions
Regexp Struct Reference

#include <Regexp.h>

Overview

Extended POSIX regular expressions.

Definition at line 43 of file Regexp.h.

Inheritance diagram for Regexp:
Object

Properties

size_t numberOfSubExpressions
 The number of parenthesized sub-expressions. More...
 
Object object
 The superclass. More...
 
int options
 A bitwise-or of REG_ICASE, REG_NEWLINE. More...
 
const char * pattern
 The pattern. More...
 
ident regex
 The backing regular expression. More...
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class. More...
 

Methods

Class_Regexp (void)
 The Regexp archetype. More...
 
RegexpinitWithPattern (Regexp *self, const char *pattern, int options)
 Initializes this regular expression. More...
 
_Bool matchesCharacters (const Regexp *self, const char *chars, int options, Range **matches)
 Matches this regular expression against the given characters. More...
 
_Bool matchesString (const Regexp *self, const String *string, int options, Range **matches)
 Matches this regular expression against the given String. More...
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype. More...
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object. More...
 
void dealloc (Object *self)
 Frees all resources held by this Object. More...
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object. More...
 
_Bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object. More...
 
_Bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership. More...
 

Protected Attributes

RegexpInterface * interface
 The interface. More...
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface. More...
 

Related Functions

OBJECTIVELY_EXPORT Regexpre (const char *pattern, int options)
 A convenience function for instantiating Regexps. More...
 

Property Details

◆ interface

RegexpInterface* Regexp::interface
protected

The interface.

Definition at line 54 of file Regexp.h.

◆ numberOfSubExpressions

size_t Regexp::numberOfSubExpressions

The number of parenthesized sub-expressions.

Definition at line 69 of file Regexp.h.

◆ object

Object Regexp::object

The superclass.

Definition at line 48 of file Regexp.h.

◆ options

int Regexp::options

A bitwise-or of REG_ICASE, REG_NEWLINE.

Definition at line 64 of file Regexp.h.

◆ pattern

const char* Regexp::pattern

The pattern.

Definition at line 59 of file Regexp.h.

◆ regex

ident Regexp::regex

The backing regular expression.

Definition at line 74 of file Regexp.h.

Method Details

◆ _Regexp()

Class * _Regexp ( void  )

The Regexp archetype.

Returns
The Regexp Class.

Definition at line 199 of file Regexp.c.

199 {
200 static Class *clazz;
201 static Once once;
202
203 do_once(&once, {
204 clazz = _initialize(&(const ClassDef) {
205 .name = "Regexp",
206 .superclass = _Object(),
207 .instanceSize = sizeof(Regexp),
208 .interfaceOffset = offsetof(Regexp, interface),
209 .interfaceSize = sizeof(RegexpInterface),
211 });
212 });
213
214 return clazz;
215}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: Regexp.c:182
long Once
The Once type.
Definition: Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition: Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition: Class.h:41
The runtime representation of a Class.
Definition: Class.h:95
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
Extended POSIX regular expressions.
Definition: Regexp.h:43
RegexpInterface * interface
The interface.
Definition: Regexp.h:54

◆ initWithPattern()

Regexp * initWithPattern ( Regexp self,
const char *  pattern,
int  options 
)

Initializes this regular expression.

Parameters
selfThe Regexp.
patternThe POSIX regular expression pattern.
optionsA bitwise-or of REG_ICASE, REG_NEWLINE.
Returns
The initialized regular expression, or NULL on error.

Definition at line 112 of file Regexp.c.

112 {
113
114 self = (Regexp *) super(Object, self, init);
115 if (self) {
116 self->regex = calloc(1, sizeof(regex_t));
117 assert(self->regex);
118
119 const int err = regcomp(self->regex, pattern, REG_EXTENDED | options);
120 assert(err == 0);
121
122 self->pattern = pattern;
123 self->options = options;
124 self->numberOfSubExpressions = ((regex_t *) self->regex)->re_nsub;
125 }
126
127 return self;
128}
#define super(type, obj, method,...)
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Object * init(Object *self)
Initializes this Object.
Definition: Object.c:83
ident regex
The backing regular expression.
Definition: Regexp.h:74
const char * pattern
The pattern.
Definition: Regexp.h:59
int options
A bitwise-or of REG_ICASE, REG_NEWLINE.
Definition: Regexp.h:64
size_t numberOfSubExpressions
The number of parenthesized sub-expressions.
Definition: Regexp.h:69

◆ matchesCharacters()

_Bool matchesCharacters ( const Regexp self,
const char *  chars,
int  options,
Range **  matches 
)

Matches this regular expression against the given characters.

Parameters
selfThe Regexp.
charsThe characters to match.
optionsA bitwise-or of REG_NOTBOL, REG_NOTEOL.
matchesAn optional pointer to return matched sub-expressions.
Returns
true if this Regexp matched chars, false otherwise.
Remarks
If provided, matches will be dynamically allocated and contain numberOfSubExpressions + 1 Ranges. matches[0] will identify the Range of chars that matched the entire pattern. matches[1..n] will identify the Range of each corresponding sub-expression. The caller must free matches when done with it.
See also
regexec(3)

Definition at line 134 of file Regexp.c.

134 {
135
136 if (ranges) {
137 const size_t numberOfMatches = self->numberOfSubExpressions + 1;
138 regmatch_t matches[numberOfMatches];
139
140 const int err = regexec(self->regex, chars, numberOfMatches, matches, options);
141 assert(err == 0 || err == REG_NOMATCH);
142
143 *ranges = calloc(numberOfMatches, sizeof(Range));
144 assert(*ranges);
145
146 Range *range = *ranges;
147 const regmatch_t *match = matches;
148 for (size_t i = 0; i < numberOfMatches; i++, range++, match++) {
149 range->location = match->rm_so;
150 if (range->location > -1) {
151 range->length = match->rm_eo - match->rm_so;
152 } else {
153 range->length = 0;
154 }
155 }
156
157 return err == 0;
158 }
159
160 const int err = regexec(self->regex, chars, 0, NULL, options);
161 assert(err == 0 || err == REG_NOMATCH);
162
163 return err == 0;
164}
A location and length into contiguous collections.
Definition: Types.h:54
ssize_t location
The location.
Definition: Types.h:59

◆ matchesString()

_Bool matchesString ( const Regexp self,
const String string,
int  options,
Range **  matches 
)

Matches this regular expression against the given String.

Parameters
selfThe Regexp.
stringThe String to match.
optionsA bitwise-or of REG_NOTBOL, REG_NOTEOL.
matchesAn optional pointer to return matched sub-expressions.
Returns
true if this expression matches string, false otherwise.
Remarks
If provided, matches will be dynamically allocated and contain numberOfSubExpressions + 1 Ranges. matches[0] will identify the Range of chars that matched the entire pattern. matches[1..n] will identify the Range of each corresponding sub-expression. The caller must free matches when done with it.

Definition at line 170 of file Regexp.c.

170 {
171
172 assert(string);
173
174 return $(self, matchesCharacters, string->chars, options, ranges);
175}
static MutableString * string(void)
_Bool matchesCharacters(const Regexp *self, const char *chars, int options, Range **matches)
Matches this regular expression against the given characters.
Definition: Regexp.c:134
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition: String.h:85

Related

◆ re()

OBJECTIVELY_EXPORT Regexp * re ( const char *  pattern,
int  options 
)
related

A convenience function for instantiating Regexps.

Parameters
patternThe POSIX regular expression pattern.
optionsA bitwise-or of REG_ICASE, REG_NEWLINE.
Returns
A new Regexp, or NULL on error.

Definition at line 219 of file Regexp.c.

219 {
221}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
Regexp * initWithPattern(Regexp *self, const char *pattern, int options)
Initializes this regular expression.
Definition: Regexp.c:112

The documentation for this struct was generated from the following files: