From fb6dc0a8b9b5537ef708778bf013f71f98fad41f Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 12 Sep 2024 03:41:44 -0400 Subject: [PATCH] Fix up CString:as_text() --- builtins/c_string.c | 9 +++++++-- builtins/c_string.h | 3 ++- environment.c | 3 ++- examples/log.tm | 13 ++++++------- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/builtins/c_string.c b/builtins/c_string.c index c7c7a56..bda127f 100644 --- a/builtins/c_string.c +++ b/builtins/c_string.c @@ -13,14 +13,19 @@ #include "types.h" #include "util.h" -public Text_t CString$as_text(const void *c_string, bool colorize, const TypeInfo *info) +public Text_t CString$as_text(const char **c_string, bool colorize, const TypeInfo *info) { (void)info; if (!c_string) return Text("CString"); - Text_t text = Text$from_str(*(char**)c_string); + Text_t text = Text$from_str(*c_string); return Text$concat(colorize ? Text("\x1b[34mCString\x1b[m(") : Text("CString("), Text$quoted(text, colorize), Text(")")); } +public Text_t CString$as_text_simple(const char *str) +{ + return Text$format("%s", str); +} + PUREFUNC public int32_t CString$compare(const char **x, const char **y) { if (x == y) diff --git a/builtins/c_string.h b/builtins/c_string.h index 10621e6..e596bf4 100644 --- a/builtins/c_string.h +++ b/builtins/c_string.h @@ -8,7 +8,8 @@ #include "types.h" -Text_t CString$as_text(const void *str, bool colorize, const TypeInfo *info); +Text_t CString$as_text(char **str, bool colorize, const TypeInfo *info); +Text_t CString$as_text_simple(const char *str); PUREFUNC int CString$compare(const char **x, const char **y); PUREFUNC bool CString$equal(const char **x, const char **y); PUREFUNC uint64_t CString$hash(const char **str); diff --git a/environment.c b/environment.c index 290ddf9..1f5493d 100644 --- a/environment.c +++ b/environment.c @@ -246,7 +246,7 @@ env_t *new_compilation_unit(CORD *libname) F2(atan2), F2(copysign), F2(fdim), F2(hypot), F2(nextafter), F2(pow), F2(remainder), )}, {"CString", Type(CStringType), "char*", "CString$info", TypedArray(ns_entry_t, - {"as_text", "CORD_from_char_star", "func(str:CString)->Text"}, + {"as_text", "CString$as_text_simple", "func(str:CString)->Text"}, )}, #undef F2 #undef F @@ -570,6 +570,7 @@ binding_t *get_namespace_binding(env_t *env, ast_t *self, const char *name) switch (cls_type->tag) { case ArrayType: return NULL; case TableType: return NULL; + case CStringType: case BoolType: case IntType: case BigIntType: case NumType: { binding_t *b = get_binding(env, CORD_to_const_char_star(type_to_cord(cls_type))); assert(b); diff --git a/examples/log.tm b/examples/log.tm index 765fc61..24397c9 100644 --- a/examples/log.tm +++ b/examples/log.tm @@ -6,17 +6,16 @@ timestamp_format := CString("%F %T") logfiles := {:Path} func _timestamp()->Text: - return inline C ( + c_str := inline C ( ({ char *str = GC_MALLOC_ATOMIC(20); - time_t t; - time(&t); - struct tm *tm_info; - tm_info = localtime(&t); + time_t t; time(&t); + struct tm *tm_info = localtime(&t); strftime(str, 20, "%F %T", tm_info); - Text$format("%s", str); + str; }) - ) : Text + ) : CString + return c_str:as_text() func info(text:Text, newline=yes): say("$\[2]⚫ $text$\[]", newline)