c3c icon indicating copy to clipboard operation
c3c copied to clipboard

Compile time string concatenation

Open cbuttner opened this issue 1 year ago • 3 comments

I often find myself needing some way to concatenate strings at compile time.

Maybe a builtin $concat with variable number of arguments could be introduced.

Some examples of what I image it to look like:

macro echo_code_loc() => $echo $concat($$FILE, ":", $$LINE);

// Console output with virtual terminal sequences
macro color_code($id) {
  $if env::WINDOWS:  
    return $concat("\x1b[1;", $id, "m")
  $else
    return $concat("\x1b[", $id, "m")
  $endif
}

const TERM_RESET = color_code("0");
const TERM_BOLD = color_code("1");

fn print_to_console(bool colored) {
  String fmt = colored ? $concat(TERM_BOLD, "%s", TERM_RESET, "%s") : $concat("%s", "%s");
  io::printfn(fmt, "Hello bold output", "1234");
}


// Some libraries such as ICU do this:
const VERSION_SUFFIX = "_74"
fn ZString getName(UScriptCode scriptCode) @extern($concat("uscript_getName", VERSION_SUFFIX));

cbuttner avatar Jun 14 '24 12:06 cbuttner

This could probably be implemented as a compiler builtin $$concat which is then exposed as a stdlib macro @concat()

lerno avatar Jun 14 '24 15:06 lerno

Other functions proposed are at #828

lerno avatar Jun 14 '24 15:06 lerno

$append and $concat are now added. Please test them out from dev

lerno avatar Jun 16 '24 19:06 lerno

I haven't encountered any issues so far.

cbuttner avatar Jul 22 '24 17:07 cbuttner