From 6659ceb551c3d1959ab8b8aa3301f66973cf1aa6 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 31 Oct 2018 10:04:30 +0000 Subject: [PATCH] Allow comparisons of `cstring`; Add `resize` --- core/runtime/core.odin | 35 +++++++++++++++++++++++++++++++++++ src/ir.cpp | 5 +++++ src/types.cpp | 4 +++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/core/runtime/core.odin b/core/runtime/core.odin index 976f34d4b..808de0a38 100644 --- a/core/runtime/core.odin +++ b/core/runtime/core.odin @@ -394,6 +394,9 @@ clear :: proc[clear_dynamic_array, clear_map]; @(builtin) reserve :: proc[reserve_dynamic_array, reserve_map]; +@(builtin) +resize :: proc[resize_dynamic_array]; + @(builtin) new :: proc[mem.new]; @@ -537,6 +540,38 @@ reserve_dynamic_array :: proc(array: ^$T/[dynamic]$E, capacity: int, loc := #cal return true; } +@(builtin) +resize_dynamic_array :: proc(array: ^$T/[dynamic]$E, length: int, loc := #caller_location) -> bool { + if array == nil do return false; + a := (^mem.Raw_Dynamic_Array)(array); + + if length <= a.cap { + a.len = max(length, 0); + return true; + } + + if a.allocator.procedure == nil { + a.allocator = context.allocator; + } + assert(a.allocator.procedure != nil); + + old_size := a.cap * size_of(E); + new_size := length * size_of(E); + allocator := a.allocator; + + new_data := allocator.procedure( + allocator.data, mem.Allocator_Mode.Resize, new_size, align_of(E), + a.data, old_size, 0, loc, + ); + if new_data == nil do return false; + + a.data = new_data; + a.len = length; + a.cap = length; + return true; +} + + @(builtin) incl_elem :: inline proc(s: ^$S/bit_set[$E; $U], elem: E) -> S { diff --git a/src/ir.cpp b/src/ir.cpp index 039b89954..7cfd8e113 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -3767,6 +3767,11 @@ irValue *ir_emit_comp(irProcedure *proc, TokenKind op_kind, irValue *left, irVal } if (is_type_string(a)) { + if (is_type_cstring(a)) { + left = ir_emit_conv(proc, left, t_string); + right = ir_emit_conv(proc, right, t_string); + } + char *runtime_proc = nullptr; switch (op_kind) { case Token_CmpEq: runtime_proc = "string_eq"; break; diff --git a/src/types.cpp b/src/types.cpp index beb94caac..f5d1bb4a9 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -1238,8 +1238,10 @@ bool is_type_comparable(Type *t) { return false; case Basic_rune: return true; + case Basic_string: + return true; case Basic_cstring: - return false; + return true; case Basic_typeid: return true; }