Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 687eef0

Browse files
modifié : README.md
modifié : src-zig/buildtestreg.zig modifié : src-zig/testreg.zig
1 parent 04f0596 commit 687eef0

File tree

3 files changed

+73
-120
lines changed

3 files changed

+73
-120
lines changed

‎README.md‎

Lines changed: 28 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,141 +11,112 @@ example:
1111

1212
```
1313
const std = @import("std");
14-
const re = @cImport({
15-
@cDefine("PCRE2_CODE_UNIT_WIDTH", "8");
16-
@cInclude("regPcre2.h");
17-
});
18-
19-
pub fn isMatch(strVal : [] const u8, regVal : [] const u8 ) bool {
20-
const allocator = std.heap.page_allocator;
21-
var slice = allocator.alignedAlloc(u8, @sizeOf(usize),@sizeOf(usize)) catch unreachable;
22-
defer allocator.free(slice);
23-
24-
const regex: *re.regex_t = @ptrCast(slice );
25-
defer re.pcre2_regfree(regex); // IMPORTANT!!
26-
27-
const creg: []u8 = allocator.alloc(u8, regVal.len , ) catch unreachable;
28-
defer allocator.free(creg);
29-
30-
std.mem.copy(u8, creg, regVal);
31-
32-
if (re.pcre2_regcomp(regex,@ptrCast(creg),re.REG_EXTENDED | re.REG_ICASE) != 0) {
33-
// TODO: the pattern is invalid
34-
// display for test
35-
// std.debug.print("error patern {s}\n", .{regVal});
36-
return false ;
37-
}
38-
39-
const cval: []u8 = allocator.alloc(u8, strVal.len ) catch unreachable;
40-
defer allocator.free(cval);
41-
42-
std.mem.copy(u8, cval, strVal);
43-
44-
return re.isMatch(regex, @ptrCast(cval));
45-
}
46-
4714
15+
// tools regex
16+
const reg = @import("match");
4817
18+
const allocatorPrint = std.heap.page_allocator;
4919
5020
pub fn main() !void {
5121
22+
const stdin = std.io.getStdIn().reader();
23+
var buf : [3]u8 = undefined;
24+
buf = [_]u8{0} ** 3;
5225
53-
std.debug.print("Macth abc {} \r\n",.{isMatch("p1","^[a-zA-Z]{1,1}[a-zA-Z0-9]{0,}$")}) ;
5426
55-
std.debug.print("Macth digit {} \r\n",.{isMatch(
27+
std.debug.print("Macth abc {} \r\n",.{reg.isMatch("p1","^[a-zA-Z]{1,1}[a-zA-Z0-9]{0,}$")}) ;
28+
29+
std.debug.print("Macth digit {} \r\n",.{reg.isMatch(
5630
"423",
5731
"^[1-9]{1,1}?[0-9]{0,}$")}) ;
5832
59-
std.debug.print("Macth tel fr{} \r\n",.{isMatch(
33+
std.debug.print("Macth tel fr{} \r\n",.{reg.isMatch(
6034
"+(33)6.12.34.56.78",
6135
"^[+]{1,1}[(]{0,1}[0-9]{1,3}[)]([0-9]{1,3}){1,1}([-. ]?[0-9]{2,3}){2,4}$")}) ;
6236
63-
std.debug.print("Macth tel us{} \r\n",.{isMatch(
64-
"+(001)456.123.089",
37+
std.debug.print("Macth tel us{} \r\n",.{reg.isMatch(
38+
"+(001)456.123.789",
6539
"^[+]{1,1}[(]([0-9]{3,3})[)]([-. ]?[0-9]{3}){2,4}$")}) ;
6640
67-
std.debug.print("Macth date fr{} \r\n",.{isMatch(
41+
std.debug.print("Macth date fr{} \r\n",.{reg.isMatch(
6842
"12/10/1951",
6943
"^(0[1-9]|[12][0-9]|3[01])[\\/](0[1-9]|1[012])[\\/][0-9]{4,4}$")});
7044
7145
72-
std.debug.print("Macth date us{} \r\n",.{isMatch(
46+
std.debug.print("Macth date us{} \r\n",.{reg.isMatch(
7347
"10/12/1951",
7448
"^(0[1-9]|1[012])[\\/](0[1-9]|[12][0-9]|3[01])[\\/][0-9]{4,4}$")});
7549
76-
std.debug.print("Macth date iso{} \r\n",.{isMatch(
77-
"1951年10月12日",
50+
std.debug.print("Macth date iso{} \r\n",.{reg.isMatch(
51+
"2003年02月25日",
7852
"^([0-9]{4,4})[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$")});
7953
8054
8155
// https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression
8256
// chapitre RFC 6532 updates 5322 to allow and include full, clean UTF-8.
8357
84-
std.debug.print("Macth Mail{} \r\n",.{isMatch(
58+
std.debug.print("Macth Mail{} \r\n",.{reg.isMatch(
8559
"myname.myfirstname@gmail.com",
8660
"^([-!#-\'*+\\/-9=?A-Z^-~]{1,64}(\\.[-!#-\'*+\\/-9=?A-Z^-~]{1,64})*|\"([]!#-[^-~ \t]|(\\[\t -~]))+\")@[0-9A-Za-z]([0-9A-Za-z-]{0,61}[0-9A-Za-z])?(\\.[0-9A-Za-z]([0-9A-Za-z-]{0,61}[0-9A-Za-z])?)+$")});
8761
8862
//oreilly
89-
std.debug.print("Macth Mail{} \r\n",.{isMatch(
63+
std.debug.print("Macth Mail{} \r\n",.{reg.isMatch(
9064
"myname.myfirstname@gmail.com",
9165
"^[A-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[A-Z0-9.-]+$")});
9266
9367
94-
var arenaPrint= std.heap.ArenaAllocator.init(std.heap.page_allocator);
95-
var allocatorPrint = arenaPrint.allocator();
9668
9769
9870
var width :usize = 5;
9971
// unsigned digit
100-
std.debug.print("Macth digit unsigned{} \r\n",.{isMatch(
72+
std.debug.print("Macth digit unsigned{} \r\n",.{reg.isMatch(
10173
"123",
10274
std.fmt.allocPrint(allocatorPrint,"^[0-9]{s}{d}{s}$",.{"{1,",width,"}"},) catch unreachable)});
10375
10476
// unsigned digit
105-
std.debug.print("Macth digit {} \r\n",.{isMatch(
77+
std.debug.print("Macth digit {} \r\n",.{reg.isMatch(
10678
"+12345",
10779
std.fmt.allocPrint(allocatorPrint,"^[+-][0-9]{s}{d}{s}$",.{"{1,",width,"}"},) catch unreachable)});
10880
10981
11082
// decimal unsigned scal = 0
111-
std.debug.print("Macth decimal unsigned scal = 0 {} \r\n",.{isMatch(
83+
std.debug.print("Macth decimal unsigned scal = 0 {} \r\n",.{reg.isMatch(
11284
"12345",
11385
std.fmt.allocPrint(allocatorPrint,"^[0-9]{s}1,{d}{s}$",.{"{",width,"}"},) catch unreachable)});
11486
11587
var scal :usize = 2;
11688
// decimal unsigned scal > 0
117-
std.debug.print("Macth decimal unsigned scal > 0 {} \r\n",.{isMatch(
89+
std.debug.print("Macth decimal unsigned scal > 0 {} \r\n",.{reg.isMatch(
11890
"12345.02",
11991
std.fmt.allocPrint(allocatorPrint,
12092
"^[0-9]{s}1,{d}{s}[.][0-9]{s}{d}{s}$",.{"{",width,"}","{",scal,"}"}
12193
) catch unreachable)});
12294
123-
95+
_= try stdin.readUntilDelimiterOrEof(buf[0..], '\n');
12496
12597
// decimal signed scal = 0
126-
std.debug.print("Macth decimal signed scal = 0 {} \r\n",.{isMatch(
98+
std.debug.print("Macth decimal signed scal = 0 {} \r\n",.{reg.isMatch(
12799
"+12345",
128100
std.fmt.allocPrint(allocatorPrint,"^[+-][0-9]{s}1,{d}{s}$",.{"{",width,"}"},) catch unreachable)});
129101
130102
131103
// decimal unsigned scal > 0
132-
std.debug.print("Macth decimal signed scal > 0 {} \r\n",.{isMatch(
104+
std.debug.print("Macth decimal signed scal > 0 {} \r\n",.{reg.isMatch(
133105
"+12345.02",
134106
std.fmt.allocPrint(allocatorPrint,
135107
"^[+-][0-9]{s}1,{d}{s}[.][0-9]{s}{d}{s}$",.{"{",width,"}","{",scal,"}"}
136108
) catch unreachable)});
137109
138110
var i: usize = 0 ;
139111
while(i < 5000) : ( i += 1 ) {
140-
_=isMatch(
112+
_=reg.isMatch(
141113
"1951年10月12日",
142114
"^([0-9]{4,4})[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$");
143115
}
144116
145-
arenaPrint.deinit();
146117
147-
const stdin = std.io.getStdIn().reader();
148-
var buf:[4]u8 = [_]u8{0} ** 4;
118+
buf = [_]u8{0} ** 3;
149119
_= try stdin.readUntilDelimiterOrEof(buf[0..], '\n');
150120
}
121+
151122
```

‎src-zig/buildtestreg.zig‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ pub fn build(b: *std.Build) void {
77
const target = b.standardTargetOptions(.{});
88
const optimize = b.standardOptimizeOption(.{});
99

10+
// zig-src source projet
11+
// zig-src/deps curs/ form / outils ....
12+
// src_c source c/c++
13+
// zig-src/srcgo source go-lang
14+
// zig-src/srcgo/lib lib.so source.h
15+
16+
17+
// Definition of module
18+
const match = b.createModule(.{
19+
.source_file = .{ .path = "./deps/curse/match.zig"},
20+
});
21+
22+
1023

1124

1225
// Building the executable
@@ -17,10 +30,12 @@ pub fn build(b: *std.Build) void {
1730
.target = target,
1831
.optimize = optimize,
1932
});
20-
Prog.addIncludePath("./lib/");
33+
Prog.addIncludePath(.{.path="./lib/"});
2134
Prog.linkLibC();
22-
Prog.addObjectFile("/usr/lib/libpcre2-posix.so");
23-
const install_exe = b.addInstallArtifact(Prog);
35+
Prog.addObjectFile(.{.cwd_relative = "/usr/lib/libpcre2-posix.so"});
36+
Prog.addModule("match" , match);
37+
38+
const install_exe = b.addInstallArtifact(Prog, .{});
2439
b.getInstallStep().dependOn(&install_exe.step);
2540

2641
}

‎src-zig/testreg.zig‎

Lines changed: 27 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,108 @@
11
const std = @import("std");
2-
const re = @cImport({
3-
@cDefine("PCRE2_CODE_UNIT_WIDTH", "8");
4-
@cInclude("regPcre2.h");
5-
});
6-
7-
//-------------------------
8-
// idem ./curse/match.zig
9-
//-------------------------
10-
pub fn isMatch(strVal : [] const u8, regVal : [] const u8 ) bool {
11-
const allocator = std.heap.page_allocator;
12-
var slice = allocator.alignedAlloc(u8, @sizeOf(usize),@sizeOf(usize)) catch unreachable;
13-
defer allocator.free(slice);
14-
15-
const regex: *re.regex_t = @ptrCast(slice );
16-
defer re.pcre2_regfree(regex); // IMPORTANT!!
17-
18-
const creg: []u8 = allocator.alloc(u8, regVal.len , ) catch unreachable;
19-
defer allocator.free(creg);
20-
21-
std.mem.copy(u8, creg, regVal);
22-
23-
if (re.pcre2_regcomp(regex,@ptrCast(creg),re.REG_EXTENDED | re.REG_ICASE) != 0) {
24-
// TODO: the pattern is invalid
25-
// display for test
26-
// std.debug.print("error patern {s}\n", .{regVal});
27-
return false ;
28-
}
29-
30-
const cval: []u8 = allocator.alloc(u8, strVal.len ) catch unreachable;
31-
defer allocator.free(cval);
32-
33-
std.mem.copy(u8, cval, strVal);
34-
35-
return re.isMatch(regex, @ptrCast(cval));
36-
}
37-
382

3+
// tools regex
4+
const reg = @import("match");
395

6+
const allocatorPrint = std.heap.page_allocator;
407

418
pub fn main() !void {
429

10+
const stdin = std.io.getStdIn().reader();
11+
var buf : [3]u8 = undefined;
12+
buf = [_]u8{0} ** 3;
4313

44-
std.debug.print("Macth abc {} \r\n",.{isMatch("p1","^[a-zA-Z]{1,1}[a-zA-Z0-9]{0,}$")}) ;
4514

46-
std.debug.print("Macth digit {} \r\n",.{isMatch(
15+
std.debug.print("Macth abc {} \r\n",.{reg.isMatch("p1","^[a-zA-Z]{1,1}[a-zA-Z0-9]{0,}$")}) ;
16+
17+
std.debug.print("Macth digit {} \r\n",.{reg.isMatch(
4718
"423",
4819
"^[1-9]{1,1}?[0-9]{0,}$")}) ;
4920

50-
std.debug.print("Macth tel fr{} \r\n",.{isMatch(
21+
std.debug.print("Macth tel fr{} \r\n",.{reg.isMatch(
5122
"+(33)6.12.34.56.78",
5223
"^[+]{1,1}[(]{0,1}[0-9]{1,3}[)]([0-9]{1,3}){1,1}([-. ]?[0-9]{2,3}){2,4}$")}) ;
5324

54-
std.debug.print("Macth tel us{} \r\n",.{isMatch(
55-
"+(001)456.123.089",
25+
std.debug.print("Macth tel us{} \r\n",.{reg.isMatch(
26+
"+(001)456.123.789",
5627
"^[+]{1,1}[(]([0-9]{3,3})[)]([-. ]?[0-9]{3}){2,4}$")}) ;
5728

58-
std.debug.print("Macth date fr{} \r\n",.{isMatch(
29+
std.debug.print("Macth date fr{} \r\n",.{reg.isMatch(
5930
"12/10/1951",
6031
"^(0[1-9]|[12][0-9]|3[01])[\\/](0[1-9]|1[012])[\\/][0-9]{4,4}$")});
6132

6233

63-
std.debug.print("Macth date us{} \r\n",.{isMatch(
34+
std.debug.print("Macth date us{} \r\n",.{reg.isMatch(
6435
"10/12/1951",
6536
"^(0[1-9]|1[012])[\\/](0[1-9]|[12][0-9]|3[01])[\\/][0-9]{4,4}$")});
6637

67-
std.debug.print("Macth date iso{} \r\n",.{isMatch(
68-
"1951年10月12日",
38+
std.debug.print("Macth date iso{} \r\n",.{reg.isMatch(
39+
"2003年02月25日",
6940
"^([0-9]{4,4})[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$")});
7041

7142

7243
// https://stackoverflow.com/questions/201323/how-can-i-validate-an-email-address-using-a-regular-expression
7344
// chapitre RFC 6532 updates 5322 to allow and include full, clean UTF-8.
7445

75-
std.debug.print("Macth Mail{} \r\n",.{isMatch(
46+
std.debug.print("Macth Mail{} \r\n",.{reg.isMatch(
7647
"myname.myfirstname@gmail.com",
7748
"^([-!#-\'*+\\/-9=?A-Z^-~]{1,64}(\\.[-!#-\'*+\\/-9=?A-Z^-~]{1,64})*|\"([]!#-[^-~ \t]|(\\[\t -~]))+\")@[0-9A-Za-z]([0-9A-Za-z-]{0,61}[0-9A-Za-z])?(\\.[0-9A-Za-z]([0-9A-Za-z-]{0,61}[0-9A-Za-z])?)+$")});
7849

7950
//oreilly
80-
std.debug.print("Macth Mail{} \r\n",.{isMatch(
51+
std.debug.print("Macth Mail{} \r\n",.{reg.isMatch(
8152
"myname.myfirstname@gmail.com",
8253
"^[A-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[A-Z0-9.-]+$")});
8354

8455

85-
var arenaPrint= std.heap.ArenaAllocator.init(std.heap.page_allocator);
86-
var allocatorPrint = arenaPrint.allocator();
8756

8857

8958
var width :usize = 5;
9059
// unsigned digit
91-
std.debug.print("Macth digit unsigned{} \r\n",.{isMatch(
60+
std.debug.print("Macth digit unsigned{} \r\n",.{reg.isMatch(
9261
"123",
9362
std.fmt.allocPrint(allocatorPrint,"^[0-9]{s}{d}{s}$",.{"{1,",width,"}"},) catch unreachable)});
9463

9564
// unsigned digit
96-
std.debug.print("Macth digit {} \r\n",.{isMatch(
65+
std.debug.print("Macth digit {} \r\n",.{reg.isMatch(
9766
"+12345",
9867
std.fmt.allocPrint(allocatorPrint,"^[+-][0-9]{s}{d}{s}$",.{"{1,",width,"}"},) catch unreachable)});
9968

10069

10170
// decimal unsigned scal = 0
102-
std.debug.print("Macth decimal unsigned scal = 0 {} \r\n",.{isMatch(
71+
std.debug.print("Macth decimal unsigned scal = 0 {} \r\n",.{reg.isMatch(
10372
"12345",
10473
std.fmt.allocPrint(allocatorPrint,"^[0-9]{s}1,{d}{s}$",.{"{",width,"}"},) catch unreachable)});
10574

10675
var scal :usize = 2;
10776
// decimal unsigned scal > 0
108-
std.debug.print("Macth decimal unsigned scal > 0 {} \r\n",.{isMatch(
77+
std.debug.print("Macth decimal unsigned scal > 0 {} \r\n",.{reg.isMatch(
10978
"12345.02",
11079
std.fmt.allocPrint(allocatorPrint,
11180
"^[0-9]{s}1,{d}{s}[.][0-9]{s}{d}{s}$",.{"{",width,"}","{",scal,"}"}
11281
) catch unreachable)});
11382

114-
83+
_=trystdin.readUntilDelimiterOrEof(buf[0..], '\n');
11584

11685
// decimal signed scal = 0
117-
std.debug.print("Macth decimal signed scal = 0 {} \r\n",.{isMatch(
86+
std.debug.print("Macth decimal signed scal = 0 {} \r\n",.{reg.isMatch(
11887
"+12345",
11988
std.fmt.allocPrint(allocatorPrint,"^[+-][0-9]{s}1,{d}{s}$",.{"{",width,"}"},) catch unreachable)});
12089

12190

12291
// decimal unsigned scal > 0
123-
std.debug.print("Macth decimal signed scal > 0 {} \r\n",.{isMatch(
92+
std.debug.print("Macth decimal signed scal > 0 {} \r\n",.{reg.isMatch(
12493
"+12345.02",
12594
std.fmt.allocPrint(allocatorPrint,
12695
"^[+-][0-9]{s}1,{d}{s}[.][0-9]{s}{d}{s}$",.{"{",width,"}","{",scal,"}"}
12796
) catch unreachable)});
12897

12998
var i: usize = 0 ;
13099
while(i < 5000) : ( i += 1 ) {
131-
_=isMatch(
100+
_=reg.isMatch(
132101
"1951年10月12日",
133102
"^([0-9]{4,4})[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$");
134103
}
135104

136-
arenaPrint.deinit();
137105

138-
const stdin = std.io.getStdIn().reader();
139-
var buf:[4]u8 = [_]u8{0} ** 4;
106+
buf = [_]u8{0} ** 3;
140107
_= try stdin.readUntilDelimiterOrEof(buf[0..], '\n');
141108
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /