kingpong-bitwise_string_ops 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,57 @@
1
+ commit 678935a5904007ccd44178f961c5ab525dba4811
2
+ Author: kingpong <philip@pastemagazine.com>
3
+ Date: Sun Aug 23 04:18:23 2009 -0400
4
+
5
+ updated documentation
6
+
7
+ commit 6d5bf6876529fc5c515d3ba108b2e1b6b4fd6184
8
+ Author: kingpong <philip@pastemagazine.com>
9
+ Date: Sun Aug 23 03:12:56 2009 -0400
10
+
11
+ expanded tabs
12
+
13
+ commit a3d7746d9f09d42593fa9446c4fb2bead2894d29
14
+ Author: kingpong <philip@pastemagazine.com>
15
+ Date: Sun Aug 23 03:02:40 2009 -0400
16
+
17
+ handle non-strings
18
+
19
+ commit 200fcb33e1479246c6229998de42ad3c27cdf00e
20
+ Author: kingpong <philip@pastemagazine.com>
21
+ Date: Sun Aug 23 01:42:07 2009 -0400
22
+
23
+ added test cases
24
+
25
+ commit 3f03b37a4deb5a578fe991b1e09f74143f74fc45
26
+ Author: kingpong <philip@pastemagazine.com>
27
+ Date: Sat Aug 22 23:34:32 2009 -0400
28
+
29
+ removed intermediate method names for the operators
30
+
31
+ commit 383ce0eca96503c6729eab72d63aad2ae84eee85
32
+ Author: kingpong <philip@pastemagazine.com>
33
+ Date: Sat Aug 22 23:27:25 2009 -0400
34
+
35
+ ignore doc dir
36
+
37
+ commit 3f94a2438be1c3da43478900d4fa2a07b50181e9
38
+ Author: kingpong <philip@pastemagazine.com>
39
+ Date: Sat Aug 22 23:27:03 2009 -0400
40
+
41
+ fixed unicode issue
42
+
43
+ commit 33ea54dfb0624905bc94ea384b58b9fe87e08cc6
44
+ Author: kingpong <philip@pastemagazine.com>
45
+ Date: Sat Aug 22 20:42:14 2009 -0400
46
+
47
+ increment version num for github gem builder
48
+
49
+ commit 33f1b27e41bc395637645b89ea37f7362094c8ab
50
+ Author: kingpong <philip@pastemagazine.com>
51
+ Date: Sat Aug 22 19:41:33 2009 -0400
52
+
53
+ include changelog and manifest in version control
54
+
1
55
  commit 074c9142a5b66b146e70e185566931e285867d06
2
56
  Author: kingpong <philip@pastemagazine.com>
3
57
  Date: Sat Aug 22 19:28:48 2009 -0400
data/Manifest CHANGED
@@ -5,14 +5,15 @@ ext/bitwise_string_ops.c
5
5
  ext/bitwise_string_ops.h
6
6
  ext/extconf.rb
7
7
  init.rb
8
- lib/bitwise_string_ops.rb
9
8
  Manifest
10
9
  Rakefile
11
10
  README.rdoc
12
11
  setup.rb
13
12
  test/test.rb
14
13
  test/test_helper.rb
14
+ test/test_perl_equivalence.rb
15
15
  test/test_string_and.rb
16
16
  test/test_string_not.rb
17
17
  test/test_string_or.rb
18
18
  test/test_string_xor.rb
19
+ test/testcases.pl
@@ -33,41 +33,41 @@ For strings of the same length, nothing unexpected:
33
33
 
34
34
  "r b " ^ " u y" # "RUBY"
35
35
  #
36
- # 01110010 00100000 01100010 00100000 ^ # "r b " ^
37
- # 00100000 01110101 00100000 01111001 # " u y"
38
- # ======== ======== ======== ========
39
- # 01010010 01010101 01000010 01011001 # "RUBY"
36
+ # 01110010 00100000 01100010 00100000 # "r b "
37
+ # ^ 00100000 01110101 00100000 01111001 # " u y"
38
+ # ======== ======== ======== ========
39
+ # 01010010 01010101 01000010 01011001 # "RUBY"
40
40
 
41
41
  OR operation expands to the larger of the two strings:
42
42
 
43
43
  "RU" | " by" # "ruby"
44
44
  #
45
- # 01010010 01010101 | # "RU"
46
- # 00100000 00100000 01100010 01111001 # " by"
47
- # ======== ======== ======== ========
48
- # 01110010 01110101 01100010 01111001 # "ruby"
45
+ # 01010010 01010101 # "RU"
46
+ # | 00100000 00100000 01100010 01111001 # " by"
47
+ # ======== ======== ======== ========
48
+ # 01110010 01110101 01100010 01111001 # "ruby"
49
49
 
50
50
  XOR expands also:
51
51
 
52
52
  "%:--!" ^ "woot" # "RUBY!"
53
53
  #
54
- # 00100101 00111010 00101101 00101101 00100001 ^ # "%:--!"
55
- # 01110111 01101111 01101111 01110100 # "woot"
56
- # ======== ======== ======== ========
57
- # 01010010 01010101 01000010 01011001 00100001" # "RUBY!"
54
+ # 00100101 00111010 00101101 00101101 00100001 # "%:--!"
55
+ # ^ 01110111 01101111 01101111 01110100 # "woot"
56
+ # ======== ======== ======== ========
57
+ # 01010010 01010101 01000010 01011001 00100001 # "RUBY!"
58
58
 
59
59
  AND truncates to the shorter of the two:
60
60
 
61
61
  "ruby!" & '____' # "ruby"
62
62
  #
63
- # 01110010 01110101 01100010 01111001 00100001 & # "ruby!"
64
- # 01011111 01011111 01011111 01011111 # "____"
65
- # ======== ======== ======== ========
66
- # 01110010 01110101 01100010 01111001 # "ruby"
63
+ # 01110010 01110101 01100010 01111001 00100001 # "ruby!"
64
+ # & 01011111 01011111 01011111 01011111 # "____"
65
+ # ======== ======== ======== ========
66
+ # 01110010 01110101 01100010 01111001 # "ruby"
67
67
 
68
68
  = Known Issues
69
69
 
70
- * Completely untested with non-string rhs operands
70
+ No known issues. If you find any bugs, please let the author know.
71
71
 
72
72
  = Author
73
73
 
data/Rakefile CHANGED
@@ -3,7 +3,7 @@ require 'rake'
3
3
  require 'echoe'
4
4
 
5
5
  project = "bitwise_string_ops"
6
- version = "0.1.3"
6
+ version = "0.1.4"
7
7
 
8
8
  Echoe.new(project, version) do |p|
9
9
  p.description = "Bitwise operations for Ruby strings"
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{bitwise_string_ops}
5
- s.version = "0.1.3"
5
+ s.version = "0.1.4"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Philip Garrett"]
@@ -10,14 +10,14 @@ Gem::Specification.new do |s|
10
10
  s.description = %q{Bitwise operations for Ruby strings}
11
11
  s.email = %q{philip@pastemagazine.com}
12
12
  s.extensions = ["ext/extconf.rb"]
13
- s.extra_rdoc_files = ["CHANGELOG", "ext/bitwise.c", "ext/bitwise_string_ops.c", "ext/bitwise_string_ops.h", "ext/extconf.rb", "lib/bitwise_string_ops.rb", "README.rdoc"]
14
- s.files = ["bitwise_string_ops.gemspec", "CHANGELOG", "ext/bitwise.c", "ext/bitwise_string_ops.c", "ext/bitwise_string_ops.h", "ext/extconf.rb", "init.rb", "lib/bitwise_string_ops.rb", "Manifest", "Rakefile", "README.rdoc", "setup.rb", "test/test.rb", "test/test_helper.rb", "test/test_string_and.rb", "test/test_string_not.rb", "test/test_string_or.rb", "test/test_string_xor.rb", "test/test_perl_equivalence.rb"]
13
+ s.extra_rdoc_files = ["CHANGELOG", "ext/bitwise.c", "ext/bitwise_string_ops.c", "ext/bitwise_string_ops.h", "ext/extconf.rb", "README.rdoc"]
14
+ s.files = ["bitwise_string_ops.gemspec", "CHANGELOG", "ext/bitwise.c", "ext/bitwise_string_ops.c", "ext/bitwise_string_ops.h", "ext/extconf.rb", "init.rb", "Manifest", "Rakefile", "README.rdoc", "setup.rb", "test/test.rb", "test/test_helper.rb", "test/test_perl_equivalence.rb", "test/test_string_and.rb", "test/test_string_not.rb", "test/test_string_or.rb", "test/test_string_xor.rb", "test/testcases.pl"]
15
15
  s.homepage = %q{http://github.com/kingpong/bitwise_string_ops}
16
16
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Bitwise_string_ops", "--main", "README.rdoc"]
17
17
  s.require_paths = ["lib", "ext"]
18
18
  s.rubyforge_project = %q{bitwise_string_ops}
19
19
  s.rubygems_version = %q{1.3.3}
20
- s.summary = %q{bitwise_string_ops 0.1.3}
20
+ s.summary = %q{bitwise_string_ops 0.1.4}
21
21
  s.test_files = ["test/test_helper.rb", "test/test_perl_equivalence.rb", "test/test_string_and.rb", "test/test_string_not.rb", "test/test_string_or.rb", "test/test_string_xor.rb"]
22
22
 
23
23
  if s.respond_to? :specification_version then
@@ -2,7 +2,7 @@
2
2
  * bitwise_string_ops.c
3
3
  *
4
4
  * Copyright (c) 2009 Philip Garrett.
5
- *
5
+ *
6
6
  * Permission is hereby granted, free of charge, to any person obtaining a
7
7
  * copy of this software and associated documentation files (the
8
8
  * "Software"), to deal in the Software without restriction, including
@@ -10,10 +10,10 @@
10
10
  * distribute, sublicense, and/or sell copies of the Software, and to
11
11
  * permit persons to whom the Software is furnished to do so, subject to
12
12
  * the following conditions:
13
- *
13
+ *
14
14
  * The above copyright notice and this permission notice shall be included
15
15
  * in all copies or substantial portions of the Software.
16
- *
16
+ *
17
17
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18
18
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
19
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
@@ -23,48 +23,62 @@
23
23
  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
24
  */
25
25
 
26
- // Include the Ruby headers and goodies
27
26
  #include "ruby.h"
28
27
  #include "bitwise_string_ops.h"
29
28
 
30
- // Defining a space for information and references about the module to be stored internally
31
- VALUE BitwiseStringOps = Qnil;
32
29
  static ID ToString;
30
+ static VALUE to_s(VALUE o);
33
31
 
34
- // Prototype for the initialization method - Ruby calls this, not you
32
+ /* callback for module init */
35
33
  void Init_bitwise_string_ops();
36
34
 
37
- // Prototypes for our methods
38
35
  VALUE method_bit_or(VALUE self, VALUE other);
39
36
  VALUE method_bit_xor(VALUE self, VALUE other);
40
37
  VALUE method_bit_and(VALUE self, VALUE other);
41
38
  VALUE method_bit_not(VALUE self);
42
39
 
43
- static VALUE to_s(VALUE o);
44
-
45
- // The initialization method for this module
46
40
  void Init_bitwise_string_ops()
47
41
  {
48
- BitwiseStringOps = rb_define_module("BitwiseStringOps");
49
- ToString = rb_intern("to_s");
50
- rb_define_method(BitwiseStringOps, "|", method_bit_or, 1);
51
- rb_define_method(BitwiseStringOps, "^", method_bit_xor, 1);
52
- rb_define_method(BitwiseStringOps, "&", method_bit_and, 1);
53
- rb_define_method(BitwiseStringOps, "~", method_bit_not, 0);
42
+ ToString = rb_intern("to_s");
43
+ rb_define_method(rb_cString, "|", method_bit_or, 1);
44
+ rb_define_method(rb_cString, "^", method_bit_xor, 1);
45
+ rb_define_method(rb_cString, "&", method_bit_and, 1);
46
+ rb_define_method(rb_cString, "~", method_bit_not, 0);
54
47
  }
55
48
 
56
49
  static VALUE to_s(VALUE o)
57
50
  {
58
- return rb_obj_is_kind_of(o,rb_cString)
59
- ? o : rb_funcall(o, ToString, 0);
51
+ return rb_obj_is_kind_of(o,rb_cString)
52
+ ? o : rb_funcall(o, ToString, 0);
60
53
  }
61
54
 
55
+ /*
56
+ * call-seq:
57
+ * |(other)
58
+ *
59
+ * Performs a byte-by-byte bitwise OR on +self+ (a String) and +other+
60
+ * (anything that responds to +to_s+) and returns the result. If one
61
+ * operand is longer than the other, the effect is as if the shorter
62
+ * operand were padded with zero bits on the right. The result is as
63
+ * long as the <b>longer</b> operand.
64
+ *
65
+ * Example:
66
+ *
67
+ * "RU" | " by" # "ruby"
68
+ * #
69
+ * # 01010010 01010101 # "RU"
70
+ * # | 00100000 00100000 01100010 01111001 # " by"
71
+ * # ======== ======== ======== ========
72
+ * # 01110010 01110101 01100010 01111001 # "ruby"
73
+ *
74
+ *
75
+ */
62
76
  VALUE method_bit_or(VALUE self, VALUE other)
63
77
  {
64
- VALUE left, right, right_s, dest;
65
- left = StringValue(self);
66
- right_s = to_s(other);
67
- right = StringValue(right_s);
78
+ VALUE left, right, right_s, dest;
79
+ left = StringValue(self);
80
+ right_s = to_s(other);
81
+ right = StringValue(right_s);
68
82
  dest = rb_str_new(NULL, string_bitwise_or_result_len(RSTRING(left)->len,
69
83
  RSTRING(right)->len));
70
84
  string_bitwise_or(RSTRING(left)->ptr, RSTRING(left)->len,
@@ -73,12 +87,32 @@ VALUE method_bit_or(VALUE self, VALUE other)
73
87
  return dest;
74
88
  }
75
89
 
90
+ /*
91
+ * call-seq:
92
+ * ^(other)
93
+ *
94
+ * Performs a byte-by-byte bitwise XOR on +self+ (a String) and +other+
95
+ * (anything that responds to +to_s+) and returns the result. If one
96
+ * operand is longer than the other, the effect is as if the shorter
97
+ * operand were padded with zero bits on the right. The result is as
98
+ * long as the <b>longer</b> operand.
99
+ *
100
+ * Example:
101
+ *
102
+ * "%:--!" ^ "woot" # "RUBY!"
103
+ * #
104
+ * # 00100101 00111010 00101101 00101101 00100001 # "%:--!"
105
+ * # ^ 01110111 01101111 01101111 01110100 # "woot"
106
+ * # ======== ======== ======== ========
107
+ * # 01010010 01010101 01000010 01011001 00100001 # "RUBY!"
108
+ *
109
+ */
76
110
  VALUE method_bit_xor(VALUE self, VALUE other)
77
111
  {
78
- VALUE left, right, right_s, dest;
79
- left = StringValue(self);
80
- right_s = to_s(other);
81
- right = StringValue(right_s);
112
+ VALUE left, right, right_s, dest;
113
+ left = StringValue(self);
114
+ right_s = to_s(other);
115
+ right = StringValue(right_s);
82
116
  dest = rb_str_new(NULL, string_bitwise_xor_result_len(RSTRING(left)->len,
83
117
  RSTRING(right)->len));
84
118
  string_bitwise_xor(RSTRING(left)->ptr, RSTRING(left)->len,
@@ -87,12 +121,32 @@ VALUE method_bit_xor(VALUE self, VALUE other)
87
121
  return dest;
88
122
  }
89
123
 
124
+ /*
125
+ * call-seq:
126
+ * &(other)
127
+ *
128
+ * Performs a byte-by-byte bitwise AND on +self+ (a String) and +other+
129
+ * (anything that responds to +to_s+) and returns the result. If one
130
+ * operand is longer than the other, the effect is as if the longer
131
+ * operand were truncated to the length of the shorter. The result is
132
+ * as long as the <b>shorter</b> operand.
133
+ *
134
+ * Example:
135
+ *
136
+ * "ruby!" & '____' # "ruby"
137
+ *
138
+ * # 01110010 01110101 01100010 01111001 00100001 # "ruby!"
139
+ * # & 01011111 01011111 01011111 01011111 # "____"
140
+ * # ======== ======== ======== ========
141
+ * # 01110010 01110101 01100010 01111001 # "ruby"
142
+ *
143
+ */
90
144
  VALUE method_bit_and(VALUE self, VALUE other)
91
145
  {
92
- VALUE left, right, right_s, dest;
93
- left = StringValue(self);
94
- right_s = to_s(other);
95
- right = StringValue(right_s);
146
+ VALUE left, right, right_s, dest;
147
+ left = StringValue(self);
148
+ right_s = to_s(other);
149
+ right = StringValue(right_s);
96
150
  dest = rb_str_new(NULL, string_bitwise_and_result_len(RSTRING(left)->len,
97
151
  RSTRING(right)->len));
98
152
  string_bitwise_and(RSTRING(left)->ptr, RSTRING(left)->len,
@@ -101,6 +155,21 @@ VALUE method_bit_and(VALUE self, VALUE other)
101
155
  return dest;
102
156
  }
103
157
 
158
+ /*
159
+ * call-seq:
160
+ * ~()
161
+ *
162
+ * Performs a byte-by-byte bitwise NOT on +self+ (a String) * and returns the result.
163
+ *
164
+ * Example:
165
+ *
166
+ * ~"ruby" # "\215\212\235\206"
167
+ *
168
+ * # ~ 01110010 01110101 01100010 01111001 # "ruby"
169
+ * # ======== ======== ======== ========
170
+ * # 10001101 10001010 10011101 10000110 # "\215\212\235\206"
171
+ *
172
+ */
104
173
  VALUE method_bit_not(VALUE self)
105
174
  {
106
175
  VALUE str = StringValue(self);
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env perl
2
+ use strict;
3
+ use warnings;
4
+
5
+ =head1 NAME
6
+
7
+ testcases.pl
8
+
9
+ =head1 DESCRIPTION
10
+
11
+ Generates a data set to test the ruby bitwise string operations against
12
+ Perl's implementation in a brute force manner.
13
+
14
+ This doesn't really prove anything, it just a little confidence booster.
15
+
16
+ =cut
17
+
18
+ sub OPERAND_MAX() { 32 }
19
+ sub TESTS_PER_OP() { 256 }
20
+
21
+ for my $i (1..TESTS_PER_OP) {
22
+ my ($left,$right) = (gen_operand(),gen_operand());
23
+ dump_testcase('|', $left | $right, $left, $right);
24
+ dump_testcase('&', $left & $right, $left, $right);
25
+ dump_testcase('^', $left ^ $right, $left, $right);
26
+ dump_testcase('~', ~$left, $left);
27
+ }
28
+
29
+ sub gen_operand {
30
+ join('', map { chr(rand(256)) } (0..rand(OPERAND_MAX)));
31
+ }
32
+
33
+ sub dump_testcase {
34
+ my ($op,$out,@args) = @_;
35
+ print map { str2bin($_) } ($op,$out,@args);
36
+ }
37
+
38
+ sub str2bin {
39
+ my ($str) = @_;
40
+ pack('N', length($str)) . $str;
41
+ }
42
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kingpong-bitwise_string_ops
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
  - Philip Garrett
@@ -25,7 +25,6 @@ extra_rdoc_files:
25
25
  - ext/bitwise_string_ops.c
26
26
  - ext/bitwise_string_ops.h
27
27
  - ext/extconf.rb
28
- - lib/bitwise_string_ops.rb
29
28
  - README.rdoc
30
29
  files:
31
30
  - bitwise_string_ops.gemspec
@@ -35,18 +34,18 @@ files:
35
34
  - ext/bitwise_string_ops.h
36
35
  - ext/extconf.rb
37
36
  - init.rb
38
- - lib/bitwise_string_ops.rb
39
37
  - Manifest
40
38
  - Rakefile
41
39
  - README.rdoc
42
40
  - setup.rb
43
41
  - test/test.rb
44
42
  - test/test_helper.rb
43
+ - test/test_perl_equivalence.rb
45
44
  - test/test_string_and.rb
46
45
  - test/test_string_not.rb
47
46
  - test/test_string_or.rb
48
47
  - test/test_string_xor.rb
49
- - test/test_perl_equivalence.rb
48
+ - test/testcases.pl
50
49
  has_rdoc: false
51
50
  homepage: http://github.com/kingpong/bitwise_string_ops
52
51
  licenses:
@@ -79,7 +78,7 @@ rubyforge_project: bitwise_string_ops
79
78
  rubygems_version: 1.3.5
80
79
  signing_key:
81
80
  specification_version: 3
82
- summary: bitwise_string_ops 0.1.3
81
+ summary: bitwise_string_ops 0.1.4
83
82
  test_files:
84
83
  - test/test_helper.rb
85
84
  - test/test_perl_equivalence.rb
@@ -1,16 +0,0 @@
1
- #
2
- # bitwise_string_ops.rb
3
- #
4
- # BitwiseStringOps provides bitwise operators for the String class that
5
- # mimic Perl's bitwise string operators.
6
- # See http://perldoc.perl.org/perlop.html#Bitwise-String-Operators.
7
- #
8
- # Author:: Philip Garrett
9
- # Copyright:: Copyright (c) 2009 Philip Garrett.
10
- # License:: MIT License (http://www.opensource.org/licenses/mit-license.php)
11
- #
12
- #
13
- require 'bitwise_string_ops.so'
14
- class String
15
- include BitwiseStringOps
16
- end