string19 0.0.1
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/Gemfile +7 -0
- data/Gemfile.lock +26 -0
- data/Rakefile +20 -0
- data/Readme.md +21 -0
- data/VERSION +1 -0
- data/lib/string19.rb +88 -0
- data/lib/string19/blank_slate.rb +5 -0
- data/lib/string19/encoding.rb +8 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/string19_spec.rb +108 -0
- data/string19.gemspec +46 -0
- metadata +76 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.5.2)
|
7
|
+
bundler (~> 1.0.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rake (0.8.7)
|
11
|
+
rspec (2.4.0)
|
12
|
+
rspec-core (~> 2.4.0)
|
13
|
+
rspec-expectations (~> 2.4.0)
|
14
|
+
rspec-mocks (~> 2.4.0)
|
15
|
+
rspec-core (2.4.0)
|
16
|
+
rspec-expectations (2.4.0)
|
17
|
+
diff-lcs (~> 1.1.2)
|
18
|
+
rspec-mocks (2.4.0)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
jeweler
|
25
|
+
rake
|
26
|
+
rspec (~> 2)
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
task :default => :spec
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
4
|
+
t.rspec_opts = '--backtrace --color'
|
5
|
+
end
|
6
|
+
|
7
|
+
begin
|
8
|
+
require 'jeweler'
|
9
|
+
Jeweler::Tasks.new do |gem|
|
10
|
+
gem.name = 'string19'
|
11
|
+
gem.summary = "A Ruby string that behaves like 1.9 and runs on 1.8"
|
12
|
+
gem.email = "michael@grosser.it"
|
13
|
+
gem.homepage = "http://github.com/grosser/#{gem.name}"
|
14
|
+
gem.authors = ["Michael Grosser"]
|
15
|
+
end
|
16
|
+
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler, or one of its dependencies, is not available. Install it with: gem install jeweler"
|
20
|
+
end
|
data/Readme.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Stop thinking about differences between 1.9 and 1.8.
|
2
|
+
|
3
|
+
size / index / slice / slice! / chars / bytes / [] etc supported
|
4
|
+
|
5
|
+
Still some methods missing, fork & pull for what you need!
|
6
|
+
|
7
|
+
|
8
|
+
Install
|
9
|
+
=======
|
10
|
+
sudo gem install string19
|
11
|
+
|
12
|
+
Usage
|
13
|
+
=====
|
14
|
+
# on 1.8 AND 1.9
|
15
|
+
String19("Á§ÐÁ§Ð").size == 6
|
16
|
+
|
17
|
+
Author
|
18
|
+
======
|
19
|
+
[Michael Grosser](http://grosser.it)
|
20
|
+
grosser.michael@gmail.com
|
21
|
+
Hereby placed under public domain, do what you want, just do not hold me accountable...
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/string19.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
module String19
|
2
|
+
VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
|
3
|
+
IS_18 = RUBY_VERSION =~ /^1\.8/
|
4
|
+
end
|
5
|
+
|
6
|
+
if String19::IS_18
|
7
|
+
$KCODE = 'U' # UTF8 Mode
|
8
|
+
require 'string19/blank_slate'
|
9
|
+
require 'string19/encoding'
|
10
|
+
|
11
|
+
module String19
|
12
|
+
VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
|
13
|
+
|
14
|
+
class Wrapper < String19::BlankSlate
|
15
|
+
attr_reader :chars
|
16
|
+
|
17
|
+
def initialize(wrapped)
|
18
|
+
@chars = wrapped.scan(/./m)
|
19
|
+
end
|
20
|
+
|
21
|
+
def chars
|
22
|
+
@chars.map{|c| String19(c) }.enum_for(:each)
|
23
|
+
end
|
24
|
+
|
25
|
+
def bytesize
|
26
|
+
to_s18.size
|
27
|
+
end
|
28
|
+
|
29
|
+
def bytes
|
30
|
+
to_s18.bytes
|
31
|
+
end
|
32
|
+
|
33
|
+
def encoding
|
34
|
+
Encoding.new('UTF-8')
|
35
|
+
end
|
36
|
+
|
37
|
+
def class
|
38
|
+
String
|
39
|
+
end
|
40
|
+
|
41
|
+
def is_a?(klass)
|
42
|
+
[String19::Wrapper, String, Object].include?(klass)
|
43
|
+
end
|
44
|
+
|
45
|
+
def ==(other)
|
46
|
+
other = other.to_s18 if other.is_a?(String19::Wrapper)
|
47
|
+
other.is_a?(String) and to_s18 == other
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_s
|
51
|
+
self
|
52
|
+
end
|
53
|
+
|
54
|
+
def to_s18
|
55
|
+
@chars.to_s
|
56
|
+
end
|
57
|
+
|
58
|
+
def valid_encoding?
|
59
|
+
true # i hope so :P
|
60
|
+
end
|
61
|
+
|
62
|
+
def index(what, offset=0)
|
63
|
+
s = to_s18
|
64
|
+
offset = self[0...offset].to_s18.size
|
65
|
+
return unless found = s.index(what, offset)
|
66
|
+
String19(s[0...found]).size
|
67
|
+
end
|
68
|
+
|
69
|
+
%w[slice slice! []].each do |wrapped|
|
70
|
+
eval("def #{wrapped}(*args, &block); String19(@chars.#{wrapped}(*args, &block)); end")
|
71
|
+
end
|
72
|
+
|
73
|
+
%w[size].each do |delegated|
|
74
|
+
eval("def #{delegated}(*args, &block); @chars.#{delegated}(*args, &block); end")
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
else
|
79
|
+
module String19
|
80
|
+
# nothing to do :P
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def String19(string)
|
85
|
+
return string if string.nil?
|
86
|
+
string = string.join('') if string.is_a?(Array)
|
87
|
+
String19::IS_18 ? String19::Wrapper.new(string) : string.dup
|
88
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require File.expand_path('spec/spec_helper')
|
3
|
+
|
4
|
+
describe String19 do
|
5
|
+
def enumerator
|
6
|
+
RUBY_VERSION =~ /^1\.8/ ? Enumerable::Enumerator : Enumerator
|
7
|
+
end
|
8
|
+
|
9
|
+
it "has a VERSION" do
|
10
|
+
String19::VERSION.should =~ /^\d+\.\d+\.\d+$/
|
11
|
+
end
|
12
|
+
|
13
|
+
it "is nil for nil" do
|
14
|
+
String19(nil).should == nil
|
15
|
+
end
|
16
|
+
|
17
|
+
it "is string for array" do
|
18
|
+
String19(['a','b']).should == 'ab'
|
19
|
+
end
|
20
|
+
|
21
|
+
it "has correct size" do
|
22
|
+
String19("äßáóð").size.should == 5
|
23
|
+
end
|
24
|
+
|
25
|
+
it "can slice" do
|
26
|
+
String19("äßáóð").slice(1,2).should == "ßá"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "can slice!" do
|
30
|
+
s = String19("äßáóð")
|
31
|
+
s.slice!(1,2).should == "ßá"
|
32
|
+
s.should == "äóð"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "is has an encoding" do
|
36
|
+
String19("Ä").encoding.name.should == 'UTF-8'
|
37
|
+
end
|
38
|
+
|
39
|
+
it "is has a valid encoding" do
|
40
|
+
String19("Ä").valid_encoding?.should == true
|
41
|
+
end
|
42
|
+
|
43
|
+
it "is a string" do
|
44
|
+
String19('x').class.should == String
|
45
|
+
String19('x').is_a?(String).should == true
|
46
|
+
end
|
47
|
+
|
48
|
+
it "can compare" do
|
49
|
+
String19('äåé').should == 'äåé'
|
50
|
+
String19('äåé').should == String19('äåé')
|
51
|
+
String19('äåé').should_not == String19('aae')
|
52
|
+
String19('äåé').should_not == 'aae'
|
53
|
+
String19('äåé').should_not == ['äåé']
|
54
|
+
end
|
55
|
+
|
56
|
+
describe :index do
|
57
|
+
it "has nil index" do
|
58
|
+
String19("bb").index('a').should == nil
|
59
|
+
end
|
60
|
+
|
61
|
+
it "has simple index" do
|
62
|
+
String19("§Ð§ß").index('ß').should == 3
|
63
|
+
end
|
64
|
+
|
65
|
+
it "has index with offset" do
|
66
|
+
String19("ßßЧß").index('ß',2).should == 4
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe :[] do
|
71
|
+
it "has [0]" do
|
72
|
+
ret = String19("ߧЧÁ")[0]
|
73
|
+
ret.should == 'ß'
|
74
|
+
ret.size.should == 1
|
75
|
+
end
|
76
|
+
|
77
|
+
it "has [0..1]" do
|
78
|
+
ret = String19("ߧЧÁ")[0..2]
|
79
|
+
ret.should == 'ß§Ð'
|
80
|
+
ret.size.should == 3
|
81
|
+
end
|
82
|
+
|
83
|
+
it "can replace with [xxx]="
|
84
|
+
it "can replace with [xxx..xxx]="
|
85
|
+
end
|
86
|
+
|
87
|
+
describe :chars do
|
88
|
+
it "is an enumerator" do
|
89
|
+
String19("ßä").chars.class.should == enumerator
|
90
|
+
end
|
91
|
+
|
92
|
+
it "has correct bytes" do
|
93
|
+
ret = String19("ßä").chars.to_a
|
94
|
+
ret.should == ['ß','ä']
|
95
|
+
ret.map(&:size).should == [1,1]
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe :bytes do
|
100
|
+
it "is an enumerator" do
|
101
|
+
String19("ßä").bytes.class.should == enumerator
|
102
|
+
end
|
103
|
+
|
104
|
+
it "has correct bytes" do
|
105
|
+
String19("ßä").bytes.to_a.should == [195, 159, 195, 164]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
data/string19.gemspec
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{string19}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Michael Grosser"]
|
12
|
+
s.date = %q{2011-01-15}
|
13
|
+
s.email = %q{michael@grosser.it}
|
14
|
+
s.files = [
|
15
|
+
"Gemfile",
|
16
|
+
"Gemfile.lock",
|
17
|
+
"Rakefile",
|
18
|
+
"Readme.md",
|
19
|
+
"VERSION",
|
20
|
+
"lib/string19.rb",
|
21
|
+
"lib/string19/blank_slate.rb",
|
22
|
+
"lib/string19/encoding.rb",
|
23
|
+
"spec/spec_helper.rb",
|
24
|
+
"spec/string19_spec.rb",
|
25
|
+
"string19.gemspec"
|
26
|
+
]
|
27
|
+
s.homepage = %q{http://github.com/grosser/string19}
|
28
|
+
s.require_paths = ["lib"]
|
29
|
+
s.rubygems_version = %q{1.3.7}
|
30
|
+
s.summary = %q{A Ruby string that behaves like 1.9 and runs on 1.8}
|
31
|
+
s.test_files = [
|
32
|
+
"spec/spec_helper.rb",
|
33
|
+
"spec/string19_spec.rb"
|
34
|
+
]
|
35
|
+
|
36
|
+
if s.respond_to? :specification_version then
|
37
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
38
|
+
s.specification_version = 3
|
39
|
+
|
40
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
41
|
+
else
|
42
|
+
end
|
43
|
+
else
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: string19
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Michael Grosser
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-01-15 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description:
|
22
|
+
email: michael@grosser.it
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- Gemfile
|
31
|
+
- Gemfile.lock
|
32
|
+
- Rakefile
|
33
|
+
- Readme.md
|
34
|
+
- VERSION
|
35
|
+
- lib/string19.rb
|
36
|
+
- lib/string19/blank_slate.rb
|
37
|
+
- lib/string19/encoding.rb
|
38
|
+
- spec/spec_helper.rb
|
39
|
+
- spec/string19_spec.rb
|
40
|
+
- string19.gemspec
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://github.com/grosser/string19
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: -691162189
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.3.7
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: A Ruby string that behaves like 1.9 and runs on 1.8
|
74
|
+
test_files:
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
- spec/string19_spec.rb
|