diff options
author | joeybloggs <Dean.Karn@gmail.com> | 2016-08-13 05:03:24 +0300 |
---|---|---|
committer | joeybloggs <Dean.Karn@gmail.com> | 2016-08-13 05:03:24 +0300 |
commit | 99657750cc781e41c9c1e3be6b6316c18fc430bd (patch) | |
tree | 3366a7d2a645600dbe8efbadc86874f94c1cedb5 /cmd | |
parent | 7639a439740dfc3cdda4ae188bf3baf7cc2ff7a3 (diff) |
ad variable grouping logic + cleanup generated code newlines
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/generate_resources.go | 102 | ||||
-rw-r--r-- | cmd/translator.tmpl | 53 |
2 files changed, 125 insertions, 30 deletions
diff --git a/cmd/generate_resources.go b/cmd/generate_resources.go index 49a3d78b..52e6a577 100644 --- a/cmd/generate_resources.go +++ b/cmd/generate_resources.go @@ -31,20 +31,21 @@ var ( "t": "t := locales.T(n, v)\n", } - translators = make(map[string]*translator) - baseTranslators = make(map[string]*translator) - globalCurrenciesMap = make(map[string]struct{}) // ["USD"] = "$" currency code, just all currencies for mapping to enum - globCurrencyIdxMap = make(map[string]int) // ["USD"] = 0 - globalCurrencies = make([]string, 0, 100) // array of currency codes index maps to enum - tmpl *template.Template - nModRegex = regexp.MustCompile("(n%[0-9]+)") - iModRegex = regexp.MustCompile("(i%[0-9]+)") - wModRegex = regexp.MustCompile("(w%[0-9]+)") - fModRegex = regexp.MustCompile("(f%[0-9]+)") - tModRegex = regexp.MustCompile("(t%[0-9]+)") - groupLenRegex = regexp.MustCompile(",([0-9#]+)\\.") - requiredNumRegex = regexp.MustCompile("([0-9]+)\\.") - requiredDecimalRegex = regexp.MustCompile("\\.([0-9]+)") + translators = make(map[string]*translator) + baseTranslators = make(map[string]*translator) + globalCurrenciesMap = make(map[string]struct{}) // ["USD"] = "$" currency code, just all currencies for mapping to enum + globCurrencyIdxMap = make(map[string]int) // ["USD"] = 0 + globalCurrencies = make([]string, 0, 100) // array of currency codes index maps to enum + tmpl *template.Template + nModRegex = regexp.MustCompile("(n%[0-9]+)") + iModRegex = regexp.MustCompile("(i%[0-9]+)") + wModRegex = regexp.MustCompile("(w%[0-9]+)") + fModRegex = regexp.MustCompile("(f%[0-9]+)") + tModRegex = regexp.MustCompile("(t%[0-9]+)") + groupLenRegex = regexp.MustCompile(",([0-9#]+)\\.") + secondaryGroupLenRegex = regexp.MustCompile(",([0-9#]+),") + requiredNumRegex = regexp.MustCompile("([0-9]+)\\.") + requiredDecimalRegex = regexp.MustCompile("\\.([0-9]+)") ) type translator struct { @@ -56,15 +57,22 @@ type translator struct { OrdinalFunc string RangeFunc string Decimal string + DecimalLen int Group string + GroupLen int Minus string + MinusLen int Percent string PerMille string Currencies string // FmtNumberFunc string - FmtNumberExists bool - FmtNumberGroupLen int - FmtNumberMinDecimalLen int + FmtNumberExists bool + FmtNumberPrefix string + FmtNumberSuffix string + + FmtNumberGroupLen int + FmtNumberSecondaryGroupLen int + FmtNumberMinDecimalLen int // calculation only fields DecimalNumberFormat string @@ -189,10 +197,12 @@ func postProcess(cldr *cldr.CLDR) { if len(trans.Decimal) == 0 { if found { + trans.DecimalLen = base.DecimalLen trans.Decimal = base.Decimal } if len(trans.Decimal) == 0 { + trans.DecimalLen = 0 trans.Decimal = "[]byte{}" } } @@ -200,10 +210,12 @@ func postProcess(cldr *cldr.CLDR) { if len(trans.Group) == 0 { if found { + trans.GroupLen = base.GroupLen trans.Group = base.Group } if len(trans.Group) == 0 { + trans.GroupLen = 0 trans.Group = "[]byte{}" } } @@ -211,10 +223,12 @@ func postProcess(cldr *cldr.CLDR) { if len(trans.Minus) == 0 { if found { + trans.MinusLen = base.MinusLen trans.Minus = base.Minus } if len(trans.Minus) == 0 { + trans.MinusLen = 0 trans.Minus = "[]byte{}" } } @@ -325,13 +339,19 @@ func preProcess(cldr *cldr.CLDR) { symbol := ldml.Numbers.Symbols[0] if len(symbol.Decimal) > 0 { - trans.Decimal = fmt.Sprintf("%#v", []byte(symbol.Decimal[0].Data())) + b := []byte(symbol.Decimal[0].Data()) + trans.DecimalLen = len(b) + trans.Decimal = fmt.Sprintf("%#v", b) } if len(symbol.Group) > 0 { - trans.Group = fmt.Sprintf("%#v", []byte(symbol.Group[0].Data())) + b := []byte(symbol.Group[0].Data()) + trans.GroupLen = len(b) + trans.Group = fmt.Sprintf("%#v", b) } if len(symbol.MinusSign) > 0 { - trans.Minus = fmt.Sprintf("%#v", []byte(symbol.MinusSign[0].Data())) + b := []byte(symbol.MinusSign[0].Data()) + trans.MinusLen = len(b) + trans.Minus = fmt.Sprintf("%#v", b) } if len(symbol.PercentSign) > 0 { trans.Percent = fmt.Sprintf("%#v", []byte(symbol.PercentSign[0].Data())) @@ -409,16 +429,54 @@ func parseDecimalNumberFormat(trans *translator) (results string) { trans.FmtNumberExists = true - match := groupLenRegex.FindString(trans.DecimalNumberFormat) + formats := strings.SplitN(trans.DecimalNumberFormat, ";", 2) + + // if len(formats) > 1 { + // trans.FmtNumberHasNegativeFormat = true + // } + + match := groupLenRegex.FindString(formats[0]) if len(match) > 0 { trans.FmtNumberGroupLen = len(match) - 2 } - match = requiredDecimalRegex.FindString(trans.DecimalNumberFormat) + match = requiredDecimalRegex.FindString(formats[0]) if len(match) > 0 { trans.FmtNumberMinDecimalLen = len(match) - 1 } + match = secondaryGroupLenRegex.FindString(formats[0]) + if len(match) > 0 { + trans.FmtNumberSecondaryGroupLen = len(match) - 2 + } + + // start := 0 + // // prefix := "" + + // // positive prefix + // for start = 0; start < len(formats[0]); start++ { + // if formats[0][start] == '#' || formats[0][start] == '0' { + // break + // } + // } + + // // if start > 0 { + // // prefix = formats[0][:start] + // // } + + // end := 0 + + // // positive prefix + // for end = len(formats[0]) - 1; end >= 0; end-- { + // if formats[0][end] == '#' || formats[0][end] == '0' { + // end++ + // break + // } + // } + + // fmt.Println(start) + // fmt.Println(end) + return } diff --git a/cmd/translator.tmpl b/cmd/translator.tmpl index b4c31b78..96a75bea 100644 --- a/cmd/translator.tmpl +++ b/cmd/translator.tmpl @@ -69,12 +69,16 @@ func({{ .BaseLocale }} *{{ .Locale }}) RangePluralRule(num1 float64, v1 uint64,n // avoid allocations; otherwise just cast as string. func({{ .BaseLocale }} *{{ .Locale }}) FmtNumber(num float64, v uint64) []byte { - s := strconv.FormatFloat(num, 'f', int(v), 64) - {{ if eq .FmtNumberExists true }} - {{ if gt .FmtNumberGroupLen 0 }} + s := strconv.FormatFloat(math.Abs(num), 'f', int(v), 64) + {{- if eq .FmtNumberExists true }} + {{- if gt .FmtNumberGroupLen 0 }} l := len(s) + len({{ .BaseLocale }}.decimal) + len({{ .BaseLocale }}.group) * len(s[:len(s)-int(v)-1]) / {{ .FmtNumberGroupLen }} count := 0 inWhole := v == 0 + {{- if gt .FmtNumberSecondaryGroupLen 0}} + inSecondary := false + groupThreshold := {{ .FmtNumberGroupLen }} + {{ end -}} {{ else }} l := len(s) + len({{ .BaseLocale }}.decimal) {{ end }} @@ -84,11 +88,14 @@ func({{ .BaseLocale }} *{{ .Locale }}) FmtNumber(num float64, v uint64) []byte { if s[i] == '.' { + {{- if eq .DecimalLen 1 }} + b = append(b, {{ .BaseLocale }}.decimal[0]) + {{- else }} for j := len({{ .BaseLocale }}.decimal) - 1; j >= 0; j-- { b = append(b, {{ .BaseLocale }}.decimal[j]) } - - {{ if gt .FmtNumberGroupLen 0 }} + {{ end -}} + {{- if gt .FmtNumberGroupLen 0 }} inWhole = true {{ end }} continue @@ -97,13 +104,33 @@ func({{ .BaseLocale }} *{{ .Locale }}) FmtNumber(num float64, v uint64) []byte { {{ if gt .FmtNumberGroupLen 0 }} if inWhole { - if count == {{ .FmtNumberGroupLen }} { - + {{- if gt .FmtNumberSecondaryGroupLen 0}} + + if count == groupThreshold { + {{- if eq .GroupLen 1 }} + b = append(b, {{ .BaseLocale }}.group[0]) + {{- else }} for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- { b = append(b, {{ .BaseLocale }}.group[j]) } + {{ end }} + count = 1 + if !inSecondary { + inSecondary = true + groupThreshold = {{ .FmtNumberSecondaryGroupLen }} + } + {{ else }} + if count == {{ .FmtNumberGroupLen }} { + {{- if eq .GroupLen 1 }} + b = append(b, {{ .BaseLocale }}.group[0]) + {{- else }} + for j := len({{ .BaseLocale }}.group) - 1; j >= 0; j-- { + b = append(b, {{ .BaseLocale }}.group[j]) + } + {{ end }} count = 1 + {{ end -}} } else { count++ } @@ -114,6 +141,16 @@ func({{ .BaseLocale }} *{{ .Locale }}) FmtNumber(num float64, v uint64) []byte { b = append(b, s[i]) } + if num < 0 { + {{- if eq .MinusLen 1 }} + b = append(b, {{ .BaseLocale }}.minus[0]) + {{ else }} + for j := len({{ .BaseLocale }}.minus) - 1; j >= 0; j-- { + b = append(b, {{ .BaseLocale }}.minus[j]) + } + {{ end -}} + } + // reverse for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 { b[i], b[j] = b[j], b[i] @@ -135,7 +172,7 @@ func({{ .BaseLocale }} *{{ .Locale }}) FmtNumber(num float64, v uint64) []byte { return b {{ else }} return []byte(s) - {{ end }} + {{ end -}} } {{ end }}
\ No newline at end of file |