github.com/go-spring/spring-base@v1.1.3/cast/string.go (about)

     1  /*
     2   * Copyright 2012-2019 the original author or authors.
     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   *      https://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 cast
    18  
    19  import (
    20  	"fmt"
    21  	"html/template"
    22  	"strconv"
    23  	"unsafe"
    24  )
    25  
    26  func StringPtr(s string) *string { return &s }
    27  
    28  // ToString casts an interface{} to a string.
    29  // When type is clear, it is recommended to use standard library functions.
    30  func ToString(i interface{}) string {
    31  	v, _ := ToStringE(i)
    32  	return v
    33  }
    34  
    35  // ToStringE casts an interface{} to a string.
    36  // When type is clear, it is recommended to use standard library functions.
    37  func ToStringE(i interface{}) (string, error) {
    38  	switch s := i.(type) {
    39  	case nil:
    40  		return "", nil
    41  	case int:
    42  		return strconv.Itoa(s), nil
    43  	case int8:
    44  		return strconv.FormatInt(int64(s), 10), nil
    45  	case int16:
    46  		return strconv.FormatInt(int64(s), 10), nil
    47  	case int32:
    48  		return strconv.Itoa(int(s)), nil
    49  	case int64:
    50  		return strconv.FormatInt(s, 10), nil
    51  	case *int:
    52  		return strconv.Itoa(*s), nil
    53  	case *int8:
    54  		return strconv.FormatInt(int64(*s), 10), nil
    55  	case *int16:
    56  		return strconv.FormatInt(int64(*s), 10), nil
    57  	case *int32:
    58  		return strconv.Itoa(int(*s)), nil
    59  	case *int64:
    60  		return strconv.FormatInt(*s, 10), nil
    61  	case uint:
    62  		return strconv.FormatUint(uint64(s), 10), nil
    63  	case uint8:
    64  		return strconv.FormatUint(uint64(s), 10), nil
    65  	case uint16:
    66  		return strconv.FormatUint(uint64(s), 10), nil
    67  	case uint32:
    68  		return strconv.FormatUint(uint64(s), 10), nil
    69  	case uint64:
    70  		return strconv.FormatUint(s, 10), nil
    71  	case *uint:
    72  		return strconv.FormatUint(uint64(*s), 10), nil
    73  	case *uint8:
    74  		return strconv.FormatUint(uint64(*s), 10), nil
    75  	case *uint16:
    76  		return strconv.FormatUint(uint64(*s), 10), nil
    77  	case *uint32:
    78  		return strconv.FormatUint(uint64(*s), 10), nil
    79  	case *uint64:
    80  		return strconv.FormatUint(*s, 10), nil
    81  	case float32:
    82  		return strconv.FormatFloat(float64(s), 'f', -1, 32), nil
    83  	case float64:
    84  		return strconv.FormatFloat(s, 'f', -1, 64), nil
    85  	case *float32:
    86  		return strconv.FormatFloat(float64(*s), 'f', -1, 32), nil
    87  	case *float64:
    88  		return strconv.FormatFloat(*s, 'f', -1, 64), nil
    89  	case string:
    90  		return s, nil
    91  	case *string:
    92  		return *s, nil
    93  	case bool:
    94  		return strconv.FormatBool(s), nil
    95  	case *bool:
    96  		return strconv.FormatBool(*s), nil
    97  	case []byte:
    98  		return string(s), nil
    99  	case template.HTML:
   100  		return string(s), nil
   101  	case template.URL:
   102  		return string(s), nil
   103  	case template.JS:
   104  		return string(s), nil
   105  	case template.CSS:
   106  		return string(s), nil
   107  	case template.HTMLAttr:
   108  		return string(s), nil
   109  	case fmt.Stringer:
   110  		return s.String(), nil
   111  	case error:
   112  		return s.Error(), nil
   113  	default:
   114  		return "", fmt.Errorf("unable to cast type (%T) to string", i)
   115  	}
   116  }
   117  
   118  // StringToBytes converts string to byte slice without memory allocation.
   119  func StringToBytes(s string) []byte {
   120  	return *(*[]byte)(unsafe.Pointer(
   121  		&struct {
   122  			string
   123  			Cap int
   124  		}{s, len(s)},
   125  	))
   126  }
   127  
   128  // BytesToString converts byte slice to string without memory allocation.
   129  func BytesToString(b []byte) string {
   130  	return *(*string)(unsafe.Pointer(&b))
   131  }