-
Notifications
You must be signed in to change notification settings - Fork 0
/
Regex.cpp
54 lines (40 loc) · 1.55 KB
/
Regex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <regex>
#include <string>
#include "cppallocator.h"
#include "gc.h"
#include "Regex.h"
#include "common.m"
extern "C" {
i8* _cpputils_Regex$compile(string* rstr) {
try {
std::regex *res = (std::regex*) _allocate(sizeof(std::regex));
new (res) std::regex(rstr->base);
return (i8*) res;
} catch (std::regex_error &e) {
return nullptr;
}
}
i1 _cpputils_Regex$matches(i8 *r, string* s) {
return std::regex_match(s->base, * (std::regex*) r);
}
string* _cpputils_Regex$first_match(i8* r, string* s) {
if (!std::regex_search(s->base, * (std::regex*) r))
return NULL;
std::string match = std::cregex_iterator(s->base, s->base + s->size, * (std::regex*) r)->str();
string* res = _allocate_string(match.length());
memcpy(res->base, match.c_str(), res->size);
return res;
}
stringarr* _cpputils_Regex$all_matches(i8* r, string* s) {
std::cregex_iterator it = std::cregex_iterator(s->base, s->base + s->size, * (std::regex*) r);
stringarr* res = (stringarr*) _allocate_blindarr(std::distance(it, std::cregex_iterator()), sizeof(string*));
for (i64 i = 0; i < res->size; i++, ++it) {
string* match = _allocate_string(it->str().length());
memcpy(match->base, it->str().c_str(), match->size);
res->base[i] = match;
_addchild((i8*) res, (i8*) res->base[i]);
_removeref((i8*) match);
}
return res;
}
}