aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/RegularExpression.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/bun.js/bindings/RegularExpression.zig')
-rw-r--r--src/bun.js/bindings/RegularExpression.zig57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/bun.js/bindings/RegularExpression.zig b/src/bun.js/bindings/RegularExpression.zig
new file mode 100644
index 000000000..9c3da4fa2
--- /dev/null
+++ b/src/bun.js/bindings/RegularExpression.zig
@@ -0,0 +1,57 @@
+const bun = @import("root").bun;
+
+pub const RegularExpression = opaque {
+ pub const Flags = enum(u16) {
+ none = 0,
+
+ hasIndices = 1 << 0,
+ global = 1 << 1,
+ ignoreCase = 1 << 2,
+ multiline = 1 << 3,
+ dotAll = 1 << 4,
+ unicode = 1 << 5,
+ unicodeSets = 1 << 6,
+ sticky = 1 << 7,
+ };
+
+ extern fn Yarr__RegularExpression__init(pattern: bun.String, flags: u16) *RegularExpression;
+ extern fn Yarr__RegularExpression__deinit(pattern: *RegularExpression) void;
+ extern fn Yarr__RegularExpression__isValid(this: *RegularExpression) bool;
+ extern fn Yarr__RegularExpression__matchedLength(this: *RegularExpression) i32;
+ extern fn Yarr__RegularExpression__searchRev(this: *RegularExpression) i32;
+ extern fn Yarr__RegularExpression__matches(this: *RegularExpression, string: bun.String) i32;
+
+ pub inline fn init(pattern: bun.String, flags: Flags) !*RegularExpression {
+ var regex = Yarr__RegularExpression__init(pattern, @intFromEnum(flags));
+ if (!regex.isValid()) {
+ regex.deinit();
+ return error.InvalidRegex;
+ }
+ return regex;
+ }
+
+ pub inline fn isValid(this: *RegularExpression) bool {
+ return Yarr__RegularExpression__isValid(this);
+ }
+
+ // Reserving `match` for a full match result.
+ // pub inline fn match(this: *RegularExpression, str: bun.String, startFrom: i32) MatchResult {
+ // }
+
+ // Simple boolean matcher
+ pub inline fn matches(this: *RegularExpression, str: bun.String) bool {
+ return Yarr__RegularExpression__matches(this, str) > 0;
+ }
+
+ pub inline fn searchRev(this: *RegularExpression, str: bun.String) i32 {
+ return Yarr__RegularExpression__searchRev(this, str);
+ }
+
+ pub inline fn matchedLength(this: *RegularExpression) i32 {
+ return Yarr__RegularExpression__matchedLength(this);
+ }
+
+ pub inline fn deinit(this: *RegularExpression) void {
+ Yarr__RegularExpression__deinit(this);
+ }
+};