aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/RegularExpression.zig
blob: 9c3da4fa2717de186c30a96441bad516f6fd36fd (plain) (blame)
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
55
56
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);
    }
};