11const std = @import ("std" );
22const testing = std .testing ;
33
4+ const strparser = @import ("../str/parser.zig" );
5+ const Reader = strparser .Reader ;
6+ const trim = strparser .trim ;
7+
48const Self = @This ();
59
610const MimeError = error {
@@ -21,91 +25,6 @@ pub const Empty = Self{ .mtype = "", .msubtype = "" };
2125pub const HTML = Self { .mtype = "text" , .msubtype = "html" };
2226pub const Javascript = Self { .mtype = "application" , .msubtype = "javascript" };
2327
24- const reader = struct {
25- s : []const u8 ,
26- i : usize = 0 ,
27-
28- fn until (self : * reader , c : u8 ) []const u8 {
29- const ln = self .s .len ;
30- const start = self .i ;
31- while (self .i < ln ) {
32- if (c == self .s [self .i ]) return self .s [start .. self .i ];
33- self .i += 1 ;
34- }
35-
36- return self .s [start .. self .i ];
37- }
38-
39- fn tail (self : * reader ) []const u8 {
40- if (self .i > self .s .len ) return "" ;
41- defer self .i = self .s .len ;
42- return self .s [self .i .. ];
43- }
44-
45- fn skip (self : * reader ) bool {
46- if (self .i >= self .s .len ) return false ;
47- self .i += 1 ;
48- return true ;
49- }
50- };
51-
52- test "reader.skip" {
53- var r = reader { .s = "foo" };
54- try testing .expect (r .skip ());
55- try testing .expect (r .skip ());
56- try testing .expect (r .skip ());
57- try testing .expect (! r .skip ());
58- try testing .expect (! r .skip ());
59- }
60-
61- test "reader.tail" {
62- var r = reader { .s = "foo" };
63- try testing .expectEqualStrings ("foo" , r .tail ());
64- try testing .expectEqualStrings ("" , r .tail ());
65- }
66-
67- test "reader.until" {
68- var r = reader { .s = "foo.bar.baz" };
69- try testing .expectEqualStrings ("foo" , r .until ('.' ));
70- _ = r .skip ();
71- try testing .expectEqualStrings ("bar" , r .until ('.' ));
72- _ = r .skip ();
73- try testing .expectEqualStrings ("baz" , r .until ('.' ));
74-
75- r = reader { .s = "foo" };
76- try testing .expectEqualStrings ("foo" , r .until ('.' ));
77-
78- r = reader { .s = "" };
79- try testing .expectEqualStrings ("" , r .until ('.' ));
80- }
81-
82- fn trim (s : []const u8 ) []const u8 {
83- const ln = s .len ;
84- if (ln == 0 ) {
85- return "" ;
86- }
87- var start : usize = 0 ;
88- while (start < ln ) {
89- if (! std .ascii .isWhitespace (s [start ])) break ;
90- start += 1 ;
91- }
92-
93- var end : usize = ln ;
94- while (end > 0 ) {
95- if (! std .ascii .isWhitespace (s [end - 1 ])) break ;
96- end -= 1 ;
97- }
98-
99- return s [start .. end ];
100- }
101-
102- test "trim" {
103- try testing .expectEqualStrings ("" , trim ("" ));
104- try testing .expectEqualStrings ("foo" , trim ("foo" ));
105- try testing .expectEqualStrings ("foo" , trim (" \n \t foo" ));
106- try testing .expectEqualStrings ("foo" , trim ("foo \n \t " ));
107- }
108-
10928// https://mimesniff.spec.whatwg.org/#http-token-code-point
11029fn isHTTPCodePoint (c : u8 ) bool {
11130 return switch (c ) {
@@ -133,7 +52,7 @@ pub fn parse(s: []const u8) Self.MimeError!Self {
13352 if (ln > 255 ) return MimeError .TooBig ;
13453
13554 var res = Self { .mtype = "" , .msubtype = "" };
136- var r = reader { .s = s };
55+ var r = Reader { .s = s };
13756
13857 res .mtype = trim (r .until ('/' ));
13958 if (res .mtype .len == 0 ) return MimeError .Invalid ;
@@ -150,7 +69,7 @@ pub fn parse(s: []const u8) Self.MimeError!Self {
15069
15170 // parse well known parameters.
15271 // don't check invalid parameter format.
153- var rp = reader { .s = res .params };
72+ var rp = Reader { .s = res .params };
15473 while (true ) {
15574 const name = trim (rp .until ('=' ));
15675 if (! rp .skip ()) return res ;
0 commit comments