string_undump 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.travis.yml +32 -0
- data/BSDL +22 -0
- data/COPYING +56 -0
- data/Gemfile +3 -0
- data/README.md +44 -0
- data/Rakefile +13 -0
- data/appveyor.yml +23 -0
- data/ext/string_undump/extconf.rb +3 -0
- data/ext/string_undump/string_undump.c +204 -0
- data/ext/string_undump/string_undump.h +23 -0
- data/lib/string_undump.rb +37 -0
- data/string_undump.gemspec +22 -0
- data/test/test_undump.rb +51 -0
- data/test/test_undump_euc-jp.rb +15 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 988fad4576888bdb14111f817871a840462f4f07d10e51dd803bf36740353e85
|
4
|
+
data.tar.gz: 0a6869d30b81b2067cd34e563ceef3bd512a2f15e677cd2799a77364de7c6959
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 212984f8f83fda5b43a00735872a3583edfca1abc87f67a1233127eaee0968d5d9312aa7a6d4228309254ed4baad213596dda3e0d5ab6c3bc0a93790e01da3ad
|
7
|
+
data.tar.gz: 46e46fb39125d8d1ee231ffcf26a4e4ab616ac4d434e6c8bef81fa15e2e1c94416432252c008be519d6d45187ec45a0b89860c0f65da2770235d4305bfbef49d
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- ruby-head
|
4
|
+
- 2.4.2
|
5
|
+
- 2.3.5
|
6
|
+
- 2.2.8
|
7
|
+
- 2.1.10
|
8
|
+
- 2.0.0
|
9
|
+
compiler:
|
10
|
+
- gcc
|
11
|
+
- clang
|
12
|
+
os:
|
13
|
+
- linux
|
14
|
+
- osx
|
15
|
+
osx_image: xcode9.1
|
16
|
+
matrix:
|
17
|
+
exclude:
|
18
|
+
- rvm: 2.0.0
|
19
|
+
os: osx
|
20
|
+
include:
|
21
|
+
- rvm: 2.0.0
|
22
|
+
os: osx
|
23
|
+
osx_image: xcode7.3
|
24
|
+
before_install:
|
25
|
+
- bundle install
|
26
|
+
install:
|
27
|
+
- rake install
|
28
|
+
script:
|
29
|
+
- rake test
|
30
|
+
notification:
|
31
|
+
email:
|
32
|
+
- tad.a.digger@gmail.com
|
data/BSDL
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (C) 2017- Tadashi Saito. All rights reserved.
|
2
|
+
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
4
|
+
modification, are permitted provided that the following conditions
|
5
|
+
are met:
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
13
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
14
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
15
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
16
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
17
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
18
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
19
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
20
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
21
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
22
|
+
SUCH DAMAGE.
|
data/COPYING
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
String#undump is copyrighted free software by Tadashi Saito <tad.a.digger@gmail.com>.
|
2
|
+
You can redistribute it and/or modify it under either the terms of the
|
3
|
+
2-clause BSDL (see the file BSDL), or the conditions below:
|
4
|
+
|
5
|
+
1. You may make and give away verbatim copies of the source form of the
|
6
|
+
software without restriction, provided that you duplicate all of the
|
7
|
+
original copyright notices and associated disclaimers.
|
8
|
+
|
9
|
+
2. You may modify your copy of the software in any way, provided that
|
10
|
+
you do at least ONE of the following:
|
11
|
+
|
12
|
+
a) place your modifications in the Public Domain or otherwise
|
13
|
+
make them Freely Available, such as by posting said
|
14
|
+
modifications to Usenet or an equivalent medium, or by allowing
|
15
|
+
the author to include your modifications in the software.
|
16
|
+
|
17
|
+
b) use the modified software only within your corporation or
|
18
|
+
organization.
|
19
|
+
|
20
|
+
c) give non-standard binaries non-standard names, with
|
21
|
+
instructions on where to get the original software distribution.
|
22
|
+
|
23
|
+
d) make other distribution arrangements with the author.
|
24
|
+
|
25
|
+
3. You may distribute the software in object code or binary form,
|
26
|
+
provided that you do at least ONE of the following:
|
27
|
+
|
28
|
+
a) distribute the binaries and library files of the software,
|
29
|
+
together with instructions (in the manual page or equivalent)
|
30
|
+
on where to get the original distribution.
|
31
|
+
|
32
|
+
b) accompany the distribution with the machine-readable source of
|
33
|
+
the software.
|
34
|
+
|
35
|
+
c) give non-standard binaries non-standard names, with
|
36
|
+
instructions on where to get the original software distribution.
|
37
|
+
|
38
|
+
d) make other distribution arrangements with the author.
|
39
|
+
|
40
|
+
4. You may modify and include the part of the software into any other
|
41
|
+
software (possibly commercial). But some files in the distribution
|
42
|
+
are not written by the author, so that they are not under these terms.
|
43
|
+
|
44
|
+
For the list of those files and their copying conditions, see the
|
45
|
+
file LEGAL.
|
46
|
+
|
47
|
+
5. The scripts and library files supplied as input to or produced as
|
48
|
+
output from the software do not automatically fall under the
|
49
|
+
copyright of the software, but belong to whomever generated them,
|
50
|
+
and may be sold commercially, and may be aggregated with this
|
51
|
+
software.
|
52
|
+
|
53
|
+
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
54
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
55
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
56
|
+
PURPOSE.
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# String#undump
|
2
|
+
|
3
|
+
Unofficial implementation of String#undump, does inverse of String#dump.
|
4
|
+
See [Feature #12275](https://bugs.ruby-lang.org/issues/12275) for details.
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
require 'string_undump'
|
10
|
+
|
11
|
+
puts '\u3059\u3054\u30FC\u3044'.undump #=> すごーい
|
12
|
+
puts '\xE3\x81\x9F\xE3\x81\xAE\xE3\x81\x97\xE3\x83\xBC'.undump #=> たのしー
|
13
|
+
puts '\u{1F43E}'.undump #=> 🐾
|
14
|
+
```
|
15
|
+
|
16
|
+
See [testcase](https://github.com/tadd/string_undump/blob/master/test/test_undump.rb) also.
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'string_undump'
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install string_undump
|
33
|
+
|
34
|
+
## Supported Ruby versions
|
35
|
+
|
36
|
+
2.0.0 or later.
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tadd/string_undump.
|
41
|
+
|
42
|
+
## License
|
43
|
+
|
44
|
+
Ruby's. See [COPYING](COPYING).
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/extensiontask'
|
4
|
+
|
5
|
+
task :build => :compile
|
6
|
+
|
7
|
+
Rake::TestTask.new {|t| t.libs << 'test' }
|
8
|
+
|
9
|
+
Rake::ExtensionTask.new("string_undump") do |ext|
|
10
|
+
ext.lib_dir = "lib/string_undump"
|
11
|
+
end
|
12
|
+
|
13
|
+
task :default => :test
|
data/appveyor.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
environment:
|
2
|
+
matrix:
|
3
|
+
- RUBY_VERSION: 24
|
4
|
+
- RUBY_VERSION: 24-x64
|
5
|
+
- RUBY_VERSION: 23
|
6
|
+
- RUBY_VERSION: 23-x64
|
7
|
+
- RUBY_VERSION: 22
|
8
|
+
- RUBY_VERSION: 22-x64
|
9
|
+
- RUBY_VERSION: 21
|
10
|
+
- RUBY_VERSION: 21-x64
|
11
|
+
- RUBY_VERSION: 200
|
12
|
+
- RUBY_VERSION: 200-x64
|
13
|
+
install:
|
14
|
+
- set PATH=C:\Ruby%RUBY_VERSION%\bin;C:\Ruby%RUBY_VERSION%\DevKit\bin;%PATH%
|
15
|
+
- bundle install
|
16
|
+
- rake -rdevkit install
|
17
|
+
build: off
|
18
|
+
before_test:
|
19
|
+
- ruby -v
|
20
|
+
- gem -v
|
21
|
+
- bundle -v
|
22
|
+
test_script:
|
23
|
+
- rake test
|
@@ -0,0 +1,204 @@
|
|
1
|
+
#include "string_undump.h"
|
2
|
+
|
3
|
+
/* is +str+ wrapped with '"'? */
|
4
|
+
static int
|
5
|
+
is_wrapped(const char *s, const char *s_end, rb_encoding *enc)
|
6
|
+
{
|
7
|
+
unsigned int cbeg, cend;
|
8
|
+
const char *prev;
|
9
|
+
|
10
|
+
cbeg = rb_enc_mbc_to_codepoint(s, s_end, enc);
|
11
|
+
if (cbeg != '"') return FALSE;
|
12
|
+
|
13
|
+
prev = rb_enc_prev_char(s, s_end, s_end, enc);
|
14
|
+
cend = rb_enc_mbc_to_codepoint(prev, s_end, enc);
|
15
|
+
return cend == '"';
|
16
|
+
}
|
17
|
+
|
18
|
+
static const char *
|
19
|
+
unescape_ascii(unsigned int c)
|
20
|
+
{
|
21
|
+
switch (c) {
|
22
|
+
case 'n':
|
23
|
+
return "\n";
|
24
|
+
case 'r':
|
25
|
+
return "\r";
|
26
|
+
case 't':
|
27
|
+
return "\t";
|
28
|
+
case 'f':
|
29
|
+
return "\f";
|
30
|
+
case 'v':
|
31
|
+
return "\v";
|
32
|
+
case 'b':
|
33
|
+
return "\b";
|
34
|
+
case 'a':
|
35
|
+
return "\a";
|
36
|
+
case 'e':
|
37
|
+
return "\e";
|
38
|
+
default:
|
39
|
+
UNREACHABLE;
|
40
|
+
}
|
41
|
+
}
|
42
|
+
|
43
|
+
/* copied from ruby/string.c:rb_strseq_index */
|
44
|
+
static const char *
|
45
|
+
find_close_brace(const char *s, const char *s_end, rb_encoding *enc)
|
46
|
+
{
|
47
|
+
const char *search_start;
|
48
|
+
long search_len = s_end - s, pos;
|
49
|
+
|
50
|
+
search_start = s;
|
51
|
+
for (;;) {
|
52
|
+
const char *t;
|
53
|
+
pos = rb_memsearch("}", 1, search_start, search_len, enc);
|
54
|
+
if (pos < 0) return NULL;
|
55
|
+
t = rb_enc_right_char_head(search_start, search_start+pos, s_end, enc);
|
56
|
+
if (t == search_start + pos) break;
|
57
|
+
search_len -= t - search_start;
|
58
|
+
if (search_len <= 0) return NULL;
|
59
|
+
search_start = t;
|
60
|
+
}
|
61
|
+
return s + pos;
|
62
|
+
}
|
63
|
+
|
64
|
+
/* definition copied from ruby/string.c */
|
65
|
+
#define IS_EVSTR(p,e) ((p) < (e) && (*(p) == '$' || *(p) == '@' || *(p) == '{'))
|
66
|
+
|
67
|
+
static int
|
68
|
+
undump_after_backslash(VALUE undumped, const char *s, const char *s_end, rb_encoding *enc)
|
69
|
+
{
|
70
|
+
unsigned int c, c2;
|
71
|
+
int n, n2, codelen;
|
72
|
+
size_t hexlen;
|
73
|
+
char buf[6];
|
74
|
+
|
75
|
+
c = rb_enc_codepoint_len(s, s_end, &n, enc);
|
76
|
+
switch (c) {
|
77
|
+
case '\\':
|
78
|
+
case '"':
|
79
|
+
rb_str_cat(undumped, s, n); /* cat itself */
|
80
|
+
n++;
|
81
|
+
break;
|
82
|
+
case 'n':
|
83
|
+
case 'r':
|
84
|
+
case 't':
|
85
|
+
case 'f':
|
86
|
+
case 'v':
|
87
|
+
case 'b':
|
88
|
+
case 'a':
|
89
|
+
case 'e':
|
90
|
+
rb_str_cat(undumped, unescape_ascii(c), n);
|
91
|
+
n++;
|
92
|
+
break;
|
93
|
+
case 'u':
|
94
|
+
if (s+1 >= s_end) {
|
95
|
+
rb_raise(rb_eArgError, "invalid Unicode escape");
|
96
|
+
}
|
97
|
+
c2 = rb_enc_codepoint_len(s+1, s_end, NULL, enc);
|
98
|
+
if (c2 == '{') { /* handle \u{...} form */
|
99
|
+
const char *p;
|
100
|
+
unsigned int hex;
|
101
|
+
|
102
|
+
if (s+2 >= s_end) {
|
103
|
+
rb_raise(rb_eArgError, "unterminated Unicode escape");
|
104
|
+
}
|
105
|
+
p = find_close_brace(s+2, s_end, enc);
|
106
|
+
if (p == NULL) {
|
107
|
+
rb_raise(rb_eArgError, "unterminated Unicode escape");
|
108
|
+
}
|
109
|
+
hex = ruby_scan_hex(s+2, p-(s+2)+1, &hexlen);
|
110
|
+
if (hexlen == 0 || hexlen > 6) {
|
111
|
+
rb_raise(rb_eArgError, "invalid Unicode escape");
|
112
|
+
}
|
113
|
+
if (hex > 0x10ffffU) {
|
114
|
+
rb_raise(rb_eArgError, "invalid Unicode codepoint (too large)");
|
115
|
+
}
|
116
|
+
if ((hex & 0xfffff800U) == 0xd800U) {
|
117
|
+
rb_raise(rb_eArgError, "invalid Unicode codepoint");
|
118
|
+
}
|
119
|
+
codelen = rb_enc_codelen(hex, enc);
|
120
|
+
rb_enc_mbcput(hex, buf, enc);
|
121
|
+
rb_str_cat(undumped, buf, codelen);
|
122
|
+
n += rb_strlen_lit("u{}") + hexlen;
|
123
|
+
}
|
124
|
+
else { /* handle \uXXXX form */
|
125
|
+
unsigned int hex = ruby_scan_hex(s+1, 4, &hexlen);
|
126
|
+
if (hexlen != 4) {
|
127
|
+
rb_raise(rb_eArgError, "invalid Unicode escape");
|
128
|
+
}
|
129
|
+
codelen = rb_enc_codelen(hex, enc);
|
130
|
+
rb_enc_mbcput(hex, buf, enc);
|
131
|
+
rb_str_cat(undumped, buf, codelen);
|
132
|
+
n += rb_strlen_lit("uXXXX");
|
133
|
+
}
|
134
|
+
break;
|
135
|
+
case 'x':
|
136
|
+
if (s+1 >= s_end) {
|
137
|
+
rb_raise(rb_eArgError, "invalid hex escape");
|
138
|
+
}
|
139
|
+
c2 = ruby_scan_hex(s+1, 2, &hexlen);
|
140
|
+
if (hexlen != 2) {
|
141
|
+
rb_raise(rb_eArgError, "invalid hex escape");
|
142
|
+
}
|
143
|
+
*buf = (char)c2;
|
144
|
+
rb_str_cat(undumped, buf, 1L);
|
145
|
+
n += rb_strlen_lit("xXX");
|
146
|
+
break;
|
147
|
+
case '#':
|
148
|
+
if (s+1 >= s_end) {
|
149
|
+
rb_str_cat(undumped, s, 1L); /* just '#' */
|
150
|
+
n++;
|
151
|
+
break;
|
152
|
+
}
|
153
|
+
n2 = rb_enc_mbclen(s+1, s_end, enc);
|
154
|
+
if (n2 == 1 && IS_EVSTR(s+1, s_end)) {
|
155
|
+
rb_str_cat(undumped, s, n);
|
156
|
+
n += n2;
|
157
|
+
}
|
158
|
+
break;
|
159
|
+
default:
|
160
|
+
rb_str_cat(undumped, "\\", 1L); /* keep backslash */
|
161
|
+
}
|
162
|
+
|
163
|
+
return n;
|
164
|
+
}
|
165
|
+
|
166
|
+
static VALUE
|
167
|
+
str_undump_roughly(VALUE str)
|
168
|
+
{
|
169
|
+
const char *s = StringValuePtr(str);
|
170
|
+
const char *s_end = RSTRING_END(str);
|
171
|
+
rb_encoding *enc = rb_enc_get(str);
|
172
|
+
int n;
|
173
|
+
unsigned int c;
|
174
|
+
VALUE undumped = rb_enc_str_new(s, 0L, enc);
|
175
|
+
|
176
|
+
rb_must_asciicompat(str);
|
177
|
+
|
178
|
+
if (is_wrapped(s, s_end, enc)) {
|
179
|
+
/* strip '"' at the begin and the end */
|
180
|
+
s++;
|
181
|
+
s_end--;
|
182
|
+
}
|
183
|
+
|
184
|
+
for (; s < s_end; s += n) {
|
185
|
+
c = rb_enc_codepoint_len(s, s_end, &n, enc);
|
186
|
+
if (c == '\\') {
|
187
|
+
if (s+1 >= s_end) {
|
188
|
+
rb_raise(rb_eArgError, "invalid escape");
|
189
|
+
}
|
190
|
+
n = undump_after_backslash(undumped, s+1, s_end, enc);
|
191
|
+
}
|
192
|
+
else {
|
193
|
+
rb_str_cat(undumped, s, n);
|
194
|
+
}
|
195
|
+
}
|
196
|
+
|
197
|
+
return undumped;
|
198
|
+
}
|
199
|
+
|
200
|
+
void
|
201
|
+
Init_string_undump(void)
|
202
|
+
{
|
203
|
+
rb_define_method(rb_cString, "undump_roughly", str_undump_roughly, 0);
|
204
|
+
}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
#ifndef EXT_STRING_UNDUMP_H_INCLUDED
|
2
|
+
#define EXT_STRING_UNDUMP_H_INCLUDED 1
|
3
|
+
|
4
|
+
#include <ruby.h>
|
5
|
+
#include <ruby/encoding.h>
|
6
|
+
#include <ruby/util.h>
|
7
|
+
|
8
|
+
#ifndef FALSE
|
9
|
+
# define FALSE 0
|
10
|
+
#elif FALSE
|
11
|
+
# error FALSE must be false
|
12
|
+
#endif
|
13
|
+
#ifndef TRUE
|
14
|
+
# define TRUE 1
|
15
|
+
#elif !TRUE
|
16
|
+
# error TRUE must be true
|
17
|
+
#endif
|
18
|
+
|
19
|
+
#ifndef rb_strlen_lit
|
20
|
+
#define rb_strlen_lit(str) (sizeof(str "") - 1)
|
21
|
+
#endif
|
22
|
+
|
23
|
+
#endif /* EXT_STRING_UNDUMP_H_INCLUDED */
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require_relative 'string_undump/string_undump.so'
|
2
|
+
|
3
|
+
class String
|
4
|
+
def undump_badly
|
5
|
+
hex = /[0-9a-fA-F]/
|
6
|
+
esctable = {
|
7
|
+
'\n' => "\n",
|
8
|
+
'\r' => "\r",
|
9
|
+
'\t' => "\t",
|
10
|
+
'\f' => "\f",
|
11
|
+
'\v' => "\v",
|
12
|
+
'\b' => "\b",
|
13
|
+
'\a' => "\a",
|
14
|
+
'\e' => "\e",
|
15
|
+
}
|
16
|
+
e = self.encoding
|
17
|
+
s = if self[0] == '"' && self[-1] == '"'
|
18
|
+
self[1..-2]
|
19
|
+
else
|
20
|
+
self.dup
|
21
|
+
end
|
22
|
+
s.gsub!(/\\\#\$/, '#$')
|
23
|
+
s.gsub!(/\\\#@/, '#@')
|
24
|
+
s.gsub!(/\\\#{/, '#{')
|
25
|
+
s.gsub!(/\\"/, '"')
|
26
|
+
s.gsub!(/\\\\/, '\\')
|
27
|
+
s.gsub!(/\\[nrtfvbae]/) {|m| esctable[m]}
|
28
|
+
s.gsub!(/\\u#{hex}{4}/) {|m| m[2..-1].hex.chr(e)}
|
29
|
+
s.gsub!(/\\u{#{hex}+}/) {|m| m[3..-1].hex.chr(e)}
|
30
|
+
s.gsub!(/(?:\\x#{hex}{2})+/) {|m|
|
31
|
+
m.gsub(/\\x/, '').scan(/../).map(&:hex).pack("C*").force_encoding(e)
|
32
|
+
}
|
33
|
+
s
|
34
|
+
end
|
35
|
+
|
36
|
+
alias_method(:undump, :undump_roughly) unless self.new.respond_to?(:undump)
|
37
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "string_undump"
|
3
|
+
spec.version = '0.1.0'
|
4
|
+
spec.authors = ["Tadashi Saito"]
|
5
|
+
spec.email = ["tad.a.digger@gmail.com"]
|
6
|
+
|
7
|
+
spec.summary = 'String#undump'
|
8
|
+
spec.description = 'Unofficial implementation of String#undump, does inverse of String#dump'
|
9
|
+
spec.homepage = 'https://github.com/tadd/string_undump'
|
10
|
+
spec.license = "Ruby's"
|
11
|
+
|
12
|
+
files = `git ls-files -z`.split("\x0")
|
13
|
+
spec.files = files.reject {|f| f.match(%r{^(test|spec|features)/})}
|
14
|
+
spec.test_files = files.grep(%r{^(test|spec|features)/})
|
15
|
+
spec.require_paths = ["lib"]
|
16
|
+
spec.extensions = ["ext/string_undump/extconf.rb"]
|
17
|
+
|
18
|
+
spec.add_development_dependency "bundler"
|
19
|
+
spec.add_development_dependency "rake"
|
20
|
+
spec.add_development_dependency "test-unit"
|
21
|
+
spec.add_development_dependency "rake-compiler"
|
22
|
+
end
|
data/test/test_undump.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'test-unit'
|
2
|
+
|
3
|
+
require_relative '../lib/string_undump'
|
4
|
+
|
5
|
+
class TestUndump < Test::Unit::TestCase
|
6
|
+
def test_undump_badly
|
7
|
+
assert_equal('foo', '"foo"'.undump_badly)
|
8
|
+
assert_equal('foo#$bar#@baz#{quxx}', 'foo\#$bar\#@baz\#{quxx}'.undump_badly)
|
9
|
+
assert_equal('\\', '\\\\'.undump_badly)
|
10
|
+
assert_equal(%(\\"), '\\\\"'.undump_badly)
|
11
|
+
assert_equal("\n", '\n'.undump_badly)
|
12
|
+
assert_equal(%(\\"\n), '\\\\\\"\n'.undump_badly)
|
13
|
+
assert_equal('すごーい', '\u3059\u3054\u30FC\u3044'.undump_badly)
|
14
|
+
assert_equal('たのしー', '\xE3\x81\x9F\xE3\x81\xAE\xE3\x81\x97\xE3\x83\xBC'.undump_badly)
|
15
|
+
assert_equal('🐾', '\u{1F43E}'.undump_badly)
|
16
|
+
assert_equal(%(すごーい\\🐾たのしー\n\#{foo}),
|
17
|
+
('"\u3059\u3054\u30FC\u3044\\\\\u{1F43E}' +
|
18
|
+
'\xE3\x81\x9F\xE3\x81\xAE\xE3\x81\x97\xE3\x83\xBC\\n\#{foo}"').undump_badly)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_undump_roughly
|
22
|
+
assert_equal('foo', '"foo"'.undump_roughly)
|
23
|
+
assert_equal('foo#$bar#@baz#{quxx}', 'foo\#$bar\#@baz\#{quxx}'.undump_roughly)
|
24
|
+
assert_equal('\\', '\\\\'.undump_roughly)
|
25
|
+
assert_equal(%(\\"), '\\\\"'.undump_roughly)
|
26
|
+
assert_equal("\n", '\n'.undump_roughly)
|
27
|
+
assert_equal(%(\\"\n), '\\\\\\"\n'.undump_roughly)
|
28
|
+
assert_equal('すごーい', '\u3059\u3054\u30FC\u3044'.undump_roughly)
|
29
|
+
assert_equal('\\a\\', '\\\\a\\\\'.undump_roughly)
|
30
|
+
assert_equal("\nn", '\nn'.undump_roughly)
|
31
|
+
assert_equal("\u30593059", '\u30593059'.undump_roughly)
|
32
|
+
assert_equal('たのしー', '\xE3\x81\x9F\xE3\x81\xAE\xE3\x81\x97\xE3\x83\xBC'.undump_roughly)
|
33
|
+
assert_equal('🐾', '\u{1F43E}'.undump_roughly)
|
34
|
+
assert_equal(%(すごーい\\🐾たのしー\n\#{foo}),
|
35
|
+
('"\u3059\u3054\u30FC\u3044\\\\\u{1F43E}' +
|
36
|
+
'\xE3\x81\x9F\xE3\x81\xAE\xE3\x81\x97\xE3\x83\xBC\\n\#{foo}"').undump_roughly)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_undump_roughly_error
|
40
|
+
assert_raise(ArgumentError.new('invalid Unicode escape')) {'\u'.undump_roughly}
|
41
|
+
assert_raise(ArgumentError.new('unterminated Unicode escape')) {'\u{'.undump_roughly}
|
42
|
+
assert_raise(ArgumentError.new('unterminated Unicode escape')) {'\u{3042'.undump_roughly}
|
43
|
+
assert_raise(ArgumentError.new('invalid hex escape')) {'\x'.undump_roughly}
|
44
|
+
assert_equal('#', '\#'.undump_roughly)
|
45
|
+
assert_raise(ArgumentError.new('invalid escape')) {'\\'.undump_roughly}
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_undump
|
49
|
+
assert_includes(String.instance_methods, :undump)
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: euc-jp
|
2
|
+
# test for multibyte and non-Unicode string
|
3
|
+
require 'test-unit'
|
4
|
+
|
5
|
+
require_relative '../lib/string_undump'
|
6
|
+
|
7
|
+
class TestUndumpEucJp < Test::Unit::TestCase
|
8
|
+
def test_undump_badly
|
9
|
+
assert_equal('��������', '\xA4\xB9\xA4\xB4\xA1\xBC\xA4\xA4'.undump_badly)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_undump_roughly
|
13
|
+
assert_equal('��������', '\xA4\xB9\xA4\xB4\xA1\xBC\xA4\xA4'.undump_roughly)
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: string_undump
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tadashi Saito
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: test-unit
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake-compiler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Unofficial implementation of String#undump, does inverse of String#dump
|
70
|
+
email:
|
71
|
+
- tad.a.digger@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions:
|
74
|
+
- ext/string_undump/extconf.rb
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".travis.yml"
|
79
|
+
- BSDL
|
80
|
+
- COPYING
|
81
|
+
- Gemfile
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- appveyor.yml
|
85
|
+
- ext/string_undump/extconf.rb
|
86
|
+
- ext/string_undump/string_undump.c
|
87
|
+
- ext/string_undump/string_undump.h
|
88
|
+
- lib/string_undump.rb
|
89
|
+
- string_undump.gemspec
|
90
|
+
- test/test_undump.rb
|
91
|
+
- test/test_undump_euc-jp.rb
|
92
|
+
homepage: https://github.com/tadd/string_undump
|
93
|
+
licenses:
|
94
|
+
- Ruby's
|
95
|
+
metadata: {}
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.7.2
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: String#undump
|
116
|
+
test_files:
|
117
|
+
- test/test_undump.rb
|
118
|
+
- test/test_undump_euc-jp.rb
|