Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Macros | Functions
Regexp.c File Reference
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "Hash.h"
#include "Regexp.h"

Go to the source code of this file.

Macros

#define _Class   _Regexp
 

Functions

Class_Regexp (void)
 
static Objectcopy (const Object *self)
 
static void dealloc (Object *self)
 
static Stringdescription (const Object *self)
 
static int hash (const Object *self)
 
static void initialize (Class *clazz)
 
static RegexpinitWithPattern (Regexp *self, const char *pattern, int options)
 
static _Bool isEqual (const Object *self, const Object *other)
 
static _Bool matchesCharacters (const Regexp *self, const char *chars, int options, Range **ranges)
 
static _Bool matchesString (const Regexp *self, const String *string, int options, Range **ranges)
 
Regexpre (const char *pattern, int options)
 

Macro Definition Documentation

◆ _Class

#define _Class   _Regexp

Definition at line 31 of file Regexp.c.

Function Documentation

◆ _Regexp()

Class * _Regexp ( void  )

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 * _Object(void)
The Object archetype.
Definition: Object.c:136
Extended POSIX regular expressions.
Definition: Regexp.h:43

◆ copy()

static Object * copy ( const Object self)
static
See also
Object::copy(const Object *)

Definition at line 38 of file Regexp.c.

38 {
39
40 const Regexp *this = (Regexp *) self;
41
42 return (Object *) re(this->pattern, this->options);
43}
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
OBJECTIVELY_EXPORT Regexp * re(const char *pattern, int options)
A convenience function for instantiating Regexps.
Definition: Regexp.c:219

◆ dealloc()

static void dealloc ( Object self)
static
See also
Object::dealloc(Object *)

Definition at line 58 of file Regexp.c.

58 {
59
60 Regexp *this = (Regexp *) self;
61
62 regfree(this->regex);
63 free(this->regex);
64
65 super(Object, self, dealloc);
66}
#define super(type, obj, method,...)
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50

◆ description()

static String * description ( const Object self)
static
See also
Object::description(const Object *)

Definition at line 48 of file Regexp.c.

48 {
49
50 const Regexp *this = (Regexp *) self;
51
52 return $$(String, stringWithCharacters, this->pattern);
53}
Immutable UTF-8 strings.
Definition: String.h:69
String * stringWithCharacters(const char *chars)
Returns a new String by copying chars.
Definition: String.c:487

◆ hash()

static int hash ( const Object self)
static
See also
Object::hash(const Object *)

Definition at line 71 of file Regexp.c.

71 {
72
73 Regexp *this = (Regexp *) self;
74
75 int hash = HASH_SEED;
76 hash = HashForInteger(hash, this->options);
77
78 const Range range = { 0, strlen(this->pattern) };
79 hash = HashForBytes(hash, (uint8_t *) this->pattern, range);
80
81 return hash;
82}
int HashForInteger(int hash, const long integer)
Accumulates the hash value of integer into hash.
Definition: Hash.c:62
int HashForBytes(int hash, const uint8_t *bytes, const Range range)
Accumulates the hash value of bytes into hash.
Definition: Hash.c:28
#define HASH_SEED
The hash seed value.
Definition: Hash.h:37
int hash(const Object *self)
Definition: Array.c:80
A location and length into contiguous collections.
Definition: Types.h:54

◆ initialize()

static void initialize ( Class clazz)
static
See also
Class::initialize(Class *)

Definition at line 182 of file Regexp.c.

182 {
183
184 ((ObjectInterface *) clazz->interface)->copy = copy;
185 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
186 ((ObjectInterface *) clazz->interface)->description = description;
187 ((ObjectInterface *) clazz->interface)->hash = hash;
188 ((ObjectInterface *) clazz->interface)->isEqual = isEqual;
189
190 ((RegexpInterface *) clazz->interface)->initWithPattern = initWithPattern;
191 ((RegexpInterface *) clazz->interface)->matchesCharacters = matchesCharacters;
192 ((RegexpInterface *) clazz->interface)->matchesString = matchesString;
193}
ident interface
The interface of the Class.
Definition: Class.h:105
Object * copy(const Object *self)
Creates a shallow copy of this Object.
Definition: Array.c:40
String * description(const Object *self)
Definition: Array.c:66
_Bool isEqual(const Object *self, const Object *other)
Tests equality of the other Object.
Definition: Array.c:96
_Bool matchesCharacters(const Regexp *self, const char *chars, int options, Range **matches)
Matches this regular expression against the given characters.
Definition: Regexp.c:134
Regexp * initWithPattern(Regexp *self, const char *pattern, int options)
Initializes this regular expression.
Definition: Regexp.c:112
_Bool matchesString(const Regexp *self, const String *string, int options, Range **matches)
Matches this regular expression against the given String.
Definition: Regexp.c:170

◆ initWithPattern()

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

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}
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
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

◆ isEqual()

static _Bool isEqual ( const Object self,
const Object other 
)
static
See also
Object::isEqual(const Object *, const Object *)

Definition at line 87 of file Regexp.c.

87 {
88
89 if (super(Object, self, isEqual, other)) {
90 return true;
91 }
92
93 if (other && $(other, isKindOfClass, _Regexp())) {
94
95 const Regexp *this = (Regexp *) self;
96 const Regexp *that = (Regexp *) other;
97
98 if (this->options == that->options) {
99 return strcmp(this->pattern, that->pattern) == 0;
100 }
101 }
102
103 return false;
104}
_Bool isKindOfClass(const Object *self, const Class *clazz)
Tests for Class hierarchy membership.
Definition: Object.c:101
Class * _Regexp(void)
The Regexp archetype.
Definition: Regexp.c:199

◆ matchesCharacters()

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

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}
ssize_t location
The location.
Definition: Types.h:59

◆ matchesString()

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

Definition at line 170 of file Regexp.c.

170 {
171
172 assert(string);
173
174 return $(self, matchesCharacters, string->chars, options, ranges);
175}
MutableString * string(void)
Returns a new MutableString.
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition: String.h:85

◆ re()

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

Definition at line 219 of file Regexp.c.

219 {
220 return $(alloc(Regexp), initWithPattern, pattern, options);
221}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159