calc-nik 0.0.4 → 0.0.5
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/Rakefile +30 -0
- data/ext/calc/Makefile +213 -0
- data/ext/calc/calc.c +105 -7
- data/ext/calc/calc.o +0 -0
- data/ext/calc/calc.so +0 -0
- data/lib/calc/calc.so +0 -0
- data/lib/calc/version.rb +1 -1
- data/spec/calc_spec.rb +8 -1
- metadata +5 -1
data/Rakefile
CHANGED
@@ -19,3 +19,33 @@ desc "Generate code coverage"
|
|
19
19
|
RSpec::Core::RakeTask.new(:coverage) do |t|
|
20
20
|
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
|
21
21
|
end
|
22
|
+
|
23
|
+
require 'rake/testtask'
|
24
|
+
require 'rake/clean'
|
25
|
+
|
26
|
+
NAME = 'calc'
|
27
|
+
|
28
|
+
# rule to build the extension: this says
|
29
|
+
# that the extension should be rebuilt
|
30
|
+
# after any change to the files in ext
|
31
|
+
file "lib/#{NAME}/#{NAME}.so" =>
|
32
|
+
Dir.glob("ext/#{NAME}/*{.rb,.c}") do
|
33
|
+
Dir.chdir("ext/#{NAME}") do
|
34
|
+
# this does essentially the same thing
|
35
|
+
# as what RubyGems does
|
36
|
+
ruby "extconf.rb"
|
37
|
+
sh "make"
|
38
|
+
end
|
39
|
+
cp "ext/#{NAME}/#{NAME}.so", "lib/#{NAME}"
|
40
|
+
end
|
41
|
+
|
42
|
+
# make the :test task depend on the shared
|
43
|
+
# object, so it will be built automatically
|
44
|
+
# before running the tests
|
45
|
+
task :spec => "lib/#{NAME}/#{NAME}.so"
|
46
|
+
|
47
|
+
# use 'rake clean' and 'rake clobber' to
|
48
|
+
# easily delete generated files
|
49
|
+
CLEAN.include('ext/**/*{.o,.log,.so}')
|
50
|
+
CLEAN.include('ext/**/Makefile')
|
51
|
+
CLOBBER.include('lib/**/*.so')
|
data/ext/calc/Makefile
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
|
2
|
+
SHELL = /bin/sh
|
3
|
+
|
4
|
+
# V=0 quiet, V=1 verbose. other values don't work.
|
5
|
+
V = 0
|
6
|
+
Q1 = $(V:1=)
|
7
|
+
Q = $(Q1:0=@)
|
8
|
+
n=$(NULLCMD)
|
9
|
+
ECHO1 = $(V:1=@$n)
|
10
|
+
ECHO = $(ECHO1:0=@echo)
|
11
|
+
|
12
|
+
#### Start of system configuration section. ####
|
13
|
+
|
14
|
+
srcdir = .
|
15
|
+
topdir = /home/nik/.rvm/rubies/ruby-1.9.3-p0/include/ruby-1.9.1
|
16
|
+
hdrdir = /home/nik/.rvm/rubies/ruby-1.9.3-p0/include/ruby-1.9.1
|
17
|
+
arch_hdrdir = /home/nik/.rvm/rubies/ruby-1.9.3-p0/include/ruby-1.9.1/$(arch)
|
18
|
+
VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
|
19
|
+
prefix = $(DESTDIR)/home/nik/.rvm/rubies/ruby-1.9.3-p0
|
20
|
+
rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
|
21
|
+
exec_prefix = $(prefix)
|
22
|
+
vendorhdrdir = $(rubyhdrdir)/vendor_ruby
|
23
|
+
sitehdrdir = $(rubyhdrdir)/site_ruby
|
24
|
+
rubyhdrdir = $(includedir)/$(RUBY_BASE_NAME)-$(ruby_version)
|
25
|
+
vendordir = $(rubylibprefix)/vendor_ruby
|
26
|
+
sitedir = $(rubylibprefix)/site_ruby
|
27
|
+
ridir = $(datarootdir)/$(RI_BASE_NAME)
|
28
|
+
mandir = $(datarootdir)/man
|
29
|
+
localedir = $(datarootdir)/locale
|
30
|
+
libdir = $(exec_prefix)/lib
|
31
|
+
psdir = $(docdir)
|
32
|
+
pdfdir = $(docdir)
|
33
|
+
dvidir = $(docdir)
|
34
|
+
htmldir = $(docdir)
|
35
|
+
infodir = $(datarootdir)/info
|
36
|
+
docdir = $(datarootdir)/doc/$(PACKAGE)
|
37
|
+
oldincludedir = $(DESTDIR)/usr/include
|
38
|
+
includedir = $(prefix)/include
|
39
|
+
localstatedir = $(prefix)/var
|
40
|
+
sharedstatedir = $(prefix)/com
|
41
|
+
sysconfdir = $(prefix)/etc
|
42
|
+
datadir = $(datarootdir)
|
43
|
+
datarootdir = $(prefix)/share
|
44
|
+
libexecdir = $(exec_prefix)/libexec
|
45
|
+
sbindir = $(exec_prefix)/sbin
|
46
|
+
bindir = $(exec_prefix)/bin
|
47
|
+
rubylibdir = $(rubylibprefix)/$(ruby_version)
|
48
|
+
archdir = $(rubylibdir)/$(arch)
|
49
|
+
sitelibdir = $(sitedir)/$(ruby_version)
|
50
|
+
sitearchdir = $(sitelibdir)/$(sitearch)
|
51
|
+
vendorlibdir = $(vendordir)/$(ruby_version)
|
52
|
+
vendorarchdir = $(vendorlibdir)/$(sitearch)
|
53
|
+
|
54
|
+
NULLCMD = :
|
55
|
+
|
56
|
+
CC = gcc
|
57
|
+
CXX = g++
|
58
|
+
LIBRUBY = $(LIBRUBY_SO)
|
59
|
+
LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
|
60
|
+
LIBRUBYARG_SHARED = -Wl,-R -Wl,$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)
|
61
|
+
LIBRUBYARG_STATIC = -Wl,-R -Wl,$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)-static
|
62
|
+
OUTFLAG = -o
|
63
|
+
COUTFLAG = -o
|
64
|
+
|
65
|
+
RUBY_EXTCONF_H =
|
66
|
+
cflags = $(optflags) $(debugflags) $(warnflags)
|
67
|
+
optflags = -O3
|
68
|
+
debugflags = -ggdb
|
69
|
+
warnflags = -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration
|
70
|
+
CFLAGS = -fPIC $(cflags) -fPIC
|
71
|
+
INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir)
|
72
|
+
DEFS =
|
73
|
+
CPPFLAGS = -I/home/nik/.rvm/usr/include $(DEFS) $(cppflags)
|
74
|
+
CXXFLAGS = $(CFLAGS) $(cxxflags)
|
75
|
+
ldflags = -L. -rdynamic -Wl,-export-dynamic
|
76
|
+
dldflags =
|
77
|
+
ARCH_FLAG =
|
78
|
+
DLDFLAGS = $(ldflags) $(dldflags)
|
79
|
+
LDSHARED = $(CC) -shared
|
80
|
+
LDSHAREDXX = $(CXX) -shared
|
81
|
+
AR = ar
|
82
|
+
EXEEXT =
|
83
|
+
|
84
|
+
RUBY_BASE_NAME = ruby
|
85
|
+
RUBY_INSTALL_NAME = ruby
|
86
|
+
RUBY_SO_NAME = ruby
|
87
|
+
arch = x86_64-linux
|
88
|
+
sitearch = $(arch)
|
89
|
+
ruby_version = 1.9.1
|
90
|
+
ruby = /home/nik/.rvm/rubies/ruby-1.9.3-p0/bin/ruby
|
91
|
+
RUBY = $(ruby)
|
92
|
+
RM = rm -f
|
93
|
+
RM_RF = $(RUBY) -run -e rm -- -rf
|
94
|
+
RMDIRS = rmdir --ignore-fail-on-non-empty -p
|
95
|
+
MAKEDIRS = /bin/mkdir -p
|
96
|
+
INSTALL = /usr/bin/install -c
|
97
|
+
INSTALL_PROG = $(INSTALL) -m 0755
|
98
|
+
INSTALL_DATA = $(INSTALL) -m 644
|
99
|
+
COPY = cp
|
100
|
+
|
101
|
+
#### End of system configuration section. ####
|
102
|
+
|
103
|
+
preload =
|
104
|
+
|
105
|
+
libpath = . $(libdir) /home/nik/.rvm/usr/lib
|
106
|
+
LIBPATH = -L. -L$(libdir) -Wl,-R$(libdir) -L/home/nik/.rvm/usr/lib -Wl,-R/home/nik/.rvm/usr/lib
|
107
|
+
DEFFILE =
|
108
|
+
|
109
|
+
CLEANFILES = mkmf.log
|
110
|
+
DISTCLEANFILES =
|
111
|
+
DISTCLEANDIRS =
|
112
|
+
|
113
|
+
extout =
|
114
|
+
extout_prefix =
|
115
|
+
target_prefix = /calc
|
116
|
+
LOCAL_LIBS =
|
117
|
+
LIBS = $(LIBRUBYARG_SHARED) -lpthread -lrt -ldl -lcrypt -lm -lc
|
118
|
+
SRCS = calc.c
|
119
|
+
OBJS = calc.o
|
120
|
+
TARGET = calc
|
121
|
+
DLLIB = $(TARGET).so
|
122
|
+
EXTSTATIC =
|
123
|
+
STATIC_LIB =
|
124
|
+
|
125
|
+
BINDIR = $(bindir)
|
126
|
+
RUBYCOMMONDIR = $(sitedir)$(target_prefix)
|
127
|
+
RUBYLIBDIR = $(sitelibdir)$(target_prefix)
|
128
|
+
RUBYARCHDIR = $(sitearchdir)$(target_prefix)
|
129
|
+
HDRDIR = $(rubyhdrdir)/ruby$(target_prefix)
|
130
|
+
ARCHHDRDIR = $(rubyhdrdir)/$(arch)/ruby$(target_prefix)
|
131
|
+
|
132
|
+
TARGET_SO = $(DLLIB)
|
133
|
+
CLEANLIBS = $(TARGET).so
|
134
|
+
CLEANOBJS = *.o *.bak
|
135
|
+
|
136
|
+
all: $(DLLIB)
|
137
|
+
static: $(STATIC_LIB)
|
138
|
+
.PHONY: all install static install-so install-rb
|
139
|
+
.PHONY: clean clean-so clean-rb
|
140
|
+
|
141
|
+
clean-rb-default::
|
142
|
+
clean-rb::
|
143
|
+
clean-so::
|
144
|
+
clean: clean-so clean-rb-default clean-rb
|
145
|
+
@-$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES)
|
146
|
+
|
147
|
+
distclean-rb-default::
|
148
|
+
distclean-rb::
|
149
|
+
distclean-so::
|
150
|
+
distclean: clean distclean-so distclean-rb-default distclean-rb
|
151
|
+
@-$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
|
152
|
+
@-$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
|
153
|
+
@-$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true
|
154
|
+
|
155
|
+
realclean: distclean
|
156
|
+
install: install-so install-rb
|
157
|
+
|
158
|
+
install-so: $(RUBYARCHDIR)
|
159
|
+
install-so: $(RUBYARCHDIR)/$(DLLIB)
|
160
|
+
$(RUBYARCHDIR)/$(DLLIB): $(DLLIB)
|
161
|
+
@-$(MAKEDIRS) $(@D)
|
162
|
+
$(INSTALL_PROG) $(DLLIB) $(@D)
|
163
|
+
install-rb: pre-install-rb install-rb-default
|
164
|
+
install-rb-default: pre-install-rb-default
|
165
|
+
pre-install-rb: Makefile
|
166
|
+
pre-install-rb-default: Makefile
|
167
|
+
pre-install-rb-default:
|
168
|
+
$(ECHO) installing default calc libraries
|
169
|
+
$(RUBYARCHDIR):
|
170
|
+
$(Q) $(MAKEDIRS) $@
|
171
|
+
|
172
|
+
site-install: site-install-so site-install-rb
|
173
|
+
site-install-so: install-so
|
174
|
+
site-install-rb: install-rb
|
175
|
+
|
176
|
+
.SUFFIXES: .c .m .cc .mm .cxx .cpp .C .o
|
177
|
+
|
178
|
+
.cc.o:
|
179
|
+
$(ECHO) compiling $(<)
|
180
|
+
$(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
|
181
|
+
|
182
|
+
.mm.o:
|
183
|
+
$(ECHO) compiling $(<)
|
184
|
+
$(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
|
185
|
+
|
186
|
+
.cxx.o:
|
187
|
+
$(ECHO) compiling $(<)
|
188
|
+
$(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
|
189
|
+
|
190
|
+
.cpp.o:
|
191
|
+
$(ECHO) compiling $(<)
|
192
|
+
$(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
|
193
|
+
|
194
|
+
.C.o:
|
195
|
+
$(ECHO) compiling $(<)
|
196
|
+
$(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $<
|
197
|
+
|
198
|
+
.c.o:
|
199
|
+
$(ECHO) compiling $(<)
|
200
|
+
$(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
|
201
|
+
|
202
|
+
.m.o:
|
203
|
+
$(ECHO) compiling $(<)
|
204
|
+
$(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $<
|
205
|
+
|
206
|
+
$(DLLIB): $(OBJS) Makefile
|
207
|
+
$(ECHO) linking shared-object calc/$(DLLIB)
|
208
|
+
@-$(RM) $(@)
|
209
|
+
$(Q) $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
$(OBJS): $(hdrdir)/ruby.h $(hdrdir)/ruby/defines.h $(arch_hdrdir)/ruby/config.h
|
data/ext/calc/calc.c
CHANGED
@@ -1,9 +1,107 @@
|
|
1
1
|
#include <ruby.h>
|
2
2
|
|
3
|
-
|
4
|
-
*
|
5
|
-
|
6
|
-
|
3
|
+
int is_palindrome(char*);
|
4
|
+
void copy_string(char*, char*);
|
5
|
+
void reverse_string(char*);
|
6
|
+
int string_length(char*);
|
7
|
+
int compare_string(char*, char*);
|
8
|
+
|
9
|
+
static VALUE palindrom(VALUE self, VALUE string_raw)
|
10
|
+
{
|
11
|
+
char *string = StringValuePtr(string_raw);
|
12
|
+
int result;
|
13
|
+
|
14
|
+
result = is_palindrome(string);
|
15
|
+
|
16
|
+
if ( result == 1 )
|
17
|
+
return Qtrue;
|
18
|
+
else
|
19
|
+
return Qfalse;
|
20
|
+
}
|
21
|
+
|
22
|
+
int is_palindrome(char *string)
|
23
|
+
{
|
24
|
+
int check, length;
|
25
|
+
char *reverse;
|
26
|
+
|
27
|
+
length = string_length(string);
|
28
|
+
reverse = (char*)malloc(length+1);
|
29
|
+
|
30
|
+
copy_string(reverse, string);
|
31
|
+
reverse_string(reverse);
|
32
|
+
|
33
|
+
check = compare_string(string, reverse);
|
34
|
+
|
35
|
+
free(reverse);
|
36
|
+
|
37
|
+
if ( check == 0 )
|
38
|
+
return 1;
|
39
|
+
else
|
40
|
+
return 0;
|
41
|
+
}
|
42
|
+
|
43
|
+
int string_length(char *string)
|
44
|
+
{
|
45
|
+
int length = 0;
|
46
|
+
|
47
|
+
while(*string)
|
48
|
+
{
|
49
|
+
length++;
|
50
|
+
string++;
|
51
|
+
}
|
52
|
+
|
53
|
+
return length;
|
54
|
+
}
|
55
|
+
|
56
|
+
void copy_string(char *target, char *source)
|
57
|
+
{
|
58
|
+
while(*source)
|
59
|
+
{
|
60
|
+
*target = *source;
|
61
|
+
source++;
|
62
|
+
target++;
|
63
|
+
}
|
64
|
+
*target = '\0';
|
65
|
+
}
|
66
|
+
|
67
|
+
void reverse_string(char *string)
|
68
|
+
{
|
69
|
+
int length, c;
|
70
|
+
char *begin, *end, temp;
|
71
|
+
|
72
|
+
length = string_length(string);
|
73
|
+
|
74
|
+
begin = string;
|
75
|
+
end = string;
|
76
|
+
|
77
|
+
for ( c = 0 ; c < ( length - 1 ) ; c++ )
|
78
|
+
end++;
|
79
|
+
|
80
|
+
for ( c = 0 ; c < length/2 ; c++ )
|
81
|
+
{
|
82
|
+
temp = *end;
|
83
|
+
*end = *begin;
|
84
|
+
*begin = temp;
|
85
|
+
|
86
|
+
begin++;
|
87
|
+
end--;
|
88
|
+
}
|
89
|
+
}
|
90
|
+
|
91
|
+
int compare_string(char *first, char *second)
|
92
|
+
{
|
93
|
+
while(*first==*second)
|
94
|
+
{
|
95
|
+
if ( *first == '\0' || *second == '\0' )
|
96
|
+
break;
|
97
|
+
|
98
|
+
first++;
|
99
|
+
second++;
|
100
|
+
}
|
101
|
+
if( *first == '\0' && *second == '\0' )
|
102
|
+
return 0;
|
103
|
+
else
|
104
|
+
return -1;
|
7
105
|
}
|
8
106
|
|
9
107
|
/* ruby calls this to load the extension */
|
@@ -12,8 +110,8 @@ void Init_calc(void) {
|
|
12
110
|
VALUE klass = rb_define_class("Calc_c",
|
13
111
|
rb_cObject);
|
14
112
|
|
15
|
-
/* the
|
16
|
-
* from ruby as "
|
113
|
+
/* the palindrom function can be called
|
114
|
+
* from ruby as "Calc_c.palindrom?" */
|
17
115
|
rb_define_singleton_method(klass,
|
18
|
-
"
|
116
|
+
"palindrom?", palindrom, 1);
|
19
117
|
}
|
data/ext/calc/calc.o
ADDED
Binary file
|
data/ext/calc/calc.so
ADDED
Binary file
|
data/lib/calc/calc.so
ADDED
Binary file
|
data/lib/calc/version.rb
CHANGED
data/spec/calc_spec.rb
CHANGED
@@ -28,4 +28,11 @@ describe Calc do
|
|
28
28
|
@c_short.minus.should == -1
|
29
29
|
@c.instance_variable_get("@numbers").should == []
|
30
30
|
end
|
31
|
-
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe Calc_c do
|
34
|
+
it "Checks for simple palindromes" do
|
35
|
+
Calc_c.palindrom?("abbba").should eql(true)
|
36
|
+
Calc_c.palindrom?("abbea").should eql(false)
|
37
|
+
end
|
38
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: calc-nik
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -74,9 +74,13 @@ files:
|
|
74
74
|
- README.md
|
75
75
|
- Rakefile
|
76
76
|
- calc.gemspec
|
77
|
+
- ext/calc/Makefile
|
77
78
|
- ext/calc/calc.c
|
79
|
+
- ext/calc/calc.o
|
80
|
+
- ext/calc/calc.so
|
78
81
|
- ext/calc/extconf.rb
|
79
82
|
- lib/calc.rb
|
83
|
+
- lib/calc/calc.so
|
80
84
|
- lib/calc/version.rb
|
81
85
|
- spec/calc_spec.rb
|
82
86
|
- spec/spec_helper.rb
|