Skip to content

Commit

Permalink
encoding: publicly expose identifier.{MIB,Interface}
Browse files Browse the repository at this point in the history
  • Loading branch information
rykov committed Dec 2, 2021
1 parent 18b340f commit b994d20
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions encoding/ianaindex/ianaindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ func (x *Index) Name(e encoding.Encoding) (string, error) {
return x.names(v), nil
}

// FindMIB searches encoding by MIBenum identifier
func (x *Index) FindMIB(mib identifier.MIB) (encoding.Encoding, error) {
v := findMIB(x.toMIB, mib)
if v == -1 {
return nil, errUnsupported
}
return x.enc[v], nil
}

// TODO: the coverage of this index is rather spotty. Allowing users to set
// encodings would allow:
// - users to increase coverage
Expand Down
15 changes: 15 additions & 0 deletions encoding/ianaindex/ianaindex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ func TestEncoding(t *testing.T) {
if got, err := tc.index.Name(enc); got != tc.canonical {
t.Errorf("%d: Name(Encoding(%q)) = %q; want %q (%v)", i, tc.name, got, tc.canonical, err)
}

id, ok := enc.(identifier.Interface)
if !ok {
t.Errorf("%d: encoding %q has no ID", i, tc.name)
}
mib, _ := id.ID()
if mib == 0 {
t.Errorf("%d: encoding %q returned 0 MIB enum", i, tc.name)
}
mibEnc, err := tc.index.FindMIB(mib)
if err != nil {
t.Errorf("%d: FindMIB error %q", i, err)
} else if mibEnc != enc {
t.Errorf("%d: FindMIB did not match encoding", i)
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions encoding/identifier/identifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright 2015 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 identifier

import (
"golang.org/x/text/encoding/internal/identifier"
)

type Interface = identifier.Interface
type MIB = identifier.MIB

0 comments on commit b994d20

Please sign in to comment.