Implement --drop (#14492)

Co-authored-by: dave caruso <me@paperdave.net>
This commit is contained in:
Jarred Sumner
2024-10-11 20:52:23 -07:00
committed by GitHub
parent bbb41beadc
commit c77fc5daa0
26 changed files with 366 additions and 147 deletions

View File

@@ -37,8 +37,8 @@ pub const MutableString = struct {
}
}
pub fn owns(this: *const MutableString, slice: []const u8) bool {
return bun.isSliceInBuffer(slice, this.list.items.ptr[0..this.list.capacity]);
pub fn owns(this: *const MutableString, items: []const u8) bool {
return bun.isSliceInBuffer(items, this.list.items.ptr[0..this.list.capacity]);
}
pub fn growIfNeeded(self: *MutableString, amount: usize) OOM!void {
@@ -119,8 +119,8 @@ pub const MutableString = struct {
str[0..start_i]);
needs_gap = false;
var slice = str[start_i..];
iterator = strings.CodepointIterator.init(slice);
var items = str[start_i..];
iterator = strings.CodepointIterator.init(items);
cursor = strings.CodepointIterator.Cursor{};
while (iterator.next(&cursor)) {
@@ -130,7 +130,7 @@ pub const MutableString = struct {
needs_gap = false;
has_needed_gap = true;
}
try mutable.append(slice[cursor.i .. cursor.i + @as(u32, cursor.width)]);
try mutable.append(items[cursor.i .. cursor.i + @as(u32, cursor.width)]);
} else if (!needs_gap) {
needs_gap = true;
// skip the code point, replace it with a single _
@@ -172,17 +172,16 @@ pub const MutableString = struct {
try self.list.ensureUnusedCapacity(self.allocator, amount);
}
pub inline fn appendSlice(self: *MutableString, slice: []const u8) !void {
try self.list.appendSlice(self.allocator, slice);
pub inline fn appendSlice(self: *MutableString, items: []const u8) !void {
try self.list.appendSlice(self.allocator, items);
}
pub inline fn appendSliceExact(self: *MutableString, slice: []const u8) !void {
if (slice.len == 0) return;
try self.list.ensureTotalCapacityPrecise(self.allocator, self.list.items.len + slice.len);
pub inline fn appendSliceExact(self: *MutableString, items: []const u8) !void {
if (items.len == 0) return;
try self.list.ensureTotalCapacityPrecise(self.allocator, self.list.items.len + items.len);
var end = self.list.items.ptr + self.list.items.len;
self.list.items.len += slice.len;
@memcpy(end[0..slice.len], slice);
self.list.items.len += items.len;
@memcpy(end[0..items.len], items);
}
pub inline fn reset(
@@ -237,7 +236,7 @@ pub const MutableString = struct {
return self.list.toOwnedSlice(self.allocator) catch bun.outOfMemory(); // TODO
}
pub fn toOwnedSliceLeaky(self: *MutableString) []u8 {
pub fn slice(self: *MutableString) []u8 {
return self.list.items;
}
@@ -248,7 +247,8 @@ pub const MutableString = struct {
return out;
}
pub fn toOwnedSentinelLeaky(self: *MutableString) [:0]u8 {
/// Appends `0` if needed
pub fn sliceWithSentinel(self: *MutableString) [:0]u8 {
if (self.list.items.len > 0 and self.list.items[self.list.items.len - 1] != 0) {
self.list.append(
self.allocator,
@@ -264,10 +264,6 @@ pub const MutableString = struct {
return self.list.toOwnedSlice(self.allocator) catch bun.outOfMemory(); // TODO
}
// pub fn deleteAt(self: *MutableString, i: usize) {
// self.list.swapRemove(i);
// }
pub fn containsChar(self: *const MutableString, char: u8) bool {
return self.indexOfChar(char) != null;
}
@@ -399,46 +395,46 @@ pub const MutableString = struct {
}
pub fn writeHTMLAttributeValue(this: *BufferedWriter, bytes: []const u8) anyerror!void {
var slice = bytes;
while (slice.len > 0) {
var items = bytes;
while (items.len > 0) {
// TODO: SIMD
if (strings.indexOfAny(slice, "\"<>")) |j| {
_ = try this.writeAll(slice[0..j]);
_ = switch (slice[j]) {
if (strings.indexOfAny(items, "\"<>")) |j| {
_ = try this.writeAll(items[0..j]);
_ = switch (items[j]) {
'"' => try this.writeAll("&quot;"),
'<' => try this.writeAll("&lt;"),
'>' => try this.writeAll("&gt;"),
else => unreachable,
};
slice = slice[j + 1 ..];
items = items[j + 1 ..];
continue;
}
_ = try this.writeAll(slice);
_ = try this.writeAll(items);
break;
}
}
pub fn writeHTMLAttributeValue16(this: *BufferedWriter, bytes: []const u16) anyerror!void {
var slice = bytes;
while (slice.len > 0) {
if (strings.indexOfAny16(slice, "\"<>")) |j| {
var items = bytes;
while (items.len > 0) {
if (strings.indexOfAny16(items, "\"<>")) |j| {
// this won't handle strings larger than 4 GB
// that's fine though, 4 GB of SSR'd HTML is quite a lot...
_ = try this.writeAll16(slice[0..j]);
_ = switch (slice[j]) {
_ = try this.writeAll16(items[0..j]);
_ = switch (items[j]) {
'"' => try this.writeAll("&quot;"),
'<' => try this.writeAll("&lt;"),
'>' => try this.writeAll("&gt;"),
else => unreachable,
};
slice = slice[j + 1 ..];
items = items[j + 1 ..];
continue;
}
_ = try this.writeAll16(slice);
_ = try this.writeAll16(items);
break;
}
}