grain icon indicating copy to clipboard operation
grain copied to clipboard

print without new line

Open cician opened this issue 3 years ago • 2 comments

I found no way in standard library to print a string without appending a new line.

So I implemented one for my needs for now based on print(), but I'm not sure it's correct as I have virtually no experience in writing wasm bytecode. Is it desirable to have such a function in standard library? Should I make a pull request?

@disableGC
export let printNoNewLine = (value) => {
  let ptr = WasmI32.fromGrain(String.toString(value))
  let buf = Memory.malloc(37n)
  let iov = buf
  let written = buf + 32n
  WasmI32.store(iov, ptr + 8n, 0n)
  WasmI32.store(iov, WasmI32.load(ptr, 4n), 4n)
  WasmI32.store(iov, 1n, 8n)
  fd_write(1n, iov, 1n, written) // originally the 3rd param is 2n in String.print. What does it do?
  Memory.free(buf)
}

cician avatar May 23 '21 19:05 cician

Yeah, we'll definitely want a way to be able to do this. I imagine that we'll have a Printf library, though there are some considerations to be made there. I'll see what the core team thinks about it.

ospencer avatar May 23 '21 20:05 ospencer

import File from "sys/file" File.fdWrite(File.stdout, stringvalue) void

bluebat avatar Aug 24 '22 04:08 bluebat

As @bluebat mentioned, this has been possible for some time.

ospencer avatar Jan 08 '23 20:01 ospencer