Merge pull request #898 from der-teufel-programming/const-vars

change unmodified `var`s to `const`s in anticipation of zig changes
This commit is contained in:
Mitchell Hashimoto
2023-11-17 07:46:58 -08:00
committed by GitHub
29 changed files with 414 additions and 414 deletions

View File

@ -143,14 +143,14 @@ pub fn build(b: *std.Build) !void {
break :patch_rpath env.get("LD_LIBRARY_PATH");
};
var version_string = b.option(
const version_string = b.option(
[]const u8,
"version-string",
"A specific version string to use for the build. " ++
"If not specified, git will be used. This must be a semantic version.",
);
var version: std.SemanticVersion = if (version_string) |v|
const version: std.SemanticVersion = if (version_string) |v|
try std.SemanticVersion.parse(v)
else version: {
const vsn = try Version.detect(b);
@ -589,7 +589,7 @@ pub fn build(b: *std.Build) !void {
// Tests
{
const test_step = b.step("test", "Run all tests");
var test_filter = b.option([]const u8, "test-filter", "Filter for test");
const test_filter = b.option([]const u8, "test-filter", "Filter for test");
const main_test = b.addTest(.{
.name = "ghostty-test",

View File

@ -43,7 +43,7 @@ test "fc-match" {
const fonts = result.fs.fonts();
try testing.expect(fonts.len > 0);
for (fonts) |font| {
var pat_prep = try cfg.fontRenderPrepare(pat, font);
const pat_prep = try cfg.fontRenderPrepare(pat, font);
try testing.expect(fs.add(pat_prep));
}
result.fs.destroy();

View File

@ -28,7 +28,7 @@ pub const Data = opaque {
test {
//const testing = std.testing;
var raw = "hello world";
const raw = "hello world";
const data = try Data.createWithBytesNoCopy(raw);
defer data.release();
}

View File

@ -169,7 +169,7 @@ test "create and destroy" {
const stride = format.strideForWidth(width);
const len = height * @as(usize, @intCast(stride));
var data = try alloc.alloc(u32, len);
const data = try alloc.alloc(u32, len);
defer alloc.free(data);
@memset(data, 0);
const img = try Image.createBitsNoClear(.g1, width, height, data.ptr, stride);
@ -191,7 +191,7 @@ test "fill boxes a1" {
// Image
const len = height * @as(usize, @intCast(stride));
var data = try alloc.alloc(u32, len);
const data = try alloc.alloc(u32, len);
defer alloc.free(data);
@memset(data, 0);
const img = try Image.createBitsNoClear(format, width, height, data.ptr, stride);

View File

@ -310,7 +310,7 @@ pub fn wait(self: Command, block: bool) !Exit {
}
var exit_code: windows.DWORD = undefined;
var has_code = windows.kernel32.GetExitCodeProcess(self.pid.?, &exit_code) != 0;
const has_code = windows.kernel32.GetExitCodeProcess(self.pid.?, &exit_code) != 0;
if (!has_code) {
return windows.unexpectedError(windows.kernel32.GetLastError());
}

View File

@ -422,7 +422,7 @@ pub fn init(
);
// The mutex used to protect our renderer state.
var mutex = try alloc.create(std.Thread.Mutex);
const mutex = try alloc.create(std.Thread.Mutex);
mutex.* = .{};
errdefer alloc.destroy(mutex);
@ -604,7 +604,7 @@ pub fn activateInspector(self: *Surface) !void {
if (self.inspector != null) return;
// Setup the inspector
var ptr = try self.alloc.create(inspector.Inspector);
const ptr = try self.alloc.create(inspector.Inspector);
errdefer self.alloc.destroy(ptr);
ptr.* = try inspector.Inspector.init(self);
self.inspector = ptr;
@ -895,7 +895,7 @@ fn setSelection(self: *Surface, sel_: ?terminal.Selection) void {
}
}
var buf = self.io.terminal.screen.selectionString(
const buf = self.io.terminal.screen.selectionString(
self.alloc,
sel,
self.config.clipboard_trim_trailing_spaces,
@ -2314,7 +2314,7 @@ pub fn performBindingAction(self: *Surface, action: input.Binding.Action) !bool
// We can read from the renderer state without holding
// the lock because only we will write to this field.
if (self.io.terminal.screen.selection) |sel| {
var buf = self.io.terminal.screen.selectionString(
const buf = self.io.terminal.screen.selectionString(
self.alloc,
sel,
self.config.clipboard_trim_trailing_spaces,

View File

@ -353,7 +353,7 @@ pub const Surface = struct {
if (self.inspector) |v| return v;
const alloc = self.app.core_app.alloc;
var inspector = try alloc.create(Inspector);
const inspector = try alloc.create(Inspector);
errdefer alloc.destroy(inspector);
inspector.* = try Inspector.init(self);
self.inspector = inspector;

View File

@ -49,7 +49,7 @@ pub fn CircBuf(comptime T: type, comptime default: T) type {
/// Initialize a new circular buffer that can store size elements.
pub fn init(alloc: Allocator, size: usize) !Self {
var buf = try alloc.alloc(T, size);
const buf = try alloc.alloc(T, size);
@memset(buf, default);
return Self{

View File

@ -1967,7 +1967,7 @@ pub const Keybinds = struct {
pub fn parseCLI(self: *Keybinds, alloc: Allocator, input: ?[]const u8) !void {
var copy: ?[]u8 = null;
var value = value: {
const value = value: {
const value = input orelse return error.ValueRequired;
// If we don't have a colon, use the value as-is, no copy

View File

@ -124,7 +124,7 @@ pub fn reserve(self: *Atlas, alloc: Allocator, width: u32, height: u32) !Region
if (width == 0 and height == 0) return region;
// Find the location in our nodes list to insert the new node for this region.
var best_idx: usize = best_idx: {
const best_idx: usize = best_idx: {
var best_height: u32 = std.math.maxInt(u32);
var best_width: u32 = best_height;
var chosen: ?usize = null;
@ -395,7 +395,7 @@ pub const Wasm = struct {
defer ctx.deinit();
// We need to draw pixels so this is format dependent.
var buf: []u8 = switch (self.format) {
const buf: []u8 = switch (self.format) {
// RGBA is the native ImageData format
.rgba => self.data,
@ -454,7 +454,7 @@ pub const Wasm = struct {
}
test "happy path" {
var atlas = atlas_new(512, @intFromEnum(Format.greyscale)).?;
const atlas = atlas_new(512, @intFromEnum(Format.greyscale)).?;
defer atlas_free(atlas);
const reg = atlas_reserve(atlas, 2, 2).?;

View File

@ -341,7 +341,7 @@ pub const Wasm = struct {
}
fn deferred_face_new_(ptr: [*]const u8, len: usize, presentation: u16) !*DeferredFace {
var font_str = try alloc.dupeZ(u8, ptr[0..len]);
const font_str = try alloc.dupeZ(u8, ptr[0..len]);
errdefer alloc.free(font_str);
var face: DeferredFace = .{
@ -353,7 +353,7 @@ pub const Wasm = struct {
};
errdefer face.deinit();
var result = try alloc.create(DeferredFace);
const result = try alloc.create(DeferredFace);
errdefer alloc.destroy(result);
result.* = face;
return result;

View File

@ -662,7 +662,7 @@ pub const Wasm = struct {
var group = try Group.init(alloc, .{}, .{ .points = pts });
errdefer group.deinit();
var result = try alloc.create(Group);
const result = try alloc.create(Group);
errdefer alloc.destroy(result);
result.* = group;
return result;
@ -749,7 +749,7 @@ pub const Wasm = struct {
.max_height = max_height,
});
var result = try alloc.create(Glyph);
const result = try alloc.create(Glyph);
errdefer alloc.destroy(result);
result.* = glyph;
return result;

View File

@ -244,7 +244,7 @@ pub const Wasm = struct {
var gc = try GroupCache.init(alloc, group.*);
errdefer gc.deinit(alloc);
var result = try alloc.create(GroupCache);
const result = try alloc.create(GroupCache);
errdefer alloc.destroy(result);
result.* = gc;
return result;
@ -304,7 +304,7 @@ pub const Wasm = struct {
.max_height = max_height,
});
var result = try alloc.create(Glyph);
const result = try alloc.create(Glyph);
errdefer alloc.destroy(result);
result.* = glyph;
return result;

View File

@ -301,7 +301,7 @@ pub const Face = struct {
// usually stabilizes pretty quickly and is very infrequent so I think
// the allocation overhead is acceptable compared to the cost of
// caching it forever or having to deal with a cache lifetime.
var buf = try alloc.alloc(u8, width * height * color.depth);
const buf = try alloc.alloc(u8, width * height * color.depth);
defer alloc.free(buf);
@memset(buf, 0);

View File

@ -389,7 +389,7 @@ pub const Face = struct {
// If we need to copy the data, we copy it into a temporary buffer.
const buffer = if (needs_copy) buffer: {
var temp = try alloc.alloc(u8, tgt_w * tgt_h * depth);
const temp = try alloc.alloc(u8, tgt_w * tgt_h * depth);
var dst_ptr = temp;
var src_ptr = bitmap.buffer;
var i: usize = 0;

View File

@ -302,7 +302,7 @@ pub const Face = struct {
}
// Set our context font
var font_val = try std.fmt.allocPrint(
const font_val = try std.fmt.allocPrint(
self.alloc,
"{d}px {s}",
.{ self.size.points, self.font_str },
@ -450,7 +450,7 @@ pub const Face = struct {
// Allocate our local memory to copy the data to.
const len = try src_array.get(u32, "length");
var bitmap = try alloc.alloc(u8, @intCast(len));
const bitmap = try alloc.alloc(u8, @intCast(len));
errdefer alloc.free(bitmap);
// Create our target Uint8Array that we can use to copy from src.
@ -506,7 +506,7 @@ pub const Wasm = struct {
);
errdefer face.deinit();
var result = try alloc.create(Face);
const result = try alloc.create(Face);
errdefer alloc.destroy(result);
result.* = face;
return result;

View File

@ -239,13 +239,13 @@ pub const Wasm = struct {
}
fn shaper_new_(cap: usize) !*Shaper {
var cell_buf = try alloc.alloc(font.shape.Cell, cap);
const cell_buf = try alloc.alloc(font.shape.Cell, cap);
errdefer alloc.free(cell_buf);
var shaper = try Shaper.init(alloc, .{ .cell_buf = cell_buf });
errdefer shaper.deinit();
var result = try alloc.create(Shaper);
const result = try alloc.create(Shaper);
errdefer alloc.destroy(result);
result.* = shaper;
return result;

View File

@ -241,7 +241,7 @@ const WebCanvasImpl = struct {
// Allocate our local memory to copy the data to.
const len = try src_array.get(u32, "length");
var bitmap = try alloc.alloc(u8, @intCast(len));
const bitmap = try alloc.alloc(u8, @intCast(len));
errdefer alloc.free(bitmap);
// Create our target Uint8Array that we can use to copy from src.
@ -314,7 +314,7 @@ const PixmanImpl = struct {
// Allocate our buffer. pixman uses []u32 so we divide our length
// by 4 since u32 / u8 = 4.
var data = try alloc.alloc(u32, len / 4);
const data = try alloc.alloc(u32, len / 4);
errdefer alloc.free(data);
@memset(data, 0);
@ -385,7 +385,7 @@ const PixmanImpl = struct {
// If we need to copy the data, we copy it into a temporary buffer.
const buffer = if (needs_copy) buffer: {
var temp = try alloc.alloc(u8, width * height * depth);
const temp = try alloc.alloc(u8, width * height * depth);
var dst_ptr = temp;
var src_ptr = data.ptr;
var i: usize = 0;

View File

@ -120,7 +120,7 @@ pub fn HashMap(
// reusing the node we would've evicted.
var node = if (!evict) try alloc.create(Queue.Node) else node: {
// Our first node is the least recently used.
var least_used = self.queue.first.?;
const least_used = self.queue.first.?;
// Move our least recently used to the end to make
// it the most recently used.
@ -186,7 +186,7 @@ pub fn HashMap(
var i: Map.Size = 0;
while (i < delta) : (i += 1) {
var node = self.queue.first.?;
const node = self.queue.first.?;
evicted[i] = node.data.value;
self.queue.remove(node);
_ = self.map.remove(node.data.key);

View File

@ -94,7 +94,7 @@ pub fn toModuleOwned(ptr: anytype) void {
test "basics" {
const testing = std.testing;
var buf = malloc(32).?;
const buf = malloc(32).?;
try testing.expect(allocs.size == 1);
free(buf);
try testing.expect(allocs.size == 0);
@ -104,7 +104,7 @@ test "toHostOwned" {
const testing = std.testing;
const Point = struct { x: u32 = 0, y: u32 = 0 };
var p = try alloc.create(Point);
const p = try alloc.create(Point);
errdefer alloc.destroy(p);
const ptr = try toHostOwned(p);
try testing.expect(allocs.size == 1);

View File

@ -1305,7 +1305,7 @@ pub fn updateCell(
// The colors for the cell.
const colors: BgFg = colors: {
// If we are selected, we our colors are just inverted fg/bg
var selection_res: ?BgFg = if (selected) .{
const selection_res: ?BgFg = if (selected) .{
.bg = self.config.selection_background orelse self.foreground_color,
.fg = self.config.selection_foreground orelse self.background_color,
} else null;

View File

@ -1087,7 +1087,7 @@ pub fn updateCell(
// The colors for the cell.
const colors: BgFg = colors: {
// If we are selected, we our colors are just inverted fg/bg
var selection_res: ?BgFg = if (selected) .{
const selection_res: ?BgFg = if (selected) .{
.bg = self.config.selection_background orelse self.foreground_color,
.fg = self.config.selection_foreground orelse self.background_color,
} else null;

View File

@ -67,8 +67,8 @@ test "SegmentedPool" {
try testing.expectEqual(@as(usize, 2), list.available);
// Get to capacity
var v1 = try list.get();
var v2 = try list.get();
const v1 = try list.get();
const v2 = try list.get();
try testing.expect(v1 != v2);
try testing.expectError(error.OutOfValues, list.get());
@ -77,13 +77,13 @@ test "SegmentedPool" {
// Put a value back
list.put();
var temp = try list.get();
const temp = try list.get();
try testing.expect(v1 == temp);
try testing.expect(temp.* == 42);
try testing.expectError(error.OutOfValues, list.get());
// Grow
var v3 = try list.getGrow(testing.allocator);
const v3 = try list.getGrow(testing.allocator);
try testing.expect(v1 != v3 and v2 != v3);
_ = try list.get();
try testing.expectError(error.OutOfValues, list.get());

File diff suppressed because it is too large Load Diff

View File

@ -215,7 +215,7 @@ test "Tabstops: count on 80" {
defer t.deinit(testing.allocator);
// Count the tabstops
var count: usize = count: {
const count: usize = count: {
var v: usize = 0;
var i: usize = 0;
while (i < 80) : (i += 1) {

File diff suppressed because it is too large Load Diff

View File

@ -454,7 +454,7 @@ pub const Rect = struct {
/// Easy base64 encoding function.
fn testB64(alloc: Allocator, data: []const u8) ![]const u8 {
const B64Encoder = std.base64.standard.Encoder;
var b64 = try alloc.alloc(u8, B64Encoder.calcSize(data.len));
const b64 = try alloc.alloc(u8, B64Encoder.calcSize(data.len));
errdefer alloc.free(b64);
return B64Encoder.encode(b64, data);
}
@ -462,7 +462,7 @@ fn testB64(alloc: Allocator, data: []const u8) ![]const u8 {
/// Easy base64 decoding function.
fn testB64Decode(alloc: Allocator, data: []const u8) ![]const u8 {
const B64Decoder = std.base64.standard.Decoder;
var result = try alloc.alloc(u8, try B64Decoder.calcSizeForSlice(data));
const result = try alloc.alloc(u8, try B64Decoder.calcSizeForSlice(data));
errdefer alloc.free(result);
try B64Decoder.decode(result, data);
return result;

View File

@ -855,7 +855,7 @@ const Subprocess = struct {
// We have to copy the cwd because there is no guarantee that
// pointers in full_config remain valid.
var cwd: ?[]u8 = if (opts.full_config.@"working-directory") |cwd|
const cwd: ?[]u8 = if (opts.full_config.@"working-directory") |cwd|
try alloc.dupe(u8, cwd)
else
null;
@ -2096,7 +2096,7 @@ const StreamHandler = struct {
build_config.version_string,
},
);
var msg = try termio.Message.writeReq(self.alloc, resp);
const msg = try termio.Message.writeReq(self.alloc, resp);
self.messageWriter(msg);
}

View File

@ -140,7 +140,7 @@ pub fn MessageData(comptime Elem: type, comptime small_size: comptime_int) type
}
// Otherwise, allocate
var buf = try alloc.dupe(Elem, data);
const buf = try alloc.dupe(Elem, data);
errdefer alloc.free(buf);
return Self{
.alloc = .{