mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 10:58:56 +00:00
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> Co-authored-by: Grigory <grigory.orlov.set@gmail.com> Co-authored-by: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Co-authored-by: Meghan Denny <hello@nektro.net> Co-authored-by: Kenta Iwasaki <63115601+lithdew@users.noreply.github.com> Co-authored-by: John-David Dalton <john.david.dalton@gmail.com> Co-authored-by: Dale Seo <5466341+DaleSeo@users.noreply.github.com> Co-authored-by: Zack Radisic <56137411+zackradisic@users.noreply.github.com> Co-authored-by: paperdave <paperdave@users.noreply.github.com> Co-authored-by: Georgijs Vilums <georgijs.vilums@gmail.com> Co-authored-by: Dylan Conway <dylan.conway567@gmail.com>
90 lines
2.5 KiB
Zig
90 lines
2.5 KiB
Zig
/// Returns true if the passed type will coerce to []const u8.
|
|
/// Any of the following are considered strings:
|
|
/// ```
|
|
/// []const u8, [:S]const u8, *const [N]u8, *const [N:S]u8,
|
|
/// []u8, [:S]u8, *[:S]u8, *[N:S]u8.
|
|
/// ```
|
|
/// These types are not considered strings:
|
|
/// ```
|
|
/// u8, [N]u8, [*]const u8, [*:0]const u8,
|
|
/// [*]const [N]u8, []const u16, []const i8,
|
|
/// *const u8, ?[]const u8, ?*const [N]u8.
|
|
/// ```
|
|
pub inline fn isZigString(comptime T: type) bool {
|
|
return comptime blk: {
|
|
// Only pointer types can be strings, no optionals
|
|
const info = @typeInfo(T);
|
|
if (info != .Pointer) break :blk false;
|
|
|
|
const ptr = &info.Pointer;
|
|
// Check for CV qualifiers that would prevent coerction to []const u8
|
|
if (ptr.is_volatile or ptr.is_allowzero) break :blk false;
|
|
|
|
// If it's already a slice, simple check.
|
|
if (ptr.size == .Slice) {
|
|
break :blk ptr.child == u8;
|
|
}
|
|
|
|
// Otherwise check if it's an array type that coerces to slice.
|
|
if (ptr.size == .One) {
|
|
const child = @typeInfo(ptr.child);
|
|
if (child == .Array) {
|
|
const arr = &child.Array;
|
|
break :blk arr.child == u8;
|
|
}
|
|
}
|
|
|
|
break :blk false;
|
|
};
|
|
}
|
|
|
|
pub inline fn isSlice(comptime T: type) bool {
|
|
const info = @typeInfo(T);
|
|
return info == .Pointer and info.Pointer.size == .Slice;
|
|
}
|
|
|
|
pub inline fn isNumber(comptime T: type) bool {
|
|
return switch (@typeInfo(T)) {
|
|
.Int, .Float, .ComptimeInt, .ComptimeFloat => true,
|
|
else => false,
|
|
};
|
|
}
|
|
|
|
pub inline fn isContainer(comptime T: type) bool {
|
|
return switch (@typeInfo(T)) {
|
|
.Struct, .Enum, .Opaque, .Union => true,
|
|
else => false,
|
|
};
|
|
}
|
|
|
|
pub inline fn isSingleItemPtr(comptime T: type) bool {
|
|
const info = @typeInfo(T);
|
|
return info == .Pointer and .Pointer.size == .One;
|
|
}
|
|
|
|
pub fn isExternContainer(comptime T: type) bool {
|
|
return switch (@typeInfo(T)) {
|
|
.Struct => |s| s.layout == .@"extern",
|
|
.Union => |u| u.layout == .@"extern",
|
|
else => false,
|
|
};
|
|
}
|
|
|
|
pub fn isConstPtr(comptime T: type) bool {
|
|
const info = @typeInfo(T);
|
|
return info == .Pointer and info.Pointer.is_const;
|
|
}
|
|
|
|
pub fn isIndexable(comptime T: type) bool {
|
|
const info = @typeInfo(T);
|
|
return switch (info) {
|
|
.Pointer => |ptr| switch (ptr.size) {
|
|
.One => @typeInfo(ptr.child) == .Array,
|
|
else => true,
|
|
},
|
|
.Array, .Vector => true,
|
|
.Struct => |s| s.is_tuple,
|
|
else => false,
|
|
};
|
|
}
|