| ... | ... | @@ -2708,9 +2708,22 @@ pub fn cmdFmt(gpa: *Allocator, args: []const []const u8) !void { |
| 2708 | 2708 | fatal("cannot use --stdin with positional arguments", .{}); |
| 2709 | 2709 | } |
| 2710 | 2710 | |
| 2711 | | const stdin = io.getStdIn().reader(); |
| 2712 | | |
| 2713 | | const source_code = try stdin.readAllAlloc(gpa, max_src_size); |
| 2711 | const stdin = io.getStdIn(); |
| 2712 | |
| 2713 | const source_code = blk: { |
| 2714 | const source_code = try stdin.readToEndAllocOptions(gpa, max_src_size, null, @alignOf(u16), null); |
| 2715 | errdefer gpa.free(source_code); |
| 2716 | |
| 2717 | // If the file starts with a UTF-16 BOM, translate it to UTF-8 |
| 2718 | if (mem.startsWith(u8, source_code, "\xff\xfe")) { |
| 2719 | const source_code_utf16_le = mem.bytesAsSlice(u16, source_code); |
| 2720 | const source_code_utf8 = try std.unicode.utf16leToUtf8Alloc(gpa, source_code_utf16_le); |
| 2721 | gpa.free(source_code); |
| 2722 | break :blk source_code_utf8; |
| 2723 | } else { |
| 2724 | break :blk source_code; |
| 2725 | } |
| 2726 | }; |
| 2714 | 2727 | defer gpa.free(source_code); |
| 2715 | 2728 | |
| 2716 | 2729 | var tree = std.zig.parse(gpa, source_code) catch |err| { |
| ... | ... | @@ -2785,6 +2798,7 @@ const FmtError = error{ |
| 2785 | 2798 | EndOfStream, |
| 2786 | 2799 | Unseekable, |
| 2787 | 2800 | NotOpenForWriting, |
| 2801 | UnknownTextFormat, |
| 2788 | 2802 | } || fs.File.OpenError; |
| 2789 | 2803 | |
| 2790 | 2804 | fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void { |
| ... | ... | @@ -2850,20 +2864,38 @@ fn fmtPathFile( |
| 2850 | 2864 | if (stat.kind == .Directory) |
| 2851 | 2865 | return error.IsDir; |
| 2852 | 2866 | |
| 2853 | | const source_code = source_file.readToEndAllocOptions( |
| 2854 | | fmt.gpa, |
| 2855 | | max_src_size, |
| 2856 | | std.math.cast(usize, stat.size) catch return error.FileTooBig, |
| 2857 | | @alignOf(u8), |
| 2858 | | null, |
| 2859 | | ) catch |err| switch (err) { |
| 2860 | | error.ConnectionResetByPeer => unreachable, |
| 2861 | | error.ConnectionTimedOut => unreachable, |
| 2862 | | error.NotOpenForReading => unreachable, |
| 2863 | | else => |e| return e, |
| 2867 | const source_code = blk: { |
| 2868 | const source_code = source_file.readToEndAllocOptions( |
| 2869 | fmt.gpa, |
| 2870 | max_src_size, |
| 2871 | std.math.cast(usize, stat.size) catch return error.FileTooBig, |
| 2872 | @alignOf(u16), |
| 2873 | null, |
| 2874 | ) catch |err| switch (err) { |
| 2875 | error.ConnectionResetByPeer => unreachable, |
| 2876 | error.ConnectionTimedOut => unreachable, |
| 2877 | error.NotOpenForReading => unreachable, |
| 2878 | else => |e| return e, |
| 2879 | }; |
| 2880 | source_file.close(); |
| 2881 | file_closed = true; |
| 2882 | errdefer fmt.gpa.free(source_code); |
| 2883 | |
| 2884 | // If the file starts with a UTF-16 BOM, translate it to UTF-8 |
| 2885 | if (mem.eql(u8, source_code[0..2], "\xff\xfe")) { |
| 2886 | const source_code_utf16_le = mem.bytesAsSlice(u16, source_code); |
| 2887 | const source_code_utf8 = std.unicode.utf16leToUtf8Alloc(fmt.gpa, source_code_utf16_le) catch |err| return switch (err) { |
| 2888 | error.DanglingSurrogateHalf => FmtError.UnknownTextFormat, |
| 2889 | error.ExpectedSecondSurrogateHalf => FmtError.UnknownTextFormat, |
| 2890 | error.UnexpectedSecondSurrogateHalf => FmtError.UnknownTextFormat, |
| 2891 | else => |e| e, |
| 2892 | }; |
| 2893 | fmt.gpa.free(source_code); |
| 2894 | break :blk source_code_utf8; |
| 2895 | } else { |
| 2896 | break :blk source_code; |
| 2897 | } |
| 2864 | 2898 | }; |
| 2865 | | source_file.close(); |
| 2866 | | file_closed = true; |
| 2867 | 2899 | defer fmt.gpa.free(source_code); |
| 2868 | 2900 | |
| 2869 | 2901 | // Add to set after no longer possible to get error.IsDir. |