curl_escape 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 55be49ada64c12d1e7d0bbd158f564503728a235
4
- data.tar.gz: 9017ed915571561a448339e296634ed9b5c8c824
3
+ metadata.gz: 608c203ad1eb50f307905ac89d5a2b8ace3477c2
4
+ data.tar.gz: 242ff7d0f8a8c5e564065e99416f83f7bd7d15ad
5
5
  SHA512:
6
- metadata.gz: 721f4544d8503376390464694b7e98b8184249e63e4975c8ce322285f6c150db4c0e8bbe0b0ce3c43948358f5340667919d6b81a70d24c87c75588eaff62eba8
7
- data.tar.gz: 7031c858e2c48907ef19fcb7bf487f801dd586a44c82a60ef187d46848df4802b031eb9b2c30fbdb33a8e8be10e1a075ffc8e84ff9916243cdf323b0e5d85ef4
6
+ metadata.gz: e7a7225a5e790764589e7878b87b5517311b85d6275e9d0bf2b283df8291c4c1380654f97e3d36a77b5c55b160deef2a4d6e31ef8618dca405714dce4ede93ba
7
+ data.tar.gz: e7f941949d0cf7f345e19a773029e79daa65047cbe61c0aadafbc5121aed7956a5823cac101eea8295ad75e1b747c96ae75684c632b1f776a9902dd62549c98d
data/bench.rb CHANGED
@@ -6,13 +6,13 @@ require_relative './lib/curl_escape'
6
6
 
7
7
  Benchmark.ips do |x|
8
8
  x.config(time: 15, warmup: 5)
9
- x.report("cgi_escape") { CGI.escape("'Stop!' said Fred") }
10
- x.report("curl_escape") { CurlEscape.escape("'Stop!' said Fred") }
9
+ x.report("cgi_escape") { CGI.escape("'Stop!' said~Fred") }
10
+ x.report("curl_escape") { CurlEscape.escape("'Stop!' said~Fred") }
11
11
  x.compare!
12
12
  end
13
13
 
14
- p CGI.escape("'Stop!' said~Fred foo~~hoge")
15
- p CurlEscape.escape("'Stop!' said~Fred foo~~hoge")
14
+ p CGI.escape("'~Stop!' said~Fred foo~~~hoge")
15
+ p CurlEscape.escape("'~Stop!' said~Fred foo~~~hoge")
16
16
 
17
- p CGI.escape("'日本語あいうえお")
17
+ p CGI.escape("'日本語あいうえお")
18
18
  p CurlEscape.escape("'日本語あいうえお")
@@ -3,12 +3,12 @@
3
3
  #include <ruby.h>
4
4
  #include <ruby/encoding.h>
5
5
 
6
- char *alloc_output_buffer(char *cOutput);
6
+ #define EXPAND_SIZE 40
7
7
 
8
- VALUE ruby_curl_escape(VALUE self, VALUE str) {
9
- int cnt, pos;
8
+ static VALUE ruby_curl_escape(VALUE self, VALUE str) {
9
+ int n, pos, len;
10
10
  char c;
11
- char *cStr, *cOutput, *buf;
11
+ char *cStr, *cOutput;
12
12
  char escaped_tilde[3] = "%7E";
13
13
  VALUE output;
14
14
  rb_encoding *enc;
@@ -22,47 +22,45 @@ VALUE ruby_curl_escape(VALUE self, VALUE str) {
22
22
  cOutput = curl_easy_escape(curl, cStr, strlen(cStr));
23
23
 
24
24
  pos = 0;
25
- buf = alloc_output_buffer(cOutput);
26
25
  if (cOutput) {
27
- for (cnt = 0; (c = *(cOutput + cnt)) != '\0'; cnt++) {
26
+ for (n = 0; (c = *(cOutput + n)) != '\0'; n++) {
28
27
  if (c == '~') {
29
- memcpy(buf + pos, escaped_tilde, 3);
30
- pos += 2;
28
+ if (n - pos >= 2) {
29
+ memcpy(cOutput + pos, escaped_tilde, 3);
30
+ pos += 2;
31
+ } else {
32
+ cOutput = realloc(cOutput, strlen(cOutput) + EXPAND_SIZE);
33
+ len = strlen(cOutput + n);
34
+ memmove(cOutput + n + EXPAND_SIZE, cOutput + n, len + 1);
35
+ n += EXPAND_SIZE;
36
+ memcpy(cOutput + pos, escaped_tilde, 3);
37
+ pos += 2;
38
+ }
31
39
  } else if (c != '%') {
32
- buf[pos] = c;
33
- } else if (strncmp(cOutput + cnt, "%20", 3) == 0) {
34
- buf[pos] = '+';
35
- cnt += 2;
40
+ if (pos != n) {
41
+ *(cOutput + pos) = c;
42
+ }
43
+ } else if (strncmp(cOutput + n, "%20", 3) == 0) {
44
+ *(cOutput + pos) = '+';
45
+ n += 2;
36
46
  } else {
37
- buf[pos] = c;
47
+ if (pos != n) {
48
+ *(cOutput + pos) = c;
49
+ }
38
50
  }
39
51
  pos++;
40
52
  }
41
- buf[pos] = '\0';
53
+ *(cOutput + pos) = '\0';
42
54
 
43
55
  enc = rb_enc_get(str);
44
- output = rb_enc_str_new_cstr(buf, enc);
56
+ output = rb_enc_str_new_cstr(cOutput, enc);
45
57
  curl_free(cOutput);
46
- free(buf);
47
58
  return output;
48
59
  } else {
49
60
  return Qnil;
50
61
  }
51
62
  }
52
63
 
53
- char *alloc_output_buffer(char *cOutput) {
54
- int i, tilde_cnt;
55
- char *buf;
56
- tilde_cnt = 0;
57
- for (i = 0; *(cOutput + i) != '\0'; i++) {
58
- if (*(cOutput + i) == '~') {
59
- tilde_cnt++;
60
- }
61
- }
62
- buf = (char *)malloc(i + tilde_cnt * 2);
63
- return buf;
64
- }
65
-
66
64
  void Init_curl_escape(void) {
67
65
  VALUE module = rb_define_module("CurlEscape");
68
66
  rb_define_singleton_method(module, "escape", ruby_curl_escape, 1);
@@ -1,3 +1,3 @@
1
1
  module CurlEscape
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: curl_escape
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - joker1007
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-31 00:00:00.000000000 Z
11
+ date: 2016-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler