-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 560aa77
Showing
9 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |