Project

General

Profile

Actions

Feature #18573

open

Object#pack1

Added by os (Shigeki OHARA) over 3 years ago. Updated almost 2 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-dev:51154]

Description

ๆฆ‚่ฆยถ

String#unpack1 ใฎ้€†ใฎ Object#pack1 ใŒๆฌฒใ—ใ„ใ€‚

่ƒŒๆ™ฏยถ

Array#pack ใจใ„ใ†ใƒกใ‚ฝใƒƒใƒ‰ใŒใ‚ใ‚Šใพใ™ใŒใ€ใƒฌใ‚ทใƒผใƒใƒผใฎ Array ใฎ่ฆ็ด ๆ•ฐใŒ 1 ใคใ—ใ‹ใชใ„ใ“ใจใŒ่‰ฏใใ‚ใ‚Šใพใ™ใ€‚

[codepoint].pack('U')
[digest].pack('m0')
[mail_body].pack('M')
[ip_address].pack('N')

ๆจ™ๆบ–ๆทปไป˜ใƒฉใ‚คใƒ–ใƒฉใƒชใƒผใชใฉใ‚’็œบใ‚ใฆใฟใฆใ‚‚ใƒใƒฉใƒ›ใƒฉใ‚ใ‚‹ใ‚ˆใ†ใงใ™ใ€‚

ใงใ™ใŒใ€ใ“ใฎใ‚ˆใ†ใชใ‚ฑใƒผใ‚นใงๅค‰ๆ›ๅฏพ่ฑกใฎใ‚ชใƒ–ใ‚ธใ‚งใ‚ฏใƒˆใ‚’ใ‚ใ–ใ‚ใ– Array ใงใใ‚‹ใพใชใใฆใฏใ„ใ‘ใชใ„ใจใ„ใ†ใฎใฏ้ขๅ€’ใชๆฐ—ใ‚‚ใ—ใพใ™ใ€‚

ๆๆกˆยถ

String#unpack ใซๅฏพใ—ใฆ String#unpack1 ใจใ„ใ†ใƒกใ‚ฝใƒƒใƒ‰ใŒใ‚ใ‚Šใพใ™ใŒใ€
Array#pack ใซๅฏพใ™ใ‚‹ Object#pack1 ใจใ„ใ†ใƒกใ‚ฝใƒƒใƒ‰ใ‚’ๆๆกˆใ—ใพใ™ใ€‚

ใ‚คใƒกใƒผใ‚ธใจใ—ใฆใฏไปฅไธ‹ใฎใ‚ณใƒผใƒ‰ใฎใ‚ˆใ†ใชๆ„Ÿใ˜ใงใ™ใ€‚

class Object
  def pack1(template, option = {})
    [self].pack(template, **option)
  end
end

่ญฐ่ซ–ใƒป่ชฒ้กŒยถ

  • Object ใง่‰ฏใ„ใ‹ใฉใ†ใ‹ใฏ่ญฐ่ซ–ใฎไฝ™ๅœฐใŒใ‚ใ‚ใ†ใ‹ใจๆ€ใ„ใพใ™
  • ใƒกใ‚ฝใƒƒใƒ‰ๅใŒ pack1 ใง่‰ฏใ„ใ‹ใฏใ‚ใ‹ใ‚Šใพใ›ใ‚“ใŒใ€ไป–ใจใ‹ใถใ‚‹ๅฏ่ƒฝๆ€งใฏไฝŽใ„ใ‹ใจๆ€ใ„ใพใ™

Updated by knu (Akinori MUSHA) over 3 years ago

packใ—ใŸใ„ใ‚‚ใฎ๏ผˆใฎclass๏ผ‰ใฏใ™ใ”ใ้™ๅฎš็š„ใชใฎใงใ€Objectใซ็”Ÿใ‚„ใ™ใฎใฏใฉใ†ใ‹ใชใ‚ใจๆ€ใ„ใพใ—ใŸใ€‚

Updated by knu (Akinori MUSHA) over 3 years ago

String#format / String#% ใฎใ‚ˆใ†ใชๆ„Ÿใ˜ใงๆ›ธๅผใฎๆ–นใ‚’ใƒฌใ‚ทใƒผใƒใซใ™ใ‚‹ๆ–นใŒใ‚ˆใ•ใใ†ใงใ™ใŒใ€unpackใจใฎๅฏพ็งฐๆ€งใ‚’่€ƒใˆใ‚‹ใจๅๅ‰ใŒ้›ฃใ—ใ„ใ‹ใ‚‚ใ€‚

Updated by knu (Akinori MUSHA) over 3 years ago

ไปŠๆ—ฅใฎOffice Hourใง่ฉฑใ—ใŸใ‚“ใงใ™ใŒใ€ใƒฌใ‚ทใƒผใƒใ”ใจใซใใ‚Œใซใตใ•ใ‚ใ—ใ„ๆ›ธๅผใฃใฆ้™ๅฎšใ•ใ‚Œใ‚‹ใฏใšใชใฎใงใ€ไฝ•ใงใ‚‚ๅฑ‹ใฎpackใงใฏใชใใ€ใŸใจใˆใฐIntegerใชใ‚‰ใƒ“ใƒƒใƒˆๅน…ใจใ‚จใƒณใƒ‡ใ‚ฃใ‚ขใƒณใ‚’ๆŒ‡ๅฎšใ—ใฆใƒใ‚คใƒŠใƒช่กจ็พใ‚’ๅพ—ใ‚‹ใƒกใ‚ฝใƒƒใƒ‰ใ€Stringใชใ‚‰NUL-terminatedใชใƒใ‚คใƒŠใƒชใ‚’ๅพ—ใ‚‹ใƒกใ‚ฝใƒƒใƒ‰ใ€ใชใฉใ‚’ใใ‚Œใžใ‚Œ็”จๆ„ใ™ในใใ‹ใ‚‚ใ€ใจๆ€ใ„ใพใ—ใŸใ€‚

Updated by byroot (Jean Boussier) over 3 years ago

(using Google Translate to understand the discussion, sorry if I misunderstood or missed things).

Since Object#pack1 might be deemed a bit too invasive, an alternative could be String.pack1(format, arg), or even String.pack(format, *args).

Updated by jeremyevans0 (Jeremy Evans) almost 2 years ago

I developed a patch for this that implemented the feature using Array.pack1 and was going to create a new feature request for it, but I'm glad to see there already is an existing feature request for it. Here's my pull request for it: https://github.com/ruby/ruby/pull/8598

Updated by Eregon (Benoit Daloze) almost 2 years ago

Array.pack1(obj, format) -> String sounds weird since there is nothing about Array in there.
I think String.pack1(format, obj) is the best option.
(String#pack1(obj) is confusing because of existing Array#pack(format) which has the reverse order).

(IMO this is something a JIT should optimize but I understand that's hard on CRuby).

Updated by matz (Yukihiro Matsumoto) almost 2 years ago

Array.pack1 is unlikely because there is no connection between the responsibilities of the method and the Array class. I also disagree with String.pack1 for the same reason.
The most natural candidate is Object#pack1, but I question the need to pollute the namespace for this trivial method.

Matz.

Updated by tenderlovemaking (Aaron Patterson) almost 2 years ago

matz (Yukihiro Matsumoto) wrote in #note-7:

Array.pack1 is unlikely because there is no connection between the responsibilities of the method and the Array class. I also disagree with String.pack1 for the same reason.
The most natural candidate is Object#pack1, but I question the need to pollute the namespace for this trivial method.

Matz.

Feature #18897 introduced a specialized instruction for Array#hash, but it created a new instruction called opt_newarray_send. I think we could leverage that instruction for the case of an array literal + pack. That would avoid the array creation. I will try to make a patch.

Updated by mame (Yusuke Endoh) almost 2 years ago

If we introduce a new method for this, I think it would be better to design a more descriptive API instead of reusing a hacky pack format directive, such as 0x1234.to_binary_string(4, endian: :big) #=> "\x00\x00\x12\x34".

Actions

Also available in: Atom PDF

Like1
Like0Like0Like0Like0Like0Like0Like0Like0Like0