CodeMonkeySteve-fast_xor 1.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.rdoc +12 -3
  2. data/rake/extensiontask.rb +191 -0
  3. metadata +5 -5
data/README.rdoc CHANGED
@@ -1,11 +1,20 @@
1
1
  = String XOR Ruby Extension
2
2
 
3
- +fast_xor+ is a simple extension which provides a fast in-place String XOR function (suitable for cryptography).
3
+ +fast_xor+ is a simple extension which provides fast in-place String XOR functions (suitable for cryptography).
4
4
 
5
5
  == How do you use it?
6
6
 
7
- a, b = ...
8
- a.xor! b
7
+ require 'xor'
8
+
9
+ # two-argument version
10
+ a, b = 'a string', 'another string'
11
+ a.xor!(b)
12
+ a == "\000N\034\000\032\f\034G"
13
+
14
+ # three-argument version
15
+ a, b, c = 'a string', 'another string', 'yet another string'
16
+ a.xor!(b, c)
17
+ a == "y+h {bs3"
9
18
 
10
19
  == How fast is "Fast"?
11
20
 
@@ -0,0 +1,191 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'rake/tasklib'
4
+
5
+ # Rake tasks to build Ruby extensions
6
+
7
+ module Rake
8
+
9
+ # Create a build task that will generate a Ruby extension (e.g. .so) from one or more
10
+ # C (.c) or C++ (.cc, .cpp, .cxx) files, and is intended as a replcaement for mkmf.
11
+ # It determines platform-specific settings (e.g. file extensions, compiler flags, etc.)
12
+ # from rbconfig (note: examples assume *nix file extensions).
13
+ #
14
+ # *Note*: Strings vs Symbols
15
+ # In places where filenames are expected (e.g. lib_name and objs), Strings are used
16
+ # as verbatim filenames, while, Symbols have the platform-dependant extension
17
+ # appended (e.g. '.so' for libraries and '.o' for objects). Also, only Symbols
18
+ # have #dir prepended to them.
19
+ #
20
+ # Example:
21
+ # desc "build sample extension"
22
+ # # build sample.so (from foo.{c,cc,cxx,cpp}, through foo.o)
23
+ # Rake::ExtensionTask.new :sample => :foo do |t|
24
+ # # all extension files under this directory
25
+ # t.dir = 'ext'
26
+ # # link libraries (libbar.so)
27
+ # t.link_libs << 'bar'
28
+ # end
29
+ #
30
+ # Author:: Steve Sloan (mailto:steve@finagle.org)
31
+ # Copyright:: Copyright (c) 2006 Steve Sloan
32
+ # License:: GPL
33
+
34
+ class ExtensionTask < Rake::TaskLib
35
+ # The name of the extension
36
+ attr_accessor :name
37
+
38
+ # The filename of the extension library file (e.g. 'extension.so')
39
+ attr_accessor :lib_name
40
+
41
+ # Object files to build and link into the extension.
42
+ attr_accessor :objs
43
+
44
+ # The directory where the extension files (source, output, and
45
+ # intermediate) are stored.
46
+ attr_accessor :dir
47
+
48
+ # Environment configuration -- i.e. CONFIG from rbconfig, with a few other
49
+ # settings, and converted to lowercase-symbols.
50
+ attr_accessor :env
51
+
52
+ # Additional link libraries
53
+ attr_accessor :link_libs
54
+
55
+ # Same arguments as Rake::define_task
56
+ def initialize( args, &blk )
57
+ @env = @@DefaultEnv.dup
58
+ @name, @objs = resolve_args(args)
59
+ set_defaults
60
+ yield self if block_given?
61
+ define_tasks
62
+ end
63
+
64
+ # Generate default values. This is called from initialize _before_ the
65
+ # yield block.
66
+ #
67
+ # Defaults:
68
+ # - lib_name: name.so
69
+ # - objs: name.o (<- name.{c,cxx,cpp,cc})
70
+ # - dir: .
71
+ # - link_libs: <none>
72
+ def set_defaults
73
+ @lib_name ||= name.to_sym
74
+ @objs = [name.to_sym] unless @objs and @objs.any?
75
+ @dir ||= '.'
76
+ @link_libs ||= []
77
+ end
78
+
79
+ # Defines the library task.
80
+ def define_tasks
81
+ output_objs = @objs.collect { |obj| filepath obj, :objext }
82
+ output_lib = filepath lib_name, :dlext
83
+
84
+ task name => output_lib
85
+
86
+ file output_lib => output_objs do |t|
87
+ sh_cmd :ldshared, :dldflags, :ldflags,
88
+ {'-L' => :libdirs}, '-o', output_lib,
89
+ output_objs.join(' '),
90
+ link_libs.join(' '),
91
+ :libs, :dldlibs, :librubyarg_shared
92
+ end
93
+
94
+ CLEAN.include output_objs
95
+ CLOBBER.include output_lib
96
+ define_rules
97
+ end
98
+
99
+ # Defines C and C++ source-to-object rules, using the source extensions from env.
100
+ def define_rules
101
+ for ext in env[:c_exts]
102
+ Rake::Task.create_rule '.'+env[:objext] => '.'+ext do |r|
103
+ sh_cmd :cc, :cflags, :cppflags, {'-D' => :defines}, {'-I' => :includedirs}, {'-I' => :topdir},
104
+ '-c', '-o', r.name, r.sources
105
+ end
106
+ end
107
+
108
+ for ext in env[:cpp_exts]
109
+ Rake::Task.create_rule '.'+env[:objext] => '.'+ext do |r|
110
+ sh_cmd :cxx, :cxxflags, :cppflags, {'-D' => :defines}, {'-I' => :includedirs}, {'-I' => :topdir},
111
+ '-o', r.name, '-c', r.sources
112
+ end
113
+ end
114
+ end
115
+
116
+ class << self
117
+ # The default environment for all extensions.
118
+ @@DefaultEnv = {}
119
+ def env
120
+ @@DefaultEnv
121
+ end
122
+ def env=(e)
123
+ @@DefaultEnv = e
124
+ end
125
+
126
+ Config::CONFIG.merge(ENV).each { |k, v| @@DefaultEnv[k.downcase.to_sym] = v }
127
+ @@DefaultEnv = {
128
+ :cxx => 'c++',
129
+ :cxxflags => '',
130
+ :c_exts => ['c'],
131
+ :cpp_exts => ['cc', 'cxx', 'cpp'],
132
+ :includedirs => [],
133
+ :libdirs => [],
134
+ }.update(@@DefaultEnv)
135
+ end
136
+
137
+ protected
138
+
139
+ # Handles convenience filenames:
140
+ # * f (String) => f
141
+ # * f (Symbol) => dir/f.ext
142
+ def filepath( f, ext )
143
+ ext = env[ext] if Symbol === ext
144
+ Symbol === f ? File.join( dir, "#{f}.#{ext}" ) : f
145
+ end
146
+
147
+ # Convenience function for cnstructing command lines for build tools.
148
+ def optify( *opts )
149
+ return optify(*opts.first) if opts.size == 1 and opts.first.kind_of? Array
150
+ opts.collect do |opt|
151
+ case opt
152
+ when String then opt
153
+ when Symbol then optify env[opt]
154
+ when Hash
155
+ opt.collect do |k, v|
156
+ v = env[v] if v.kind_of? Symbol
157
+ if v.kind_of? Array
158
+ optify v.collect { |w| k.to_s + w.to_s }
159
+ elsif v
160
+ k.to_s + v.to_s
161
+ end
162
+ end
163
+ else
164
+ opt.to_s
165
+ end
166
+ end.join(' ').squeeze(' ')
167
+ end
168
+
169
+ def sh_cmd( cmd, *opts )
170
+ sh optify( cmd, *opts )
171
+ end
172
+
173
+ # For some reason, Rake::TaskManager.resolve_args can't be found, so snarf it.
174
+ def resolve_args(args)
175
+ case args
176
+ when Hash
177
+ fail "Too Many Task Names: #{args.keys.join(' ')}" if args.size > 1
178
+ fail "No Task Name Given" if args.size < 1
179
+ task_name = args.keys[0]
180
+ deps = args[task_name]
181
+ deps = [deps] if (String===deps) || (Regexp===deps) || (Proc===deps)
182
+ else
183
+ task_name = args
184
+ deps = []
185
+ end
186
+ [task_name, deps]
187
+ end
188
+
189
+ end
190
+
191
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: CodeMonkeySteve-fast_xor
3
3
  version: !ruby/object:Gem::Version
4
- version: "1.0"
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Sloan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-28 00:00:00 -07:00
12
+ date: 2009-08-30 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -24,17 +24,17 @@ extra_rdoc_files: []
24
24
  files:
25
25
  - README.rdoc
26
26
  - MIT-LICENSE
27
+ - rake/extensiontask.rb
27
28
  - ext/xor.c
28
29
  - test/test_xor.rb
29
30
  - test/benchmark.rb
30
31
  has_rdoc: false
31
32
  homepage: http://github.com/CodeMonkeySteve/fast_xor
32
- licenses:
33
33
  post_install_message:
34
34
  rdoc_options: []
35
35
 
36
36
  require_paths:
37
- - lib
37
+ - ext
38
38
  required_ruby_version: !ruby/object:Gem::Requirement
39
39
  requirements:
40
40
  - - ">="
@@ -50,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
50
  requirements: []
51
51
 
52
52
  rubyforge_project:
53
- rubygems_version: 1.3.5
53
+ rubygems_version: 1.2.0
54
54
  signing_key:
55
55
  specification_version: 2
56
56
  summary: Fast String XOR operator