mirror of
https://github.com/oven-sh/bun
synced 2026-02-14 12:51:54 +00:00
merge
This commit is contained in:
@@ -247,17 +247,17 @@ pub fn takeSlice(self: *MutableString) []u8 {
|
||||
}
|
||||
|
||||
pub fn toOwnedSlice(self: *MutableString) []u8 {
|
||||
return bun.handleOom(self.list.toOwnedSlice(self.allocator));
|
||||
return bun.handleOom(self.list.toOwnedSlice(self.allocator)); // TODO
|
||||
}
|
||||
|
||||
pub fn toDynamicOwned(self: *MutableString) DynamicOwned([]u8) {
|
||||
return .fromRawOwned(self.toOwnedSlice(), self.allocator);
|
||||
return .fromRawIn(self.toOwnedSlice(), self.allocator);
|
||||
}
|
||||
|
||||
/// `self.allocator` must be `bun.default_allocator`.
|
||||
pub fn toDefaultOwned(self: *MutableString) Owned([]u8) {
|
||||
bun.safety.alloc.assertEq(self.allocator, bun.default_allocator);
|
||||
return .fromRawOwned(self.toOwnedSlice());
|
||||
return .fromRaw(self.toOwnedSlice());
|
||||
}
|
||||
|
||||
pub fn slice(self: *MutableString) []u8 {
|
||||
|
||||
@@ -22,7 +22,7 @@ const Node = struct {
|
||||
next: ?*Node = null,
|
||||
|
||||
pub fn init(joiner_alloc: Allocator, slice: []const u8, slice_alloc: ?Allocator) *Node {
|
||||
const node = joiner_alloc.create(Node) catch bun.outOfMemory();
|
||||
const node = bun.handleOom(joiner_alloc.create(Node));
|
||||
node.* = .{
|
||||
.slice = slice,
|
||||
.allocator = NullableAllocator.init(slice_alloc),
|
||||
@@ -51,7 +51,7 @@ pub fn pushStatic(this: *StringJoiner, data: []const u8) void {
|
||||
pub fn pushCloned(this: *StringJoiner, data: []const u8) void {
|
||||
if (data.len == 0) return;
|
||||
this.push(
|
||||
this.allocator.dupe(u8, data) catch bun.outOfMemory(),
|
||||
bun.handleOom(this.allocator.dupe(u8, data)),
|
||||
this.allocator,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ pub const WTFStringImplStruct = extern struct {
|
||||
|
||||
pub fn toUTF8(this: WTFStringImpl, allocator: std.mem.Allocator) ZigString.Slice {
|
||||
if (this.is8Bit()) {
|
||||
if (bun.strings.toUTF8FromLatin1(allocator, this.latin1Slice()) catch bun.outOfMemory()) |utf8| {
|
||||
if (bun.handleOom(bun.strings.toUTF8FromLatin1(allocator, this.latin1Slice()))) |utf8| {
|
||||
return ZigString.Slice.init(allocator, utf8.items);
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ pub const WTFStringImplStruct = extern struct {
|
||||
|
||||
return ZigString.Slice.init(
|
||||
allocator,
|
||||
bun.strings.toUTF8Alloc(allocator, this.utf16Slice()) catch bun.outOfMemory(),
|
||||
bun.handleOom(bun.strings.toUTF8Alloc(allocator, this.utf16Slice())),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -141,7 +141,7 @@ pub const WTFStringImplStruct = extern struct {
|
||||
|
||||
pub fn toUTF8WithoutRef(this: WTFStringImpl, allocator: std.mem.Allocator) ZigString.Slice {
|
||||
if (this.is8Bit()) {
|
||||
if (bun.strings.toUTF8FromLatin1(allocator, this.latin1Slice()) catch bun.outOfMemory()) |utf8| {
|
||||
if (bun.handleOom(bun.strings.toUTF8FromLatin1(allocator, this.latin1Slice()))) |utf8| {
|
||||
return ZigString.Slice.init(allocator, utf8.items);
|
||||
}
|
||||
|
||||
@@ -150,24 +150,24 @@ pub const WTFStringImplStruct = extern struct {
|
||||
|
||||
return ZigString.Slice.init(
|
||||
allocator,
|
||||
bun.strings.toUTF8Alloc(allocator, this.utf16Slice()) catch bun.outOfMemory(),
|
||||
bun.handleOom(bun.strings.toUTF8Alloc(allocator, this.utf16Slice())),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn toOwnedSliceZ(this: WTFStringImpl, allocator: std.mem.Allocator) [:0]u8 {
|
||||
if (this.is8Bit()) {
|
||||
if (bun.strings.toUTF8FromLatin1Z(allocator, this.latin1Slice()) catch bun.outOfMemory()) |utf8| {
|
||||
if (bun.handleOom(bun.strings.toUTF8FromLatin1Z(allocator, this.latin1Slice()))) |utf8| {
|
||||
return utf8.items[0 .. utf8.items.len - 1 :0];
|
||||
}
|
||||
|
||||
return allocator.dupeZ(u8, this.latin1Slice()) catch bun.outOfMemory();
|
||||
return bun.handleOom(allocator.dupeZ(u8, this.latin1Slice()));
|
||||
}
|
||||
return bun.strings.toUTF8AllocZ(allocator, this.utf16Slice()) catch bun.outOfMemory();
|
||||
return bun.handleOom(bun.strings.toUTF8AllocZ(allocator, this.utf16Slice()));
|
||||
}
|
||||
|
||||
pub fn toUTF8IfNeeded(this: WTFStringImpl, allocator: std.mem.Allocator) ?ZigString.Slice {
|
||||
if (this.is8Bit()) {
|
||||
if (bun.strings.toUTF8FromLatin1(allocator, this.latin1Slice()) catch bun.outOfMemory()) |utf8| {
|
||||
if (bun.handleOom(bun.strings.toUTF8FromLatin1(allocator, this.latin1Slice()))) |utf8| {
|
||||
return ZigString.Slice.init(allocator, utf8.items);
|
||||
}
|
||||
|
||||
@@ -176,7 +176,7 @@ pub const WTFStringImplStruct = extern struct {
|
||||
|
||||
return ZigString.Slice.init(
|
||||
allocator,
|
||||
bun.strings.toUTF8Alloc(allocator, this.utf16Slice()) catch bun.outOfMemory(),
|
||||
bun.handleOom(bun.strings.toUTF8Alloc(allocator, this.utf16Slice())),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1546,11 +1546,11 @@ const LineRange = struct {
|
||||
start: u32,
|
||||
end: u32,
|
||||
};
|
||||
pub fn indexOfLineRanges(text: []const u8, target_line: u32, comptime line_range_count: usize) std.BoundedArray(LineRange, line_range_count) {
|
||||
pub fn indexOfLineRanges(text: []const u8, target_line: u32, comptime line_range_count: usize) bun.BoundedArray(LineRange, line_range_count) {
|
||||
const remaining = text;
|
||||
if (remaining.len == 0) return .{};
|
||||
|
||||
var ranges = std.BoundedArray(LineRange, line_range_count){};
|
||||
var ranges = bun.BoundedArray(LineRange, line_range_count){};
|
||||
|
||||
var current_line: u32 = 0;
|
||||
const first_newline_or_nonascii_i = strings.indexOfNewlineOrNonASCIICheckStart(text, 0, true) orelse {
|
||||
@@ -1644,7 +1644,7 @@ pub fn indexOfLineRanges(text: []const u8, target_line: u32, comptime line_range
|
||||
};
|
||||
|
||||
if (ranges.len == line_range_count and current_line <= target_line) {
|
||||
var new_ranges = std.BoundedArray(LineRange, line_range_count){};
|
||||
var new_ranges = bun.BoundedArray(LineRange, line_range_count){};
|
||||
new_ranges.appendSliceAssumeCapacity(ranges.slice()[1..]);
|
||||
ranges = new_ranges;
|
||||
}
|
||||
@@ -1658,7 +1658,7 @@ pub fn indexOfLineRanges(text: []const u8, target_line: u32, comptime line_range
|
||||
}
|
||||
|
||||
if (ranges.len == line_range_count and current_line <= target_line) {
|
||||
var new_ranges = std.BoundedArray(LineRange, line_range_count){};
|
||||
var new_ranges = bun.BoundedArray(LineRange, line_range_count){};
|
||||
new_ranges.appendSliceAssumeCapacity(ranges.slice()[1..]);
|
||||
ranges = new_ranges;
|
||||
}
|
||||
@@ -1667,10 +1667,10 @@ pub fn indexOfLineRanges(text: []const u8, target_line: u32, comptime line_range
|
||||
}
|
||||
|
||||
/// Get N lines from the start of the text
|
||||
pub fn getLinesInText(text: []const u8, line: u32, comptime line_range_count: usize) ?std.BoundedArray([]const u8, line_range_count) {
|
||||
pub fn getLinesInText(text: []const u8, line: u32, comptime line_range_count: usize) ?bun.BoundedArray([]const u8, line_range_count) {
|
||||
const ranges = indexOfLineRanges(text, line, line_range_count);
|
||||
if (ranges.len == 0) return null;
|
||||
var results = std.BoundedArray([]const u8, line_range_count){};
|
||||
var results = bun.BoundedArray([]const u8, line_range_count){};
|
||||
results.len = ranges.len;
|
||||
|
||||
for (results.slice()[0..ranges.len], ranges.slice()) |*chunk, range| {
|
||||
@@ -2239,6 +2239,7 @@ pub const convertUTF8toUTF16InBuffer = unicode.convertUTF8toUTF16InBuffer;
|
||||
pub const convertUTF8toUTF16InBufferZ = unicode.convertUTF8toUTF16InBufferZ;
|
||||
pub const copyLatin1IntoASCII = unicode.copyLatin1IntoASCII;
|
||||
pub const copyLatin1IntoUTF16 = unicode.copyLatin1IntoUTF16;
|
||||
pub const copyCP1252IntoUTF16 = unicode.copyCP1252IntoUTF16;
|
||||
pub const copyLatin1IntoUTF8 = unicode.copyLatin1IntoUTF8;
|
||||
pub const copyLatin1IntoUTF8StopOnNonASCII = unicode.copyLatin1IntoUTF8StopOnNonASCII;
|
||||
pub const copyU16IntoU8 = unicode.copyU16IntoU8;
|
||||
@@ -2251,7 +2252,7 @@ pub const copyUTF16IntoUTF8WithBufferImpl = unicode.copyUTF16IntoUTF8WithBufferI
|
||||
pub const decodeCheck = unicode.decodeCheck;
|
||||
pub const decodeWTF8RuneT = unicode.decodeWTF8RuneT;
|
||||
pub const decodeWTF8RuneTMultibyte = unicode.decodeWTF8RuneTMultibyte;
|
||||
pub const elementLengthLatin1IntoUTF16 = unicode.elementLengthLatin1IntoUTF16;
|
||||
pub const elementLengthCP1252IntoUTF16 = unicode.elementLengthCP1252IntoUTF16;
|
||||
pub const elementLengthLatin1IntoUTF8 = unicode.elementLengthLatin1IntoUTF8;
|
||||
pub const elementLengthUTF16IntoUTF8 = unicode.elementLengthUTF16IntoUTF8;
|
||||
pub const elementLengthUTF8IntoUTF16 = unicode.elementLengthUTF8IntoUTF16;
|
||||
@@ -2262,9 +2263,9 @@ pub const eqlUtf16 = unicode.eqlUtf16;
|
||||
pub const isAllASCII = unicode.isAllASCII;
|
||||
pub const isValidUTF8 = unicode.isValidUTF8;
|
||||
pub const isValidUTF8WithoutSIMD = unicode.isValidUTF8WithoutSIMD;
|
||||
pub const latin1ToCodepointAssumeNotASCII = unicode.latin1ToCodepointAssumeNotASCII;
|
||||
pub const latin1ToCodepointBytesAssumeNotASCII = unicode.latin1ToCodepointBytesAssumeNotASCII;
|
||||
pub const latin1ToCodepointBytesAssumeNotASCII16 = unicode.latin1ToCodepointBytesAssumeNotASCII16;
|
||||
pub const cp1252ToCodepointAssumeNotASCII = unicode.cp1252ToCodepointAssumeNotASCII;
|
||||
pub const cp1252ToCodepointBytesAssumeNotASCII = unicode.cp1252ToCodepointBytesAssumeNotASCII;
|
||||
pub const cp1252ToCodepointBytesAssumeNotASCII16 = unicode.cp1252ToCodepointBytesAssumeNotASCII16;
|
||||
pub const literal = unicode.literal;
|
||||
pub const nonASCIISequenceLength = unicode.nonASCIISequenceLength;
|
||||
pub const replaceLatin1WithUTF8 = unicode.replaceLatin1WithUTF8;
|
||||
|
||||
@@ -839,7 +839,7 @@ pub fn elementLengthLatin1IntoUTF8(slice: []const u8) usize {
|
||||
return bun.simdutf.length.utf8.from.latin1(slice);
|
||||
}
|
||||
|
||||
pub fn copyLatin1IntoUTF16(comptime Buffer: type, buf_: Buffer, comptime Type: type, latin1_: Type) EncodeIntoResult {
|
||||
pub fn copyCP1252IntoUTF16(comptime Buffer: type, buf_: Buffer, comptime Type: type, latin1_: Type) EncodeIntoResult {
|
||||
var buf = buf_;
|
||||
var latin1 = latin1_;
|
||||
while (buf.len > 0 and latin1.len > 0) {
|
||||
@@ -853,7 +853,7 @@ pub fn copyLatin1IntoUTF16(comptime Buffer: type, buf_: Buffer, comptime Type: t
|
||||
latin1 = latin1[to_write..];
|
||||
buf = buf[to_write..];
|
||||
if (latin1.len > 0 and buf.len >= 1) {
|
||||
buf[0] = latin1ToCodepointBytesAssumeNotASCII16(latin1[0]);
|
||||
buf[0] = cp1252ToCodepointBytesAssumeNotASCII16(latin1[0]);
|
||||
latin1 = latin1[1..];
|
||||
buf = buf[1..];
|
||||
}
|
||||
@@ -865,13 +865,15 @@ pub fn copyLatin1IntoUTF16(comptime Buffer: type, buf_: Buffer, comptime Type: t
|
||||
};
|
||||
}
|
||||
|
||||
pub fn elementLengthLatin1IntoUTF16(comptime Type: type, latin1_: Type) usize {
|
||||
// latin1 is always at most 1 UTF-16 code unit long
|
||||
if (comptime std.meta.Child([]const u16) == Type) {
|
||||
return latin1_.len;
|
||||
}
|
||||
pub fn copyLatin1IntoUTF16(comptime Buffer: type, buf_: Buffer, comptime Type: type, latin1_: Type) EncodeIntoResult {
|
||||
const len = @min(buf_.len, latin1_.len);
|
||||
for (buf_[0..len], latin1_[0..len]) |*out, in| out.* = in;
|
||||
return .{ .read = @as(u32, @truncate(len)), .written = @as(u32, @truncate(len)) };
|
||||
}
|
||||
|
||||
return bun.simdutf.length.utf16.from.latin1(latin1_);
|
||||
pub fn elementLengthCP1252IntoUTF16(comptime Type: type, cp1252_: Type) usize {
|
||||
// cp1252 is always at most 1 UTF-16 code unit long
|
||||
return cp1252_.len;
|
||||
}
|
||||
|
||||
pub fn eqlUtf16(comptime self: string, other: []const u16) bool {
|
||||
@@ -1629,14 +1631,14 @@ pub fn convertUTF16toUTF8InBuffer(
|
||||
return buf[0..result];
|
||||
}
|
||||
|
||||
pub fn latin1ToCodepointAssumeNotASCII(char: u8, comptime CodePointType: type) CodePointType {
|
||||
pub fn cp1252ToCodepointAssumeNotASCII(char: u8, comptime CodePointType: type) CodePointType {
|
||||
return @as(
|
||||
CodePointType,
|
||||
@intCast(latin1ToCodepointBytesAssumeNotASCII16(char)),
|
||||
@intCast(cp1252ToCodepointBytesAssumeNotASCII16(char)),
|
||||
);
|
||||
}
|
||||
|
||||
const latin1_to_utf16_conversion_table = [256]u16{
|
||||
const cp1252_to_utf16_conversion_table = [256]u16{
|
||||
0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, // 00-07
|
||||
0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, // 08-0F
|
||||
0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, // 10-17
|
||||
@@ -1677,8 +1679,8 @@ pub fn latin1ToCodepointBytesAssumeNotASCII(char: u32) [2]u8 {
|
||||
return bytes[0..2].*;
|
||||
}
|
||||
|
||||
pub fn latin1ToCodepointBytesAssumeNotASCII16(char: u32) u16 {
|
||||
return latin1_to_utf16_conversion_table[@as(u8, @truncate(char))];
|
||||
pub fn cp1252ToCodepointBytesAssumeNotASCII16(char: u32) u16 {
|
||||
return cp1252_to_utf16_conversion_table[@as(u8, @truncate(char))];
|
||||
}
|
||||
|
||||
/// Copy a UTF-16 string as UTF-8 into `buf`
|
||||
|
||||
Reference in New Issue
Block a user