Skip to content

Commit 93542c9

Browse files
committed
support Blob.slice
1 parent 4be7fa1 commit 93542c9

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/browser/file/Blob.zig

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,55 @@ pub fn _bytes(self: *const Blob, page: *Page) !js.Promise {
175175
return resolver.promise();
176176
}
177177

178+
/// Returns a new Blob object which contains data
179+
/// from a subset of the blob on which it's called.
180+
pub fn _slice(
181+
self: *const Blob,
182+
maybe_start: ?i32,
183+
maybe_end: ?i32,
184+
maybe_content_type: ?[]const u8,
185+
page: *Page,
186+
) !Blob {
187+
const mime: []const u8 = blk: {
188+
if (maybe_content_type) |content_type| {
189+
if (content_type.len == 0) {
190+
break :blk "";
191+
}
192+
193+
break :blk try page.arena.dupe(u8, content_type);
194+
}
195+
196+
break :blk "";
197+
};
198+
199+
const slice = self.slice;
200+
if (maybe_start) |_start| {
201+
const start = blk: {
202+
if (_start < 0) {
203+
break :blk slice.len -| @abs(_start);
204+
}
205+
206+
break :blk @min(slice.len, @as(u31, @intCast(_start)));
207+
};
208+
209+
const end: usize = blk: {
210+
if (maybe_end) |_end| {
211+
if (_end < 0) {
212+
break :blk @max(start, slice.len -| @abs(_end));
213+
}
214+
215+
break :blk @min(slice.len, @max(start, @as(u31, @intCast(_end))));
216+
}
217+
218+
break :blk slice.len;
219+
};
220+
221+
return .{ .slice = slice[start..end], .mime = mime };
222+
}
223+
224+
return .{ .slice = slice, .mime = mime };
225+
}
226+
178227
/// Returns the size of the Blob in bytes.
179228
pub fn get_size(self: *const Blob) usize {
180229
return self.slice.len;

0 commit comments

Comments
 (0)