koka-lang / koka

Koka language compiler and interpreter
http://koka-lang.org
Other
3.32k stars 166 forks source link

Inconsistent printing of non-ASCII characters #606

Open ksev opened 2 weeks ago

ksev commented 2 weeks ago

This code:

fun main() 
  "åäö".println
  Just("åäö").println
  ("åä","ö").println
  ["åäö"].println

Prints the following:

åäö
Just("\xE5\xE4\xF6")
("\xE5\xE4","\xF6")
["\xE5\xE4\xF6"]

I'm using Koka 3.1.2 on Fedora 41.

This does not seem like the intended behaviour?

TimWhiting commented 2 weeks ago

This is due to std/core/show.kk.

// Show a string as a string literal
pub noinline fun string/show( s : string ) : string
  "\"" ++ s.list.map(show-char).join ++ "\""

I think we should change this to

// Show a string as a string literal
pub noinline fun string/show( s : string ) : string
  "\"" ++ s ++ "\""

// Show a string as an escaped string literal
pub noinline fun string/show-raw( s : string ) : string
  "\"" ++ s.list.map(show-char).join ++ "\""

The name of the old functionality should maybe be show-raw, show-ascii, or something like that.

I don't think we should get rid of the surrounding quotes though.

ksev commented 2 weeks ago

Thanks, for some context i was working on some C bindings (https://github.com/ksev/koka-duckdb/) and could not for the life of me figure out why converting duckdb's strings to koka strings did not work for me, turns out they did i was just fooled by what was shown.