diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-08-31 10:47:44 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2006-08-31 10:47:44 +0000 |
commit | 54af80844fbcf132f85e9275673eaa66b72da996 (patch) | |
tree | 4509d254ff81222c7b4db1b34cbcc377c2311e54 /pack.c | |
parent | 25c50cd193d89ad0737219142bab191f12b8abe8 (diff) | |
download | ruby-54af80844fbcf132f85e9275673eaa66b72da996.tar.gz |
* ruby.h (struct RString): embed small strings.
(RSTRING_LEN): defined for accessing string members.
(RSTRING_PTR): ditto.
* string.c: use RSTRING_LEN and RSTRING_PTR.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10810 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'pack.c')
-rw-r--r-- | pack.c | 88 |
1 files changed, 42 insertions, 46 deletions
@@ -448,8 +448,8 @@ pack_pack(VALUE ary, VALUE fmt) #endif StringValue(fmt); - p = RSTRING(fmt)->ptr; - pend = p + RSTRING(fmt)->len; + p = RSTRING_PTR(fmt); + pend = p + RSTRING_LEN(fmt); res = rb_str_buf_new(0); items = RARRAY(ary)->len; @@ -460,7 +460,7 @@ pack_pack(VALUE ary, VALUE fmt) #define NEXTFROM (items-- > 0 ? RARRAY(ary)->ptr[idx++] : TOO_FEW) while (p < pend) { - if (RSTRING(fmt)->ptr + RSTRING(fmt)->len != pend) { + if (RSTRING_PTR(fmt) + RSTRING_LEN(fmt) != pend) { rb_raise(rb_eRuntimeError, "format string modified"); } type = *p++; /* get data type */ @@ -510,8 +510,8 @@ pack_pack(VALUE ary, VALUE fmt) } else { StringValue(from); - ptr = RSTRING(from)->ptr; - plen = RSTRING(from)->len; + ptr = RSTRING_PTR(from); + plen = RSTRING_LEN(from); OBJ_INFECT(res, from); } @@ -843,15 +843,14 @@ pack_pack(VALUE ary, VALUE fmt) case 'X': /* back up byte */ shrink: - plen = RSTRING(res)->len; + plen = RSTRING_LEN(res); if (plen < len) rb_raise(rb_eArgError, "X outside of string"); - RSTRING(res)->len = plen - len; - RSTRING(res)->ptr[plen - len] = '\0'; + rb_str_set_len(res, plen - len); break; case '@': /* null fill to absolute position */ - len -= RSTRING(res)->len; + len -= RSTRING_LEN(res); if (len > 0) goto grow; len = -len; if (len > 0) goto shrink; @@ -882,8 +881,8 @@ pack_pack(VALUE ary, VALUE fmt) case 'm': /* base64 encoded string */ from = NEXTFROM; StringValue(from); - ptr = RSTRING(from)->ptr; - plen = RSTRING(from)->len; + ptr = RSTRING_PTR(from); + plen = RSTRING_LEN(from); if (len <= 2) len = 45; @@ -913,9 +912,9 @@ pack_pack(VALUE ary, VALUE fmt) from = THISFROM; if (!NIL_P(from)) { StringValue(from); - if (RSTRING(from)->len < len) { + if (RSTRING_LEN(from) < len) { rb_raise(rb_eArgError, "too short buffer for P(%ld for %ld)", - RSTRING(from)->len, len); + RSTRING_LEN(from), len); } } len = 1; @@ -970,16 +969,16 @@ pack_pack(VALUE ary, VALUE fmt) ul >>= 7; } - if (RSTRING(buf)->len) { - bufs = RSTRING(buf)->ptr; - bufe = bufs + RSTRING(buf)->len - 1; + if (RSTRING_LEN(buf)) { + bufs = RSTRING_PTR(buf); + bufe = bufs + RSTRING_LEN(buf) - 1; *bufs &= 0x7f; /* clear continue bit */ while (bufs < bufe) { /* reverse */ c = *bufs; *bufs++ = *bufe; *bufe-- = c; } - rb_str_buf_cat(res, RSTRING(buf)->ptr, RSTRING(buf)->len); + rb_str_buf_cat(res, RSTRING_PTR(buf), RSTRING_LEN(buf)); } else { c = 0; @@ -1050,8 +1049,8 @@ qpencode(VALUE str, VALUE from, long len) { char buff[1024]; long i = 0, n = 0, prev = EOF; - unsigned char *s = (unsigned char*)RSTRING(from)->ptr; - unsigned char *send = s + RSTRING(from)->len; + unsigned char *s = (unsigned char*)RSTRING_PTR(from); + unsigned char *send = s + RSTRING_LEN(from); while (s < send) { if ((*s > 126) || @@ -1301,10 +1300,10 @@ pack_unpack(VALUE str, VALUE fmt) StringValue(str); StringValue(fmt); - s = RSTRING(str)->ptr; - send = s + RSTRING(str)->len; - p = RSTRING(fmt)->ptr; - pend = p + RSTRING(fmt)->len; + s = RSTRING_PTR(str); + send = s + RSTRING_LEN(str); + p = RSTRING_PTR(fmt); + pend = p + RSTRING_LEN(fmt); ary = rb_ary_new(); while (p < pend) { @@ -1398,7 +1397,7 @@ pack_unpack(VALUE str, VALUE fmt) len = (send - s) * 8; bits = 0; rb_ary_push(ary, bitstr = rb_str_new(0, len)); - t = RSTRING(bitstr)->ptr; + t = RSTRING_PTR(bitstr); for (i=0; i<len; i++) { if (i & 7) bits >>= 1; else bits = *s++; @@ -1418,7 +1417,7 @@ pack_unpack(VALUE str, VALUE fmt) len = (send - s) * 8; bits = 0; rb_ary_push(ary, bitstr = rb_str_new(0, len)); - t = RSTRING(bitstr)->ptr; + t = RSTRING_PTR(bitstr); for (i=0; i<len; i++) { if (i & 7) bits <<= 1; else bits = *s++; @@ -1438,7 +1437,7 @@ pack_unpack(VALUE str, VALUE fmt) len = (send - s) * 2; bits = 0; rb_ary_push(ary, bitstr = rb_str_new(0, len)); - t = RSTRING(bitstr)->ptr; + t = RSTRING_PTR(bitstr); for (i=0; i<len; i++) { if (i & 1) bits >>= 4; @@ -1460,7 +1459,7 @@ pack_unpack(VALUE str, VALUE fmt) len = (send - s) * 2; bits = 0; rb_ary_push(ary, bitstr = rb_str_new(0, len)); - t = RSTRING(bitstr)->ptr; + t = RSTRING_PTR(bitstr); for (i=0; i<len; i++) { if (i & 1) bits <<= 4; @@ -1714,7 +1713,7 @@ pack_unpack(VALUE str, VALUE fmt) case 'u': { VALUE buf = infected_str_new(0, (send - s)*3/4, str); - char *ptr = RSTRING(buf)->ptr; + char *ptr = RSTRING_PTR(buf); long total = 0; while (s < send && *s > ' ' && *s < 'a') { @@ -1724,9 +1723,9 @@ pack_unpack(VALUE str, VALUE fmt) hunk[3] = '\0'; len = (*s++ - ' ') & 077; total += len; - if (total > RSTRING(buf)->len) { - len -= total - RSTRING(buf)->len; - total = RSTRING(buf)->len; + if (total > RSTRING_LEN(buf)) { + len -= total - RSTRING_LEN(buf); + total = RSTRING_LEN(buf); } while (len > 0) { @@ -1760,9 +1759,8 @@ pack_unpack(VALUE str, VALUE fmt) else if (s < send && (s+1 == send || s[1] == '\n')) s += 2; /* possible checksum byte */ } - - RSTRING(buf)->ptr[total] = '\0'; - RSTRING(buf)->len = total; + + rb_str_set_len(buf, total); rb_ary_push(ary, buf); } break; @@ -1770,7 +1768,7 @@ pack_unpack(VALUE str, VALUE fmt) case 'm': { VALUE buf = infected_str_new(0, (send - s)*3/4, str); - char *ptr = RSTRING(buf)->ptr; + char *ptr = RSTRING_PTR(buf); int a = -1,b = -1,c = 0,d; static int first = 1; static int b64_xtable[256]; @@ -1805,8 +1803,7 @@ pack_unpack(VALUE str, VALUE fmt) *ptr++ = b << 4 | c >> 2; } } - *ptr = '\0'; - RSTRING(buf)->len = ptr - RSTRING(buf)->ptr; + rb_str_set_len(buf, ptr - RSTRING_PTR(buf)); rb_ary_push(ary, buf); } break; @@ -1814,7 +1811,7 @@ pack_unpack(VALUE str, VALUE fmt) case 'M': { VALUE buf = infected_str_new(0, send - s, str); - char *ptr = RSTRING(buf)->ptr; + char *ptr = RSTRING_PTR(buf); int c1, c2; while (s < send) { @@ -1834,20 +1831,19 @@ pack_unpack(VALUE str, VALUE fmt) } s++; } - *ptr = '\0'; - RSTRING(buf)->len = ptr - RSTRING(buf)->ptr; + rb_str_set_len(buf, ptr - RSTRING_PTR(buf)); rb_ary_push(ary, buf); } break; case '@': - if (len > RSTRING(str)->len) + if (len > RSTRING_LEN(str)) rb_raise(rb_eArgError, "@ outside of string"); - s = RSTRING(str)->ptr + len; + s = RSTRING_PTR(str) + len; break; case 'X': - if (len > s - RSTRING(str)->ptr) + if (len > s - RSTRING_PTR(str)) rb_raise(rb_eArgError, "X outside of string"); s -= len; break; @@ -1875,8 +1871,8 @@ pack_unpack(VALUE str, VALUE fmt) p = RARRAY(a)->ptr; pend = p + RARRAY(a)->len; while (p < pend) { - if (TYPE(*p) == T_STRING && RSTRING(*p)->ptr == t) { - if (len < RSTRING(*p)->len) { + if (TYPE(*p) == T_STRING && RSTRING_PTR(*p) == t) { + if (len < RSTRING_LEN(*p)) { tmp = rb_tainted_str_new(t, len); rb_str_associate(tmp, a); } @@ -1920,7 +1916,7 @@ pack_unpack(VALUE str, VALUE fmt) p = RARRAY(a)->ptr; pend = p + RARRAY(a)->len; while (p < pend) { - if (TYPE(*p) == T_STRING && RSTRING(*p)->ptr == t) { + if (TYPE(*p) == T_STRING && RSTRING_PTR(*p) == t) { tmp = *p; break; } |