summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorTomoyuki Sahara <[email protected]>2017-02-09 11:07:55 +0900
committerTomoyuki Sahara <[email protected]>2017-02-09 11:07:55 +0900
commit88a7fedea413568a1ff0410e109ff55a03b63a5f (patch)
treeb489a66b51e9a8f0e30542e1c831ca396dcca2ff /src
parentf2d4880098d4e3661fba1dc5e38861006089f9e8 (diff)
downloadmruby-88a7fedea413568a1ff0410e109ff55a03b63a5f.tar.gz
mruby-88a7fedea413568a1ff0410e109ff55a03b63a5f.zip
support "x".
Diffstat (limited to 'src')
-rw-r--r--src/pack.c55
1 files changed, 44 insertions, 11 deletions
diff --git a/src/pack.c b/src/pack.c
index 9095ad03b..6a0075f5a 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -22,19 +22,20 @@ struct tmpl {
};
enum {
- PACK_DIR_CHAR, /* C */
- PACK_DIR_SHORT, /* S */
- PACK_DIR_LONG, /* L */
- PACK_DIR_QUAD, /* Q */
- //PACK_DIR_INT, /* i */
+ PACK_DIR_CHAR, /* C */
+ PACK_DIR_SHORT, /* S */
+ PACK_DIR_LONG, /* L */
+ PACK_DIR_QUAD, /* Q */
+ //PACK_DIR_INT, /* i */
//PACK_DIR_VAX,
- PACK_DIR_UTF8, /* U */
+ PACK_DIR_UTF8, /* U */
//PACK_DIR_BER,
- PACK_DIR_DOUBLE, /* E */
- PACK_DIR_FLOAT, /* f */
- PACK_DIR_STR, /* A */
- PACK_DIR_HEX, /* h */
- PACK_DIR_BASE64, /* m */
+ PACK_DIR_DOUBLE, /* E */
+ PACK_DIR_FLOAT, /* f */
+ PACK_DIR_STR, /* A */
+ PACK_DIR_HEX, /* h */
+ PACK_DIR_BASE64, /* m */
+ PACK_DIR_NUL, /* x */
PACK_DIR_INVALID
};
@@ -731,6 +732,26 @@ done:
return sptr - sptr0;
}
+static int
+pack_x(mrb_state *mrb, mrb_value src, mrb_value dst, mrb_int didx, long count, unsigned int flags)
+{
+ long i;
+
+ dst = str_len_ensure(mrb, dst, didx + count);
+ for (i = 0; i < count; i++) {
+ RSTRING_PTR(dst)[didx] = '\0';
+ }
+ return count;
+}
+
+static int
+unpack_x(mrb_state *mrb, const void *src, int slen, mrb_value ary, int count, unsigned int flags)
+{
+ if (slen < count) {
+ mrb_raise(mrb, E_ARGUMENT_ERROR, "x outside of string");
+ }
+ return count;
+}
static void
prepare_tmpl(mrb_state *mrb, struct tmpl *tmpl)
@@ -911,6 +932,10 @@ alias:
size = 2;
flags |= PACK_FLAG_LT;
break;
+ case 'x':
+ dir = PACK_DIR_NUL;
+ type = PACK_TYPE_NONE;
+ break;
case 'Z':
dir = PACK_DIR_STR;
type = PACK_TYPE_STRING;
@@ -982,6 +1007,10 @@ mrb_pack_pack(mrb_state *mrb, mrb_value ary)
if (dir == PACK_DIR_INVALID)
continue;
+ else if (dir == PACK_DIR_NUL) {
+ ridx += pack_x(mrb, mrb_nil_value(), result, ridx, count, flags);
+ continue;
+ }
for (; aidx < RARRAY_LEN(ary); aidx++) {
if (count == 0 && !(flags & PACK_FLAG_WIDTH))
@@ -1073,6 +1102,10 @@ mrb_pack_unpack(mrb_state *mrb, mrb_value str)
if (dir == PACK_DIR_INVALID)
continue;
+ else if (dir == PACK_DIR_NUL) {
+ srcidx += unpack_x(mrb, sptr, srclen - srcidx, result, count, flags);
+ continue;
+ }
if (flags & PACK_FLAG_COUNT2) {
sptr = (const unsigned char *)RSTRING_PTR(str) + srcidx;