diff options
author | guy Decoux <ts@moulon.inra.fr> | 2009-02-28 20:04:27 +0100 |
---|---|---|
committer | guy Decoux <ts@moulon.inra.fr> | 2009-02-28 20:04:27 +0100 |
commit | 9a643db7074054fb1af20497a5e36119a833b0c3 (patch) | |
tree | ca6a2d9152ab161cfe847b0fdff937542adf952f /mmap.c | |
parent | 37e4ca938b10acde659f03f69035dda34c49f437 (diff) | |
download | mmap-ruby-9a643db7074054fb1af20497a5e36119a833b0c3.zip mmap-ruby-9a643db7074054fb1af20497a5e36119a833b0c3.tar.gz |
mmap-0.1.4
Diffstat (limited to 'mmap.c')
-rw-r--r-- | mmap.c | 79 |
1 files changed, 76 insertions, 3 deletions
@@ -907,6 +907,8 @@ mm_concat(str1, str2) return str1; } +#if RUBY_VERSION_CODE < 171 + static VALUE mm_strip_bang(str) VALUE str; @@ -940,6 +942,69 @@ mm_strip_bang(str) return str; } +#else + +static VALUE +mm_lstrip_bang(str) + VALUE str; +{ + char *s, *t, *e; + mm_mmap *t_mm; + + GetMmap(str, t_mm, MM_MODIFY); + s = (char *)t_mm->addr; + e = t = s + t_mm->real; + while (s < t && ISSPACE(*s)) s++; + + if (t_mm->real != (t - s) && (t_mm->frozen & MM_FIXED)) { + rb_raise(rb_eTypeError, "try to change the size of a fixed map"); + } + t_mm->real = t - s; + if (s > (char *)t_mm->addr) { + memmove(t_mm->addr, s, t_mm->real); + ((char *)t_mm->addr)[t_mm->real] = '\0'; + return str; + } + return Qnil; +} + +static VALUE +mm_rstrip_bang(str) + VALUE str; +{ + char *s, *t, *e; + mm_mmap *t_mm; + + GetMmap(str, t_mm, MM_MODIFY); + s = (char *)t_mm->addr; + e = t = s + t_mm->real; + t--; + while (s <= t && ISSPACE(*t)) t--; + t++; + if (t_mm->real != (t - s) && (t_mm->frozen & MM_FIXED)) { + rb_raise(rb_eTypeError, "try to change the size of a fixed map"); + } + t_mm->real = t - s; + if (t < e) { + ((char *)t_mm->addr)[t_mm->real] = '\0'; + return str; + } + return Qnil; +} + +static VALUE +mm_strip_bang(str) + VALUE str; +{ + VALUE l = mm_lstrip_bang(str); + VALUE r = mm_str_rstrip_bang(str); + + if (NIL_P(l) && NIL_P(r)) return Qnil; + return str; +} + +#endif + #define MmapStr(b, recycle) \ do { \ recycle = 0; \ @@ -1079,7 +1144,7 @@ mm_bang_i(obj, flag, id, argc, argv) if (res == Qnil) return res; GetMmap(obj, t_mm, 0); t_mm->real = RSTRING(str)->len; - return res; + return (flag & MM_ORIGIN)?res:obj; } @@ -1424,7 +1489,7 @@ Init_mmap() rb_define_method(mm_cMap, "unlock", mm_munlock, 0); rb_define_method(mm_cMap, "extend", mm_extend, 1); - rb_define_method(mm_cMap, "freeze", mm_freeze, 1); + rb_define_method(mm_cMap, "freeze", mm_freeze, 0); rb_define_method(mm_cMap, "clone", mm_undefined, -1); rb_define_method(mm_cMap, "dup", mm_undefined, -1); rb_define_method(mm_cMap, "<=>", mm_cmp, 1); @@ -1497,10 +1562,18 @@ Init_mmap() rb_define_method(mm_cMap, "chop", mm_undefined, -1); rb_define_method(mm_cMap, "chomp", mm_undefined, -1); rb_define_method(mm_cMap, "strip", mm_undefined, -1); +#if RUBY_VERSION_CODE >= 171 + rb_define_method(mm_cMap, "lstrip", mm_undefined, -1); + rb_define_method(mm_cMap, "rstrip", mm_undefined, -1); +#endif rb_define_method(mm_cMap, "sub!", mm_sub_bang, -1); rb_define_method(mm_cMap, "gsub!", mm_gsub_bang, -1); - rb_define_method(mm_cMap, "strip!", mm_strip_bang, -1); + rb_define_method(mm_cMap, "strip!", mm_strip_bang, 0); +#if RUBY_VERSION_CODE >= 171 + rb_define_method(mm_cMap, "lstrip!", mm_lstrip_bang, 0); + rb_define_method(mm_cMap, "rstrip!", mm_rstrip_bang, 0); +#endif rb_define_method(mm_cMap, "chop!", mm_chop_bang, 0); rb_define_method(mm_cMap, "chomp!", mm_chomp_bang, -1); |