@@ -24,6 +24,7 @@ pub const Mime = struct {
2424 // IANA defines max. charset value length as 40.
2525 // We keep 41 for null-termination since HTML parser expects in this format.
2626 charset : [41 ]u8 = default_charset ,
27+ charset_len : usize = 5 ,
2728
2829 /// String "UTF-8" continued by null characters.
2930 pub const default_charset = .{ 'U' , 'T' , 'F' , '-' , '8' } ++ .{0 } ** 36 ;
@@ -53,7 +54,7 @@ pub const Mime = struct {
5354 other : struct { type : []const u8 , sub_type : []const u8 },
5455 };
5556
56- pub fn contentTypeString (mime : * const Mime ) [: 0 ]const u8 {
57+ pub fn contentTypeString (mime : * const Mime ) []const u8 {
5758 return switch (mime .content_type ) {
5859 .text_xml = > "text/xml" ,
5960 .text_html = > "text/html" ,
@@ -66,8 +67,12 @@ pub const Mime = struct {
6667 }
6768
6869 /// Returns the null-terminated charset value.
69- pub fn charsetString (mime : * const Mime ) [:0 ]const u8 {
70- return @ptrCast (& mime .charset );
70+ pub fn charsetStringZ (mime : * const Mime ) [:0 ]const u8 {
71+ return mime .charset [0.. mime .charset_len :0 ];
72+ }
73+
74+ pub fn charsetString (mime : * const Mime ) []const u8 {
75+ return mime .charset [0.. mime .charset_len ];
7176 }
7277
7378 /// Removes quotes of value if quotes are given.
@@ -111,6 +116,7 @@ pub const Mime = struct {
111116 const params = trimLeft (normalized [type_len .. ]);
112117
113118 var charset : [41 ]u8 = undefined ;
119+ var charset_len : usize = undefined ;
114120
115121 var it = std .mem .splitScalar (u8 , params , ';' );
116122 while (it .next ()) | attr | {
@@ -136,13 +142,15 @@ pub const Mime = struct {
136142 @memcpy (charset [0.. attribute_value .len ], attribute_value );
137143 // Null-terminate right after attribute value.
138144 charset [attribute_value .len ] = 0 ;
145+ charset_len = attribute_value .len ;
139146 },
140147 }
141148 }
142149
143150 return .{
144151 .params = params ,
145152 .charset = charset ,
153+ .charset_len = charset_len ,
146154 .content_type = content_type ,
147155 };
148156 }
@@ -523,9 +531,9 @@ fn expect(expected: Expectation, input: []const u8) !void {
523531
524532 if (expected .charset ) | ec | {
525533 // We remove the null characters for testing purposes here.
526- try testing .expectEqual (ec , actual .charsetString ()[0 .. ec . len ] );
534+ try testing .expectEqual (ec , actual .charsetString ());
527535 } else {
528536 const m : Mime = .unknown ;
529- try testing .expectEqual (m .charsetString (), actual .charsetString ());
537+ try testing .expectEqual (m .charsetStringZ (), actual .charsetStringZ ());
530538 }
531539}
0 commit comments