Skip to content

Commit

Permalink
Initial import.
Browse files Browse the repository at this point in the history
  • Loading branch information
Manfred committed May 5, 2017
0 parents commit 560aa77
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 0 deletions.
18 changes: 18 additions & 0 deletions COPYING
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Copyright (c) Manfred Stienstra, Fingertips <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Tedrahcu

Tedrahcu is a Ruby wrapper for Uchardet.

```sh
gem install tedrahcu
```

```ruby
require 'tedrahcu'
Tedrahcu.detect('hey') #=> 'ASCII'
```
65 changes: 65 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require 'rdoc/task'

$:.unshift(File.expand_path('../ext', __FILE__))

task default: :test
task compile: "extconf:compile"

desc "Run all tests"
task test: :compile do
Dir[File.dirname(__FILE__) + '/test/**/*_test.rb'].each do |file|
ruby file
end
end

namespace :profile do
desc "Profile memory"
task memory: :compile do
sh 'valgrind --tool=memcheck --leak-check=yes --num-callers=10 --track-fds=yes ruby test/profile/memory.rb'
end
end

task :clean do
crap = "*.{bundle,so,o,obj,log}"
["*.gem", "ext/**/#{crap}", "ext/**/Makefile"].each do |glob|
Dir.glob(glob).each do |file|
rm(file)
end
end
end

namespace :extconf do
task :makefile do
Dir.chdir('ext') do
ruby "extconf.rb"
end
end

task make: :makefile do
Dir.chdir('ext') do
sh(RUBY_PLATFORM =~ /win32/ ? 'nmake' : 'make') do |ok, res|
if !ok
require "fileutils"
FileUtils.rm_rf(Dir.glob('*.{so,o,dll,bundle}'))
end
end
end
end

desc "Compile the Ruby extension"
task compile: :make do
if Dir.glob("ext/*.{o,so,dll}").length == 0
$stderr.puts("Failed to build ext.")
exit(1)
end
end
end

namespace :docs do
Rake::RDocTask.new('generate') do |rdoc|
rdoc.title = 'Tedrahcu'
rdoc.main = "README.md"
rdoc.rdoc_files.include('README.md', 'ext/tedrahcu.c')
rdoc.options << "--all" << "--charset" << "utf-8"
end
end
5 changes: 5 additions & 0 deletions ext/extconf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'mkmf'

dir_config('uchardet')
pkg_config('uchardet')
create_makefile('tedrahcu', 'tedrahcu')
37 changes: 37 additions & 0 deletions ext/tedrahcu/tedrahcu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <ruby.h>
#include <uchardet.h>

/*
* call-seq:
* detect(input)
*
* Return the charset that is detected in the input.
*/
static VALUE tedrahcu_detect(VALUE self, VALUE input)
{
VALUE result;
uchardet_t detector;

// When the input is nil we return nil.
if (NIL_P(input)) return Qnil;

// In other cases we expect a string.
Check_Type(input, T_STRING);

detector = uchardet_new();
uchardet_handle_data(detector, StringValuePtr(input), RSTRING_LEN(input));
uchardet_data_end(detector);
result = rb_str_new2(uchardet_get_charset(detector));
uchardet_delete(detector);

return result;
}

void Init_tedrahcu()
{
VALUE mRingcurl;

mRingcurl = rb_define_module("Tedrahcu");

rb_define_module_function(mRingcurl, "detect", tedrahcu_detect, 1);
}
24 changes: 24 additions & 0 deletions tedrahcu.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
$:.unshift File.expand_path('../lib', __FILE__)

Gem::Specification.new do |spec|
spec.name = "tedrahcu"
spec.version = '1.0.0'
spec.licenses = ['MIT']
spec.date = Date.today

spec.summary = "Ruby wrapper for uchardet"
spec.description = "Uchardet tries to guess the encoding of a byte stream."
spec.authors = ["Manfred Stienstra"]
spec.homepage = "http://github.com/Manfred/tedrahcu"
spec.email = ['[email protected]']

spec.extensions << 'ext/extconf.rb'
spec.files = Dir.glob('{bin,lib,ext}/**/*') + [
'README.md',
'COPYING'
]
spec.extra_rdoc_files = %w(
README.md
COPYING
)
end
3 changes: 3 additions & 0 deletions test/preamble.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'minitest/autorun'
$:.unshift(File.expand_path("../ext", __dir__))
require 'tedrahcu'
3 changes: 3 additions & 0 deletions test/profile/memory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$:.unshift(File.expand_path("../../ext", __dir__))
require 'tedrahcu'
Tedrahcu.detect(File.read('tedrahcu.gemspec'))
10 changes: 10 additions & 0 deletions test/unit/tedrahcu_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require_relative '../preamble'

class TedrahcuTest < Minitest::Test
def test_detects_charset
assert_equal nil, Tedrahcu.detect(nil)
assert_equal '', Tedrahcu.detect('')
assert_equal 'UTF-8', Tedrahcu.detect('é')
assert_equal 'ASCII', Tedrahcu.detect('There are so many ASCII characters.')
end
end

0 comments on commit 560aa77

Please sign in to comment.