FAQ
Reviewers: r,

Message:
Hello r@golang.org (cc: golang-dev@googlegroups.com),

I'd like you to review this change to
https://code.google.com/p/go.exp/


Description:
go.ext/locale: added a function for the common pattern [n<<2:][:4] and
changed some of the functions to take the index as an argument so that
testing functions do not need to modify global variables.

Please review this at https://codereview.appspot.com/8357044/

Affected files:
M locale/lookup.go
M locale/lookup_test.go


Index: locale/lookup.go
===================================================================
--- a/locale/lookup.go
+++ b/locale/lookup.go
@@ -10,13 +10,18 @@
"strconv"
)

+// get gets the string of length n for id from the given 4-byte string
index.
+func get(idx string, id, n int) string {
+ return idx[id<<2:][:n]
+}
+
// search searchs for the insertion point of key in smap, which is a
// string with consecutive 4-byte entries. Only the first len(key)
// bytes from the start of the 4-byte entries will be considered.
func search(smap, key string) int {
n := len(key)
return sort.Search(len(smap)>>2, func(i int) bool {
- return smap[i<<2:][:n] >= key
+ return get(smap, i, n) >= key
}) << 2
}

@@ -164,7 +169,7 @@
if l[3] == 0 {
return l[:3]
} else if l[2] == 0 {
- return mappedLang[l[3]<<2:][:3]
+ return get(mappedLang, int(l[3]), 3)
}
// This allocation will only happen for 3-letter ISO codes
// that are non-canonical BCP47 language identifiers.
@@ -234,7 +239,7 @@
return fmt.Sprintf("%03d", r.m49())
}
r -= isoRegionOffset
- return regionISO[r<<2:][:2]
+ return get(regionISO, int(r), 2)
}

// The use of this is uncommon.
@@ -262,30 +267,30 @@

// getScriptID returns the script id for string s. It assumes that s
// is of the format [A-Z][a-z]{3}.
-func getScriptID(s string) scriptID {
- if i := index(script, fixCase("Zzzz", s)); i != -1 {
+func getScriptID(idx, s string) scriptID {
+ if i := index(idx, fixCase("Zzzz", s)); i != -1 {
return scriptID(i >> 2)
}
return unknownScript
}

func (s scriptID) String() string {
- return script[int(s)<<2:][:4]
+ return get(script, int(s), 4)
}

type currencyID uint16

-func getCurrencyID(s string) currencyID {
- if i := index(currency, fixCase("XXX", s)); i != -1 {
+func getCurrencyID(idx, s string) currencyID {
+ if i := index(idx, fixCase("XXX", s)); i != -1 {
return currencyID(i >> 2)
}
return unknownCurrency
}

-func (c currencyID) round() int {
- return int(currency[c<<2+3] >> 2)
+func round(index string, c currencyID) int {
+ return int(index[c<<2+3] >> 2)
}

-func (c currencyID) decimals() int {
- return int(currency[c<<2+3] & 0x03)
+func decimals(index string, c currencyID) int {
+ return int(index[c<<2+3] & 0x03)
}
Index: locale/lookup_test.go
===================================================================
--- a/locale/lookup_test.go
+++ b/locale/lookup_test.go
@@ -174,7 +174,7 @@
}

func TestScript(t *testing.T) {
- script = "BbbbDdddEeeeZzzz\xff\xff\xff\xff"
+ idx := "BbbbDdddEeeeZzzz\xff\xff\xff\xff"
const und = 3
tests := []struct {
in string
@@ -192,7 +192,7 @@
{"Zzzz", 3},
}
for i, tt := range tests {
- if id := getScriptID(tt.in); id != tt.out {
+ if id := getScriptID(idx, tt.in); id != tt.out {
t.Errorf("%d:%s: found %d; want %d", i, tt.in, id, tt.out)
}
}
@@ -202,7 +202,7 @@
curInfo := func(round, dec int) string {
return string(round<<2 + dec)
}
- currency = strings.Join([]string{
+ idx := strings.Join([]string{
"BBB" + curInfo(5, 2),
"DDD\x00",
"XXX\x00",
@@ -227,14 +227,14 @@
{"Zzz", 3, 0, 0},
}
for i, tt := range tests {
- id := getCurrencyID(tt.in)
+ id := getCurrencyID(idx, tt.in)
if id != tt.out {
t.Errorf("%d:%s: found %d; want %d", i, tt.in, id, tt.out)
}
- if d := id.decimals(); d != tt.dec {
+ if d := decimals(idx, id); d != tt.dec {
t.Errorf("%d:dec(%s): found %d; want %d", i, tt.in, d, tt.dec)
}
- if d := id.round(); d != tt.round {
+ if d := round(idx, id); d != tt.round {
t.Errorf("%d:round(%s): found %d; want %d", i, tt.in, d, tt.round)
}
}


--

---
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedApr 4, '13 at 2:30p
activeApr 4, '13 at 2:30p
posts1
users1
websitegolang.org

1 user in discussion

Mpvl: 1 post

People

Translate

site design / logo © 2022 Grokbase