Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
d91b419e
Commit
d91b419e
authored
Dec 06, 2015
by
Jeromy
Browse files
WIP
parent
a40ef343
Changes
365
Show whitespace changes
Inline
Side-by-side
Too many changes to show.
To preserve performance only
365 of 365+
files are displayed.
Plain diff
Email patch
vendor/gx/QmVgdgtv5rUcWWcHvHN1vdGkXi8UNztJ7JuT72YUJgG2ic/go-text/cldr/examples_test.go
deleted
100644 → 0
View file @
a40ef343
package
cldr_test
import
(
"fmt"
"gx/QmVgdgtv5rUcWWcHvHN1vdGkXi8UNztJ7JuT72YUJgG2ic/go-text/cldr"
)
func
ExampleSlice
()
{
var
dr
*
cldr
.
CLDR
// assume this is initalized
x
,
_
:=
dr
.
LDML
(
"en"
)
cs
:=
x
.
Collations
.
Collation
// remove all but the default
cldr
.
MakeSlice
(
&
cs
)
.
Filter
(
func
(
e
cldr
.
Elem
)
bool
{
return
e
.
GetCommon
()
.
Type
!=
x
.
Collations
.
Default
()
})
for
i
,
c
:=
range
cs
{
fmt
.
Println
(
i
,
c
.
Type
)
}
}
vendor/gx/QmVgdgtv5rUcWWcHvHN1vdGkXi8UNztJ7JuT72YUJgG2ic/go-text/cldr/makexml.go
deleted
100644 → 0
View file @
a40ef343
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ignore
// This tool generates types for the various XML formats of CLDR.
package
main
import
(
"archive/zip"
"bytes"
"encoding/xml"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
"golang.org/x/text/internal/gen"
)
var
outputFile
=
flag
.
String
(
"output"
,
"xml.go"
,
"output file name"
)
func
main
()
{
flag
.
Parse
()
r
:=
gen
.
OpenCLDRCoreZip
()
buffer
,
err
:=
ioutil
.
ReadAll
(
r
)
if
err
!=
nil
{
log
.
Fatal
(
"Could not read zip file"
)
}
r
.
Close
()
z
,
err
:=
zip
.
NewReader
(
bytes
.
NewReader
(
buffer
),
int64
(
len
(
buffer
)))
if
err
!=
nil
{
log
.
Fatalf
(
"Could not read zip archive: %v"
,
err
)
}
var
buf
bytes
.
Buffer
version
:=
gen
.
CLDRVersion
()
for
_
,
dtd
:=
range
files
{
for
_
,
f
:=
range
z
.
File
{
if
strings
.
HasSuffix
(
f
.
Name
,
dtd
.
file
+
".dtd"
)
{
r
,
err
:=
f
.
Open
()
failOnError
(
err
)
b
:=
makeBuilder
(
&
buf
,
dtd
)
b
.
parseDTD
(
r
)
b
.
resolve
(
b
.
index
[
dtd
.
top
[
0
]])
b
.
write
()
if
b
.
version
!=
""
&&
version
!=
b
.
version
{
println
(
f
.
Name
)
log
.
Fatalf
(
"main: inconsistent versions: found %s; want %s"
,
b
.
version
,
version
)
}
break
}
}
}
fmt
.
Fprintln
(
&
buf
,
"// Version is the version of CLDR from which the XML definitions are generated."
)
fmt
.
Fprintf
(
&
buf
,
"const Version = %q
\n
"
,
version
)
gen
.
WriteGoFile
(
*
outputFile
,
"cldr"
,
buf
.
Bytes
())
}
func
failOnError
(
err
error
)
{
if
err
!=
nil
{
log
.
New
(
os
.
Stderr
,
""
,
log
.
Lshortfile
)
.
Output
(
2
,
err
.
Error
())
os
.
Exit
(
1
)
}
}
// configuration data per DTD type
type
dtd
struct
{
file
string
// base file name
root
string
// Go name of the root XML element
top
[]
string
// create a different type for this section
skipElem
[]
string
// hard-coded or deprecated elements
skipAttr
[]
string
// attributes to exclude
predefined
[]
string
// hard-coded elements exist of the form <name>Elem
forceRepeat
[]
string
// elements to make slices despite DTD
}
var
files
=
[]
dtd
{
{
file
:
"ldmlBCP47"
,
root
:
"LDMLBCP47"
,
top
:
[]
string
{
"ldmlBCP47"
},
skipElem
:
[]
string
{
"cldrVersion"
,
// deprecated, not used
},
},
{
file
:
"ldmlSupplemental"
,
root
:
"SupplementalData"
,
top
:
[]
string
{
"supplementalData"
},
skipElem
:
[]
string
{
"cldrVersion"
,
// deprecated, not used
},
forceRepeat
:
[]
string
{
"plurals"
,
// data defined in plurals.xml and ordinals.xml
},
},
{
file
:
"ldml"
,
root
:
"LDML"
,
top
:
[]
string
{
"ldml"
,
"collation"
,
"calendar"
,
"timeZoneNames"
,
"localeDisplayNames"
,
"numbers"
,
},
skipElem
:
[]
string
{
"cp"
,
// not used anywhere
"special"
,
// not used anywhere
"fallback"
,
// deprecated, not used
"alias"
,
// in Common
"default"
,
// in Common
},
skipAttr
:
[]
string
{
"hiraganaQuarternary"
,
// typo in DTD, correct version included as well
},
predefined
:
[]
string
{
"rules"
},
},
}
var
comments
=
map
[
string
]
string
{
"ldmlBCP47"
:
`
// LDMLBCP47 holds information on allowable values for various variables in LDML.
`
,
"supplementalData"
:
`
// SupplementalData holds information relevant for internationalization
// and proper use of CLDR, but that is not contained in the locale hierarchy.
`
,
"ldml"
:
`
// LDML is the top-level type for locale-specific data.
`
,
"collation"
:
`
// Collation contains rules that specify a certain sort-order,
// as a tailoring of the root order.
// The parsed rules are obtained by passing a RuleProcessor to Collation's
// Process method.
`
,
"calendar"
:
`
// Calendar specifies the fields used for formatting and parsing dates and times.
// The month and quarter names are identified numerically, starting at 1.
// The day (of the week) names are identified with short strings, since there is
// no universally-accepted numeric designation.
`
,
"dates"
:
`
// Dates contains information regarding the format and parsing of dates and times.
`
,
"localeDisplayNames"
:
`
// LocaleDisplayNames specifies localized display names for for scripts, languages,
// countries, currencies, and variants.
`
,
"numbers"
:
`
// Numbers supplies information for formatting and parsing numbers and currencies.
`
,
}
type
element
struct
{
name
string
// XML element name
category
string
// elements contained by this element
signature
string
// category + attrKey*
attr
[]
*
attribute
// attributes supported by this element.
sub
[]
struct
{
// parsed and evaluated sub elements of this element.
e
*
element
repeat
bool
// true if the element needs to be a slice
}
resolved
bool
// prevent multiple resolutions of this element.
}
type
attribute
struct
{
name
string
key
string
list
[]
string
tag
string
// Go tag
}
var
(
reHead
=
regexp
.
MustCompile
(
` *(\w+) +([\w\-]+)`
)
reAttr
=
regexp
.
MustCompile
(
` *(\w+) *(?:(\w+)|\(([\w\- \|]+)\)) *(?:#([A-Z]*) *(?:\"([\.\d+])\")?)? *("[\w\-:]*")?`
)
reElem
=
regexp
.
MustCompile
(
`^ *(EMPTY|ANY|\(.*\)[\*\+\?]?) *$`
)
reToken
=
regexp
.
MustCompile
(
`\w\-`
)
)
// builder is used to read in the DTD files from CLDR and generate Go code
// to be used with the encoding/xml package.
type
builder
struct
{
w
io
.
Writer
index
map
[
string
]
*
element
elem
[]
*
element
info
dtd
version
string
}
func
makeBuilder
(
w
io
.
Writer
,
d
dtd
)
builder
{
return
builder
{
w
:
w
,
index
:
make
(
map
[
string
]
*
element
),
elem
:
[]
*
element
{},
info
:
d
,
}
}
// parseDTD parses a DTD file.
func
(
b
*
builder
)
parseDTD
(
r
io
.
Reader
)
{
for
d
:=
xml
.
NewDecoder
(
r
);
;
{
t
,
err
:=
d
.
Token
()
if
t
==
nil
{
break
}
failOnError
(
err
)
dir
,
ok
:=
t
.
(
xml
.
Directive
)
if
!
ok
{
continue
}
m
:=
reHead
.
FindSubmatch
(
dir
)
dir
=
dir
[
len
(
m
[
0
])
:
]
ename
:=
string
(
m
[
2
])
el
,
elementFound
:=
b
.
index
[
ename
]
switch
string
(
m
[
1
])
{
case
"ELEMENT"
:
if
elementFound
{
log
.
Fatal
(
"parseDTD: duplicate entry for element %q"
,
ename
)
}
m
:=
reElem
.
FindSubmatch
(
dir
)
if
m
==
nil
{
log
.
Fatalf
(
"parseDTD: invalid element %q"
,
string
(
dir
))
}
if
len
(
m
[
0
])
!=
len
(
dir
)
{
log
.
Fatal
(
"parseDTD: invalid element %q"
,
string
(
dir
),
len
(
dir
),
len
(
m
[
0
]),
string
(
m
[
0
]))
}
s
:=
string
(
m
[
1
])
el
=
&
element
{
name
:
ename
,
category
:
s
,
}
b
.
index
[
ename
]
=
el
case
"ATTLIST"
:
if
!
elementFound
{
log
.
Fatalf
(
"parseDTD: unknown element %q"
,
ename
)
}
s
:=
string
(
dir
)
m
:=
reAttr
.
FindStringSubmatch
(
s
)
if
m
==
nil
{
log
.
Fatal
(
fmt
.
Errorf
(
"parseDTD: invalid attribute %q"
,
string
(
dir
)))
}
if
m
[
4
]
==
"FIXED"
{
b
.
version
=
m
[
5
]
}
else
{
switch
m
[
1
]
{
case
"draft"
,
"references"
,
"alt"
,
"validSubLocales"
,
"standard"
/* in Common */
:
case
"type"
,
"choice"
:
default
:
el
.
attr
=
append
(
el
.
attr
,
&
attribute
{
name
:
m
[
1
],
key
:
s
,
list
:
reToken
.
FindAllString
(
m
[
3
],
-
1
),
})
el
.
signature
=
fmt
.
Sprintf
(
"%s=%s+%s"
,
el
.
signature
,
m
[
1
],
m
[
2
])
}
}
}
}
}
var
reCat
=
regexp
.
MustCompile
(
`[ ,\|]*(?:(\(|\)|\#?[\w_-]+)([\*\+\?]?))?`
)
// resolve takes a parsed element and converts it into structured data
// that can be used to generate the XML code.
func
(
b
*
builder
)
resolve
(
e
*
element
)
{
if
e
.
resolved
{
return
}
b
.
elem
=
append
(
b
.
elem
,
e
)
e
.
resolved
=
true
s
:=
e
.
category
found
:=
make
(
map
[
string
]
bool
)
sequenceStart
:=
[]
int
{}
for
len
(
s
)
>
0
{
m
:=
reCat
.
FindStringSubmatch
(
s
)
if
m
==
nil
{
log
.
Fatalf
(
"%s: invalid category string %q"
,
e
.
name
,
s
)
}
repeat
:=
m
[
2
]
==
"*"
||
m
[
2
]
==
"+"
||
in
(
b
.
info
.
forceRepeat
,
m
[
1
])
switch
m
[
1
]
{
case
""
:
case
"("
:
sequenceStart
=
append
(
sequenceStart
,
len
(
e
.
sub
))
case
")"
:
if
len
(
sequenceStart
)
==
0
{
log
.
Fatalf
(
"%s: unmatched closing parenthesis"
,
e
.
name
)
}
for
i
:=
sequenceStart
[
len
(
sequenceStart
)
-
1
];
i
<
len
(
e
.
sub
);
i
++
{
e
.
sub
[
i
]
.
repeat
=
e
.
sub
[
i
]
.
repeat
||
repeat
}
sequenceStart
=
sequenceStart
[
:
len
(
sequenceStart
)
-
1
]
default
:
if
in
(
b
.
info
.
skipElem
,
m
[
1
])
{
}
else
if
sub
,
ok
:=
b
.
index
[
m
[
1
]];
ok
{
if
!
found
[
sub
.
name
]
{
e
.
sub
=
append
(
e
.
sub
,
struct
{
e
*
element
repeat
bool
}{
sub
,
repeat
})
found
[
sub
.
name
]
=
true
b
.
resolve
(
sub
)
}
}
else
if
m
[
1
]
==
"#PCDATA"
||
m
[
1
]
==
"ANY"
{
}
else
if
m
[
1
]
!=
"EMPTY"
{
log
.
Fatalf
(
"resolve:%s: element %q not found"
,
e
.
name
,
m
[
1
])
}
}
s
=
s
[
len
(
m
[
0
])
:
]
}
}
// return true if s is contained in set.
func
in
(
set
[]
string
,
s
string
)
bool
{
for
_
,
v
:=
range
set
{
if
v
==
s
{
return
true
}
}
return
false
}
var
repl
=
strings
.
NewReplacer
(
"-"
,
" "
,
"_"
,
" "
)
// title puts the first character or each character following '_' in title case and
// removes all occurrences of '_'.
func
title
(
s
string
)
string
{
return
strings
.
Replace
(
strings
.
Title
(
repl
.
Replace
(
s
)),
" "
,
""
,
-
1
)
}
// writeElem generates Go code for a single element, recursively.
func
(
b
*
builder
)
writeElem
(
tab
int
,
e
*
element
)
{
p
:=
func
(
f
string
,
x
...
interface
{})
{
f
=
strings
.
Replace
(
f
,
"
\n
"
,
"
\n
"
+
strings
.
Repeat
(
"
\t
"
,
tab
),
-
1
)
fmt
.
Fprintf
(
b
.
w
,
f
,
x
...
)
}
if
len
(
e
.
sub
)
==
0
&&
len
(
e
.
attr
)
==
0
{
p
(
"Common"
)
return
}
p
(
"struct {"
)
tab
++
p
(
"
\n
Common"
)
for
_
,
attr
:=
range
e
.
attr
{
if
!
in
(
b
.
info
.
skipAttr
,
attr
.
name
)
{
p
(
"
\n
%s string `xml:
\"
%s,attr
\"
`"
,
title
(
attr
.
name
),
attr
.
name
)
}
}
for
_
,
sub
:=
range
e
.
sub
{
if
in
(
b
.
info
.
predefined
,
sub
.
e
.
name
)
{
p
(
"
\n
%sElem"
,
sub
.
e
.
name
)
continue
}
if
in
(
b
.
info
.
skipElem
,
sub
.
e
.
name
)
{
continue
}
p
(
"
\n
%s "
,
title
(
sub
.
e
.
name
))
if
sub
.
repeat
{
p
(
"[]"
)
}
p
(
"*"
)
if
in
(
b
.
info
.
top
,
sub
.
e
.
name
)
{
p
(
title
(
sub
.
e
.
name
))
}
else
{
b
.
writeElem
(
tab
,
sub
.
e
)
}
p
(
" `xml:
\"
%s
\"
`"
,
sub
.
e
.
name
)
}
tab
--
p
(
"
\n
}"
)
}
// write generates the Go XML code.
func
(
b
*
builder
)
write
()
{
for
i
,
name
:=
range
b
.
info
.
top
{
e
:=
b
.
index
[
name
]
if
e
!=
nil
{
fmt
.
Fprintf
(
b
.
w
,
comments
[
name
])
name
:=
title
(
e
.
name
)
if
i
==
0
{
name
=
b
.
info
.
root
}
fmt
.
Fprintf
(
b
.
w
,
"type %s "
,
name
)
b
.
writeElem
(
0
,
e
)
fmt
.
Fprint
(
b
.
w
,
"
\n
"
)
}
}
}
vendor/gx/QmVgdgtv5rUcWWcHvHN1vdGkXi8UNztJ7JuT72YUJgG2ic/go-text/cldr/resolve.go
deleted
100644 → 0
View file @
a40ef343
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
cldr
// This file implements the various inheritance constructs defined by LDML.
// See http://www.unicode.org/reports/tr35/#Inheritance_and_Validity
// for more details.
import
(
"fmt"
"log"
"reflect"
"regexp"
"sort"
"strings"
)
// fieldIter iterates over fields in a struct. It includes
// fields of embedded structs.
type
fieldIter
struct
{
v
reflect
.
Value
index
,
n
[]
int
}
func
iter
(
v
reflect
.
Value
)
fieldIter
{
if
v
.
Kind
()
!=
reflect
.
Struct
{
log
.
Panicf
(
"value %v must be a struct"
,
v
)
}
i
:=
fieldIter
{
v
:
v
,
index
:
[]
int
{
0
},
n
:
[]
int
{
v
.
NumField
()},
}
i
.
descent
()
return
i
}
func
(
i
*
fieldIter
)
descent
()
{
for
f
:=
i
.
field
();
f
.
Anonymous
&&
f
.
Type
.
NumField
()
>
0
;
f
=
i
.
field
()
{
i
.
index
=
append
(
i
.
index
,
0
)
i
.
n
=
append
(
i
.
n
,
f
.
Type
.
NumField
())
}
}
func
(
i
*
fieldIter
)
done
()
bool
{
return
len
(
i
.
index
)
==
1
&&
i
.
index
[
0
]
>=
i
.
n
[
0
]
}
func
skip
(
f
reflect
.
StructField
)
bool
{
return
!
f
.
Anonymous
&&
(
f
.
Name
[
0
]
<
'A'
||
f
.
Name
[
0
]
>
'Z'
)
}
func
(
i
*
fieldIter
)
next
()
{
for
{
k
:=
len
(
i
.
index
)
-
1
i
.
index
[
k
]
++
if
i
.
index
[
k
]
<
i
.
n
[
k
]
{
if
!
skip
(
i
.
field
())
{
break
}
}
else
{
if
k
==
0
{
return
}
i
.
index
=
i
.
index
[
:
k
]
i
.
n
=
i
.
n
[
:
k
]
}
}
i
.
descent
()
}
func
(
i
*
fieldIter
)
value
()
reflect
.
Value
{
return
i
.
v
.
FieldByIndex
(
i
.
index
)
}
func
(
i
*
fieldIter
)
field
()
reflect
.
StructField
{
return
i
.
v
.
Type
()
.
FieldByIndex
(
i
.
index
)
}
type
visitor
func
(
v
reflect
.
Value
)
error
var
stopDescent
=
fmt
.
Errorf
(
"do not recurse"
)
func
(
f
visitor
)
visit
(
x
interface
{})
error
{
return
f
.
visitRec
(
reflect
.
ValueOf
(
x
))
}
// visit recursively calls f on all nodes in v.
func
(
f
visitor
)
visitRec
(
v
reflect
.
Value
)
error
{
if
v
.
Kind
()
==
reflect
.
Ptr
{
if
v
.
IsNil
()
{
return
nil
}
return
f
.
visitRec
(
v
.
Elem
())
}
if
err
:=
f
(
v
);
err
!=
nil
{
if
err
==
stopDescent
{
return
nil
}
return
err
}
switch
v
.
Kind
()
{
case
reflect
.
Struct
:
for
i
:=
iter
(
v
);
!
i
.
done
();
i
.
next
()
{
if
err
:=
f
.
visitRec
(
i
.
value
());
err
!=
nil
{
return
err
}
}
case
reflect
.
Slice
:
for
i
:=
0
;
i
<
v
.
Len
();
i
++
{
if
err
:=
f
.
visitRec
(
v
.
Index
(
i
));
err
!=
nil
{
return
err
}
}
}
return
nil
}
// getPath is used for error reporting purposes only.
func
getPath
(
e
Elem
)
string
{
if
e
==
nil
{
return
"<nil>"
}
if
e
.
enclosing
()
==
nil
{
return
e
.
GetCommon
()
.
name
}
if
e
.
GetCommon
()
.
Type
==
""
{
return
fmt
.
Sprintf
(
"%s.%s"
,
getPath
(
e
.
enclosing
()),
e
.
GetCommon
()
.
name
)
}
return
fmt
.
Sprintf
(
"%s.%s[type=%s]"
,
getPath
(
e
.
enclosing
()),
e
.
GetCommon
()
.
name
,
e
.
GetCommon
()
.
Type
)
}
// xmlName returns the xml name of the element or attribute
func
xmlName
(
f
reflect
.
StructField
)
(
name
string
,
attr
bool
)
{
tags
:=
strings
.
Split
(
f
.
Tag
.
Get
(
"xml"
),
","
)
for
_
,
s
:=
range
tags
{
attr
=
attr
||
s
==
"attr"
}
return
tags
[
0
],
attr
}
func
findField
(
v
reflect
.
Value
,
key
string
)
(
reflect
.
Value
,
error
)
{
v
=
reflect
.
Indirect
(
v
)
for
i
:=
iter
(
v
);
!
i
.
done
();
i
.
next
()
{
if
n
,
_
:=
xmlName
(
i
.
field
());
n
==
key
{
return
i
.
value
(),
nil
}
}
return
reflect
.
Value
{},
fmt
.
Errorf
(
"cldr: no field %q in element %#v"
,
key
,
v
.
Interface
())
}
var
xpathPart
=
regexp
.
MustCompile
(
`(\pL+)(?:\[@(\pL+)='([\w-]+)'\])?`
)
func
walkXPath
(
e
Elem
,
path
string
)
(
res
Elem
,
err
error
)
{
for
_
,
c
:=
range
strings
.
Split
(
path
,
"/"
)
{
if
c
==
".."
{
if
e
=
e
.
enclosing
();
e
==
nil
{
panic
(
"path .."
)
return
nil
,
fmt
.
Errorf
(
`cldr: ".." moves past root in path %q`
,
path
)
}
continue
}
else
if
c
==
""
{
continue
}
m
:=
xpathPart
.
FindStringSubmatch
(
c
)
if
len
(
m
)
==
0
||
len
(
m
[
0
])
!=
len
(
c
)
{
return
nil
,
fmt
.
Errorf
(
"cldr: syntax error in path component %q"
,
c
)
}
v
,
err
:=
findField
(
reflect
.
ValueOf
(
e
),
m
[
1
])
if
err
!=
nil
{
return
nil
,
err
}
switch
v
.
Kind
()
{
case
reflect
.
Slice
:
i
:=
0
if
m
[
2
]
!=
""
||
v
.
Len
()
>
1
{
if
m
[
2
]
==
""
{
m
[
2
]
=
"type"
if
m
[
3
]
=
e
.
GetCommon
()
.
Default
();
m
[
3
]
==
""
{
return
nil
,
fmt
.
Errorf
(
"cldr: type selector or default value needed for element %s"
,
m
[
1
])
}
}
for
;
i
<
v
.
Len
();
i
++
{
vi
:=
v
.
Index
(
i
)
key
,
err
:=
findField
(
vi
.
Elem
(),
m
[
2
])
if
err
!=
nil
{
return
nil
,
err
}
key
=
reflect
.
Indirect
(
key
)
if
key
.
Kind
()
==
reflect
.
String
&&
key
.
String
()
==
m
[
3
]
{
break
}
}
}
if
i
==
v
.
Len
()
||
v
.
Index
(
i
)
.
IsNil
()
{
return
nil
,
fmt
.
Errorf
(
"no %s found with %s==%s"
,
m
[
1
],
m
[
2
],
m
[
3
])
}
e
=
v
.
Index
(
i
)
.
Interface
()
.
(
Elem
)
case
reflect
.
Ptr
:
if
v
.
IsNil
()
{
return
nil
,
fmt
.
Errorf
(
"cldr: element %q not found within element %q"
,
m
[
1
],
e
.
GetCommon
()
.
name
)
}
var
ok
bool
if
e
,
ok
=
v
.
Interface
()
.
(
Elem
);
!
ok
{
return
nil
,
fmt
.
Errorf
(
"cldr: %q is not an XML element"
,
m
[
1
])
}
else
if
m
[
2
]
!=
""
||
m
[
3
]
!=
""
{
return
nil
,
fmt
.
Errorf
(
"cldr: no type selector allowed for element %s"
,
m
[
1
])
}
default
:
return
nil
,
fmt
.
Errorf
(
"cldr: %q is not an XML element"
,
m
[
1
])
}
}
return
e
,
nil
}
const
absPrefix
=
"//ldml/"
func
(
cldr
*
CLDR
)
resolveAlias
(
e
Elem
,
src
,
path
string
)
(
res
Elem
,
err
error
)
{
if
src
!=
"locale"
{
if
!
strings
.
HasPrefix
(
path
,
absPrefix
)
{
return
nil
,
fmt
.
Errorf
(
"cldr: expected absolute path, found %q"
,
path
)
}
path
=
path
[
len
(
absPrefix
)
:
]
if
e
,
err
=
cldr
.
resolve
(
src
);
err
!=
nil
{
return
nil
,
err
}
}
return
walkXPath
(
e
,
path
)
}
func
(
cldr
*
CLDR
)
resolveAndMergeAlias
(
e
Elem
)
error
{
alias
:=
e
.
GetCommon
()
.
Alias
if
alias
==
nil
{
return
nil
}
a
,
err
:=
cldr
.
resolveAlias
(
e
,
alias
.
Source
,
alias
.
Path
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"%v: error evaluating path %q: %v"
,
getPath
(
e
),
alias
.
Path
,
err
)
}
// Ensure alias node was already evaluated. TODO: avoid double evaluation.
err
=
cldr
.
resolveAndMergeAlias
(
a
)
v
:=
reflect
.
ValueOf
(
e
)
.
Elem
()
for
i
:=
iter
(
reflect
.
ValueOf
(
a
)
.
Elem
());
!
i
.
done
();
i
.
next
()
{
if
vv
:=
i
.
value
();
vv
.
Kind
()
!=
reflect
.
Ptr
||
!
vv
.
IsNil
()
{
if
_
,
attr
:=
xmlName
(
i
.
field
());
!
attr
{
v
.
FieldByIndex
(
i
.
index
)
.
Set
(
vv
)
}
}
}
return
err
}
func
(
cldr
*
CLDR
)
aliasResolver
()
visitor
{
return
func
(
v
reflect
.
Value
)
(
err
error
)
{
if
e
,
ok
:=
v
.
Addr
()
.
Interface
()
.
(
Elem
);
ok
{
err
=
cldr
.
resolveAndMergeAlias
(
e
)
if
err
==
nil
&&
blocking
[
e
.
GetCommon
()
.
name
]
{
return
stopDescent
}
}
return
err
}
}
// elements within blocking elements do not inherit.
// Taken from CLDR's supplementalMetaData.xml.
var
blocking
=
map
[
string
]
bool
{
"identity"
:
true
,
"supplementalData"
:
true
,
"cldrTest"
:
true
,
"collation"
:
true
,
"transform"
:
true
,
}
// Distinguishing attributes affect inheritance; two elements with different
// distinguishing attributes are treated as different for purposes of inheritance,
// except when such attributes occur in the indicated elements.
// Taken from CLDR's supplementalMetaData.xml.
var
distinguishing
=
map
[
string
][]
string
{
"key"
:
nil
,
"request_id"
:
nil
,
"id"
:
nil
,
"registry"
:
nil
,
"alt"
:
nil
,
"iso4217"
:
nil
,
"iso3166"
:
nil
,
"mzone"
:
nil
,
"from"
:
nil
,
"to"
:
nil
,
"type"
:
[]
string
{
"abbreviationFallback"
,
"default"
,
"mapping"
,
"measurementSystem"
,
"preferenceOrdering"
,
},
"numberSystem"
:
nil
,
}
func
in
(
set
[]
string
,
s
string
)
bool
{
for
_
,
v
:=
range
set
{
if
v
==
s
{
return
true
}
}
return
false
}
// attrKey computes a key based on the distinguishable attributes of
// an element and it's values.
func
attrKey
(
v
reflect
.
Value
,
exclude
...
string
)
string
{
parts
:=
[]
string
{}
ename
:=
v
.
Interface
()
.
(
Elem
)
.
GetCommon
()
.
name
v
=
v
.
Elem
()
for
i
:=
iter
(
v
);
!
i
.
done
();
i
.
next
()
{
if
name
,
attr
:=
xmlName
(
i
.
field
());
attr
{
if
except
,
ok
:=
distinguishing
[
name
];
ok
&&
!
in
(
exclude
,
name
)
&&
!
in
(
except
,
ename
)
{
v
:=
i
.
value
()
if
v
.
Kind
()
==
reflect
.
Ptr
{
v
=
v
.
Elem
()
}
if
v
.
IsValid
()
{
parts
=
append
(
parts
,
fmt
.
Sprintf
(
"%s=%s"
,
name
,
v
.
String
()))
}
}
}
}
sort
.
Strings
(
parts
)
return
strings
.
Join
(
parts
,
";"
)
}
// Key returns a key for e derived from all distinguishing attributes
// except those specified by exclude.
func
Key
(
e
Elem
,
exclude
...
string
)
string
{
return
attrKey
(
reflect
.
ValueOf
(
e
),
exclude
...
)
}
// linkEnclosing sets the enclosing element as well as the name
// for all sub-elements of child, recursively.
func
linkEnclosing
(
parent
,
child
Elem
)
{
child
.
setEnclosing
(
parent
)
v
:=
reflect
.
ValueOf
(
child
)
.
Elem
()
for
i
:=
iter
(
v
);
!
i
.
done
();
i
.
next
()
{
vf
:=
i
.
value
()
if
vf
.
Kind
()
==
reflect
.
Slice
{
for
j
:=
0
;
j
<
vf
.
Len
();
j
++
{
linkEnclosing
(
child
,
vf
.
Index
(
j
)
.
Interface
()
.
(
Elem
))
}
}
else
if
vf
.
Kind
()
==
reflect
.
Ptr
&&
!
vf
.
IsNil
()
&&
vf
.
Elem
()
.
Kind
()
==
reflect
.
Struct
{
linkEnclosing
(
child
,
vf
.
Interface
()
.
(
Elem
))
}
}
}
func
setNames
(
e
Elem
,
name
string
)
{
e
.
setName
(
name
)
v
:=
reflect
.
ValueOf
(
e
)
.
Elem
()
for
i
:=
iter
(
v
);
!
i
.
done
();
i
.
next
()
{
vf
:=
i
.
value
()
name
,
_
=
xmlName
(
i
.
field
())
if
vf
.
Kind
()
==
reflect
.
Slice
{
for
j
:=
0
;
j
<
vf
.
Len
();
j
++
{
setNames
(
vf
.
Index
(
j
)
.
Interface
()
.
(
Elem
),
name
)
}
}
else
if
vf
.
Kind
()
==
reflect
.
Ptr
&&
!
vf
.
IsNil
()
&&
vf
.
Elem
()
.
Kind
()
==
reflect
.
Struct
{
setNames
(
vf
.
Interface
()
.
(
Elem
),
name
)
}
}
}
// deepCopy copies elements of v recursively. All elements of v that may
// be modified by inheritance are explicitly copied.
func
deepCopy
(
v
reflect
.
Value
)
reflect
.
Value
{
switch
v
.
Kind
()
{
case
reflect
.
Ptr
:
if
v
.
IsNil
()
||
v
.
Elem
()
.
Kind
()
!=
reflect
.
Struct
{
return
v
}
nv
:=
reflect
.
New
(
v
.
Elem
()
.
Type
())
nv
.
Elem
()
.
Set
(
v
.
Elem
())
deepCopyRec
(
nv
.
Elem
(),
v
.
Elem
())
return
nv
case
reflect
.
Slice
:
nv
:=
reflect
.
MakeSlice
(
v
.
Type
(),
v
.
Len
(),
v
.
Len
())
for
i
:=
0
;
i
<
v
.
Len
();
i
++
{
deepCopyRec
(
nv
.
Index
(
i
),
v
.
Index
(
i
))
}
return
nv
}
panic
(
"deepCopy: must be called with pointer or slice"
)
}
// deepCopyRec is only called by deepCopy.
func
deepCopyRec
(
nv
,
v
reflect
.
Value
)
{
if
v
.
Kind
()
==
reflect
.
Struct
{
t
:=
v
.
Type
()
for
i
:=
0
;
i
<
v
.
NumField
();
i
++
{
if
name
,
attr
:=
xmlName
(
t
.
Field
(
i
));
name
!=
""
&&
!
attr
{
deepCopyRec
(
nv
.
Field
(
i
),
v
.
Field
(
i
))
}
}
}
else
{
nv
.
Set
(
deepCopy
(
v
))
}
}
// newNode is used to insert a missing node during inheritance.
func
(
cldr
*
CLDR
)
newNode
(
v
,
enc
reflect
.
Value
)
reflect
.
Value
{
n
:=
reflect
.
New
(
v
.
Type
())
for
i
:=
iter
(
v
);
!
i
.
done
();
i
.
next
()
{
if
name
,
attr
:=
xmlName
(
i
.
field
());
name
==
""
||
attr
{
n
.
Elem
()
.
FieldByIndex
(
i
.
index
)
.
Set
(
i
.
value
())
}
}
n
.
Interface
()
.
(
Elem
)
.
GetCommon
()
.
setEnclosing
(
enc
.
Addr
()
.
Interface
()
.
(
Elem
))
return
n
}
// v, parent must be pointers to struct
func
(
cldr
*
CLDR
)
inheritFields
(
v
,
parent
reflect
.
Value
)
(
res
reflect
.
Value
,
err
error
)
{
t
:=
v
.
Type
()
nv
:=
reflect
.
New
(
t
)
nv
.
Elem
()
.
Set
(
v
)
for
i
:=
iter
(
v
);
!
i
.
done
();
i
.
next
()
{
vf
:=
i
.
value
()
f
:=
i
.
field
()
name
,
attr
:=
xmlName
(
f
)
if
name
==
""
||
attr
{
continue
}
pf
:=
parent
.
FieldByIndex
(
i
.
index
)
if
blocking
[
name
]
{
if
vf
.
IsNil
()
{
vf
=
pf
}
nv
.
Elem
()
.
FieldByIndex
(
i
.
index
)
.
Set
(
deepCopy
(
vf
))
continue
}
switch
f
.
Type
.
Kind
()
{
case
reflect
.
Ptr
:
if
f
.
Type
.
Elem
()
.
Kind
()
==
reflect
.
Struct
{
if
!
vf
.
IsNil
()
{
if
vf
,
err
=
cldr
.
inheritStructPtr
(
vf
,
pf
);
err
!=
nil
{
return
reflect
.
Value
{},
err
}
vf
.
Interface
()
.
(
Elem
)
.
setEnclosing
(
nv
.
Interface
()
.
(
Elem
))
nv
.
Elem
()
.
FieldByIndex
(
i
.
index
)
.
Set
(
vf
)
}
else
if
!
pf
.
IsNil
()
{
n
:=
cldr
.
newNode
(
pf
.
Elem
(),
v
)
if
vf
,
err
=
cldr
.
inheritStructPtr
(
n
,
pf
);
err
!=
nil
{
return
reflect
.
Value
{},
err
}
vf
.
Interface
()
.
(
Elem
)
.
setEnclosing
(
nv
.
Interface
()
.
(
Elem
))
nv
.
Elem
()
.
FieldByIndex
(
i
.
index
)
.
Set
(
vf
)
}
}
case
reflect
.
Slice
:
vf
,
err
:=
cldr
.
inheritSlice
(
nv
.
Elem
(),
vf
,
pf
)
if
err
!=
nil
{
return
reflect
.
Zero
(
t
),
err
}
nv
.
Elem
()
.
FieldByIndex
(
i
.
index
)
.
Set
(
vf
)
}
}
return
nv
,
nil
}
func
root
(
e
Elem
)
*
LDML
{
for
;
e
.
enclosing
()
!=
nil
;
e
=
e
.
enclosing
()
{
}
return
e
.
(
*
LDML
)
}
// inheritStructPtr first merges possible aliases in with v and then inherits
// any underspecified elements from parent.
func
(
cldr
*
CLDR
)
inheritStructPtr
(
v
,
parent
reflect
.
Value
)
(
r
reflect
.
Value
,
err
error
)
{
if
!
v
.
IsNil
()
{
e
:=
v
.
Interface
()
.
(
Elem
)
.
GetCommon
()
alias
:=
e
.
Alias
if
alias
==
nil
&&
!
parent
.
IsNil
()
{
alias
=
parent
.
Interface
()
.
(
Elem
)
.
GetCommon
()
.
Alias
}
if
alias
!=
nil
{
a
,
err
:=
cldr
.
resolveAlias
(
v
.
Interface
()
.
(
Elem
),
alias
.
Source
,
alias
.
Path
)
if
a
!=
nil
{
if
v
,
err
=
cldr
.
inheritFields
(
v
.
Elem
(),
reflect
.
ValueOf
(
a
)
.
Elem
());
err
!=
nil
{
return
reflect
.
Value
{},
err
}
}
}
if
!
parent
.
IsNil
()
{
return
cldr
.
inheritFields
(
v
.
Elem
(),
parent
.
Elem
())
}
}
else
if
parent
.
IsNil
()
{
panic
(
"should not reach here"
)
}
return
v
,
nil
}
// Must be slice of struct pointers.
func
(
cldr
*
CLDR
)
inheritSlice
(
enc
,
v
,
parent
reflect
.
Value
)
(
res
reflect
.
Value
,
err
error
)
{
t
:=
v
.
Type
()
index
:=
make
(
map
[
string
]
reflect
.
Value
)
if
!
v
.
IsNil
()
{
for
i
:=
0
;
i
<
v
.
Len
();
i
++
{
vi
:=
v
.
Index
(
i
)
key
:=
attrKey
(
vi
)
index
[
key
]
=
vi
}
}
if
!
parent
.
IsNil
()
{
for
i
:=
0
;
i
<
parent
.
Len
();
i
++
{
vi
:=
parent
.
Index
(
i
)
key
:=
attrKey
(
vi
)
if
w
,
ok
:=
index
[
key
];
ok
{
index
[
key
],
err
=
cldr
.
inheritStructPtr
(
w
,
vi
)
}
else
{
n
:=
cldr
.
newNode
(
vi
.
Elem
(),
enc
)
index
[
key
],
err
=
cldr
.
inheritStructPtr
(
n
,
vi
)
}
index
[
key
]
.
Interface
()
.
(
Elem
)
.
setEnclosing
(
enc
.
Addr
()
.
Interface
()
.
(
Elem
))
if
err
!=
nil
{
return
v
,
err
}
}
}
keys
:=
make
([]
string
,
0
,
len
(
index
))
for
k
,
_
:=
range
index
{
keys
=
append
(
keys
,
k
)
}
sort
.
Strings
(
keys
)
sl
:=
reflect
.
MakeSlice
(
t
,
len
(
index
),
len
(
index
))
for
i
,
k
:=
range
keys
{
sl
.
Index
(
i
)
.
Set
(
index
[
k
])
}
return
sl
,
nil
}
func
parentLocale
(
loc
string
)
string
{
parts
:=
strings
.
Split
(
loc
,
"_"
)
if
len
(
parts
)
==
1
{
return
"root"
}
parts
=
parts
[
:
len
(
parts
)
-
1
]
key
:=
strings
.
Join
(
parts
,
"_"
)
return
key
}
func
(
cldr
*
CLDR
)
resolve
(
loc
string
)
(
res
*
LDML
,
err
error
)
{
if
r
:=
cldr
.
resolved
[
loc
];
r
!=
nil
{
return
r
,
nil
}
x
:=
cldr
.
RawLDML
(
loc
)
if
x
==
nil
{
return
nil
,
fmt
.
Errorf
(
"cldr: unknown locale %q"
,
loc
)
}
var
v
reflect
.
Value
if
loc
==
"root"
{
x
=
deepCopy
(
reflect
.
ValueOf
(
x
))
.
Interface
()
.
(
*
LDML
)
linkEnclosing
(
nil
,
x
)
err
=
cldr
.
aliasResolver
()
.
visit
(
x
)
}
else
{
key
:=
parentLocale
(
loc
)
var
parent
*
LDML
for
;
cldr
.
locale
[
key
]
==
nil
;
key
=
parentLocale
(
key
)
{
}
if
parent
,
err
=
cldr
.
resolve
(
key
);
err
!=
nil
{
return
nil
,
err
}
v
,
err
=
cldr
.
inheritFields
(
reflect
.
ValueOf
(
x
)
.
Elem
(),
reflect
.
ValueOf
(
parent
)
.
Elem
())
x
=
v
.
Interface
()
.
(
*
LDML
)
linkEnclosing
(
nil
,
x
)
}
if
err
!=
nil
{
return
nil
,
err
}
cldr
.
resolved
[
loc
]
=
x
return
x
,
err
}
// finalize finalizes the initialization of the raw LDML structs. It also
// removed unwanted fields, as specified by filter, so that they will not
// be unnecessarily evaluated.
func
(
cldr
*
CLDR
)
finalize
(
filter
[]
string
)
{
for
_
,
x
:=
range
cldr
.
locale
{
if
filter
!=
nil
{
v
:=
reflect
.
ValueOf
(
x
)
.
Elem
()
t
:=
v
.
Type
()
for
i
:=
0
;
i
<
v
.
NumField
();
i
++
{
f
:=
t
.
Field
(
i
)
name
,
_
:=
xmlName
(
f
)
if
name
!=
""
&&
name
!=
"identity"
&&
!
in
(
filter
,
name
)
{
v
.
Field
(
i
)
.
Set
(
reflect
.
Zero
(
f
.
Type
))
}
}
}
linkEnclosing
(
nil
,
x
)
// for resolving aliases and paths
setNames
(
x
,
"ldml"
)
}
}
vendor/gx/QmVgdgtv5rUcWWcHvHN1vdGkXi8UNztJ7JuT72YUJgG2ic/go-text/cldr/resolve_test.go
deleted
100644 → 0
View file @
a40ef343
package
cldr
import
(
"fmt"
"log"
"reflect"
"testing"
)
func
failOnError
(
err
error
)
{
if
err
!=
nil
{
log
.
Panic
(
err
)
}
}
func
data
()
*
CLDR
{
d
:=
Decoder
{}
data
,
err
:=
d
.
Decode
(
testLoader
{})
failOnError
(
err
)
return
data
}
type
h
struct
{
A
string
`xml:"ha,attr"`
E
string
`xml:"he"`
D
string
`xml:",chardata"`
X
string
}
type
fieldTest
struct
{
Common
To
string
`xml:"to,attr"`
Key
string
`xml:"key,attr"`
E
string
`xml:"e"`
D
string
`xml:",chardata"`
X
string
h
}
var
testStruct
=
fieldTest
{
Common
:
Common
{
name
:
"mapping"
,
// exclude "type" as distinguising attribute
Type
:
"foo"
,
Alt
:
"foo"
,
},
To
:
"nyc"
,
Key
:
"k"
,
E
:
"E"
,
D
:
"D"
,
h
:
h
{
A
:
"A"
,
E
:
"E"
,
D
:
"D"
,
},
}
func
TestIter
(
t
*
testing
.
T
)
{
tests
:=
map
[
string
]
string
{
"Type"
:
"foo"
,
"Alt"
:
"foo"
,
"To"
:
"nyc"
,
"A"
:
"A"
,
"Alias"
:
"<nil>"
,
}
k
:=
0
for
i
:=
iter
(
reflect
.
ValueOf
(
testStruct
));
!
i
.
done
();
i
.
next
()
{
v
:=
i
.
value
()
if
v
.
Kind
()
==
reflect
.
Ptr
&&
v
.
Elem
()
.
Kind
()
==
reflect
.
String
{
v
=
v
.
Elem
()
}
name
:=
i
.
field
()
.
Name
if
w
,
ok
:=
tests
[
name
];
ok
{
s
:=
fmt
.
Sprint
(
v
.
Interface
())
if
w
!=
s
{
t
.
Errorf
(
"value: found %q; want %q"
,
w
,
s
)
}
delete
(
tests
,
name
)
}
k
++
}
if
len
(
tests
)
!=
0
{
t
.
Errorf
(
"missing fields: %v"
,
tests
)
}
}
func
TestFindField
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
,
val
string
exist
bool
}{
{
"type"
,
"foo"
,
true
},
{
"alt"
,
"foo"
,
true
},
{
"to"
,
"nyc"
,
true
},
{
"he"
,
"E"
,
true
},
{
"q"
,
""
,
false
},
}
vf
:=
reflect
.
ValueOf
(
testStruct
)
for
i
,
tt
:=
range
tests
{
v
,
err
:=
findField
(
vf
,
tt
.
name
)
if
(
err
==
nil
)
!=
tt
.
exist
{
t
.
Errorf
(
"%d: field %q present is %v; want %v"
,
i
,
tt
.
name
,
err
==
nil
,
tt
.
exist
)
}
else
if
tt
.
exist
{
if
v
.
Kind
()
==
reflect
.
Ptr
{
if
v
.
IsNil
()
{
continue
}
v
=
v
.
Elem
()
}
if
v
.
String
()
!=
tt
.
val
{
t
.
Errorf
(
"%d: found value %q; want %q"
,
i
,
v
.
String
(),
tt
.
val
)
}
}
}
}
var
keyTests
=
[]
struct
{
exclude
[]
string
key
string
}{
{[]
string
{},
"alt=foo;key=k;to=nyc"
},
{[]
string
{
"type"
},
"alt=foo;key=k;to=nyc"
},
{[]
string
{
"choice"
},
"alt=foo;key=k;to=nyc"
},
{[]
string
{
"alt"
},
"key=k;to=nyc"
},
{[]
string
{
"a"
},
"alt=foo;key=k;to=nyc"
},
{[]
string
{
"to"
},
"alt=foo;key=k"
},
{[]
string
{
"alt"
,
"to"
},
"key=k"
},
{[]
string
{
"alt"
,
"to"
,
"key"
},
""
},
}
func
TestAttrKey
(
t
*
testing
.
T
)
{
v
:=
reflect
.
ValueOf
(
&
testStruct
)
for
i
,
tt
:=
range
keyTests
{
key
:=
attrKey
(
v
,
tt
.
exclude
...
)
if
key
!=
tt
.
key
{
t
.
Errorf
(
"%d: found %q, want %q"
,
i
,
key
,
tt
.
key
)
}
}
}
func
TestKey
(
t
*
testing
.
T
)
{
for
i
,
tt
:=
range
keyTests
{
key
:=
Key
(
&
testStruct
,
tt
.
exclude
...
)
if
key
!=
tt
.
key
{
t
.
Errorf
(
"%d: found %q, want %q"
,
i
,
key
,
tt
.
key
)
}
}
}
func
testEnclosing
(
t
*
testing
.
T
,
x
*
LDML
,
name
string
)
{
eq
:=
func
(
a
,
b
Elem
,
i
int
)
{
for
;
i
>
0
;
i
--
{
b
=
b
.
enclosing
()
}
if
a
!=
b
{
t
.
Errorf
(
"%s: found path %q, want %q"
,
name
,
getPath
(
a
),
getPath
(
b
))
}
}
eq
(
x
,
x
,
0
)
eq
(
x
,
x
.
Identity
,
1
)
eq
(
x
,
x
.
Dates
.
Calendars
,
2
)
eq
(
x
,
x
.
Dates
.
Calendars
.
Calendar
[
0
],
3
)
eq
(
x
,
x
.
Dates
.
Calendars
.
Calendar
[
1
],
3
)
//eq(x, x.Dates.Calendars.Calendar[0].Months, 4)
eq
(
x
,
x
.
Dates
.
Calendars
.
Calendar
[
1
]
.
Months
,
4
)
}
func
TestEnclosing
(
t
*
testing
.
T
)
{
testEnclosing
(
t
,
data
()
.
RawLDML
(
"de"
),
"enclosing-raw"
)
de
,
_
:=
data
()
.
LDML
(
"de"
)
testEnclosing
(
t
,
de
,
"enclosing"
)
}
func
TestDeepCopy
(
t
*
testing
.
T
)
{
eq
:=
func
(
have
,
want
string
)
{
if
have
!=
want
{
t
.
Errorf
(
"found %q; want %q"
,
have
,
want
)
}
}
x
,
_
:=
data
()
.
LDML
(
"de"
)
vc
:=
deepCopy
(
reflect
.
ValueOf
(
x
))
c
:=
vc
.
Interface
()
.
(
*
LDML
)
linkEnclosing
(
nil
,
c
)
if
x
==
c
{
t
.
Errorf
(
"did not copy"
)
}
eq
(
c
.
name
,
"ldml"
)
eq
(
c
.
Dates
.
name
,
"dates"
)
testEnclosing
(
t
,
c
,
"deepCopy"
)
}
type
getTest
struct
{
loc
string
path
string
field
string
// used in combination with length
data
string
altData
string
// used for buddhist calendar if value != ""
typ
string
length
int
missing
bool
}
const
(
budMon
=
"dates/calendars/calendar[@type='buddhist']/months/"
chnMon
=
"dates/calendars/calendar[@type='chinese']/months/"
greMon
=
"dates/calendars/calendar[@type='gregorian']/months/"
)
func
monthVal
(
path
,
context
,
width
string
,
month
int
)
string
{
const
format
=
"%s/monthContext[@type='%s']/monthWidth[@type='%s']/month[@type='%d']"
return
fmt
.
Sprintf
(
format
,
path
,
context
,
width
,
month
)
}
var
rootGetTests
=
[]
getTest
{
{
loc
:
"root"
,
path
:
"identity/language"
,
typ
:
"root"
},
{
loc
:
"root"
,
path
:
"characters/moreInformation"
,
data
:
"?"
},
{
loc
:
"root"
,
path
:
"characters"
,
field
:
"exemplarCharacters"
,
length
:
3
},
{
loc
:
"root"
,
path
:
greMon
,
field
:
"monthContext"
,
length
:
2
},
{
loc
:
"root"
,
path
:
greMon
+
"monthContext[@type='format']/monthWidth[@type='narrow']"
,
field
:
"month"
,
length
:
4
},
{
loc
:
"root"
,
path
:
greMon
+
"monthContext[@type='stand-alone']/monthWidth[@type='wide']"
,
field
:
"month"
,
length
:
4
},
// unescaping character data
{
loc
:
"root"
,
path
:
"characters/exemplarCharacters[@type='punctuation']"
,
data
:
`[\- ‐ – — … ' ‘ ‚ " “ „ \& #]`
},
// default resolution
{
loc
:
"root"
,
path
:
"dates/calendars/calendar"
,
typ
:
"gregorian"
},
// alias resolution
{
loc
:
"root"
,
path
:
budMon
,
field
:
"monthContext"
,
length
:
2
},
// crossing but non-circular alias resolution
{
loc
:
"root"
,
path
:
budMon
+
"monthContext[@type='format']/monthWidth[@type='narrow']"
,
field
:
"month"
,
length
:
4
},
{
loc
:
"root"
,
path
:
budMon
+
"monthContext[@type='stand-alone']/monthWidth[@type='wide']"
,
field
:
"month"
,
length
:
4
},
{
loc
:
"root"
,
path
:
monthVal
(
greMon
,
"format"
,
"wide"
,
1
),
data
:
"11"
},
{
loc
:
"root"
,
path
:
monthVal
(
greMon
,
"format"
,
"narrow"
,
2
),
data
:
"2"
},
{
loc
:
"root"
,
path
:
monthVal
(
greMon
,
"stand-alone"
,
"wide"
,
3
),
data
:
"33"
},
{
loc
:
"root"
,
path
:
monthVal
(
greMon
,
"stand-alone"
,
"narrow"
,
4
),
data
:
"4"
},
{
loc
:
"root"
,
path
:
monthVal
(
budMon
,
"format"
,
"wide"
,
1
),
data
:
"11"
},
{
loc
:
"root"
,
path
:
monthVal
(
budMon
,
"format"
,
"narrow"
,
2
),
data
:
"2"
},
{
loc
:
"root"
,
path
:
monthVal
(
budMon
,
"stand-alone"
,
"wide"
,
3
),
data
:
"33"
},
{
loc
:
"root"
,
path
:
monthVal
(
budMon
,
"stand-alone"
,
"narrow"
,
4
),
data
:
"4"
},
}
// 19
var
deGetTests
=
[]
getTest
{
{
loc
:
"de"
,
path
:
"identity/language"
,
typ
:
"de"
},
{
loc
:
"de"
,
path
:
"posix"
,
length
:
2
},
{
loc
:
"de"
,
path
:
"characters"
,
field
:
"exemplarCharacters"
,
length
:
4
},
{
loc
:
"de"
,
path
:
"characters/exemplarCharacters[@type='auxiliary']"
,
data
:
`[á à ă]`
},
// identity is a blocking element, so de should not inherit generation from root.
{
loc
:
"de"
,
path
:
"identity/generation"
,
missing
:
true
},
// default resolution
{
loc
:
"root"
,
path
:
"dates/calendars/calendar"
,
typ
:
"gregorian"
},
// absolute path alias resolution
{
loc
:
"gsw"
,
path
:
"posix"
,
field
:
"messages"
,
length
:
1
},
{
loc
:
"gsw"
,
path
:
"posix/messages/yesstr"
,
data
:
"yes:y"
},
}
// 27(greMon) - 52(budMon) - 77(chnMon)
func
calGetTests
(
s
string
)
[]
getTest
{
tests
:=
[]
getTest
{
{
loc
:
"de"
,
path
:
s
,
length
:
2
},
{
loc
:
"de"
,
path
:
s
+
"monthContext[@type='format']/monthWidth[@type='wide']"
,
field
:
"month"
,
length
:
5
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"format"
,
"wide"
,
1
),
data
:
"11"
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"format"
,
"wide"
,
2
),
data
:
"22"
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"format"
,
"wide"
,
3
),
data
:
"Maerz"
,
altData
:
"bbb"
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"format"
,
"wide"
,
4
),
data
:
"April"
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"format"
,
"wide"
,
5
),
data
:
"Mai"
},
{
loc
:
"de"
,
path
:
s
+
"monthContext[@type='format']/monthWidth[@type='narrow']"
,
field
:
"month"
,
length
:
5
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"format"
,
"narrow"
,
1
),
data
:
"1"
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"format"
,
"narrow"
,
2
),
data
:
"2"
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"format"
,
"narrow"
,
3
),
data
:
"M"
,
altData
:
"BBB"
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"format"
,
"narrow"
,
4
),
data
:
"A"
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"format"
,
"narrow"
,
5
),
data
:
"m"
},
{
loc
:
"de"
,
path
:
s
+
"monthContext[@type='stand-alone']/monthWidth[@type='wide']"
,
field
:
"month"
,
length
:
5
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"stand-alone"
,
"wide"
,
1
),
data
:
"11"
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"stand-alone"
,
"wide"
,
2
),
data
:
"22"
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"stand-alone"
,
"wide"
,
3
),
data
:
"Maerz"
,
altData
:
"bbb"
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"stand-alone"
,
"wide"
,
4
),
data
:
"april"
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"stand-alone"
,
"wide"
,
5
),
data
:
"mai"
},
{
loc
:
"de"
,
path
:
s
+
"monthContext[@type='stand-alone']/monthWidth[@type='narrow']"
,
field
:
"month"
,
length
:
5
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"stand-alone"
,
"narrow"
,
1
),
data
:
"1"
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"stand-alone"
,
"narrow"
,
2
),
data
:
"2"
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"stand-alone"
,
"narrow"
,
3
),
data
:
"m"
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"stand-alone"
,
"narrow"
,
4
),
data
:
"4"
},
{
loc
:
"de"
,
path
:
monthVal
(
s
,
"stand-alone"
,
"narrow"
,
5
),
data
:
"m"
},
}
if
s
==
budMon
{
for
i
,
t
:=
range
tests
{
if
t
.
altData
!=
""
{
tests
[
i
]
.
data
=
t
.
altData
}
}
}
return
tests
}
var
getTests
=
append
(
rootGetTests
,
append
(
deGetTests
,
append
(
calGetTests
(
greMon
),
append
(
calGetTests
(
budMon
),
calGetTests
(
chnMon
)
...
)
...
)
...
)
...
)
func
TestPath
(
t
*
testing
.
T
)
{
d
:=
data
()
for
i
,
tt
:=
range
getTests
{
x
,
_
:=
d
.
LDML
(
tt
.
loc
)
e
,
err
:=
walkXPath
(
x
,
tt
.
path
)
if
err
!=
nil
{
if
!
tt
.
missing
{
t
.
Errorf
(
"%d:error: %v %v"
,
i
,
err
,
tt
.
missing
)
}
continue
}
if
tt
.
missing
{
t
.
Errorf
(
"%d: missing is %v; want %v"
,
i
,
e
==
nil
,
tt
.
missing
)
continue
}
if
tt
.
data
!=
""
&&
e
.
GetCommon
()
.
Data
()
!=
tt
.
data
{
t
.
Errorf
(
"%d: data is %v; want %v"
,
i
,
e
.
GetCommon
()
.
Data
(),
tt
.
data
)
continue
}
if
tt
.
typ
!=
""
&&
e
.
GetCommon
()
.
Type
!=
tt
.
typ
{
t
.
Errorf
(
"%d: type is %v; want %v"
,
i
,
e
.
GetCommon
()
.
Type
,
tt
.
typ
)
continue
}
if
tt
.
field
!=
""
{
slice
,
_
:=
findField
(
reflect
.
ValueOf
(
e
),
tt
.
field
)
if
slice
.
Len
()
!=
tt
.
length
{
t
.
Errorf
(
"%d: length is %v; want %v"
,
i
,
slice
.
Len
(),
tt
.
length
)
continue
}
}
}
}
func
TestGet
(
t
*
testing
.
T
)
{
d
:=
data
()
for
i
,
tt
:=
range
getTests
{
x
,
_
:=
d
.
LDML
(
tt
.
loc
)
e
,
err
:=
Get
(
x
,
tt
.
path
)
if
err
!=
nil
{
if
!
tt
.
missing
{
t
.
Errorf
(
"%d:error: %v %v"
,
i
,
err
,
tt
.
missing
)
}
continue
}
if
tt
.
missing
{
t
.
Errorf
(
"%d: missing is %v; want %v"
,
i
,
e
==
nil
,
tt
.
missing
)
continue
}
if
tt
.
data
!=
""
&&
e
.
GetCommon
()
.
Data
()
!=
tt
.
data
{
t
.
Errorf
(
"%d: data is %v; want %v"
,
i
,
e
.
GetCommon
()
.
Data
(),
tt
.
data
)
continue
}
if
tt
.
typ
!=
""
&&
e
.
GetCommon
()
.
Type
!=
tt
.
typ
{
t
.
Errorf
(
"%d: type is %v; want %v"
,
i
,
e
.
GetCommon
()
.
Type
,
tt
.
typ
)
continue
}
if
tt
.
field
!=
""
{
slice
,
_
:=
findField
(
reflect
.
ValueOf
(
e
),
tt
.
field
)
if
slice
.
Len
()
!=
tt
.
length
{
t
.
Errorf
(
"%d: length is %v; want %v"
,
i
,
slice
.
Len
(),
tt
.
length
)
continue
}
}
}
}
vendor/gx/QmVgdgtv5rUcWWcHvHN1vdGkXi8UNztJ7JuT72YUJgG2ic/go-text/cldr/slice.go
deleted
100644 → 0
View file @
a40ef343
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
cldr
import
(
"fmt"
"reflect"
"sort"
)
// Slice provides utilities for modifying slices of elements.
// It can be wrapped around any slice of which the element type implements
// interface Elem.
type
Slice
struct
{
ptr
reflect
.
Value
typ
reflect
.
Type
}
// Value returns the reflect.Value of the underlying slice.
func
(
s
*
Slice
)
Value
()
reflect
.
Value
{
return
s
.
ptr
.
Elem
()
}
// MakeSlice wraps a pointer to a slice of Elems.
// It replaces the array pointed to by the slice so that subsequent modifications
// do not alter the data in a CLDR type.
// It panics if an incorrect type is passed.
func
MakeSlice
(
slicePtr
interface
{})
Slice
{
ptr
:=
reflect
.
ValueOf
(
slicePtr
)
if
ptr
.
Kind
()
!=
reflect
.
Ptr
{
panic
(
fmt
.
Sprintf
(
"MakeSlice: argument must be pointer to slice, found %v"
,
ptr
.
Type
()))
}
sl
:=
ptr
.
Elem
()
if
sl
.
Kind
()
!=
reflect
.
Slice
{
panic
(
fmt
.
Sprintf
(
"MakeSlice: argument must point to a slice, found %v"
,
sl
.
Type
()))
}
intf
:=
reflect
.
TypeOf
((
*
Elem
)(
nil
))
.
Elem
()
if
!
sl
.
Type
()
.
Elem
()
.
Implements
(
intf
)
{
panic
(
fmt
.
Sprintf
(
"MakeSlice: element type of slice (%v) does not implement Elem"
,
sl
.
Type
()
.
Elem
()))
}
nsl
:=
reflect
.
MakeSlice
(
sl
.
Type
(),
sl
.
Len
(),
sl
.
Len
())
reflect
.
Copy
(
nsl
,
sl
)
sl
.
Set
(
nsl
)
return
Slice
{
ptr
:
ptr
,
typ
:
sl
.
Type
()
.
Elem
()
.
Elem
(),
}
}
func
(
s
Slice
)
indexForAttr
(
a
string
)
[]
int
{
for
i
:=
iter
(
reflect
.
Zero
(
s
.
typ
));
!
i
.
done
();
i
.
next
()
{
if
n
,
_
:=
xmlName
(
i
.
field
());
n
==
a
{
return
i
.
index
}
}
panic
(
fmt
.
Sprintf
(
"MakeSlice: no attribute %q for type %v"
,
a
,
s
.
typ
))
}
// Filter filters s to only include elements for which fn returns true.
func
(
s
Slice
)
Filter
(
fn
func
(
e
Elem
)
bool
)
{
k
:=
0
sl
:=
s
.
Value
()
for
i
:=
0
;
i
<
sl
.
Len
();
i
++
{
vi
:=
sl
.
Index
(
i
)
if
fn
(
vi
.
Interface
()
.
(
Elem
))
{
sl
.
Index
(
k
)
.
Set
(
vi
)
k
++
}
}
sl
.
Set
(
sl
.
Slice
(
0
,
k
))
}
// Group finds elements in s for which fn returns the same value and groups
// them in a new Slice.
func
(
s
Slice
)
Group
(
fn
func
(
e
Elem
)
string
)
[]
Slice
{
m
:=
make
(
map
[
string
][]
reflect
.
Value
)
sl
:=
s
.
Value
()
for
i
:=
0
;
i
<
sl
.
Len
();
i
++
{
vi
:=
sl
.
Index
(
i
)
key
:=
fn
(
vi
.
Interface
()
.
(
Elem
))
m
[
key
]
=
append
(
m
[
key
],
vi
)
}
keys
:=
[]
string
{}
for
k
,
_
:=
range
m
{
keys
=
append
(
keys
,
k
)
}
sort
.
Strings
(
keys
)
res
:=
[]
Slice
{}
for
_
,
k
:=
range
keys
{
nsl
:=
reflect
.
New
(
sl
.
Type
())
nsl
.
Elem
()
.
Set
(
reflect
.
Append
(
nsl
.
Elem
(),
m
[
k
]
...
))
res
=
append
(
res
,
MakeSlice
(
nsl
.
Interface
()))
}
return
res
}
// SelectAnyOf filters s to contain only elements for which attr matches
// any of the values.
func
(
s
Slice
)
SelectAnyOf
(
attr
string
,
values
...
string
)
{
index
:=
s
.
indexForAttr
(
attr
)
s
.
Filter
(
func
(
e
Elem
)
bool
{
vf
:=
reflect
.
ValueOf
(
e
)
.
Elem
()
.
FieldByIndex
(
index
)
return
in
(
values
,
vf
.
String
())
})
}
// SelectOnePerGroup filters s to include at most one element e per group of
// elements matching Key(attr), where e has an attribute a that matches any
// the values in v.
// If more than one element in a group matches a value in v preference
// is given to the element that matches the first value in v.
func
(
s
Slice
)
SelectOnePerGroup
(
a
string
,
v
[]
string
)
{
index
:=
s
.
indexForAttr
(
a
)
grouped
:=
s
.
Group
(
func
(
e
Elem
)
string
{
return
Key
(
e
,
a
)
})
sl
:=
s
.
Value
()
sl
.
Set
(
sl
.
Slice
(
0
,
0
))
for
_
,
g
:=
range
grouped
{
e
:=
reflect
.
Value
{}
found
:=
len
(
v
)
gsl
:=
g
.
Value
()
for
i
:=
0
;
i
<
gsl
.
Len
();
i
++
{
vi
:=
gsl
.
Index
(
i
)
.
Elem
()
.
FieldByIndex
(
index
)
j
:=
0
for
;
j
<
len
(
v
)
&&
v
[
j
]
!=
vi
.
String
();
j
++
{
}
if
j
<
found
{
found
=
j
e
=
gsl
.
Index
(
i
)
}
}
if
found
<
len
(
v
)
{
sl
.
Set
(
reflect
.
Append
(
sl
,
e
))
}
}
}
// SelectDraft drops all elements from the list with a draft level smaller than d
// and selects the highest draft level of the remaining.
// This method assumes that the input CLDR is canonicalized.
func
(
s
Slice
)
SelectDraft
(
d
Draft
)
{
s
.
SelectOnePerGroup
(
"draft"
,
drafts
[
len
(
drafts
)
-
2
-
int
(
d
)
:
])
}
Prev
1
…
15
16
17
18
19
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment