Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/string_undump.rb
Instance Method Summary collapse
- #undump_badly ⇒ Object
- #undump_roughly ⇒ Object (also: #undump)
Instance Method Details
#undump_badly ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/string_undump.rb', line 4 def undump_badly hex = /[0-9a-fA-F]/ esctable = { '\n' => "\n", '\r' => "\r", '\t' => "\t", '\f' => "\f", '\v' => "\v", '\b' => "\b", '\a' => "\a", '\e' => "\e", } e = self.encoding s = if self[0] == '"' && self[-1] == '"' self[1..-2] else self.dup end s.gsub!(/\\\#\$/, '#$') s.gsub!(/\\\#@/, '#@') s.gsub!(/\\\#{/, '#{') s.gsub!(/\\"/, '"') s.gsub!(/\\\\/, '\\') s.gsub!(/\\[nrtfvbae]/) {|m| esctable[m]} s.gsub!(/\\u#{hex}{4}/) {|m| m[2..-1].hex.chr(e)} s.gsub!(/\\u{#{hex}+}/) {|m| m[3..-1].hex.chr(e)} s.gsub!(/(?:\\x#{hex}{2})+/) {|m| m.gsub(/\\x/, '').scan(/../).map(&:hex).pack("C*").force_encoding(e) } s end |
#undump_roughly ⇒ Object Also known as: undump
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'ext/string_undump/string_undump.c', line 166 static VALUE str_undump_roughly(VALUE str) { const char *s = StringValuePtr(str); const char *s_end = RSTRING_END(str); rb_encoding *enc = rb_enc_get(str); int n; unsigned int c; VALUE undumped = rb_enc_str_new(s, 0L, enc); rb_must_asciicompat(str); if (is_wrapped(s, s_end, enc)) { /* strip '"' at the begin and the end */ s++; s_end--; } for (; s < s_end; s += n) { c = rb_enc_codepoint_len(s, s_end, &n, enc); if (c == '\\') { if (s+1 >= s_end) { rb_raise(rb_eArgError, "invalid escape"); } n = undump_after_backslash(undumped, s+1, s_end, enc); } else { rb_str_cat(undumped, s, n); } } OBJ_INFECT(undumped, str); return undumped; } |