Project

General

Profile

Actions

Feature #3947

closed

Array#packのにエンディアン指定修飾子</>を追加

Added by naruse (Yui NARUSE) over 13 years ago. Updated almost 13 years ago.

Status:
Closed
Assignee:
-
Target version:
[ruby-dev:42376]

Description

=begin
Ruby の伏魔殿 Array#pack ですが、機種依存な部分をいじる際にはしばしば強力かつ唯一の手段になる事があります。
具体的には RubySpec 書く時とか。

さて、pack のようにバイト列を扱う場合、しばしば問題になるのがエンディアンです。
ここで、Ruby の Array#pack は 16bit/32bit 固定の整数に対してはエンディアン固定の
n/N/v/V 指定子を用意していますが、short/int/long/long long のように、
マシン依存の型をエンディアン固定で pack する手段を提供していません。
というわけで、これが欲しいです。

ここで問題になるのがどのように指定するかなのですが、pack のネタ元である Perl さんでは既に </> 修飾子を
この目的のために提供しています。
http://perldoc.perl.org/functions/pack.html
というわけで、これに追従するのがよろしいと思います。

 >   sSiIlLqQ   Force big-endian byte-order on the type.
     jJfFdDpP   (The "big end" touches the construct.)
 <   sSiIlLqQ   Force little-endian byte-order on the type.
     jJfFdDpP   (The "little end" touches the construct.)

なお、「</> とかきもい」という意見もあるかとは思いますが、pack は機種依存とかに触るきもいメソッドなので、
そこを気にするよりは長い物に巻かれておいた方が無難ではないかなと思うため、</> 修飾子がベストかと思います。
=end


Related issues 2 (0 open2 closed)

Related to Ruby master - Feature #4084: pack should support 64bit network byte order longsClosed11/24/2010Actions
Is duplicate of Ruby master - Feature #3491: Pack missing directives for signed types with specified byte-orderClosed06/28/2010Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0