Unexpected length of £ (pound) character?

A

Anthony Ss

Hi,

Today I came across an issue with a customer custom report which was out
by 1 char over 40 or so lines. At first thought I had incorrectly
limited the field length, however the problem is only present where
there is a '£' char.

For example:
"1234".length => 4
"1234£".length => 6 (Expect 5)
"1234£6".length => 7 (Expect 6)

Tested on:
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]
and:
ruby 1.8.6 (2009-03-31 patchlevel 368) [x86_64-linux]

I could not find anything in google covering this (Perhaps my google-fu
needs work) which brought me here.

Is this expected functionality in ruby? It does not seem right in my
mind.

Thanks. :)
 
B

botp

For example:
=A0"1234".length =3D> 4
=A0"1234=A3".length =3D> 6 (Expect 5)
=A0"1234=A36".length =3D> 7 (Expect 6)

can't help you there, but fyi
RUBY_VERSION =3D> "1.9.2"
"1234".length =3D> 4
"1234=A3".length =3D> 5
"1234=A36".length
=3D> 6

kind regards -botp
 
B

Brian Candler

Anthony said:
Hi,

Today I came across an issue with a customer custom report which was out
by 1 char over 40 or so lines. At first thought I had incorrectly
limited the field length, however the problem is only present where
there is a '£' char.

For example:
"1234".length => 4
"1234£".length => 6 (Expect 5)
"1234£6".length => 7 (Expect 6)

In UTF-8, "£" is two bytes, and ruby 1.8 gives you the number of bytes.

If you want to capture (say) the first 6 characters of the string, try
this:
1234£6
=> nil

This may be sufficient for simple wrapping functions. Or look at the
Iconv library.
Is this expected functionality in ruby? It does not seem right in my
mind.

ruby 1.9 works in characters. It brings with it enormous complexity,
pitfalls and inconsistencies. Pick your poison :)
 
M

MrZombie

Hi,

Today I came across an issue with a customer custom report which was out
by 1 char over 40 or so lines. At first thought I had incorrectly
limited the field length, however the problem is only present where
there is a '£' char.

Is this expected functionality in ruby? It does not seem right in my
mind.

Thanks. :)

Do yourself a favor, friend, and read this excellent article on
Character Encoding:

http://www.joelonsoftware.com/articles/Unicode.html

Then, find out if the character encoding for your file and your
interpreter is the same. :p
 
B

botp

If you want to capture (say) the first 6 characters of the string, try
1234=A36
=3D> nil
"1234=A36789"[0..5]
=3D> "1234=A36"
ruby 1.9 works in characters. It brings with it enormous complexity,
pitfalls and inconsistencies. Pick your poison :)

All programmers are optimists. -Frederick Brooks, Jr.

;-)

kind regards -botp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top