zen_to_i 0.2.0 → 0.4.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.
- checksums.yaml +4 -4
- data/README.md +46 -24
- data/bin/zen_to_i +2 -1
- data/lib/zen_to_i/constant.rb +5 -3
- data/lib/zen_to_i/mixin.rb +10 -41
- data/lib/zen_to_i/parser.rb +118 -0
- data/lib/zen_to_i/refine.rb +1 -0
- data/lib/zen_to_i/string_ext.rb +2 -3
- data/lib/zen_to_i/to_i.rb +5 -8
- data/lib/zen_to_i/version.rb +1 -1
- metadata +8 -93
- data/.github/workflows/ci.yaml +0 -23
- data/.github/workflows/codeql-analysis.yml +0 -70
- data/.gitignore +0 -17
- data/.rubocop.yml +0 -6
- data/Gemfile +0 -3
- data/Rakefile +0 -1
- data/spec/constant_spec.rb +0 -20
- data/spec/load_order_spec.rb +0 -72
- data/spec/refine_spec.rb +0 -30
- data/spec/string_ext_spec.rb +0 -28
- data/spec/zen_to_i_spec.rb +0 -27
- data/zen_to_i.gemspec +0 -26
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2caafd58ade6791a65d28c27e8e11d0ad4c67bb24d89640b9b7d1fc80831844a
|
|
4
|
+
data.tar.gz: dbe72c43236b37f92064779d2a3637d4945fc3c7536ee83e68c2e2d0a8f2166c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 95264694be883ae9fb324a90fd4fb61047f1ff6a117aa3aab8aed04d7586f9417c7c222a29a807cd1e2853b4d81726d8543de1a26321c3ed5dffe41dda6ff37a
|
|
7
|
+
data.tar.gz: 78c65fc1b572ccd8983c5115eb79b536e10c8d7e34ffa3f664928573ceb9c746c124bb928fbbe01c920ebafbbd9ea8b52feb0ffad3a41c68ed69c6cd2ba93a8d
|
data/README.md
CHANGED
|
@@ -1,39 +1,38 @@
|
|
|
1
1
|
# ZenToI
|
|
2
|
-
|
|
3
|
-
漢数字が続いていたらそれぞれ変換します。
|
|
2
|
+
兆までの漢数字を半角数字に変換します。文章中に含まれる漢数字も変換できます。
|
|
4
3
|
|
|
5
4
|
[](https://github.com/yoshitsugu/zen_to_i/actions)
|
|
6
5
|
|
|
7
|
-
##
|
|
6
|
+
## インストール
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
RubyGems からインストールします。
|
|
10
9
|
|
|
11
10
|
$ gem install zen_to_i
|
|
12
11
|
|
|
13
|
-
|
|
12
|
+
または Bundler を使います。アプリケーションの Gemfile に次の行を追加してください。
|
|
14
13
|
|
|
15
|
-
```ruby
|
|
16
|
-
gem
|
|
14
|
+
```ruby
|
|
15
|
+
gem "zen_to_i"
|
|
17
16
|
```
|
|
18
17
|
|
|
19
|
-
|
|
18
|
+
その後、次を実行します。
|
|
20
19
|
|
|
21
20
|
$ bundle
|
|
22
21
|
|
|
23
|
-
##
|
|
22
|
+
## 使い方
|
|
24
23
|
|
|
25
|
-
|
|
24
|
+
### 1. シェルから `zen_to_i` コマンドを使う
|
|
26
25
|
|
|
27
26
|
$ zen_to_i "一二三"
|
|
28
27
|
123
|
|
29
28
|
|
|
30
|
-
#
|
|
29
|
+
# パイプで渡すこともできます
|
|
31
30
|
$ echo "一二三" | zen_to_i
|
|
32
31
|
123
|
|
33
32
|
|
|
34
|
-
|
|
33
|
+
Bundler経由で gem をインストールした場合は `bundle exec zen_to_i` を実行してください。
|
|
35
34
|
|
|
36
|
-
### 2.
|
|
35
|
+
### 2. Ruby スクリプト内で使う
|
|
37
36
|
|
|
38
37
|
```ruby
|
|
39
38
|
require 'zen_to_i'
|
|
@@ -50,10 +49,24 @@ require 'zen_to_i'
|
|
|
50
49
|
"三兆五十二万四十八".zen_to_i
|
|
51
50
|
#=> "3000000520048"
|
|
52
51
|
```
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
`zen_to_i` は Integer ではなく String を返すことに注意してください。
|
|
53
|
+
|
|
54
|
+
位(十・百・千)が大きい順に並ぶ場合は同じ数の桁として解釈します。同じ位が重なったり小さい位から大きい位へ並んだりした場合は、別の数として連結します。
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
"百十".zen_to_i
|
|
58
|
+
#=> "110"
|
|
59
|
+
|
|
60
|
+
"十十".zen_to_i
|
|
61
|
+
#=> "1010"
|
|
62
|
+
|
|
63
|
+
"十百".zen_to_i
|
|
64
|
+
#=> "10100"
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
万・億・兆の並びは従来どおりセクションの加算として扱います(`一億一億` は `200000000`)。位記号を含まない漢数字だけの並び(例: `〇〇七`)は、先頭のゼロを落とさずそのまま連結します。
|
|
68
|
+
|
|
69
|
+
refinement で `to_i` を上書きすることもできます。上書きは `using ZenToI::Refine` が書かれた語彙スコープ内だけで有効で、組み込みの `to_i` メソッドと同様に Integer を返します。
|
|
57
70
|
|
|
58
71
|
```ruby
|
|
59
72
|
require "zen_to_i/refine"
|
|
@@ -67,8 +80,7 @@ using ZenToI::Refine
|
|
|
67
80
|
#=> 123
|
|
68
81
|
```
|
|
69
82
|
|
|
70
|
-
|
|
71
|
-
Of course it is very dangerous. Please be careful to use it.
|
|
83
|
+
`zen_to_i/string_ext` を require すると `String#to_i` をプロセス全体で置き換えることもできます。これはプロセス内のすべての文字列に影響します。元の `to_i` の実装は `String#_to_i` として残ります。グローバルな上書きが意図している場合にのみ使ってください。
|
|
72
84
|
|
|
73
85
|
```ruby
|
|
74
86
|
require "zen_to_i/string_ext"
|
|
@@ -80,11 +92,21 @@ require "zen_to_i/string_ext"
|
|
|
80
92
|
#=> 123
|
|
81
93
|
```
|
|
82
94
|
|
|
95
|
+
基数を指定した `to_i` も使えます。基数は漢数字を変換した後の文字列に適用されるため、漢数字と基数を組み合わせると直感と異なる値になります。
|
|
96
|
+
|
|
97
|
+
```ruby
|
|
98
|
+
"十六".to_i(16)
|
|
99
|
+
#=> 22 # 十六 は 16 ではない
|
|
100
|
+
|
|
101
|
+
"一〇".to_i(2)
|
|
102
|
+
#=> 2
|
|
103
|
+
```
|
|
104
|
+
|
|
83
105
|
|
|
84
106
|
## Contributing
|
|
85
107
|
|
|
86
|
-
1. Fork
|
|
87
|
-
2. Create
|
|
88
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
89
|
-
4. Push
|
|
90
|
-
5. Create
|
|
108
|
+
1. Fork the repository.
|
|
109
|
+
2. Create a feature branch (`git checkout -b my-new-feature`).
|
|
110
|
+
3. Commit your changes (`git commit -am 'Add some feature'`).
|
|
111
|
+
4. Push the branch (`git push origin my-new-feature`).
|
|
112
|
+
5. Create a pull request.
|
data/bin/zen_to_i
CHANGED
data/lib/zen_to_i/constant.rb
CHANGED
|
@@ -12,9 +12,11 @@ module ZenToI
|
|
|
12
12
|
KANORDER_F = { "万" => 10**4, "億" => 10**8, "兆" => 10**12,
|
|
13
13
|
"" => 1, "萬" => 10**4 }.freeze
|
|
14
14
|
ALL_KANSUUJI = KANNUM.keys + KANORDER.keys + KANORDER_F.keys
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
ASCII_DIGITS = (0..9).map(&:to_s)
|
|
16
|
+
# 半角数字が漢数字ブロックに混ざる場合(例: 3万)も漢数字側として扱う
|
|
17
|
+
ALL_KANSUUJI_REGEX = /([#{ALL_KANSUUJI.join("")}#{ASCII_DIGITS.join("")}]+)/
|
|
18
|
+
# 半角数字だけのブロック(例: 007)は数値として評価せず素通しする
|
|
19
|
+
ASCII_DIGITS_ONLY_REGEX = /\A[0-9]+\z/
|
|
18
20
|
end
|
|
19
21
|
end
|
|
20
22
|
|
data/lib/zen_to_i/mixin.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require_relative "
|
|
3
|
+
require_relative "constant"
|
|
4
|
+
require_relative "parser"
|
|
4
5
|
|
|
5
6
|
module ZenToI
|
|
6
7
|
module Mixin
|
|
@@ -14,8 +15,7 @@ module ZenToI
|
|
|
14
15
|
private
|
|
15
16
|
|
|
16
17
|
def kanji2num(str)
|
|
17
|
-
array = split_kansuuji(str)
|
|
18
|
-
array = split_kansuuji_detail(array)
|
|
18
|
+
array = split_kansuuji(zen2han(str))
|
|
19
19
|
result = convert_kansuuji(array)
|
|
20
20
|
result.join("")
|
|
21
21
|
end
|
|
@@ -26,49 +26,18 @@ module ZenToI
|
|
|
26
26
|
|
|
27
27
|
# 漢数字のところだけ切り出す
|
|
28
28
|
def split_kansuuji(string)
|
|
29
|
-
string.split(
|
|
29
|
+
string.split(ALL_KANSUUJI_REGEX)
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
-
#
|
|
33
|
-
def split_kansuuji_detail(array)
|
|
34
|
-
array_tmp = array.clone
|
|
35
|
-
array_result = array.clone
|
|
36
|
-
(1..MAX_LOOP).each do |_i|
|
|
37
|
-
array_result = split_kansuuji_detail_inner(array_result)
|
|
38
|
-
return array_result if array_result == array_tmp
|
|
39
|
-
|
|
40
|
-
array_tmp = array_result.clone
|
|
41
|
-
end
|
|
42
|
-
array_result
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
def split_kansuuji_detail_inner(array)
|
|
46
|
-
ary = array.map do |a|
|
|
47
|
-
if /([#{ALL_KANSUUJI.join('')}]+)/.match?(a)
|
|
48
|
-
kannum = KANNUM.keys.join("")
|
|
49
|
-
a.gsub(/([#{kannum}])([#{kannum}])/, '\1,\2').split(",")
|
|
50
|
-
else
|
|
51
|
-
a
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
ary.flatten
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
# 4桁ごとの単位(万、億、兆など)でまずわけ、
|
|
58
|
-
# それぞれに対して漢数字→数字を実行している。
|
|
32
|
+
# 漢数字ブロックだけを数値化する。半角数字だけのブロック(007 など)は素通しする。
|
|
59
33
|
def convert_kansuuji(array)
|
|
60
34
|
array.map do |a|
|
|
61
|
-
|
|
62
|
-
a.scan(KANORDER_F_KEYS_REGEX).inject(0) do |ret, (unit, order_f)|
|
|
63
|
-
unit_i = unit.scan(KANORDER_KEYS_REGEX).inject(-1) do |tmp, (kanji, order)|
|
|
64
|
-
tmp + KANNUM[kanji].to_i * KANORDER[order].to_i
|
|
65
|
-
end
|
|
66
|
-
ret + unit_i * KANORDER_F[order_f].to_i
|
|
67
|
-
end
|
|
68
|
-
else
|
|
69
|
-
a
|
|
70
|
-
end
|
|
35
|
+
kansuuji_block?(a) ? ::ZenToI::Parser.call(a) : a
|
|
71
36
|
end
|
|
72
37
|
end
|
|
38
|
+
|
|
39
|
+
def kansuuji_block?(str)
|
|
40
|
+
!ASCII_DIGITS_ONLY_REGEX.match?(str) && ALL_KANSUUJI_REGEX.match?(str)
|
|
41
|
+
end
|
|
73
42
|
end
|
|
74
43
|
end
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "constant"
|
|
4
|
+
|
|
5
|
+
module ZenToI
|
|
6
|
+
# 漢数字ブロック(例: "三兆五十二万四十八")を整数に変換する。
|
|
7
|
+
module Parser
|
|
8
|
+
include ::ZenToI::Constant
|
|
9
|
+
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
# 位記号(十百千万億兆)を含まない数字だけのブロックは、先頭ゼロを保持するため
|
|
13
|
+
# 数値化せず各桁をそのまま連結する(例: 〇〇七 => "007")。
|
|
14
|
+
def call(str)
|
|
15
|
+
return digit_string(str) unless order_marker?(str)
|
|
16
|
+
|
|
17
|
+
chunks = tokenize(str).slice_after { |kind, _| kind == :order_f }
|
|
18
|
+
chunks.sum do |chunk|
|
|
19
|
+
body, multiplier = split_chunk(chunk)
|
|
20
|
+
section_value(body, multiplier)
|
|
21
|
+
end.to_s
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def digit_string(str)
|
|
25
|
+
tokenize(str).map { |_kind, value| value.to_s }.join
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# 万・億・兆で終わるまとまりは、その単位を乗数として切り離す。
|
|
29
|
+
def split_chunk(chunk)
|
|
30
|
+
last = chunk.last
|
|
31
|
+
return [chunk, 1] unless last.first == :order_f
|
|
32
|
+
|
|
33
|
+
[chunk[0..-2], last.last]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# 単位単体(万など)は 1 個分として数える。
|
|
37
|
+
# セクション内は「位のあとの数字2桁目以降で分割」してから各チャンクを評価し連結する。
|
|
38
|
+
def section_value(tokens, multiplier)
|
|
39
|
+
return multiplier if tokens.empty?
|
|
40
|
+
|
|
41
|
+
split_after_order_tail(tokens).map { |chunk| eval_section(chunk) }.join.to_i * multiplier
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# 位記号(十百千)または大単位(万億兆)を含むか。
|
|
45
|
+
def order_marker?(str)
|
|
46
|
+
str.each_char.any? { |char| KANORDER.key?(char) || KANORDER_F.key?(char) }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# 半角数字の連続は1つの数として扱う(例: 3千500 の "500")。
|
|
50
|
+
def tokenize(str)
|
|
51
|
+
str.scan(/[0-9]+|./).filter_map do |char|
|
|
52
|
+
kind, value = token_of(char)
|
|
53
|
+
[kind, value] if kind
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def token_of(char)
|
|
58
|
+
if KANNUM.key?(char)
|
|
59
|
+
[:num, KANNUM[char]]
|
|
60
|
+
elsif ASCII_DIGITS_ONLY_REGEX.match?(char)
|
|
61
|
+
[:num, char.to_i]
|
|
62
|
+
elsif KANORDER.key?(char)
|
|
63
|
+
[:order, KANORDER[char]]
|
|
64
|
+
elsif KANORDER_F.key?(char)
|
|
65
|
+
[:order_f, KANORDER_F[char]]
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# 位のあとに続く数字は1桁だけ同じ数に入れ、2桁目以降は別の数として分割する。
|
|
70
|
+
# (例: 百二三十 => 百二 | 三十 => "102" + "30" = "10230")
|
|
71
|
+
def split_after_order_tail(tokens)
|
|
72
|
+
seen_order = false
|
|
73
|
+
tokens.slice_when do |before, after|
|
|
74
|
+
cut = seen_order && before.first == :num && after.first == :num
|
|
75
|
+
seen_order ||= before.first == :order
|
|
76
|
+
cut
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# 位が小さくなる間は同じ数の桁として加算し、同じ位か大きい位が来たら別の数として連結する。
|
|
81
|
+
def eval_section(tokens)
|
|
82
|
+
state = { completed: [], current: 0, last_order: nil, pending: nil }
|
|
83
|
+
tokens.each { |token| apply_token(state, token) }
|
|
84
|
+
finish_section(state)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def apply_token(state, (kind, value))
|
|
88
|
+
case kind
|
|
89
|
+
when :num
|
|
90
|
+
apply_num(state, value)
|
|
91
|
+
when :order
|
|
92
|
+
apply_order(state, value)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def apply_num(state, value)
|
|
97
|
+
state[:pending] = state[:pending].nil? ? value : (state[:pending] * 10) + value
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def apply_order(state, value)
|
|
101
|
+
coefficient = state[:pending].nil? ? 1 : state[:pending]
|
|
102
|
+
if state[:last_order].nil? || value < state[:last_order]
|
|
103
|
+
state[:current] += coefficient * value
|
|
104
|
+
else
|
|
105
|
+
state[:completed] << state[:current]
|
|
106
|
+
state[:current] = coefficient * value
|
|
107
|
+
end
|
|
108
|
+
state[:last_order] = value
|
|
109
|
+
state[:pending] = nil
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def finish_section(state)
|
|
113
|
+
state[:current] += state[:pending] unless state[:pending].nil?
|
|
114
|
+
state[:completed] << state[:current]
|
|
115
|
+
state[:completed].join.to_i
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
data/lib/zen_to_i/refine.rb
CHANGED
data/lib/zen_to_i/string_ext.rb
CHANGED
|
@@ -3,10 +3,9 @@
|
|
|
3
3
|
require_relative "../zen_to_i"
|
|
4
4
|
require_relative "to_i"
|
|
5
5
|
|
|
6
|
+
# 読み込むと String#to_i が漢数字変換に差し替わる。元の実装は String#_to_i に残る。
|
|
6
7
|
class String
|
|
7
|
-
unless String.
|
|
8
|
-
alias_method :_to_i, :to_i
|
|
9
|
-
end
|
|
8
|
+
alias _to_i to_i unless String.method_defined?(:_to_i, false)
|
|
10
9
|
|
|
11
10
|
define_method(:to_i, ZenToI::ToI.instance_method(:to_i))
|
|
12
11
|
end
|
data/lib/zen_to_i/to_i.rb
CHANGED
|
@@ -3,21 +3,18 @@
|
|
|
3
3
|
require_relative "../zen_to_i"
|
|
4
4
|
|
|
5
5
|
module ZenToI
|
|
6
|
-
# String#to_i を漢数字変換に差し替えるための実装。
|
|
7
6
|
# string_ext.rb(グローバルに適用)と refine.rb(refinement 経由)で共有する。
|
|
8
7
|
module ToI
|
|
9
8
|
# 差し替え前の String#to_i を保持する。読み込み時点で String 自身が持つ
|
|
10
9
|
# ネイティブ実装を捕捉し、どのロード順でも素の to_i を参照できるようにする。
|
|
11
|
-
unless const_defined?(:ORIGINAL_TO_I, false)
|
|
12
|
-
ORIGINAL_TO_I = String.instance_method(:to_i)
|
|
13
|
-
end
|
|
10
|
+
ORIGINAL_TO_I = String.instance_method(:to_i) unless const_defined?(:ORIGINAL_TO_I, false)
|
|
14
11
|
|
|
15
|
-
def to_i
|
|
16
|
-
ORIGINAL_TO_I.bind(
|
|
12
|
+
def to_i(*)
|
|
13
|
+
ORIGINAL_TO_I.bind(zen_to_i).call(*)
|
|
17
14
|
end
|
|
18
15
|
|
|
19
|
-
def _to_i(*
|
|
20
|
-
ORIGINAL_TO_I.bind(self).call(*
|
|
16
|
+
def _to_i(*)
|
|
17
|
+
ORIGINAL_TO_I.bind(self).call(*)
|
|
21
18
|
end
|
|
22
19
|
end
|
|
23
20
|
end
|
data/lib/zen_to_i/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,84 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: zen_to_i
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- yoshitsugu
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
-
dependencies:
|
|
12
|
-
- !ruby/object:Gem::Dependency
|
|
13
|
-
name: bundler
|
|
14
|
-
requirement: !ruby/object:Gem::Requirement
|
|
15
|
-
requirements:
|
|
16
|
-
- - ">="
|
|
17
|
-
- !ruby/object:Gem::Version
|
|
18
|
-
version: '0'
|
|
19
|
-
type: :development
|
|
20
|
-
prerelease: false
|
|
21
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
-
requirements:
|
|
23
|
-
- - ">="
|
|
24
|
-
- !ruby/object:Gem::Version
|
|
25
|
-
version: '0'
|
|
26
|
-
- !ruby/object:Gem::Dependency
|
|
27
|
-
name: onkcop
|
|
28
|
-
requirement: !ruby/object:Gem::Requirement
|
|
29
|
-
requirements:
|
|
30
|
-
- - ">="
|
|
31
|
-
- !ruby/object:Gem::Version
|
|
32
|
-
version: '0'
|
|
33
|
-
type: :development
|
|
34
|
-
prerelease: false
|
|
35
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
-
requirements:
|
|
37
|
-
- - ">="
|
|
38
|
-
- !ruby/object:Gem::Version
|
|
39
|
-
version: '0'
|
|
40
|
-
- !ruby/object:Gem::Dependency
|
|
41
|
-
name: pry
|
|
42
|
-
requirement: !ruby/object:Gem::Requirement
|
|
43
|
-
requirements:
|
|
44
|
-
- - ">="
|
|
45
|
-
- !ruby/object:Gem::Version
|
|
46
|
-
version: '0'
|
|
47
|
-
type: :development
|
|
48
|
-
prerelease: false
|
|
49
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
-
requirements:
|
|
51
|
-
- - ">="
|
|
52
|
-
- !ruby/object:Gem::Version
|
|
53
|
-
version: '0'
|
|
54
|
-
- !ruby/object:Gem::Dependency
|
|
55
|
-
name: rake
|
|
56
|
-
requirement: !ruby/object:Gem::Requirement
|
|
57
|
-
requirements:
|
|
58
|
-
- - ">="
|
|
59
|
-
- !ruby/object:Gem::Version
|
|
60
|
-
version: '0'
|
|
61
|
-
type: :development
|
|
62
|
-
prerelease: false
|
|
63
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
-
requirements:
|
|
65
|
-
- - ">="
|
|
66
|
-
- !ruby/object:Gem::Version
|
|
67
|
-
version: '0'
|
|
68
|
-
- !ruby/object:Gem::Dependency
|
|
69
|
-
name: rspec
|
|
70
|
-
requirement: !ruby/object:Gem::Requirement
|
|
71
|
-
requirements:
|
|
72
|
-
- - ">="
|
|
73
|
-
- !ruby/object:Gem::Version
|
|
74
|
-
version: '0'
|
|
75
|
-
type: :development
|
|
76
|
-
prerelease: false
|
|
77
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
78
|
-
requirements:
|
|
79
|
-
- - ">="
|
|
80
|
-
- !ruby/object:Gem::Version
|
|
81
|
-
version: '0'
|
|
11
|
+
dependencies: []
|
|
82
12
|
description: 全角文字に含まれる数字を半角の数値に変換。兆まで対応
|
|
83
13
|
email:
|
|
84
14
|
- zeroinon1206bass@gmail.com
|
|
@@ -87,32 +17,22 @@ executables:
|
|
|
87
17
|
extensions: []
|
|
88
18
|
extra_rdoc_files: []
|
|
89
19
|
files:
|
|
90
|
-
- ".github/workflows/ci.yaml"
|
|
91
|
-
- ".github/workflows/codeql-analysis.yml"
|
|
92
|
-
- ".gitignore"
|
|
93
|
-
- ".rubocop.yml"
|
|
94
|
-
- Gemfile
|
|
95
20
|
- LICENSE.txt
|
|
96
21
|
- README.md
|
|
97
|
-
- Rakefile
|
|
98
22
|
- bin/zen_to_i
|
|
99
23
|
- lib/zen_to_i.rb
|
|
100
24
|
- lib/zen_to_i/constant.rb
|
|
101
25
|
- lib/zen_to_i/mixin.rb
|
|
26
|
+
- lib/zen_to_i/parser.rb
|
|
102
27
|
- lib/zen_to_i/refine.rb
|
|
103
28
|
- lib/zen_to_i/string_ext.rb
|
|
104
29
|
- lib/zen_to_i/to_i.rb
|
|
105
30
|
- lib/zen_to_i/version.rb
|
|
106
|
-
- spec/constant_spec.rb
|
|
107
|
-
- spec/load_order_spec.rb
|
|
108
|
-
- spec/refine_spec.rb
|
|
109
|
-
- spec/string_ext_spec.rb
|
|
110
|
-
- spec/zen_to_i_spec.rb
|
|
111
|
-
- zen_to_i.gemspec
|
|
112
31
|
homepage: https://github.com/yoshitsugu/zen_to_i
|
|
113
32
|
licenses:
|
|
114
33
|
- MIT
|
|
115
|
-
metadata:
|
|
34
|
+
metadata:
|
|
35
|
+
rubygems_mfa_required: 'true'
|
|
116
36
|
rdoc_options: []
|
|
117
37
|
require_paths:
|
|
118
38
|
- lib
|
|
@@ -120,19 +40,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
120
40
|
requirements:
|
|
121
41
|
- - ">="
|
|
122
42
|
- !ruby/object:Gem::Version
|
|
123
|
-
version:
|
|
43
|
+
version: 3.4.0
|
|
124
44
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
45
|
requirements:
|
|
126
46
|
- - ">="
|
|
127
47
|
- !ruby/object:Gem::Version
|
|
128
48
|
version: '0'
|
|
129
49
|
requirements: []
|
|
130
|
-
rubygems_version: 4.0.
|
|
50
|
+
rubygems_version: 4.0.20
|
|
131
51
|
specification_version: 4
|
|
132
52
|
summary: Japanese zenkaku to number
|
|
133
|
-
test_files:
|
|
134
|
-
- spec/constant_spec.rb
|
|
135
|
-
- spec/load_order_spec.rb
|
|
136
|
-
- spec/refine_spec.rb
|
|
137
|
-
- spec/string_ext_spec.rb
|
|
138
|
-
- spec/zen_to_i_spec.rb
|
|
53
|
+
test_files: []
|
data/.github/workflows/ci.yaml
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
name: test
|
|
2
|
-
|
|
3
|
-
on: [push]
|
|
4
|
-
|
|
5
|
-
jobs:
|
|
6
|
-
build:
|
|
7
|
-
name: >-
|
|
8
|
-
Ruby: ${{ matrix.ruby }}
|
|
9
|
-
runs-on: ubuntu-latest
|
|
10
|
-
strategy:
|
|
11
|
-
matrix:
|
|
12
|
-
ruby:
|
|
13
|
-
- '3.4'
|
|
14
|
-
- '4.0'
|
|
15
|
-
steps:
|
|
16
|
-
- uses: actions/checkout@v7
|
|
17
|
-
- uses: ruby/setup-ruby@v1
|
|
18
|
-
with:
|
|
19
|
-
ruby-version: ${{ matrix.ruby }}
|
|
20
|
-
bundler-cache: true
|
|
21
|
-
- name: run rspec
|
|
22
|
-
run: |
|
|
23
|
-
bundle exec rspec
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
# For most projects, this workflow file will not need changing; you simply need
|
|
2
|
-
# to commit it to your repository.
|
|
3
|
-
#
|
|
4
|
-
# You may wish to alter this file to override the set of languages analyzed,
|
|
5
|
-
# or to provide custom queries or build logic.
|
|
6
|
-
#
|
|
7
|
-
# ******** NOTE ********
|
|
8
|
-
# We have attempted to detect the languages in your repository. Please check
|
|
9
|
-
# the `language` matrix defined below to confirm you have the correct set of
|
|
10
|
-
# supported CodeQL languages.
|
|
11
|
-
#
|
|
12
|
-
name: "CodeQL"
|
|
13
|
-
|
|
14
|
-
on:
|
|
15
|
-
push:
|
|
16
|
-
branches: [ master ]
|
|
17
|
-
pull_request:
|
|
18
|
-
# The branches below must be a subset of the branches above
|
|
19
|
-
branches: [ master ]
|
|
20
|
-
schedule:
|
|
21
|
-
- cron: '37 20 * * 2'
|
|
22
|
-
|
|
23
|
-
jobs:
|
|
24
|
-
analyze:
|
|
25
|
-
name: Analyze
|
|
26
|
-
runs-on: ubuntu-latest
|
|
27
|
-
permissions:
|
|
28
|
-
actions: read
|
|
29
|
-
contents: read
|
|
30
|
-
security-events: write
|
|
31
|
-
|
|
32
|
-
strategy:
|
|
33
|
-
fail-fast: false
|
|
34
|
-
matrix:
|
|
35
|
-
language: [ 'ruby' ]
|
|
36
|
-
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
|
|
37
|
-
# Learn more about CodeQL language support at https://git.io/codeql-language-support
|
|
38
|
-
|
|
39
|
-
steps:
|
|
40
|
-
- name: Checkout repository
|
|
41
|
-
uses: actions/checkout@v2
|
|
42
|
-
|
|
43
|
-
# Initializes the CodeQL tools for scanning.
|
|
44
|
-
- name: Initialize CodeQL
|
|
45
|
-
uses: github/codeql-action/init@v1
|
|
46
|
-
with:
|
|
47
|
-
languages: ${{ matrix.language }}
|
|
48
|
-
# If you wish to specify custom queries, you can do so here or in a config file.
|
|
49
|
-
# By default, queries listed here will override any specified in a config file.
|
|
50
|
-
# Prefix the list here with "+" to use these queries and those in the config file.
|
|
51
|
-
# queries: ./path/to/local/query, your-org/your-repo/queries@main
|
|
52
|
-
|
|
53
|
-
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
|
54
|
-
# If this step fails, then you should remove it and run the build manually (see below)
|
|
55
|
-
- name: Autobuild
|
|
56
|
-
uses: github/codeql-action/autobuild@v1
|
|
57
|
-
|
|
58
|
-
# ℹ️ Command-line programs to run using the OS shell.
|
|
59
|
-
# 📚 https://git.io/JvXDl
|
|
60
|
-
|
|
61
|
-
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
|
|
62
|
-
# and modify them (or add more) to build your code if your project
|
|
63
|
-
# uses a compiled language
|
|
64
|
-
|
|
65
|
-
#- run: |
|
|
66
|
-
# make bootstrap
|
|
67
|
-
# make release
|
|
68
|
-
|
|
69
|
-
- name: Perform CodeQL Analysis
|
|
70
|
-
uses: github/codeql-action/analyze@v1
|
data/.gitignore
DELETED
data/.rubocop.yml
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
require "bundler/gem_tasks"
|
data/spec/constant_spec.rb
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "zen_to_i"
|
|
4
|
-
|
|
5
|
-
describe ZenToI::Constant do
|
|
6
|
-
describe "複数回読み込んでも例外にならない" do
|
|
7
|
-
it "constantを再度読み込んでも定数が残る" do
|
|
8
|
-
expect { load File.expand_path("../lib/zen_to_i/constant.rb", __dir__) }.not_to raise_error
|
|
9
|
-
|
|
10
|
-
expect(ZenToI::Constant::KANNUM["一"]).to eq 1
|
|
11
|
-
expect("一二三".zen_to_i).to eq "123"
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
it "定数定義がスキップされModuleがfrozenのまま維持される" do
|
|
15
|
-
load File.expand_path("../lib/zen_to_i/constant.rb", __dir__)
|
|
16
|
-
|
|
17
|
-
expect(ZenToI::Constant).to be_frozen
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
data/spec/load_order_spec.rb
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "open3"
|
|
4
|
-
require "rbconfig"
|
|
5
|
-
|
|
6
|
-
RSpec.describe "ロード順" do
|
|
7
|
-
# refinement の定義は読み込み時点のグローバル String#to_i に影響されるため、
|
|
8
|
-
# ロード順によって挙動が変わらないことをサブプロセスで検証する。
|
|
9
|
-
def run_script(script)
|
|
10
|
-
stdout, stderr, status = Open3.capture3(
|
|
11
|
-
RbConfig.ruby, "-I", File.expand_path("../lib", __dir__), "-e", script
|
|
12
|
-
)
|
|
13
|
-
raise "script failed: #{stderr}" unless status.success?
|
|
14
|
-
|
|
15
|
-
stdout.strip
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
it "string_ext の後に refine を読み込んでも to_i が変換結果を返す" do
|
|
19
|
-
output = run_script(<<~RUBY)
|
|
20
|
-
require "zen_to_i/string_ext"
|
|
21
|
-
require "zen_to_i/refine"
|
|
22
|
-
using ZenToI::Refine
|
|
23
|
-
print "一二三".to_i
|
|
24
|
-
RUBY
|
|
25
|
-
|
|
26
|
-
expect(output).to eq "123"
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
it "refine の後に string_ext を読み込んでも to_i が変換結果を返す" do
|
|
30
|
-
output = run_script(<<~RUBY)
|
|
31
|
-
require "zen_to_i/refine"
|
|
32
|
-
require "zen_to_i/string_ext"
|
|
33
|
-
using ZenToI::Refine
|
|
34
|
-
print "一二三".to_i
|
|
35
|
-
RUBY
|
|
36
|
-
|
|
37
|
-
expect(output).to eq "123"
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
it "ロード順によらず _to_i は素の to_i と同じ結果を返す" do
|
|
41
|
-
outputs = [
|
|
42
|
-
<<~RUBY,
|
|
43
|
-
require "zen_to_i/string_ext"
|
|
44
|
-
require "zen_to_i/refine"
|
|
45
|
-
using ZenToI::Refine
|
|
46
|
-
print "一二三"._to_i
|
|
47
|
-
RUBY
|
|
48
|
-
<<~RUBY,
|
|
49
|
-
require "zen_to_i/refine"
|
|
50
|
-
require "zen_to_i/string_ext"
|
|
51
|
-
using ZenToI::Refine
|
|
52
|
-
print "一二三"._to_i
|
|
53
|
-
RUBY
|
|
54
|
-
].map { |script| run_script(script) }
|
|
55
|
-
|
|
56
|
-
expect(outputs).to eq ["0", "0"]
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
it "先に定義された String#_to_i を上書きしない" do
|
|
60
|
-
output = run_script(<<~RUBY)
|
|
61
|
-
class String
|
|
62
|
-
def _to_i(*)
|
|
63
|
-
"third-party"
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
require "zen_to_i/string_ext"
|
|
67
|
-
print "一二三"._to_i, " ", "一二三".to_i
|
|
68
|
-
RUBY
|
|
69
|
-
|
|
70
|
-
expect(output).to eq "third-party 123"
|
|
71
|
-
end
|
|
72
|
-
end
|
data/spec/refine_spec.rb
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "zen_to_i/refine"
|
|
4
|
-
|
|
5
|
-
using ZenToI::Refine
|
|
6
|
-
|
|
7
|
-
describe ZenToI::Refine do
|
|
8
|
-
describe "String#to_i refined" do
|
|
9
|
-
it "usingを使っていると漢数字は変換できる" do
|
|
10
|
-
expect("一二三".to_i).to eq 123
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
describe "String#_to_i refined" do
|
|
15
|
-
it "_to_iは差し替え前のto_iと同じ結果を返す" do
|
|
16
|
-
expect("一二三"._to_i).to eq 0
|
|
17
|
-
expect("123"._to_i).to eq 123
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
describe "複数回読み込んでも無限再帰にならない" do
|
|
22
|
-
it "refineを再度読み込んでもto_iが動作する" do
|
|
23
|
-
load File.expand_path("../lib/zen_to_i/refine.rb", __dir__)
|
|
24
|
-
|
|
25
|
-
expect { "一二三".to_i }.not_to raise_error
|
|
26
|
-
expect("一二三".to_i).to eq 123
|
|
27
|
-
expect("四五六".to_i).to eq 456
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
data/spec/string_ext_spec.rb
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "zen_to_i/string_ext"
|
|
4
|
-
|
|
5
|
-
describe String do
|
|
6
|
-
describe "#to_i overwritten" do
|
|
7
|
-
it "to_iで漢数字変換ができる" do
|
|
8
|
-
expect("一二三".to_i).to eq 123
|
|
9
|
-
end
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
describe "String#_to_i overwritten" do
|
|
13
|
-
it "_to_iは差し替え前のto_iと同じ結果を返す" do
|
|
14
|
-
expect("一二三"._to_i).to eq 0
|
|
15
|
-
expect("123"._to_i).to eq 123
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
describe "複数回読み込んでも無限再帰にならない" do
|
|
20
|
-
it "string_extを再度読み込んでもto_iが動作する" do
|
|
21
|
-
load File.expand_path("../lib/zen_to_i/string_ext.rb", __dir__)
|
|
22
|
-
|
|
23
|
-
expect { "一二三".to_i }.not_to raise_error
|
|
24
|
-
expect("一二三".to_i).to eq 123
|
|
25
|
-
expect("四五六".to_i).to eq 456
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
end
|
data/spec/zen_to_i_spec.rb
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "zen_to_i"
|
|
4
|
-
|
|
5
|
-
describe ZenToI do
|
|
6
|
-
describe "String#zen_to_i" do
|
|
7
|
-
it "数字以外の文字の場合はそのまま返す" do
|
|
8
|
-
expect("hoge".zen_to_i).to eq "hoge"
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
it "漢数字の場合は半角数字に変換する" do
|
|
12
|
-
expect("一二三".zen_to_i).to eq "123"
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
it "位をあらわす漢数字を含む場合も半角数字に変換する" do
|
|
16
|
-
expect("百三".zen_to_i).to eq "103"
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
it "兆の位まで半角数字に変換する" do
|
|
20
|
-
expect("三兆五十二万四十八".zen_to_i).to eq "3000000520048"
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
it "大字でも変換可能" do
|
|
24
|
-
expect("肆佰参拾捌".zen_to_i).to eq "438"
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
end
|
data/zen_to_i.gemspec
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
lib = File.expand_path("lib", __dir__)
|
|
2
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
|
-
require "zen_to_i/version"
|
|
4
|
-
|
|
5
|
-
Gem::Specification.new do |spec|
|
|
6
|
-
spec.name = "zen_to_i"
|
|
7
|
-
spec.version = ZenToI::VERSION
|
|
8
|
-
spec.authors = ["yoshitsugu"]
|
|
9
|
-
spec.email = ["zeroinon1206bass@gmail.com"]
|
|
10
|
-
spec.description = "全角文字に含まれる数字を半角の数値に変換。兆まで対応"
|
|
11
|
-
spec.summary = "Japanese zenkaku to number"
|
|
12
|
-
spec.homepage = "https://github.com/yoshitsugu/zen_to_i"
|
|
13
|
-
spec.license = "MIT"
|
|
14
|
-
spec.required_ruby_version = ">= 2.4.6"
|
|
15
|
-
|
|
16
|
-
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
|
17
|
-
spec.executables = spec.files.grep(%r{^bin/}) {|f| File.basename(f) }
|
|
18
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
|
-
spec.require_paths = ["lib"]
|
|
20
|
-
|
|
21
|
-
spec.add_development_dependency "bundler"
|
|
22
|
-
spec.add_development_dependency "onkcop"
|
|
23
|
-
spec.add_development_dependency "pry"
|
|
24
|
-
spec.add_development_dependency "rake"
|
|
25
|
-
spec.add_development_dependency "rspec"
|
|
26
|
-
end
|