added: `any_unwrap` and `parse_any` changes
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 27 Sep 2023 23:42:50 +0000 (18:42 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 27 Sep 2023 23:42:50 +0000 (18:42 -0500)
core/conv/parse.onyx
core/misc/any_utils.onyx

index c3e4b6f391e493df495afd755c0eee976dffd204..390509e4f703838d433ae6310b773b47ea5c3699 100644 (file)
@@ -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;
         }
index 6a8d208abd22f9426f4b4cf5c903010ed0270907..66c25c39aa88cbec3fd7b56e167e32a1796ef03b 100644 (file)
@@ -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 {