From 7a5fa0bb2e8d2961ed2c562c88b14f72bfcba22d Mon Sep 17 00:00:00 2001 From: melhindi Date: Thu, 18 Jan 2024 13:43:45 +0100 Subject: [PATCH] Replace var with const where applicable --- Package.zig | 4 ++-- codegen/src/main.zig | 12 ++++++------ src/aws.zig | 4 ++-- src/aws_credentials.zig | 10 +++++----- src/aws_http.zig | 2 +- src/aws_signing.zig | 14 +++++++------- src/date.zig | 16 ++++++++-------- src/json.zig | 2 +- src/xml.zig | 2 +- 9 files changed, 33 insertions(+), 33 deletions(-) diff --git a/Package.zig b/Package.zig index eea3212..644a097 100644 --- a/Package.zig +++ b/Package.zig @@ -33,7 +33,7 @@ pub fn fetchOneAndUnpack( var http_client: std.http.Client = .{ .allocator = allocator }; defer http_client.deinit(); - var global_cache_directory: std.Build.Cache.Directory = .{ + const global_cache_directory: std.Build.Cache.Directory = .{ .handle = try std.fs.cwd().makeOpenPath(cache_directory, .{}), .path = cache_directory, }; @@ -464,7 +464,7 @@ test "fetch and unpack" { var http_client: std.http.Client = .{ .allocator = alloc }; defer http_client.deinit(); - var global_cache_directory: std.Build.Cache.Directory = .{ + const global_cache_directory: std.Build.Cache.Directory = .{ .handle = try std.fs.cwd().makeOpenPath("test-pkg", .{}), .path = "test-pkg", }; diff --git a/codegen/src/main.zig b/codegen/src/main.zig index 3fcc457..18709ef 100644 --- a/codegen/src/main.zig +++ b/codegen/src/main.zig @@ -37,7 +37,7 @@ pub fn main() anyerror!void { try output_dir.writeFile("json.zig", json_zig); // TODO: We need a different way to handle this file... - var manifest_file_started = false; + const manifest_file_started = false; var manifest_file: std.fs.File = undefined; defer if (manifest_file_started) manifest_file.close(); var manifest: std.fs.File.Writer = undefined; @@ -201,7 +201,7 @@ fn processFile(file_name: []const u8, output_dir: std.fs.Dir, manifest: anytype) defer allocator.free(output_file_name); for (service_names) |name| { const seperator = if (output_file_name.len > 0) "-" else ""; - var new_output_file_name = try std.fmt.allocPrint( + const new_output_file_name = try std.fmt.allocPrint( allocator, "{s}{s}{s}", .{ output_file_name, seperator, name }, @@ -211,7 +211,7 @@ fn processFile(file_name: []const u8, output_dir: std.fs.Dir, manifest: anytype) } { // append .zig on to the file name - var new_output_file_name = try std.fmt.allocPrint( + const new_output_file_name = try std.fmt.allocPrint( allocator, "{s}.zig", .{output_file_name}, @@ -337,7 +337,7 @@ fn generateServices(allocator: std.mem.Allocator, comptime _: []const u8, file: var generated = std.StringHashMap(void).init(allocator); defer generated.deinit(); - var state = FileGenerationState{ + const state = FileGenerationState{ .shape_references = shape_references, .additional_types_to_generate = &unresolved, .additional_types_generated = &generated, @@ -345,8 +345,8 @@ fn generateServices(allocator: std.mem.Allocator, comptime _: []const u8, file: }; for (services.items) |service| { var sdk_id: []const u8 = undefined; - var version: []const u8 = service.shape.service.version; - var name: []const u8 = service.name; + const version: []const u8 = service.shape.service.version; + const name: []const u8 = service.name; var arn_namespace: []const u8 = undefined; var sigv4_name: []const u8 = undefined; var endpoint_prefix: []const u8 = undefined; diff --git a/src/aws.zig b/src/aws.zig index fcf2bbb..c8487bf 100644 --- a/src/aws.zig +++ b/src/aws.zig @@ -365,7 +365,7 @@ pub fn Request(comptime request_action: anytype) type { .raw_parsed = .{ .raw = .{} }, .allocator = options.client.allocator, }; - var body_field = @field(rc.response, action.Response.http_payload); + const body_field = @field(rc.response, action.Response.http_payload); const BodyField = @TypeOf(body_field); if (BodyField == []const u8 or BodyField == ?[]const u8) { expected_body_field_len = 0; @@ -875,7 +875,7 @@ fn FullResponse(comptime action: anytype) type { } } if (@hasDecl(Response, "http_payload")) { - var body_field = @field(self.response, Response.http_payload); + const body_field = @field(self.response, Response.http_payload); const BodyField = @TypeOf(body_field); if (BodyField == []const u8) { self.allocator.free(body_field); diff --git a/src/aws_credentials.zig b/src/aws_credentials.zig index f3fc8ab..166fe88 100644 --- a/src/aws_credentials.zig +++ b/src/aws_credentials.zig @@ -140,7 +140,7 @@ fn getContainerCredentials(allocator: std.mem.Allocator) !?auth.Credentials { var resp_payload = try std.ArrayList(u8).initCapacity(allocator, @intCast(req.response.content_length.?)); defer resp_payload.deinit(); try resp_payload.resize(@intCast(req.response.content_length.?)); - var response_data = try resp_payload.toOwnedSlice(); + const response_data = try resp_payload.toOwnedSlice(); defer allocator.free(response_data); _ = try req.readAll(response_data); log.debug("Read {d} bytes from container credentials endpoint", .{response_data.len}); @@ -243,7 +243,7 @@ fn getImdsRoleName(allocator: std.mem.Allocator, client: *std.http.Client, imds_ log.warn("Unexpected empty response from IMDS endpoint post token", .{}); return null; } - var resp = try allocator.alloc(u8, @intCast(req.response.content_length.?)); + const resp = try allocator.alloc(u8, @intCast(req.response.content_length.?)); defer allocator.free(resp); _ = try req.readAll(resp); @@ -296,7 +296,7 @@ fn getImdsCredentials(allocator: std.mem.Allocator, client: *std.http.Client, ro log.warn("Unexpected empty response from IMDS role endpoint", .{}); return null; } - var resp = try allocator.alloc(u8, @intCast(req.response.content_length.?)); + const resp = try allocator.alloc(u8, @intCast(req.response.content_length.?)); defer allocator.free(resp); _ = try req.readAll(resp); @@ -455,7 +455,7 @@ const LineIterator = struct { pub fn next(self: *Self) ?[]const u8 { if (self.inx >= self.text.len) return null; var current = self.inx; - var start = self.inx; + const start = self.inx; for (self.text[self.inx..], 0..) |c, i| { if (c == '\n') { // log.debug("got \\n: {d}", .{i}); @@ -571,7 +571,7 @@ const EvaluatedPath = struct { evaluated_path: []const u8, }; fn getDefaultPath(allocator: std.mem.Allocator, home_dir: ?[]const u8, dir: []const u8, file: []const u8) !EvaluatedPath { - var home = home_dir orelse try getHomeDir(allocator); + const home = home_dir orelse try getHomeDir(allocator); log.debug("Home directory: {s}", .{home}); const rc = try std.fs.path.join(allocator, &[_][]const u8{ home, dir, file }); log.debug("Path evaluated as: {s}", .{rc}); diff --git a/src/aws_http.zig b/src/aws_http.zig index b3f886e..31e84eb 100644 --- a/src/aws_http.zig +++ b/src/aws_http.zig @@ -255,7 +255,7 @@ pub const AwsHttp = struct { try req.reader().readAllAlloc(self.allocator, std.math.maxInt(usize)) else blk: { // content length - var tmp_data = try self.allocator.alloc(u8, content_length); + const tmp_data = try self.allocator.alloc(u8, content_length); errdefer self.allocator.free(tmp_data); _ = try req.readAll(tmp_data); break :blk tmp_data; diff --git a/src/aws_signing.zig b/src/aws_signing.zig index 4286bd9..03d384e 100644 --- a/src/aws_signing.zig +++ b/src/aws_signing.zig @@ -364,7 +364,7 @@ fn verifyParsedAuthorization( const service = credential_iterator.next().?; const aws4_request = credential_iterator.next().?; if (!std.mem.eql(u8, aws4_request, "aws4_request")) return error.UnexpectedCredentialValue; - var config = Config{ + const config = Config{ .service = service, .credentials = credentials, .region = region, @@ -444,7 +444,7 @@ fn getSigningKey(allocator: std.mem.Allocator, signing_date: []const u8, config: \\ region: {s} \\ service: {s} , .{ signing_date, config.region, config.service }); - var secret = try std.fmt.allocPrint(allocator, "AWS4{s}", .{config.credentials.secret_key}); + const secret = try std.fmt.allocPrint(allocator, "AWS4{s}", .{config.credentials.secret_key}); defer { // secureZero avoids compiler optimizations that may say // "WTF are you doing this thing? Looks like nothing to me. It's silly and we will remove it" @@ -716,7 +716,7 @@ fn canonicalQueryString(allocator: std.mem.Allocator, path: []const u8) ![]const for (sort_me.items) |i| { if (!first) try normalized.append('&'); first = false; - var first_equals = std.mem.indexOf(u8, i, "="); + const first_equals = std.mem.indexOf(u8, i, "="); if (first_equals == null) { // Rare. This is "foo=" const normed_item = try encodeUri(allocator, i); @@ -744,7 +744,7 @@ fn canonicalQueryString(allocator: std.mem.Allocator, path: []const u8) ![]const } fn replace(allocator: std.mem.Allocator, haystack: []const u8, needle: []const u8, replacement_value: []const u8) ![]const u8 { - var buffer = try allocator.alloc(u8, std.mem.replacementSize(u8, haystack, needle, replacement_value)); + const buffer = try allocator.alloc(u8, std.mem.replacementSize(u8, haystack, needle, replacement_value)); _ = std.mem.replace(u8, haystack, needle, replacement_value, buffer); return buffer; } @@ -837,7 +837,7 @@ fn canonicalHeaders(allocator: std.mem.Allocator, headers: []base.Header, servic fn canonicalHeaderValue(allocator: std.mem.Allocator, value: []const u8) ![]const u8 { var started = false; - var in_quote = false; + const in_quote = false; var start: usize = 0; const rc = try allocator.alloc(u8, value.len); var rc_inx: usize = 0; @@ -1002,7 +1002,7 @@ test "can sign" { try headers.append(.{ .name = "Content-Type", .value = "application/x-www-form-urlencoded; charset=utf-8" }); try headers.append(.{ .name = "Content-Length", .value = "13" }); try headers.append(.{ .name = "Host", .value = "example.amazonaws.com" }); - var req = base.Request{ + const req = base.Request{ .path = "/", .query = "", .body = "Param1=value1", @@ -1071,7 +1071,7 @@ test "can verify server request" { var buf = "bar".*; var fis = std.io.fixedBufferStream(&buf); - var request = std.http.Server.Request{ + const request = std.http.Server.Request{ .method = std.http.Method.PUT, .target = "/mysfitszj3t6webstack-hostingbucketa91a61fe-1ep3ezkgwpxr0/i/am/a/teapot/foo?x-id=PutObject", .version = .@"HTTP/1.1", diff --git a/src/date.zig b/src/date.zig index cd81843..aec04e7 100644 --- a/src/date.zig +++ b/src/date.zig @@ -21,7 +21,7 @@ pub fn timestampToDateTime(timestamp: i64) DateTime { const DAY_NUMBER_ADJUSTED_1970_01_01 = 719468; //* Day number relates to March 1st */ var dayN: u64 = DAY_NUMBER_ADJUSTED_1970_01_01 + unixtime / SECONDS_PER_DAY; - var seconds_since_midnight: u64 = unixtime % SECONDS_PER_DAY; + const seconds_since_midnight: u64 = unixtime % SECONDS_PER_DAY; var temp: u64 = 0; // Leap year rules for Gregorian Calendars @@ -37,7 +37,7 @@ pub fn timestampToDateTime(timestamp: i64) DateTime { // dayN calculates the days of the year in relation to March 1 var month = @as(u8, @intCast((5 * dayN + 2) / 153)); - var day = @as(u8, @intCast(dayN - (@as(u64, @intCast(month)) * 153 + 2) / 5 + 1)); + const day = @as(u8, @intCast(dayN - (@as(u64, @intCast(month)) * 153 + 2) / 5 + 1)); // 153 = 31+30+31+30+31 Days for the 5 months from March through July // 153 = 31+30+31+30+31 Days for the 5 months from August through December // 31+28 Days for January and February (see below) @@ -50,9 +50,9 @@ pub fn timestampToDateTime(timestamp: i64) DateTime { year += 1; } - var hours = @as(u8, @intCast(seconds_since_midnight / 3600)); - var minutes = @as(u8, @intCast(seconds_since_midnight % 3600 / 60)); - var seconds = @as(u8, @intCast(seconds_since_midnight % 60)); + const hours = @as(u8, @intCast(seconds_since_midnight / 3600)); + const minutes = @as(u8, @intCast(seconds_since_midnight % 3600 / 60)); + const seconds = @as(u8, @intCast(seconds_since_midnight % 60)); return DateTime{ .day = day, .month = month, .year = year, .hour = hours, .minute = minutes, .second = seconds }; } @@ -275,10 +275,10 @@ fn secondsBetween(start: DateTime, end: DateTime) DateTimeToTimestampError!i64 { return (try secondsBetween(new_start, end)) - seconds_into_start_year; } const leap_years_between = leapYearsBetween(start.year, end.year); - var add_days: u1 = 0; + const add_days: u1 = 0; const years_diff = end.year - start.year; // log.debug("Years from epoch: {d}, Leap years: {d}", .{ years_diff, leap_years_between }); - var days_diff: i32 = (years_diff * DAYS_PER_YEAR) + leap_years_between + add_days; + const days_diff: i32 = (years_diff * DAYS_PER_YEAR) + leap_years_between + add_days; // log.debug("Days with leap year, without month: {d}", .{days_diff}); const seconds_into_year = secondsFromBeginningOfYear( @@ -306,7 +306,7 @@ fn secondsFromBeginningOfYear(year: u16, month: u8, day: u8, hour: u8, minute: u const normal_days_per_month: [12]u5 = .{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; const days_per_month = if (current_year_is_leap_year) leap_year_days_per_month else normal_days_per_month; var current_month: usize = 1; - var end_month = month; + const end_month = month; var days_diff: u32 = 0; while (current_month != end_month) { days_diff += days_per_month[current_month - 1]; // months are 1-based vs array is 0-based diff --git a/src/json.zig b/src/json.zig index 83429c3..7b280e6 100644 --- a/src/json.zig +++ b/src/json.zig @@ -2299,7 +2299,7 @@ pub const Parser = struct { }, .ObjectValue => { var object = &p.stack.items[p.stack.items.len - 2].Object; - var key = p.stack.items[p.stack.items.len - 1].String; + const key = p.stack.items[p.stack.items.len - 1].String; switch (token) { .ObjectBegin => { diff --git a/src/xml.zig b/src/xml.zig index d554c6d..ca6fda4 100644 --- a/src/xml.zig +++ b/src/xml.zig @@ -260,7 +260,7 @@ const ParseContext = struct { begin = prev_nl + 1; } - var end = mem.indexOfScalarPos(u8, self.source, self.offset, '\n') orelse self.source.len; + const end = mem.indexOfScalarPos(u8, self.source, self.offset, '\n') orelse self.source.len; return self.source[begin..end]; } };