String.char()

classic Classic list List threaded Threaded
20 messages Options
Reply | Threaded
Open this post in threaded view
|

String.char()

Hugo <Nabble>
I am trying to generate a sequence of random chars as explained here:
http://www.computercraft.info/forums2/index.php?/topic/12894-how-to-easly-make-random-strings-of-any-length/

So I did this:
import "String"
import "Math"

local s = ""
for i in range(1,5) do
  n = Math.random(32, 126)
  if n == 96 then 
    n = Math.random(32, 95) 
  end
  s = s .. String.char(n)
end

print(s)
String.char() doesn't exist. Why?
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

fschmidt
Administrator
I thought "char" is a meaningless name, so the name is "from_integers".  But I also didn't completely implement Math.random() which you need.  This is fixed in rev 245.  You can copy dist/jars/*.jar if you need them.
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

Hugo <Nabble>
I think "char" is better than "from_integers". At the end, I gave up on my original idea and just implemented a random sequence of numbers like this:
local s = ""
for i in range(1, 8) do
	s = s .. Math.random(0, 10)
end
Math.random() seems to be working fine now, thanks.

You can close this thread.
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

fschmidt
Administrator
Okay I renamed "from_integers" to "char".

Looking at your code, I realized I implemented Math.random() wrong because your code should be Math.random(0,9) .  I fixed this in rev 247.
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

Hugo <Nabble>
It looks much better now, thanks.

I am closing this thread.
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

fschmidt
Administrator
In reply to this post by Hugo <Nabble>
The 2 related methods are byte() and char(), see:

http://www.lua.org/manual/5.2/manual.html#6.4

The byte() method is clearly misnamed for Java because a String isn't made out of bytes.  And I still don't like the name of char().  So how about having pack() and unpack() like the names in Table:

http://www.lua.org/manual/5.2/manual.html#6.5

I am also using pack() and unpack() for the Binary module which is for the luan binary type which is actually byte[] in java.
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

Hugo <Nabble>
If you prefer you can rename those methods to to_byte() and from_char(). It would imply a conversion, so doesn't matter what is underneath. The names pack() and unpack() make no sense to me in this case. For strings, I would believe that pack()/unpack() is used for compression/uncompression of the string and that would be completely wrong.
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

fschmidt
Administrator
to_byte() is just as bad because this has nothing to do with bytes.  Lua is based on C where a char is a byte but Luan is based on Java where a char is not a byte, so the function that "Returns the internal numerical codes of the characters" in Luan does not return bytes.
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

Hugo <Nabble>
A char in java has two bytes, so "has nothing to do with bytes" is not true as well. But I accept your complaint and we could have to_chars(), but I am not sure what we could do with that. Can we, for example, do this?
local chars = str.to_chars()
if chars[1] == 'A' then
    print('A')
end
If not, how can we use that method?
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

fschmidt
Administrator
Everything about a computer has something to do with bytes, but char no more than anything else.

To do what you want:
if str.sub(1,1) == 'A' then
	print 'A'
end

The method we are discussing is just good for checking what the numerical values of chars are.  So what should it be called?
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

Hugo <Nabble>
fschmidt wrote
The method we are discussing is just good for checking what the numerical values of chars are.  So what should it be called?
The problem is that this depends on the encoding (UTF8, US-ASCII, ISO-8859-1, etc). The from_char() also depends on the encoding. So the encoding should probably be a parameter and we could have:

from_char(code, encoding)
to_chars(encoding)

In both cases "encoding" is optional and defaults to US-ASCII. Makes sense?
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

fschmidt
Administrator
No, it has nothing to do with the encoding.  It is basically just String.toCharArray().
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

Hugo <Nabble>
What about the from_char()? Don't we need the encoding? What if I want a specific UTF8 char code?
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

fschmidt
Administrator
Encoding is about mapping chars to bytes.  A string is just an array of chars (conceptually) so encoding doesn't matter.

So now you want to name from_char() what you previously called to_chars() .  I don't understand.  What 2 names do you suggest to map numbers to a string and to map a string to numbers?
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

Hugo <Nabble>
fschmidt wrote
So now you want to name from_char() what you previously called to_chars() .  I don't understand.
No. Please read the root post of this thread. We need a way to build a string with specific character codes. In java we can do this:
String literal = "\u2190\u2195";
How can we do this in Luan?
So my point was that we could have something like this:
local str = String.from_char(2190, 'UTF8') .. String.from_char(2195, 'UTF8')

or

local str = String.from_char('u2190') .. String.from_char('u2195')

The to_chars() is another story. In java, the char values are mainly used for comparison with literal values, like c == 'A' and this doesn't exist in Luan (as you explained, we should use str.sub(1,1) instead). I have never seen people actually checking the numeric value of a char, so I suggest we keep this method off until someone explains why we need it.
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

fschmidt
Administrator
This makes no sense to me.  Let's discuss this on skype.
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

Hugo <Nabble>
We discussed. A good idea is to have:

from_unicode()
to_unicodes()
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

fschmidt
Administrator
After more thought, I did this:

I left String.char() alone.  It is exactly as in Lua.

I changed String.byte() to String.unicode() but otherwise it is just like as in Lua.

The Binary package has 2 functions.  Binary.binary() is analogous to String.char().  Binary.byte() is analogous to String.unicode() or Lua's string.byte() .

Binary functions can be used as object calls, like with String.

Here is an example:
import "luan:Binary"
import "luan:String"

s = "abc"
bin = Binary.binary(s.unicode(1,-1))
s2 = String.char(bin.byte(1,-1))
print(s2)  -- prints "abc"

Does this make sense?
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

Hugo <Nabble>
This is fine.
Reply | Threaded
Open this post in threaded view
|

Re: String.char()

fschmidt
Administrator
closing
Woe to those who call bad good and good bad -- Isaiah 5:20
Following the Old Testament, not evil modern culture