mb_string 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 42d9fbeb4970240ee0ead7b5764c18865632d681
4
+ data.tar.gz: 8226f57ff834662fdffd44262e3cf5da4c30a02c
5
+ SHA512:
6
+ metadata.gz: 7d8603bd3ccc2264eb4bde97ff4bbbadb86bf163ba16a9a77887d4f1755c0598bc7d2aaf7ca4f7085fd5469f29cbca5bf985cb650bc9936611491dfb594a2b42
7
+ data.tar.gz: 05942965b8b3bd34f84b7659f1746e3a587f88766f0c94a7795c4821a4544970f0cdaf1c4a3cdc5cb2ca5ad55505b46f79bf1e78a9d602f3592a3dc6efd6b659
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --require spec_helper
3
+ #--warnings
4
+ --format doc
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mb_string.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # mb_string
2
+
3
+ `mb_string` is extended Ruby string class for dealing multi-byte strings, add `mb_ljust`, `mb_rjust` and 'mb_center' methods.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mb_string'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mb_string
20
+
21
+ ## Usage
22
+ `mb_ljust`, `mb_rjust` and `mb_center`:
23
+
24
+ require 'mb_string'
25
+ "あいうえお".mb_ljust(20)
26
+ => "あいうえお "
27
+
28
+ "あいうえお".mb_ljust(20, 'ん')
29
+ => "あいうえおんんんんん"
30
+
31
+ "あいうえお".mb_ljust(20, 'ほげ')
32
+ => "あいうえおほげほげほ"
33
+
34
+
35
+
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
39
+
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rochefort/mb_string.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'mb_string'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/mb_string.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'mb_string/version'
2
+
3
+ module MbString
4
+ require 'mb_string/core_ext/string'
5
+ end
@@ -0,0 +1,65 @@
1
+ require 'unicode/display_width'
2
+
3
+ class String
4
+ def mb_ljust(width, pad_str = ' ')
5
+ mb_execute(width) do |pad_size|
6
+ self + mb_build_padding(pad_size, pad_str)
7
+ end
8
+ end
9
+
10
+ def mb_rjust(width, pad_str = ' ')
11
+ mb_execute(width) do |pad_size|
12
+ mb_build_padding(pad_size, pad_str, is_append_right: false) + self
13
+ end
14
+ end
15
+
16
+ def mb_center(width, pad_str = ' ')
17
+ mb_execute(width) do |pad_size|
18
+ left_pad_size = pad_size / 2
19
+ right_pad_size = pad_size - left_pad_size
20
+
21
+ right_padding = mb_build_padding(right_pad_size, pad_str)
22
+ left_padding = mb_build_padding(left_pad_size, pad_str, is_append_right: false)
23
+
24
+ left_padding + self + right_padding
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def mb_execute(width)
31
+ pad_size = [width - display_width, 0].max
32
+ return self if pad_size.zero?
33
+ yield(pad_size)
34
+ end
35
+
36
+ def mb_build_padding(pad_size, pad_str, is_append_right: true)
37
+ pad_display_width = pad_str.display_width
38
+ pad_num, mod = pad_size.divmod(pad_display_width)
39
+ padding = pad_str * pad_num
40
+ if mod > 0
41
+ padding = mb_tweak_reminder(pad_str, mod, padding)
42
+ padding = append_reminder_space(padding, is_append_right) if pad_size > padding.display_width
43
+ end
44
+ padding
45
+ end
46
+
47
+ def mb_tweak_reminder(pad_str, mod, padding)
48
+ pad_str.each_char do |c|
49
+ char_display_width = c.display_width
50
+ if mod >= char_display_width
51
+ padding << c
52
+ mod -= char_display_width
53
+ end
54
+ end
55
+ padding
56
+ end
57
+
58
+ def append_reminder_space(padding, is_append_right)
59
+ if is_append_right
60
+ padding + ' '
61
+ else
62
+ ' ' + padding
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module MbString
2
+ VERSION = '0.1.0'
3
+ end
data/mb_string.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mb_string/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'mb_string'
8
+ spec.version = MbString::VERSION
9
+ spec.authors = ['rochefort']
10
+ spec.email = ['terasawan@gmail.com']
11
+
12
+ spec.summary = 'You can use mb_ljust, mb_rjust, mb_center for multibyte strings.'
13
+ spec.description = spec.summary
14
+ spec.homepage = 'https://github.com/rochefort/mb_string'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'unicode-display_width', '~> 0.1.1'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.10'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.4.0'
27
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mb_string
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - rochefort
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: unicode-display_width
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.4.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.4.0
69
+ description: You can use mb_ljust, mb_rjust, mb_center for multibyte strings.
70
+ email:
71
+ - terasawan@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - bin/console
83
+ - bin/setup
84
+ - lib/mb_string.rb
85
+ - lib/mb_string/core_ext/string.rb
86
+ - lib/mb_string/version.rb
87
+ - mb_string.gemspec
88
+ homepage: https://github.com/rochefort/mb_string
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.5.1
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: You can use mb_ljust, mb_rjust, mb_center for multibyte strings.
112
+ test_files: []