kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/util/encoding/text/text.go (about)

     1  /*
     2   * Copyright 2015 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  // Package text contains utilities dealing with the encoding of source text.
    18  package text // import "kythe.io/kythe/go/util/encoding/text"
    19  
    20  import (
    21  	"errors"
    22  
    23  	"golang.org/x/text/encoding"
    24  	"golang.org/x/text/encoding/htmlindex"
    25  	"golang.org/x/text/transform"
    26  )
    27  
    28  // ErrUnsupportedEncoding is returned if an encoding is unsupported.
    29  var ErrUnsupportedEncoding = errors.New("unsupported encoding")
    30  
    31  // ToUTF8 converts the given encoded text to a UTF-8 string.
    32  func ToUTF8(encodingName string, b []byte) (string, error) {
    33  	if encodingName == "" {
    34  		return transformBytes(encoding.Replacement.NewEncoder(), b)
    35  	}
    36  
    37  	e, err := htmlindex.Get(encodingName)
    38  	if err != nil {
    39  		return "", ErrUnsupportedEncoding
    40  	}
    41  	var t transform.Transformer
    42  	if e == encoding.Replacement {
    43  		t = encoding.Replacement.NewEncoder()
    44  	} else {
    45  		t = e.NewDecoder()
    46  	}
    47  	return transformBytes(t, b)
    48  }
    49  
    50  func transformBytes(e transform.Transformer, text []byte) (string, error) {
    51  	res, _, err := transform.Bytes(e, text)
    52  	return string(res), err
    53  }