diff options
author | 2022-12-01 20:51:35 -0800 | |
---|---|---|
committer | 2022-12-01 20:51:35 -0800 | |
commit | b4e6ca046291058ed9a1ba7fa4733acbc649f36c (patch) | |
tree | 253ab31838241f3c6cf391f9ae5451c9556ee987 /src | |
parent | f408749182f49607902921c55307f2cf94eb23db (diff) | |
download | bun-b4e6ca046291058ed9a1ba7fa4733acbc649f36c.tar.gz bun-b4e6ca046291058ed9a1ba7fa4733acbc649f36c.tar.zst bun-b4e6ca046291058ed9a1ba7fa4733acbc649f36c.zip |
ComptimeStringMap is faster than ExactSizeMatcher
Diffstat (limited to 'src')
-rw-r--r-- | src/http/method.zig | 59 |
1 files changed, 22 insertions, 37 deletions
diff --git a/src/http/method.zig b/src/http/method.zig index 01d2daa87..33d24f71c 100644 --- a/src/http/method.zig +++ b/src/http/method.zig @@ -46,43 +46,28 @@ pub const Method = enum { return with_request_body.contains(this); } + const Map = bun.ComptimeStringMap(Method, .{ + .{ "CONNECT", Method.CONNECT }, + .{ "DELETE", Method.DELETE }, + .{ "GET", Method.GET }, + .{ "HEAD", Method.HEAD }, + .{ "OPTIONS", Method.OPTIONS }, + .{ "PATCH", Method.PATCH }, + .{ "POST", Method.POST }, + .{ "PUT", Method.PUT }, + .{ "TRACE", Method.TRACE }, + .{ "connect", Method.CONNECT }, + .{ "delete", Method.DELETE }, + .{ "get", Method.GET }, + .{ "head", Method.HEAD }, + .{ "options", Method.OPTIONS }, + .{ "patch", Method.PATCH }, + .{ "post", Method.POST }, + .{ "put", Method.PUT }, + .{ "trace", Method.TRACE }, + }); + pub fn which(str: []const u8) ?Method { - if (str.len < 3) { - return null; - } - const Match = strings.ExactSizeMatcher(2); - // we already did the length check - switch (Match.match(str[0..2])) { - Match.case("GE"), Match.case("ge") => { - return .GET; - }, - Match.case("HE"), Match.case("he") => { - return .HEAD; - }, - Match.case("PA"), Match.case("pa") => { - return .PATCH; - }, - Match.case("PO"), Match.case("po") => { - return .POST; - }, - Match.case("PU"), Match.case("pu") => { - return .PUT; - }, - Match.case("OP"), Match.case("op") => { - return .OPTIONS; - }, - Match.case("CO"), Match.case("co") => { - return .CONNECT; - }, - Match.case("TR"), Match.case("tr") => { - return .TRACE; - }, - Match.case("DE"), Match.case("de") => { - return .DELETE; - }, - else => { - return null; - }, - } + return Map.get(str); } }; |