Skip to content

Commit b994d20

Browse files
committed
encoding: publicly expose identifier.{MIB,Interface}
1 parent 18b340f commit b994d20

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

encoding/ianaindex/ianaindex.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ func (x *Index) Name(e encoding.Encoding) (string, error) {
104104
return x.names(v), nil
105105
}
106106

107+
// FindMIB searches encoding by MIBenum identifier
108+
func (x *Index) FindMIB(mib identifier.MIB) (encoding.Encoding, error) {
109+
v := findMIB(x.toMIB, mib)
110+
if v == -1 {
111+
return nil, errUnsupported
112+
}
113+
return x.enc[v], nil
114+
}
115+
107116
// TODO: the coverage of this index is rather spotty. Allowing users to set
108117
// encodings would allow:
109118
// - users to increase coverage

encoding/ianaindex/ianaindex_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,21 @@ func TestEncoding(t *testing.T) {
105105
if got, err := tc.index.Name(enc); got != tc.canonical {
106106
t.Errorf("%d: Name(Encoding(%q)) = %q; want %q (%v)", i, tc.name, got, tc.canonical, err)
107107
}
108+
109+
id, ok := enc.(identifier.Interface)
110+
if !ok {
111+
t.Errorf("%d: encoding %q has no ID", i, tc.name)
112+
}
113+
mib, _ := id.ID()
114+
if mib == 0 {
115+
t.Errorf("%d: encoding %q returned 0 MIB enum", i, tc.name)
116+
}
117+
mibEnc, err := tc.index.FindMIB(mib)
118+
if err != nil {
119+
t.Errorf("%d: FindMIB error %q", i, err)
120+
} else if mibEnc != enc {
121+
t.Errorf("%d: FindMIB did not match encoding", i)
122+
}
108123
}
109124
}
110125

encoding/identifier/identifier.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2015 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 identifier
6+
7+
import (
8+
"golang.org/x/text/encoding/internal/identifier"
9+
)
10+
11+
type Interface = identifier.Interface
12+
type MIB = identifier.MIB

0 commit comments

Comments
 (0)