kingpong-bitwise_string_ops 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,41 @@
1
+ commit 074c9142a5b66b146e70e185566931e285867d06
2
+ Author: kingpong <philip@pastemagazine.com>
3
+ Date: Sat Aug 22 19:28:48 2009 -0400
4
+
5
+ updated gem summary to avoid warning. now ignoring locally built gems
6
+
7
+ commit 15eaea26144f53acb6115e69c61849fd6acc271f
8
+ Author: kingpong <philip@pastemagazine.com>
9
+ Date: Sat Aug 22 19:18:42 2009 -0400
10
+
11
+ updated description
12
+
13
+ commit 6564737f4bde3fc862536f2ca783f25d7f27e876
14
+ Author: kingpong <philip@pastemagazine.com>
15
+ Date: Sat Aug 22 19:10:43 2009 -0400
16
+
17
+ fixed heading
18
+
19
+ commit 87fa61f9253a8d43764ac52acd09583da8c39d26
20
+ Author: kingpong <philip@pastemagazine.com>
21
+ Date: Sat Aug 22 19:04:45 2009 -0400
22
+
23
+ converted to gem. added readme and license spam.
24
+
25
+ commit 6ec05ea0131d5b86c6af5b20a872cd266ce76a2a
26
+ Author: kingpong <philip@pastemagazine.com>
27
+ Date: Sat Aug 22 17:00:01 2009 -0400
28
+
29
+ improved organization; added setup.rb
30
+
31
+ commit bc3246fb8724510be668bd64b4769e712658042e
32
+ Author: Philip Garrett <philip@philip.(none)>
33
+ Date: Sat Aug 22 15:51:01 2009 -0400
34
+
35
+ ignore build files
36
+
37
+ commit 2c66ca95fb973a0029dd655ab3aaa5427ece778b
38
+ Author: Philip Garrett <philip@philip.(none)>
39
+ Date: Sat Aug 22 15:42:23 2009 -0400
40
+
41
+ working, barely-tested bitwise string operations
data/Manifest ADDED
@@ -0,0 +1,18 @@
1
+ bitwise_string_ops.gemspec
2
+ CHANGELOG
3
+ ext/bitwise.c
4
+ ext/bitwise_string_ops.c
5
+ ext/bitwise_string_ops.h
6
+ ext/extconf.rb
7
+ init.rb
8
+ lib/bitwise_string_ops.rb
9
+ Manifest
10
+ Rakefile
11
+ README.rdoc
12
+ setup.rb
13
+ test/test.rb
14
+ test/test_helper.rb
15
+ test/test_string_and.rb
16
+ test/test_string_not.rb
17
+ test/test_string_or.rb
18
+ test/test_string_xor.rb
data/README.rdoc ADDED
@@ -0,0 +1,98 @@
1
+ = Name
2
+
3
+ bitwise_string_ops - Bitwise operators for the String class
4
+
5
+ = Installation
6
+
7
+ gem install kingpong-bitwise_string_ops --source http://gems.github.com
8
+
9
+ # in your code:
10
+ require 'rubygems'
11
+ require 'bitwise_string_ops'
12
+
13
+ # or in Rails' environment.rb:
14
+ gem "kingpong-bitwise_string_ops", :lib => "bitwise_string_ops"
15
+
16
+ = Description
17
+
18
+ This gem extends the String class to include the bitwise operators '|'
19
+ (OR), '&' (AND), '^' (XOR) and '~' (NOT). The semantics are as close to
20
+ Perl's bitwise string operators as possible (see "perldoc perlop").
21
+
22
+ The operation is applied to each successive byte (not necessarily
23
+ character) in the source strings in parallel. For example, the result
24
+ of "AB" & "CD" is ("A" & "C") + ("B" & "D").
25
+
26
+ If the operands to a binary bitwise op are strings of different sizes, ⎪
27
+ and ^ ops act as though the shorter operand had additional zero bits on
28
+ the right, while the & op acts as though the longer operand were
29
+ truncated to the length of the shorter. The granularity for such
30
+ extension or truncation is one or more bytes. [source: perldoc perlop]
31
+
32
+ For strings of the same length, nothing unexpected:
33
+
34
+ "r b " ^ " u y" # "RUBY"
35
+ #
36
+ # 01110010 00100000 01100010 00100000 ^ # "r b " ^
37
+ # 00100000 01110101 00100000 01111001 # " u y"
38
+ # ======== ======== ======== ========
39
+ # 01010010 01010101 01000010 01011001 # "RUBY"
40
+
41
+ OR operation expands to the larger of the two strings:
42
+
43
+ "RU" | " by" # "ruby"
44
+ #
45
+ # 01010010 01010101 | # "RU"
46
+ # 00100000 00100000 01100010 01111001 # " by"
47
+ # ======== ======== ======== ========
48
+ # 01110010 01110101 01100010 01111001 # "ruby"
49
+
50
+ XOR expands also:
51
+
52
+ "%:--!" ^ "woot" # "RUBY!"
53
+ #
54
+ # 00100101 00111010 00101101 00101101 00100001 ^ # "%:--!"
55
+ # 01110111 01101111 01101111 01110100 # "woot"
56
+ # ======== ======== ======== ========
57
+ # 01010010 01010101 01000010 01011001 00100001" # "RUBY!"
58
+
59
+ AND truncates to the shorter of the two:
60
+
61
+ "ruby!" & '____' # "ruby"
62
+ #
63
+ # 01110010 01110101 01100010 01111001 00100001 & # "ruby!"
64
+ # 01011111 01011111 01011111 01011111 # "____"
65
+ # ======== ======== ======== ========
66
+ # 01110010 01110101 01100010 01111001 # "ruby"
67
+
68
+ = Known Issues
69
+
70
+ * Largely untested
71
+ * Completely untested with non-string rhs operands
72
+
73
+ = Author
74
+
75
+ * Philip Garrett <philip at pastemagazine.com>
76
+
77
+ = Copyright and License
78
+
79
+ Copyright (c) 2009 Philip Garrett.
80
+
81
+ Permission is hereby granted, free of charge, to any person obtaining a
82
+ copy of this software and associated documentation files (the
83
+ "Software"), to deal in the Software without restriction, including
84
+ without limitation the rights to use, copy, modify, merge, publish,
85
+ distribute, sublicense, and/or sell copies of the Software, and to
86
+ permit persons to whom the Software is furnished to do so, subject to
87
+ the following conditions:
88
+
89
+ The above copyright notice and this permission notice shall be included
90
+ in all copies or substantial portions of the Software.
91
+
92
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
93
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
94
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
95
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
96
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
97
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
98
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ project = "bitwise_string_ops"
6
+ version = "0.1.2"
7
+
8
+ Echoe.new(project, version) do |p|
9
+ p.description = "Bitwise operations for Ruby strings"
10
+ p.summary = "#{project} #{version}"
11
+ p.url = "http://github.com/kingpong/bitwise_string_ops"
12
+ p.author = "Philip Garrett"
13
+ p.email = "philip@pastemagazine.com"
14
+ p.ignore_pattern = ["tmp/*","script/*","InstalledFiles"]
15
+ p.development_dependencies = []
16
+ end
17
+
18
+ desc "Export changes from git into CHANGELOG"
19
+ task :changes do
20
+ sh "git log > CHANGELOG"
21
+ end
22
+
23
+ task :manifest => :changes
24
+ task :gem => :changes
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{bitwise_string_ops}
5
+ s.version = "0.1.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Philip Garrett"]
9
+ s.date = %q{2009-08-22}
10
+ s.description = %q{Bitwise operations for Ruby strings}
11
+ s.email = %q{philip@pastemagazine.com}
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"]
15
+ s.homepage = %q{http://github.com/kingpong/bitwise_string_ops}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Bitwise_string_ops", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib", "ext"]
18
+ s.rubyforge_project = %q{bitwise_string_ops}
19
+ s.rubygems_version = %q{1.3.3}
20
+ s.summary = %q{bitwise_string_ops 0.1.2}
21
+ s.test_files = ["test/test_helper.rb", "test/test_string_and.rb", "test/test_string_not.rb", "test/test_string_or.rb", "test/test_string_xor.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 3
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ else
29
+ end
30
+ else
31
+ end
32
+ end
data/ext/bitwise.c ADDED
@@ -0,0 +1,71 @@
1
+ /*
2
+ * bitwise.c
3
+ *
4
+ * Copyright (c) 2009 Philip Garrett.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a
7
+ * copy of this software and associated documentation files (the
8
+ * "Software"), to deal in the Software without restriction, including
9
+ * without limitation the rights to use, copy, modify, merge, publish,
10
+ * distribute, sublicense, and/or sell copies of the Software, and to
11
+ * permit persons to whom the Software is furnished to do so, subject to
12
+ * the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included
15
+ * in all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ */
25
+
26
+ #include <sys/types.h>
27
+ #include "bitwise_string_ops.h"
28
+
29
+ void string_bitwise_or(const char *left, size_t llen,
30
+ const char *right, size_t rlen,
31
+ char *dest)
32
+ {
33
+ const char *s = llen < rlen ? left : right; /* smaller */
34
+ const char *b = llen < rlen ? right : left; /* bigger */
35
+ size_t len = MIN(llen,rlen);
36
+ size_t ext = MAX(llen,rlen) - len;
37
+ while (len--)
38
+ *(dest++) = *(s++) | *(b++);
39
+ while (ext--)
40
+ *(dest++) = *(b++);
41
+ }
42
+
43
+ void string_bitwise_xor(const char *left, size_t llen,
44
+ const char *right, size_t rlen,
45
+ char *dest)
46
+ {
47
+ const char *s = llen < rlen ? left : right; /* smaller */
48
+ const char *b = llen < rlen ? right : left; /* bigger */
49
+ size_t len = MIN(llen,rlen);
50
+ size_t ext = MAX(llen,rlen) - len;
51
+ while (len--)
52
+ *(dest++) = *(s++) ^ *(b++);
53
+ while (ext--)
54
+ *(dest++) = *(b++);
55
+ }
56
+
57
+ void string_bitwise_and(const char *l, size_t llen,
58
+ const char *r, size_t rlen,
59
+ char *dest)
60
+ {
61
+ size_t len = MIN(llen,rlen);
62
+ while (len--)
63
+ *(dest++) = *(l++) & *(r++);
64
+ }
65
+
66
+ void string_bitwise_not(const char *s, size_t slen,
67
+ char *dest)
68
+ {
69
+ while (slen--)
70
+ *(dest++) = ~(*s++);
71
+ }
@@ -0,0 +1,96 @@
1
+ /*
2
+ * bitwise_string_ops.c
3
+ *
4
+ * Copyright (c) 2009 Philip Garrett.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a
7
+ * copy of this software and associated documentation files (the
8
+ * "Software"), to deal in the Software without restriction, including
9
+ * without limitation the rights to use, copy, modify, merge, publish,
10
+ * distribute, sublicense, and/or sell copies of the Software, and to
11
+ * permit persons to whom the Software is furnished to do so, subject to
12
+ * the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included
15
+ * in all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ */
25
+
26
+ // Include the Ruby headers and goodies
27
+ #include "ruby.h"
28
+ #include "bitwise_string_ops.h"
29
+
30
+ // Defining a space for information and references about the module to be stored internally
31
+ VALUE BitwiseStringOps = Qnil;
32
+
33
+ // Prototype for the initialization method - Ruby calls this, not you
34
+ void Init_bitwise_string_ops();
35
+
36
+ // Prototypes for our methods
37
+ VALUE method_bit_or(VALUE self, VALUE other);
38
+ VALUE method_bit_xor(VALUE self, VALUE other);
39
+ VALUE method_bit_and(VALUE self, VALUE other);
40
+ VALUE method_bit_not(VALUE self);
41
+
42
+ // The initialization method for this module
43
+ void Init_bitwise_string_ops()
44
+ {
45
+ BitwiseStringOps = rb_define_module("BitwiseStringOps");
46
+ rb_define_method(BitwiseStringOps, "bit_or", method_bit_or, 1);
47
+ rb_define_method(BitwiseStringOps, "bit_xor", method_bit_xor, 1);
48
+ rb_define_method(BitwiseStringOps, "bit_and", method_bit_and, 1);
49
+ rb_define_method(BitwiseStringOps, "bit_not", method_bit_not, 0);
50
+ }
51
+
52
+ VALUE method_bit_or(VALUE self, VALUE other)
53
+ {
54
+ VALUE left = StringValue(self), right = StringValue(other);
55
+ VALUE dest = rb_str_new(NULL,
56
+ string_bitwise_or_result_len(RSTRING(left)->len,
57
+ RSTRING(right)->len));
58
+ string_bitwise_or(RSTRING(left)->ptr, RSTRING(left)->len,
59
+ RSTRING(right)->ptr, RSTRING(right)->len,
60
+ RSTRING(dest)->ptr);
61
+ return dest;
62
+ }
63
+
64
+ VALUE method_bit_xor(VALUE self, VALUE other)
65
+ {
66
+ VALUE left = StringValue(self), right = StringValue(other);
67
+ VALUE dest = rb_str_new(NULL,
68
+ string_bitwise_xor_result_len(RSTRING(left)->len,
69
+ RSTRING(right)->len));
70
+ string_bitwise_xor(RSTRING(left)->ptr, RSTRING(left)->len,
71
+ RSTRING(right)->ptr, RSTRING(right)->len,
72
+ RSTRING(dest)->ptr);
73
+ return dest;
74
+ }
75
+
76
+ VALUE method_bit_and(VALUE self, VALUE other)
77
+ {
78
+ VALUE left = StringValue(self), right = StringValue(other);
79
+ VALUE dest = rb_str_new(NULL,
80
+ string_bitwise_and_result_len(RSTRING(left)->len,
81
+ RSTRING(right)->len));
82
+ string_bitwise_and(RSTRING(left)->ptr, RSTRING(left)->len,
83
+ RSTRING(right)->ptr, RSTRING(right)->len,
84
+ RSTRING(dest)->ptr);
85
+ return dest;
86
+ }
87
+
88
+ VALUE method_bit_not(VALUE self)
89
+ {
90
+ VALUE str = StringValue(self);
91
+ VALUE dest = rb_str_new(NULL,
92
+ string_bitwise_not_result_len(RSTRING(str)->len));
93
+ string_bitwise_not(RSTRING(str)->ptr, RSTRING(str)->len,
94
+ RSTRING(dest)->ptr);
95
+ return dest;
96
+ }
@@ -0,0 +1,49 @@
1
+ /*
2
+ * bitwise_string_ops.h
3
+ *
4
+ * Copyright (c) 2009 Philip Garrett.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a
7
+ * copy of this software and associated documentation files (the
8
+ * "Software"), to deal in the Software without restriction, including
9
+ * without limitation the rights to use, copy, modify, merge, publish,
10
+ * distribute, sublicense, and/or sell copies of the Software, and to
11
+ * permit persons to whom the Software is furnished to do so, subject to
12
+ * the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included
15
+ * in all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+ */
25
+
26
+ #ifndef BITWISE_STRING_OPS_H
27
+ #define BITWISE_STRING_OPS_H
28
+
29
+ #include <sys/types.h>
30
+
31
+ #ifndef MAX
32
+ # define MAX(i,j) (i > j ? i : j)
33
+ #endif
34
+ #ifndef MIN
35
+ # define MIN(i,j) (i < j ? i : j)
36
+ #endif
37
+
38
+ #define string_bitwise_or_result_len(i,j) (MAX(i,j))
39
+ #define string_bitwise_xor_result_len(i,j) (MAX(i,j))
40
+ #define string_bitwise_and_result_len(i,j) (MIN(i,j))
41
+ #define string_bitwise_not_result_len(i) (i)
42
+
43
+ void string_bitwise_or(const char *l, size_t llen, const char *r, size_t rlen, char *dest);
44
+ void string_bitwise_xor(const char *l, size_t llen, const char *r, size_t rlen, char *dest);
45
+ void string_bitwise_and(const char *l, size_t llen, const char *r, size_t rlen, char *dest);
46
+ void string_bitwise_not(const char *s, size_t slen, char *dest);
47
+
48
+ #endif /* BITWISE_STRING_OPS_H */
49
+
data/ext/extconf.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'mkmf'
2
+ extension_name = 'bitwise_string_ops'
3
+ dir_config(extension_name)
4
+ create_makefile(extension_name)