chinese_truncate 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fb28787286211d2b72e68d5a5c43e966f55984d9
4
+ data.tar.gz: c41bab064360ef13137cfa8dfb970c3e98185a7e
5
+ SHA512:
6
+ metadata.gz: 9f1b90a9bdef8cfb1a1a0020d77fcbac57d474e871a251280d0f56d6ebc96920ce6d0e6b87b3843d247b0fa09af7e0cf0eb660a952f40e4377f9d5a5ec34f593
7
+ data.tar.gz: 99dcff60127119d6154a9531fab28d5ce47b9a00e88f05c9fa56a823fcdb3b64cf658442ac4d12721e07449e8ffba3da92f87e88c31f093e804114f8dfccad17
@@ -0,0 +1,37 @@
1
+ # chinese_truncate
2
+
3
+ helper method for truncate chinese string. 1 chinese word = 2 byte.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ $ gem install chinese_truncate
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ``` ruby
14
+
15
+ require 'chinese_truncate'
16
+
17
+ str = "中文 a和d english"
18
+
19
+ puts str.chinese_size # => 9
20
+
21
+ puts str.chinese_bytesize # => 17
22
+
23
+ puts str.is_chinese_shorter(8) # => false
24
+
25
+ puts str.is_chinese_longer(8) # => 13 (not true)
26
+
27
+ puts str.chinese_truncate(8) # => 中文 a和d eng...
28
+
29
+ puts str.chinese_truncate(8, omission:'???') # => 中文 a和d eng???
30
+
31
+ puts str.chinese_truncate(8, omission:false) # => 中文 a和d englis
32
+
33
+ puts str.chinese_truncate(9) # => 中文 a和d english
34
+
35
+ puts str.chinese_truncate(10) # => 中文 a和d english
36
+
37
+ ```
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sinarey_cache/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "chinese_truncate"
8
+ spec.version = "1.0.0"
9
+ spec.authors = ["Jeffrey"]
10
+ spec.email = ["jeffrey6052@163.com"]
11
+ spec.description = "helper method for truncate chinese string. 1 chinese word = 2 byte."
12
+ spec.summary = ""
13
+ spec.homepage = "https://github.com/jeffrey6052/chinese_truncate"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = ['lib/chinese_truncate.rb',
17
+ 'test/chinese_truncate.rb',
18
+ 'chinese_truncate.gemspec',
19
+ 'README.md']
20
+ end
@@ -0,0 +1,67 @@
1
+ class String
2
+
3
+ #中文字符串字数 > 指定字数
4
+ def is_chinese_longer(sum, omission_bytesize=0)
5
+ bsize = sum * 2
6
+ tsize = bsize - omission_bytesize
7
+ l = i = 0
8
+ out = nil
9
+ is_longer = false
10
+ self.each_char do |word|
11
+ w = word.bytesize>1 ? 2 : 1
12
+ xsize = l + w
13
+ if xsize > tsize
14
+ out ||= i
15
+ if xsize > bsize
16
+ is_longer = true
17
+ break
18
+ end
19
+ end
20
+
21
+ i += 1
22
+ l += w
23
+ end
24
+
25
+ is_longer && out
26
+ end
27
+
28
+ #中文字符串字数 <= 指定字数
29
+ def is_chinese_shorter(sum)
30
+ !is_chinese_longer(sum)
31
+ end
32
+
33
+ #中文字符串字符数
34
+ def chinese_bytesize
35
+ self.each_char.inject(0) do |sum,word|
36
+ w = word.bytesize>1 ? 2 : 1
37
+ sum += w
38
+ end
39
+ end
40
+
41
+ #中文字符串字数
42
+ def chinese_size
43
+ sum,remain = self.chinese_bytesize.divmod(2)
44
+ sum += 1 if remain > 0
45
+ sum
46
+ end
47
+
48
+ #中文字符串截取
49
+ def chinese_truncate(truncate_at, options = {})
50
+ if (omission = options[:omission])
51
+ ol = omission.chinese_bytesize
52
+ elsif omission==false
53
+ omission = ''
54
+ ol = 0
55
+ else
56
+ omission = '...'
57
+ ol = 3
58
+ end
59
+ if i = self.is_chinese_longer(truncate_at,ol)
60
+ head = self[0, i]
61
+ head + omission
62
+ else
63
+ self
64
+ end
65
+ end
66
+
67
+ end
@@ -0,0 +1,26 @@
1
+
2
+ $:.unshift File.expand_path("../lib", __dir__)
3
+
4
+ require 'chinese_truncate'
5
+
6
+ str = "中文 a和d english"
7
+
8
+ puts str.chinese_size # => 9
9
+
10
+ puts str.chinese_bytesize # => 17
11
+
12
+ puts str.is_chinese_shorter(8) # => false
13
+
14
+ puts str.is_chinese_longer(8) # => 13 (not true)
15
+
16
+ puts str.chinese_truncate(8) # => 中文 a和d eng...
17
+
18
+ puts str.chinese_truncate(8, omission:'???') # => 中文 a和d eng???
19
+
20
+ puts str.chinese_truncate(8, omission:false) # => 中文 a和d englis
21
+
22
+ puts str.chinese_truncate(9) # => 中文 a和d english
23
+
24
+ puts str.chinese_truncate(10) # => 中文 a和d english
25
+
26
+
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chinese_truncate
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeffrey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: helper method for truncate chinese string. 1 chinese word = 2 byte.
14
+ email:
15
+ - jeffrey6052@163.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/chinese_truncate.rb
21
+ - test/chinese_truncate.rb
22
+ - chinese_truncate.gemspec
23
+ - README.md
24
+ homepage: https://github.com/jeffrey6052/chinese_truncate
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.0.14
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: ''
48
+ test_files: []