style: compact inner groups and simple guard bodies

This commit is contained in:
2026-10-27 12:40:41 +01:00
parent f062389ccc
commit 8d1a98f982
22 changed files with 165 additions and 430 deletions
+4 -11
View File
@@ -38,15 +38,10 @@ reserve proc(
new_capacity := 8
if list.capacity >= 8 {
half :: divtrunc!(list.capacity, 2)
if list.capacity > maxval!(usize) - half {
new_capacity = min_capacity
} else {
new_capacity = list.capacity + half
}
}
if new_capacity < min_capacity {
new_capacity = min_capacity
if (list.capacity > maxval!(usize) - half) new_capacity = min_capacity
else new_capacity = list.capacity + half
}
if (new_capacity < min_capacity) new_capacity = min_capacity
length :: list.items.len
allocation :: list.items.ptr[..list.capacity]
@@ -60,9 +55,7 @@ reserve proc(
append proc($T type, list @mut ArrayList(T), value T) void ! mem.AllocError {
length :: list.items.len
if length == maxval!(usize) {
return .out_of_memory
}
if (length == maxval!(usize)) return .out_of_memory
try reserve(list, length + 1)
list.items = list.items.ptr[..length + 1]
list.items[length] = value
+5 -13
View File
@@ -71,11 +71,8 @@ get proc(
if (map.count == 0) return null
hash usize := 0
if $(hash_key == null) {
hash = normalize(auto_hash(K, key))
} else {
hash = normalize(call_hash(K, hash_key?, key))
}
if ($(hash_key == null)) hash = normalize(auto_hash(K, key))
else hash = normalize(call_hash(K, hash_key?, key))
idx := hash & (map.entries.len - 1)
while true {
@@ -114,9 +111,7 @@ put proc(
if (entry.hash == 0) continue
idx := entry.hash & (new_entries.len - 1)
while new_entries[idx].hash != 0 {
idx = (idx + 1) & (new_entries.len - 1)
}
while (new_entries[idx].hash != 0) idx = (idx + 1) & (new_entries.len - 1)
new_entries[idx] = entry
}
@@ -126,11 +121,8 @@ put proc(
}
hash usize := 0
if $(hash_key == null) {
hash = normalize(auto_hash(K, key))
} else {
hash = normalize(call_hash(K, hash_key?, key))
}
if ($(hash_key == null)) hash = normalize(auto_hash(K, key))
else hash = normalize(call_hash(K, hash_key?, key))
idx := hash & (map.entries.len - 1)
while true {
+6 -18
View File
@@ -269,9 +269,7 @@ parse_format proc($N usize, $format []u8, $Args type) [N]mut FormatToken {
while cursor < format.len {
byte :: format[cursor]
if byte == '{' {
if cursor + 1 >= format.len {
compile_error!("io.print format has an unmatched '{'")
}
if (cursor + 1 >= format.len) compile_error!("io.print format has an unmatched '{'")
if cursor > literal_start {
tokens[token_count] = FormatToken{
kind = .literal,
@@ -298,9 +296,7 @@ parse_format proc($N usize, $format []u8, $Args type) [N]mut FormatToken {
kind FormatTokenKind := .default
width := 2
if next != '}' {
if cursor + 2 >= format.len or format[cursor + 2] != '}' {
compile_error!("io.print format expects a one-character specifier")
}
if (cursor + 2 >= format.len or format[cursor + 2] != '}') compile_error!("io.print format expects a one-character specifier")
width = 3
if (next == 's') kind = .string
@@ -314,9 +310,7 @@ parse_format proc($N usize, $format []u8, $Args type) [N]mut FormatToken {
else compile_error!("io.print format has an unknown specifier")
}
if argument_count >= field_count {
compile_error!("io.print format argument count does not match the tuple")
}
if (argument_count >= field_count) compile_error!("io.print format argument count does not match the tuple")
tokens[token_count] = FormatToken{
kind = kind,
@@ -333,9 +327,7 @@ parse_format proc($N usize, $format []u8, $Args type) [N]mut FormatToken {
}
if byte == '}' {
if cursor + 1 >= format.len or format[cursor + 1] != '}' {
compile_error!("io.print format has an unmatched '}'")
}
if (cursor + 1 >= format.len or format[cursor + 1] != '}') compile_error!("io.print format has an unmatched '}'")
if cursor > literal_start {
tokens[token_count] = FormatToken{
kind = .literal,
@@ -368,9 +360,7 @@ parse_format proc($N usize, $format []u8, $Args type) [N]mut FormatToken {
}
}
if argument_count != field_count {
compile_error!("io.print format argument count does not match the tuple")
}
if (argument_count != field_count) compile_error!("io.print format argument count does not match the tuple")
return tokens
}
@@ -466,9 +456,7 @@ write_decimal proc($T type, output Writer, value T) void ! WriteError {
write_character proc($T type, output Writer, value T) void ! WriteError {
match typeinfo!(T) {
.integer: {
if minval!(T) < 0 or maxval!(T) > 255 {
compile_error!("io.print '{c}' requires an unsigned integer that fits in u8")
}
if (minval!(T) < 0 or maxval!(T) > 255) compile_error!("io.print '{c}' requires an unsigned integer that fits in u8")
buffer [1]u8 :: [u8(value)]
try write_all(output, buffer[..])
}
+2 -6
View File
@@ -70,9 +70,7 @@ alloc proc($T type, allocator Allocator, count usize) []mut T ! AllocError {
element_size :: sizeof!(T)
if (element_size == 0) return empty_slice(T, count)
if count > divtrunc!(maxval!(usize), element_size) {
return .out_of_memory
}
if (count > divtrunc!(maxval!(usize), element_size)) return .out_of_memory
memory :: raw_alloc(allocator, count * element_size, alignof!(T))
if memory |bytes| {
@@ -194,9 +192,7 @@ c_realloc proc(
}
if memory |old_memory| {
if alignment <= malloc_alignment {
return ptrcast!(c.realloc(old_memory, c_ulong(new_size)))
}
if (alignment <= malloc_alignment) return ptrcast!(c.realloc(old_memory, c_ulong(new_size)))
new_memory :: c_alloc(null, new_size, alignment)
if new_memory |new_bytes| {
+1 -3
View File
@@ -58,9 +58,7 @@ parse_float proc(allocator mem.Allocator, text []u8) f64 ! (mem.AllocError | Par
magnitude >= u64(9218868437227405312)
or (magnitude == 0 and nonzero)
or (range_error and magnitude >= u64(4607182418800017408))
) {
return .out_of_range
}
) return .out_of_range
return value
}
+3 -9
View File
@@ -18,23 +18,17 @@ Pair proc($V type) type {
#! initializes a static string map from a list of key-value pairs (constructed at compile-time).
#! returned views use compile-time storage and require no cleanup.
init proc($V type, $N usize, $entries [N]Pair(V)) StaticStringMap(V) {
if N > usize(maxval!(u32)) {
compile_error!("static string map has too many entries")
}
if (N > usize(maxval!(u32))) compile_error!("static string map has too many entries")
keys [N]mut []u8 := undefined
values [N]mut V := undefined
# assert no duplicate keys
for entries |entry, i| {
if entry.0.len > usize(maxval!(u32)) {
compile_error!("static string map key is too long")
}
if (entry.0.len > usize(maxval!(u32))) compile_error!("static string map key is too long")
for (0..i) |prior| {
if mem.eql(u8, entry.0, entries[prior].0) {
compile_error!("duplicate static string map key")
}
if mem.eql(u8, entry.0, entries[prior].0) compile_error!("duplicate static string map key")
}
keys[i] = entry.0