Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose MIBenum via identifier package #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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