github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/text/cldr/base.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package cldr provides a parser for LDML and related XML formats. 6 // This package is inteded to be used by the table generation tools 7 // for the various internationalization-related packages. 8 // As the XML types are generated from the CLDR DTD, and as the CLDR standard 9 // is periodically amended, this package may change considerably over time. 10 // This mostly means that data may appear and disappear between versions. 11 // That is, old code should keep compiling for newer versions, but data 12 // may have moved or changed. 13 // CLDR version 22 is the first version supported by this package. 14 // Older versions may not work. 15 package cldr 16 17 import ( 18 "encoding/xml" 19 "regexp" 20 "strconv" 21 ) 22 23 // Elem is implemented by every XML element. 24 type Elem interface { 25 setEnclosing(Elem) 26 setName(string) 27 enclosing() Elem 28 29 GetCommon() *Common 30 } 31 32 type hidden struct { 33 CharData string `xml:",chardata"` 34 Alias *struct { 35 Common 36 Source string `xml:"source,attr"` 37 Path string `xml:"path,attr"` 38 } `xml:"alias"` 39 Def *struct { 40 Common 41 Choice string `xml:"choice,attr,omitempty"` 42 Type string `xml:"type,attr,omitempty"` 43 } `xml:"default"` 44 } 45 46 // Common holds several of the most common attributes and sub elements 47 // of an XML element. 48 type Common struct { 49 XMLName xml.Name 50 name string 51 enclElem Elem 52 Type string `xml:"type,attr,omitempty"` 53 Reference string `xml:"reference,attr,omitempty"` 54 Alt string `xml:"alt,attr,omitempty"` 55 ValidSubLocales string `xml:"validSubLocales,attr,omitempty"` 56 Draft string `xml:"draft,attr,omitempty"` 57 hidden 58 } 59 60 // Default returns the default type to select from the enclosed list 61 // or "" if no default value is specified. 62 func (e *Common) Default() string { 63 if e.Def == nil { 64 return "" 65 } 66 if e.Def.Choice != "" { 67 return e.Def.Choice 68 } else if e.Def.Type != "" { 69 // Type is still used by the default element in collation. 70 return e.Def.Type 71 } 72 return "" 73 } 74 75 // GetCommon returns e. It is provided such that Common implements Elem. 76 func (e *Common) GetCommon() *Common { 77 return e 78 } 79 80 // Data returns the character data accumulated for this element. 81 func (e *Common) Data() string { 82 e.CharData = charRe.ReplaceAllStringFunc(e.CharData, replaceUnicode) 83 return e.CharData 84 } 85 86 func (e *Common) setName(s string) { 87 e.name = s 88 } 89 90 func (e *Common) enclosing() Elem { 91 return e.enclElem 92 } 93 94 func (e *Common) setEnclosing(en Elem) { 95 e.enclElem = en 96 } 97 98 // Escape characters that can be escaped without further escaping the string. 99 var charRe = regexp.MustCompile(`&#x[0-9a-fA-F]*;|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}|\\x[0-9a-fA-F]{2}|\\[0-7]{3}|\\[abtnvfr]`) 100 101 // replaceUnicode converts hexadecimal Unicode codepoint notations to a one-rune string. 102 // It assumes the input string is correctly formatted. 103 func replaceUnicode(s string) string { 104 if s[1] == '#' { 105 r, _ := strconv.ParseInt(s[3:len(s)-1], 16, 32) 106 return string(r) 107 } 108 r, _, _, _ := strconv.UnquoteChar(s, 0) 109 return string(r) 110 }