mirror of
https://github.com/oven-sh/bun
synced 2026-02-12 03:48:56 +00:00
Tweak formatting of console.table (#7927)
* Tweak alignment of console.table * " " empty string instead of "#" * Fix assertion failure * Only get the length when necessary * Avoid stale copies * Add `asUTF8` helper to `bun.String` * Cautiously handle recursion * Further tweaks to output * output * Add native implementation of `getStringWidth` * [autofix.ci] apply automated fixes * If its not a string then quote it by default * Add snapshot for headers * Make it easier to debug when a builtin throws an exception * This must be hoisted * Fix bugs in `getStringWidth` * Update getStringWidth.test.ts * Eager load it --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -618,16 +618,22 @@ pub const String = extern struct {
|
||||
pub inline fn utf16(self: String) []const u16 {
|
||||
if (self.tag == .Empty)
|
||||
return &[_]u16{};
|
||||
std.debug.assert(self.tag == .WTFStringImpl);
|
||||
return self.value.WTFStringImpl.utf16Slice();
|
||||
if (self.tag == .WTFStringImpl) {
|
||||
return self.value.WTFStringImpl.utf16Slice();
|
||||
}
|
||||
|
||||
return self.toZigString().utf16SliceAligned();
|
||||
}
|
||||
|
||||
pub inline fn latin1(self: String) []const u8 {
|
||||
if (self.tag == .Empty)
|
||||
return &[_]u8{};
|
||||
|
||||
std.debug.assert(self.tag == .WTFStringImpl);
|
||||
return self.value.WTFStringImpl.latin1Slice();
|
||||
if (self.tag == .WTFStringImpl) {
|
||||
return self.value.WTFStringImpl.latin1Slice();
|
||||
}
|
||||
|
||||
return self.toZigString().slice();
|
||||
}
|
||||
|
||||
pub fn isUTF8(self: String) bool {
|
||||
@@ -637,6 +643,30 @@ pub const String = extern struct {
|
||||
return self.value.ZigString.isUTF8();
|
||||
}
|
||||
|
||||
pub inline fn asUTF8(self: String) ?[]const u8 {
|
||||
if (self.tag == .WTFStringImpl) {
|
||||
if (self.value.WTFStringImpl.is8Bit() and bun.strings.isAllASCII(self.value.WTFStringImpl.latin1Slice())) {
|
||||
return self.value.WTFStringImpl.latin1Slice();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if (self.tag == .ZigString or self.tag == .StaticZigString) {
|
||||
if (self.value.ZigString.isUTF8()) {
|
||||
return self.value.ZigString.slice();
|
||||
}
|
||||
|
||||
if (bun.strings.isAllASCII(self.toZigString().slice())) {
|
||||
return self.value.ZigString.slice();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
pub fn encoding(self: String) bun.strings.Encoding {
|
||||
if (self.isUTF16()) {
|
||||
return .utf16;
|
||||
@@ -707,8 +737,13 @@ pub const String = extern struct {
|
||||
if (self.tag == .WTFStringImpl)
|
||||
return self.value.WTFStringImpl.is8Bit() and bun.strings.isAllASCII(self.value.WTFStringImpl.latin1Slice());
|
||||
|
||||
if (self.tag == .ZigString or self.tag == .StaticZigString)
|
||||
return self.value.ZigString.isUTF8();
|
||||
if (self.tag == .ZigString or self.tag == .StaticZigString) {
|
||||
if (self.value.ZigString.isUTF8()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return bun.strings.isAllASCII(self.toZigString().slice());
|
||||
}
|
||||
|
||||
return self.tag == .Empty;
|
||||
}
|
||||
@@ -898,6 +933,16 @@ pub const String = extern struct {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn visibleWidth(this: *const String) usize {
|
||||
if (this.isUTF8()) {
|
||||
return bun.strings.visibleUTF8Width(this.utf8());
|
||||
} else if (this.isUTF16()) {
|
||||
return bun.strings.visibleUTF16Width(this.utf16());
|
||||
} else {
|
||||
return bun.strings.visibleLatin1Width(this.latin1());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn indexOfComptimeWithCheckLen(this: String, comptime values: []const []const u8, comptime check_len: usize) ?usize {
|
||||
if (this.is8Bit()) {
|
||||
const bytes = this.byteSlice();
|
||||
@@ -1091,6 +1136,25 @@ pub const String = extern struct {
|
||||
pub inline fn createFromConcat(allocator: std.mem.Allocator, strings: anytype) !String {
|
||||
return try concat(strings.len, allocator, strings);
|
||||
}
|
||||
|
||||
pub export fn BunString__getStringWidth(globalObject: *JSC.JSGlobalObject, callFrame: *JSC.CallFrame) callconv(.C) JSC.JSValue {
|
||||
var str: String = String.dead;
|
||||
|
||||
const args = callFrame.arguments(1).slice();
|
||||
|
||||
if (args.len == 0 or !args.ptr[0].isString()) {
|
||||
return JSC.jsNumber(@as(i32, 0));
|
||||
}
|
||||
|
||||
str = args[0].toBunString(globalObject);
|
||||
|
||||
if (str.isEmpty()) {
|
||||
return JSC.jsNumber(@as(i32, 0));
|
||||
}
|
||||
|
||||
const width = str.visibleWidth();
|
||||
return JSC.jsNumber(width);
|
||||
}
|
||||
};
|
||||
|
||||
pub const SliceWithUnderlyingString = struct {
|
||||
|
||||
Reference in New Issue
Block a user