tweaks per laytan suggestions

This commit is contained in:
Colin Davidson
2024-10-10 09:14:29 -07:00
parent a6502c3e8c
commit 4c8e355444
11 changed files with 33 additions and 42 deletions
+2 -4
View File
@@ -1026,7 +1026,7 @@ absolute_path_from_handle :: proc(fd: Handle) -> (path: string, err: Error) {
} }
@(require_results) @(require_results)
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Error) { absolute_path_from_relative :: proc(rel: string, allocator := context.allocator) -> (path: string, err: Error) {
rel := rel rel := rel
if rel == "" { if rel == "" {
rel = "." rel = "."
@@ -1041,9 +1041,7 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Error) {
} }
defer _unix_free(rawptr(path_ptr)) defer _unix_free(rawptr(path_ptr))
path = strings.clone(string(path_ptr)) return strings.clone(string(path_ptr), allocator)
return path, nil
} }
access :: proc(path: string, mask: int) -> bool { access :: proc(path: string, mask: int) -> bool {
+2 -5
View File
@@ -789,7 +789,7 @@ absolute_path_from_handle :: proc(fd: Handle) -> (string, Error) {
} }
@(require_results) @(require_results)
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Error) { absolute_path_from_relative :: proc(rel: string, allocator := context.allocator) -> (path: string, err: Error) {
rel := rel rel := rel
if rel == "" { if rel == "" {
rel = "." rel = "."
@@ -804,10 +804,7 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Error) {
} }
defer _unix_free(rawptr(path_ptr)) defer _unix_free(rawptr(path_ptr))
return strings.clone(string(path_ptr), allocator)
path = strings.clone(string(path_ptr))
return path, nil
} }
access :: proc(path: string, mask: int) -> (bool, Error) { access :: proc(path: string, mask: int) -> (bool, Error) {
+2 -4
View File
@@ -431,7 +431,7 @@ absolute_path_from_handle :: proc(fd: Handle) -> (string, Error) {
} }
@(require_results) @(require_results)
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Error) { absolute_path_from_relative :: proc(rel: string, allocator := context.allocator) -> (path: string, err: Error) {
rel := rel rel := rel
if rel == "" { if rel == "" {
rel = "." rel = "."
@@ -447,9 +447,7 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Error) {
defer _unix_free(path_ptr) defer _unix_free(path_ptr)
path_cstr := cstring(path_ptr) path_cstr := cstring(path_ptr)
path = strings.clone(string(path_cstr)) return strings.clone(string(path_cstr), allocator)
return path, nil
} }
access :: proc(path: string, mask: int) -> (bool, Error) { access :: proc(path: string, mask: int) -> (bool, Error) {
+2 -4
View File
@@ -914,7 +914,7 @@ absolute_path_from_handle :: proc(fd: Handle) -> (string, Error) {
} }
@(require_results) @(require_results)
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Error) { absolute_path_from_relative :: proc(rel: string, allocator := context.allocator) -> (path: string, err: Error) {
rel := rel rel := rel
if rel == "" { if rel == "" {
rel = "." rel = "."
@@ -929,9 +929,7 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Error) {
} }
defer _unix_free(rawptr(path_ptr)) defer _unix_free(rawptr(path_ptr))
path = strings.clone(string(path_ptr)) return strings.clone(string(path_ptr), allocator)
return path, nil
} }
access :: proc(path: string, mask: int) -> (bool, Error) { access :: proc(path: string, mask: int) -> (bool, Error) {
+2 -4
View File
@@ -844,7 +844,7 @@ absolute_path_from_handle :: proc(fd: Handle) -> (path: string, err: Error) {
} }
@(require_results) @(require_results)
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Error) { absolute_path_from_relative :: proc(rel: string, allocator := context.allocator) -> (path: string, err: Error) {
rel := rel rel := rel
if rel == "" { if rel == "" {
rel = "." rel = "."
@@ -859,9 +859,7 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Error) {
} }
defer _unix_free(rawptr(path_ptr)) defer _unix_free(rawptr(path_ptr))
path = strings.clone(string(path_ptr)) return strings.clone(string(path_ptr), allocator)
return path, nil
} }
access :: proc(path: string, mask: int) -> (bool, Error) { access :: proc(path: string, mask: int) -> (bool, Error) {
+2 -4
View File
@@ -758,7 +758,7 @@ absolute_path_from_handle :: proc(fd: Handle) -> (string, Error) {
} }
@(require_results) @(require_results)
absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Error) { absolute_path_from_relative :: proc(rel: string, allocator := context.allocator) -> (path: string, err: Error) {
rel := rel rel := rel
if rel == "" { if rel == "" {
rel = "." rel = "."
@@ -773,9 +773,7 @@ absolute_path_from_relative :: proc(rel: string) -> (path: string, err: Error) {
} }
defer _unix_free(rawptr(path_ptr)) defer _unix_free(rawptr(path_ptr))
path = strings.clone(string(path_ptr)) return strings.clone(string(path_ptr), allocator)
return path, nil
} }
access :: proc(path: string, mask: int) -> (bool, Error) { access :: proc(path: string, mask: int) -> (bool, Error) {
+2 -2
View File
@@ -85,9 +85,9 @@ TZ_Record :: struct {
} }
TZ_Date_Kind :: enum { TZ_Date_Kind :: enum {
NoLeap, No_Leap,
Leap, Leap,
MonthWeekDay, Month_Week_Day,
} }
TZ_Transition_Date :: struct { TZ_Transition_Date :: struct {
+5 -3
View File
@@ -11,7 +11,7 @@ local_tz_name :: proc(allocator := context.allocator) -> (name: string, success:
local_str, ok := os.lookup_env("TZ", allocator) local_str, ok := os.lookup_env("TZ", allocator)
if !ok { if !ok {
orig_localtime_path := "/etc/localtime" orig_localtime_path := "/etc/localtime"
path, err := os.absolute_path_from_relative(orig_localtime_path) path, err := os.absolute_path_from_relative(orig_localtime_path, allocator)
if err != nil { if err != nil {
// If we can't find /etc/localtime, fallback to UTC // If we can't find /etc/localtime, fallback to UTC
if err == .ENOENT { if err == .ENOENT {
@@ -22,11 +22,11 @@ local_tz_name :: proc(allocator := context.allocator) -> (name: string, success:
return return
} }
defer delete(path) defer delete(path, allocator)
// FreeBSD makes me sad. // FreeBSD makes me sad.
if path == orig_localtime_path { if path == orig_localtime_path {
data := os.read_entire_file("/var/db/zoneinfo") or_return data := os.read_entire_file("/var/db/zoneinfo", allocator) or_return
return strings.trim_right_space(string(data)), true return strings.trim_right_space(string(data)), true
} }
@@ -52,6 +52,8 @@ local_tz_name :: proc(allocator := context.allocator) -> (name: string, success:
} }
if local_str == "" { if local_str == "" {
delete(local_str)
str, err := strings.clone("UTC", allocator) str, err := strings.clone("UTC", allocator)
if err != nil { return } if err != nil { return }
return str, true return str, true
+3 -1
View File
@@ -38,6 +38,7 @@ region_destroy :: proc(region: ^datetime.TZ_Region, allocator := context.allocat
} }
@private
region_get_nearest :: proc(region: ^datetime.TZ_Region, tm: time.Time) -> (out: datetime.TZ_Record, success: bool) { region_get_nearest :: proc(region: ^datetime.TZ_Region, tm: time.Time) -> (out: datetime.TZ_Record, success: bool) {
if len(region.records) == 0 { if len(region.records) == 0 {
return process_rrule(region.rrule, tm) return process_rrule(region.rrule, tm)
@@ -86,7 +87,7 @@ trans_date_to_seconds :: proc(year: i64, td: datetime.TZ_Transition_Date) -> (se
ONE_DAY :: 86_400 ONE_DAY :: 86_400
#partial switch td.type { #partial switch td.type {
case .MonthWeekDay: case .Month_Week_Day:
year_start := datetime.DateTime{{year, 1, 1}, {0, 0, 0, 0}, nil} year_start := datetime.DateTime{{year, 1, 1}, {0, 0, 0, 0}, nil}
year_start_time := time.datetime_to_time(year_start) or_return year_start_time := time.datetime_to_time(year_start) or_return
@@ -116,6 +117,7 @@ trans_date_to_seconds :: proc(year: i64, td: datetime.TZ_Transition_Date) -> (se
return return
} }
@private
process_rrule :: proc(rrule: datetime.TZ_RRule, tm: time.Time) -> (out: datetime.TZ_Record, success: bool) { process_rrule :: proc(rrule: datetime.TZ_RRule, tm: time.Time) -> (out: datetime.TZ_Record, success: bool) {
if !rrule.has_dst { if !rrule.has_dst {
return datetime.TZ_Record{ return datetime.TZ_Record{
+2 -2
View File
@@ -282,7 +282,7 @@ parse_posix_rrule :: proc(str: string) -> (out: datetime.TZ_Transition_Date, idx
} }
return datetime.TZ_Transition_Date{ return datetime.TZ_Transition_Date{
type = .NoLeap, type = .No_Leap,
day = u16(day), day = u16(day),
time = offset, time = offset,
}, i, true }, i, true
@@ -350,7 +350,7 @@ parse_posix_rrule :: proc(str: string) -> (out: datetime.TZ_Transition_Date, idx
} }
return datetime.TZ_Transition_Date{ return datetime.TZ_Transition_Date{
type = .MonthWeekDay, type = .Month_Week_Day,
month = u8(month), month = u8(month),
week = u8(week), week = u8(week),
day = u16(day), day = u16(day),
+8 -8
View File
@@ -455,7 +455,7 @@ test_check_timezone_posix_tz :: proc(t: ^testing.T) {
std_name = "EST", std_name = "EST",
std_offset = -(5 * 60 * 60), std_offset = -(5 * 60 * 60),
std_date = dt.TZ_Transition_Date{ std_date = dt.TZ_Transition_Date{
type = .MonthWeekDay, type = .Month_Week_Day,
month = 3, month = 3,
week = 2, week = 2,
day = 0, day = 0,
@@ -465,7 +465,7 @@ test_check_timezone_posix_tz :: proc(t: ^testing.T) {
dst_name = "EDT", dst_name = "EDT",
dst_offset = -(4 * 60 * 60), dst_offset = -(4 * 60 * 60),
dst_date = dt.TZ_Transition_Date{ dst_date = dt.TZ_Transition_Date{
type = .MonthWeekDay, type = .Month_Week_Day,
month = 11, month = 11,
week = 1, week = 1,
day = 0, day = 0,
@@ -484,7 +484,7 @@ test_check_timezone_posix_tz :: proc(t: ^testing.T) {
std_name = "IST", std_name = "IST",
std_offset = (2 * 60 * 60), std_offset = (2 * 60 * 60),
std_date = dt.TZ_Transition_Date{ std_date = dt.TZ_Transition_Date{
type = .MonthWeekDay, type = .Month_Week_Day,
month = 3, month = 3,
week = 4, week = 4,
day = 4, day = 4,
@@ -494,7 +494,7 @@ test_check_timezone_posix_tz :: proc(t: ^testing.T) {
dst_name = "IDT", dst_name = "IDT",
dst_offset = (3 * 60 * 60), dst_offset = (3 * 60 * 60),
dst_date = dt.TZ_Transition_Date{ dst_date = dt.TZ_Transition_Date{
type = .MonthWeekDay, type = .Month_Week_Day,
month = 10, month = 10,
week = 5, week = 5,
day = 0, day = 0,
@@ -513,7 +513,7 @@ test_check_timezone_posix_tz :: proc(t: ^testing.T) {
std_name = "WART", std_name = "WART",
std_offset = -(4 * 60 * 60), std_offset = -(4 * 60 * 60),
std_date = dt.TZ_Transition_Date{ std_date = dt.TZ_Transition_Date{
type = .NoLeap, type = .No_Leap,
day = 1, day = 1,
time = 0 * 60 * 60, time = 0 * 60 * 60,
}, },
@@ -521,7 +521,7 @@ test_check_timezone_posix_tz :: proc(t: ^testing.T) {
dst_name = "WARST", dst_name = "WARST",
dst_offset = -(3 * 60 * 60), dst_offset = -(3 * 60 * 60),
dst_date = dt.TZ_Transition_Date{ dst_date = dt.TZ_Transition_Date{
type = .NoLeap, type = .No_Leap,
day = 365, day = 365,
time = 25 * 60 * 60, time = 25 * 60 * 60,
}, },
@@ -538,7 +538,7 @@ test_check_timezone_posix_tz :: proc(t: ^testing.T) {
std_name = "WGT", std_name = "WGT",
std_offset = -(3 * 60 * 60), std_offset = -(3 * 60 * 60),
std_date = dt.TZ_Transition_Date{ std_date = dt.TZ_Transition_Date{
type = .MonthWeekDay, type = .Month_Week_Day,
month = 3, month = 3,
week = 5, week = 5,
day = 0, day = 0,
@@ -548,7 +548,7 @@ test_check_timezone_posix_tz :: proc(t: ^testing.T) {
dst_name = "WGST", dst_name = "WGST",
dst_offset = -(2 * 60 * 60), dst_offset = -(2 * 60 * 60),
dst_date = dt.TZ_Transition_Date{ dst_date = dt.TZ_Transition_Date{
type = .MonthWeekDay, type = .Month_Week_Day,
month = 10, month = 10,
week = 5, week = 5,
day = 0, day = 0,