Skip to content

Commit 3de88d7

Browse files
author
youthlin
committed
po file almost done
1 parent 4cee556 commit 3de88d7

File tree

10 files changed

+639
-434
lines changed

10 files changed

+639
-434
lines changed

plurals/exp.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
// Eval 传入复数表达式,返回 n 应该使用哪种复数形式
14-
func Eval(ctx context.Context, exp string, n int) (result int, err error) {
14+
func Eval(ctx context.Context, exp string, n int64) (result int64, err error) {
1515
defer func() {
1616
if e := recover(); e != nil {
1717
err = errors.Errorf("unexpected error: %v", e)
@@ -43,17 +43,17 @@ const (
4343
type myListener struct {
4444
// 实现这个父类上感兴趣的方法(相当于一个 adaptor)
4545
*parser.BasepluralListener
46-
stack antlr.IntStack // 操作数栈
46+
stack Int64Stack // 操作数栈
4747
ctx context.Context // 外部传入的 ctx, 用于 debug
48-
n int // 参数 n
49-
result int // 结果
48+
n int64 // 参数 n
49+
result int64 // 结果
5050
}
5151

52-
func newListener(ctx context.Context, n int) *myListener {
52+
func newListener(ctx context.Context, n int64) *myListener {
5353
return &myListener{
5454
ctx: ctx,
5555
n: n,
56-
stack: make(antlr.IntStack, 0),
56+
stack: make(Int64Stack, 0),
5757
}
5858
}
5959

@@ -291,7 +291,7 @@ func (s *myListener) ExitPrimary(ctx *parser.PrimaryContext) {
291291
if err != nil {
292292
panic("assert error: not a number: " + num)
293293
}
294-
s.stack.Push(int(iNum))
294+
s.stack.Push(iNum)
295295
}
296296
s.debug("primary: %v stack=%v", ctx.GetText(), s.stack)
297297
}

plurals/exp_test.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ var ctx = context.Background()
1313
type args struct {
1414
ctx context.Context
1515
exp string
16-
n int
16+
n int64
1717
}
1818
type tCase struct {
1919
name string
2020
args args
21-
want int
21+
want int64
2222
wantErr bool
2323
}
2424

@@ -46,11 +46,11 @@ func okCase() []tCase {
4646
}
4747

4848
// 拉脱维亚语 Latvian 三种复数,0特殊
49-
for i := 0; i < 215; i++ {
49+
for i := int64(0); i < 215; i++ {
5050
tests = append(tests,
5151
tCase{fmt.Sprintf("test-case-3-#%v", i),
5252
args{ctx, "n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2", i},
53-
func(n int) int {
53+
func(n int64) int64 {
5454
// n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2
5555
if n%10 == 1 && n%100 != 11 {
5656
return 0
@@ -63,11 +63,11 @@ func okCase() []tCase {
6363
)
6464
}
6565
// 三种复数,00 [2-9][0-9] Romanian 罗马尼亚
66-
for i := 0; i < 215; i++ {
66+
for i := int64(0); i < 215; i++ {
6767
tests = append(tests,
6868
tCase{fmt.Sprintf("test-case-4-#%v", i),
6969
args{ctx, "n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2", i},
70-
func(n int) int {
70+
func(n int64) int64 {
7171
// n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2
7272
if n == 1 {
7373
return 0
@@ -80,11 +80,11 @@ func okCase() []tCase {
8080
)
8181
}
8282
// 三种复数,1[2-9] 立陶宛 Lithuanian
83-
for i := 0; i < 215; i++ {
83+
for i := int64(0); i < 215; i++ {
8484
tests = append(tests,
8585
tCase{fmt.Sprintf("test-case-5-#%v", i),
8686
args{ctx, "n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2", i},
87-
func(n int) int {
87+
func(n int64) int64 {
8888
// n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2
8989
if n%10 == 1 && n%100 != 11 {
9090
return 0
@@ -97,11 +97,11 @@ func okCase() []tCase {
9797
)
9898
}
9999
// 三种复数,1234结尾的特殊,但不是 1[1-4] Russian, Ukrainian, Belarusian, Serbian, Croatian
100-
for i := 0; i < 215; i++ {
100+
for i := int64(0); i < 215; i++ {
101101
tests = append(tests,
102102
tCase{fmt.Sprintf("test-case-6-#%v", i),
103103
args{ctx, "n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2", i},
104-
func(n int) int {
104+
func(n int64) int64 {
105105
// n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2
106106
if n%10 == 1 && n%100 != 11 {
107107
return 0
@@ -114,11 +114,11 @@ func okCase() []tCase {
114114
)
115115
}
116116
// 三种复数,1234结尾的特殊 Czech, Slovak
117-
for i := 0; i < 215; i++ {
117+
for i := int64(0); i < 215; i++ {
118118
tests = append(tests,
119119
tCase{fmt.Sprintf("test-case-7-#%v", i),
120120
args{ctx, "(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2", i},
121-
func(n int) int {
121+
func(n int64) int64 {
122122
// (n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2
123123
if n == 1 {
124124
return 0
@@ -132,11 +132,11 @@ func okCase() []tCase {
132132
}
133133
// 三种复数,Three forms, special case for one and some numbers ending in 2, 3, or 4
134134
// Polish
135-
for i := 0; i < 215; i++ {
135+
for i := int64(0); i < 215; i++ {
136136
tests = append(tests,
137137
tCase{fmt.Sprintf("test-case-8-#%v", i),
138138
args{ctx, "n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2", i},
139-
func(n int) int {
139+
func(n int64) int64 {
140140
// n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2
141141
if n == 1 {
142142
return 0
@@ -150,11 +150,11 @@ func okCase() []tCase {
150150
}
151151
// Four forms, special case for one and all numbers ending in 02, 03, or 04
152152
// Slovenian
153-
for i := 0; i < 215; i++ {
153+
for i := int64(0); i < 215; i++ {
154154
tests = append(tests,
155155
tCase{fmt.Sprintf("test-case-9-#%v", i),
156156
args{ctx, "n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3", i},
157-
func(n int) int {
157+
func(n int64) int64 {
158158
//n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3
159159
if n%100 == 1 {
160160
return 0
@@ -171,11 +171,11 @@ func okCase() []tCase {
171171
}
172172
// Six forms, special cases for one, two, all numbers ending in 02, 03, … 10, all numbers ending in 11 … 99, and others
173173
// Arabic
174-
for i := 0; i < 215; i++ {
174+
for i := int64(0); i < 215; i++ {
175175
tests = append(tests,
176176
tCase{fmt.Sprintf("test-case-10-#%v", i),
177177
args{ctx, " n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 ? 4 : 5", i},
178-
func(n int) int {
178+
func(n int64) int64 {
179179
if n == 0 {
180180
return 0
181181
}

plurals/stack.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package plurals
2+
3+
import "errors"
4+
5+
// A simple integer stack
6+
7+
// see antlr.IntStack
8+
type Int64Stack []int64
9+
10+
var ErrEmptyStack = errors.New("Stack is empty")
11+
12+
func (s *Int64Stack) Pop() (int64, error) {
13+
l := len(*s) - 1
14+
if l < 0 {
15+
return 0, ErrEmptyStack
16+
}
17+
v := (*s)[l]
18+
*s = (*s)[0:l]
19+
return v, nil
20+
}
21+
22+
func (s *Int64Stack) Push(e int64) {
23+
*s = append(*s, e)
24+
}

0 commit comments

Comments
 (0)