Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Unexpected length of £ (pound) character?

Reply
Thread Tools

Unexpected length of £ (pound) character?

 
 
Anthony Ss
Guest
Posts: n/a
 
      06-04-2010
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 36 [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.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
botp
Guest
Posts: n/a
 
      06-04-2010
On Fri, Jun 4, 2010 at 6:19 PM, Anthony Ss
<(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
 
Brian Candler
Guest
Posts: n/a
 
      06-04-2010
Anthony Stenhouse wrote:
> 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:

>> a = "1234£6789"

=> "1234\302\2436789"
>> a =~ /\A(.{6})/u

=> 0
>> puts $1

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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
MrZombie
Guest
Posts: n/a
 
      06-04-2010
On 2010-06-04 06:19:09 -0400, Anthony Ss 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.
>
> 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.
--
Thank you for your brain.
-MrZombie

 
Reply With Quote
 
botp
Guest
Posts: n/a
 
      06-04-2010
On Fri, Jun 4, 2010 at 6:47 PM, Brian Candler <(E-Mail Removed)> wrote:
> If you want to capture (say) the first 6 characters of the string, try
>>> a =3D "1234=A36789"

> =3D> "1234\302\2436789"
>>> a =3D~ /\A(.{6})/u

> =3D> 0
>>> puts $1

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Smart or stupid? Tying textbox length to database column length Dan Manes ASP .Net 1 04-23-2006 10:57 PM
911...Need Help! : Length cannot be less than zero. Parameter name : length manmit.walia@gmail.com ASP .Net 2 01-10-2006 03:29 PM
left(string, length) or right(string, length)? Sam ASP .Net 3 02-17-2005 12:01 PM
System.ArgumentOutOfRangeException: Length cannot be less than zero. Parameter name: length =?Utf-8?B?SG96aQ==?= ASP .Net 1 06-01-2004 11:06 PM
How to get length of string? length() problems Mitchua Perl 5 07-17-2003 12:08 AM



Advertisments