From: Brendan Hansen Date: Wed, 27 Sep 2023 23:42:50 +0000 (-0500) Subject: added: `any_unwrap` and `parse_any` changes X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=03d9d5e3d0c3fd69e5d5cb7fe1081e35bfa4c541;p=onyx.git added: `any_unwrap` and `parse_any` changes --- diff --git a/core/conv/parse.onyx b/core/conv/parse.onyx index c3e4b6f3..390509e4 100644 --- a/core/conv/parse.onyx +++ b/core/conv/parse.onyx @@ -67,12 +67,21 @@ parse_any :: (target: rawptr, data_type: type_expr, to_parse: str, string_alloca } case str { - if to_parse.count == 0 do return false; - if to_parse[0] != #char "\"" do return false; + if to_parse.count == 0 do return false; + + dest := cast(&str) target; + + // If the string does *look* like a quoted string, + // simply return a copy of the whole string. + if to_parse[0] != #char "\"" { + *dest = string.alloc_copy(to_parse, string_allocator); + return true; + } + + line := to_parse; string.advance(&line); - dest := cast(&str) target; *dest = string.read_until(&line, #char "\"") |> string.alloc_copy(string_allocator); // @BUG // This does not handle escaped strings! return true; } diff --git a/core/misc/any_utils.onyx b/core/misc/any_utils.onyx index 6a8d208a..66c25c39 100644 --- a/core/misc/any_utils.onyx +++ b/core/misc/any_utils.onyx @@ -13,7 +13,8 @@ use runtime.info { Type_Info_Slice, Type_Info_Dynamic_Array, - get_struct_member + get_struct_member, + union_constructed_from } // Either to_any or as_any will work. I prefer `as_any` because @@ -38,6 +39,19 @@ any_dereference :: (v: any) -> any { return v; } +#doc "Unwraps an optional any, if the any is an optional. `? T -> T`" +any_unwrap :: (v: any) -> any { + if union_constructed_from(v.type, Optional) { + t := v.type->info()->as_union(); + return any.{ + cast([&] u8, v.data) + t.alignment, + t.variants[1].type + }; + } + + return v; +} + #doc "Subscript an array-like any." any_subscript :: (v: any, index: i32) -> any {