#1262 How to find if a given character is printable

rosarinjroy Sun 24 Oct 2010

Hi everyone, I am new to Fantom. I recently started picking up learning Fantom and wanted to implement the Unix "strings" utility in Fantom. The utility reads a file (which can possibly be a binary file) into a buffer (sys::MemBuf). It iterates through the bytes in the file (using Buf.get(index)). The problem I am facing is this: how do I find if the byte I have read is a printable character?

I could not use java.lang.Character, as all the isXXX methods take a character, not an Int.

For sake of simplicity, we can assume that even though the file is binary, the characters that can possibly in that file are only ASCII. So its enough if I check only one byte to be printable or not.

Thank you for your help. Roy

vkuzkokov Sun 24 Oct 2010

What version of JVM do you use? java.lang.Character has methods taking int (like isLetterOrDigit(int codePoint)) marked as since 1.5.

rosarinjroy Mon 25 Oct 2010

Hi vkuzkokov, When I attempted to use that, I got the following error: C:\Users\roy\fantom\helloworld.fan(37,17): Ambiguous call isLetterOrDigit(sys::Int)

go4 Mon 25 Oct 2010

Do you mean isAlphaNum?

rosarinjroy Mon 25 Oct 2010

Exactly. Thats what I was looking for. Appreciate your help.

I didn't expect that method to be a part of Int :-(

tactics Mon 25 Oct 2010

Yeah, it's a quirky feature(?) of Fantom, but the Int class is shared between integers and unicode codepoints.

This is how it works in a few other languages, too. If I recall correctly, Erlang and (old?) Ruby do this.

Once you know where to look, it's not so hard, though.

go4 Tue 26 Oct 2010

btw,'isAlphaNum' is only a subset of the printable characters.

Login or Signup to reply.