windows: random things (#9046)

* random things

* fix reliability of loading napi stuff

* fix posix build

* a
This commit is contained in:
dave caruso
2024-02-27 16:30:34 -08:00
committed by GitHub
parent c9b5191fc2
commit c54844b30b
11 changed files with 97 additions and 38 deletions

View File

@@ -17,6 +17,13 @@ pub const Encoding = enum {
utf16,
};
/// Returned by classification functions that do not discriminate between utf8 and ascii.
pub const EncodingNonAscii = enum {
utf8,
utf16,
latin1,
};
pub inline fn containsChar(self: string, char: u8) bool {
return indexOfChar(self, char) != null;
}
@@ -1774,17 +1781,8 @@ pub fn toWPathNormalizeAutoExtend(wbuf: []u16, utf8: []const u8) [:0]const u16 {
pub fn toWPathNormalized(wbuf: []u16, utf8: []const u8) [:0]const u16 {
var renormalized: [bun.MAX_PATH_BYTES]u8 = undefined;
var path_to_use = utf8;
if (bun.strings.containsChar(utf8, '/')) {
@memcpy(renormalized[0..utf8.len], utf8);
for (renormalized[0..utf8.len]) |*c| {
if (c.* == '/') {
c.* = '\\';
}
}
path_to_use = renormalized[0..utf8.len];
}
var path_to_use = normalizeSlashesOnly(&renormalized, utf8, '\\');
// is there a trailing slash? Let's remove it before converting to UTF-16
if (path_to_use.len > 3 and bun.path.isSepAny(path_to_use[path_to_use.len - 1])) {
@@ -1794,6 +1792,23 @@ pub fn toWPathNormalized(wbuf: []u16, utf8: []const u8) [:0]const u16 {
return toWPath(wbuf, path_to_use);
}
pub fn normalizeSlashesOnly(buf: []u8, utf8: []const u8, comptime desired_slash: u8) []const u8 {
comptime std.debug.assert(desired_slash == '/' or desired_slash == '\\');
const undesired_slash = if (desired_slash == '/') '\\' else '/';
if (bun.strings.containsChar(utf8, undesired_slash)) {
@memcpy(buf[0..utf8.len], utf8);
for (buf[0..utf8.len]) |*c| {
if (c.* == undesired_slash) {
c.* = desired_slash;
}
}
return buf[0..utf8.len];
}
return utf8;
}
pub fn toWDirNormalized(wbuf: []u16, utf8: []const u8) [:0]const u16 {
var renormalized: [bun.MAX_PATH_BYTES]u8 = undefined;
var path_to_use = utf8;