github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xstrings/split.go (about)

     1  package xstrings
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  )
     7  
     8  func SplitInt(str string, sep string) ([]int, error) {
     9  	items := strings.Split(str, sep)
    10  	result := make([]int, 0, len(items))
    11  	for _, item := range items {
    12  		i, err := strconv.Atoi(item)
    13  		if err != nil {
    14  			return result, err
    15  		}
    16  		result = append(result, i)
    17  	}
    18  	return result, nil
    19  }
    20  
    21  func SplitInt8(str string, sep string) ([]int8, error) {
    22  	items := strings.Split(str, sep)
    23  	result := make([]int8, 0, len(items))
    24  	for _, item := range items {
    25  		i, err := strconv.Atoi(item)
    26  		if err != nil {
    27  			return result, err
    28  		}
    29  		result = append(result, int8(i))
    30  	}
    31  	return result, nil
    32  }
    33  
    34  func SplitInt16(str string, sep string) ([]int16, error) {
    35  	items := strings.Split(str, sep)
    36  	result := make([]int16, 0, len(items))
    37  	for _, item := range items {
    38  		i, err := strconv.Atoi(item)
    39  		if err != nil {
    40  			return result, err
    41  		}
    42  		result = append(result, int16(i))
    43  	}
    44  	return result, nil
    45  }
    46  
    47  func SplitInt32(str string, sep string) ([]int32, error) {
    48  	items := strings.Split(str, sep)
    49  	result := make([]int32, 0, len(items))
    50  	for _, item := range items {
    51  		i, err := strconv.Atoi(item)
    52  		if err != nil {
    53  			return result, err
    54  		}
    55  		result = append(result, int32(i))
    56  	}
    57  	return result, nil
    58  }
    59  
    60  func SplitInt64(str string, sep string) ([]int64, error) {
    61  	items := strings.Split(str, sep)
    62  	result := make([]int64, 0, len(items))
    63  	for _, item := range items {
    64  		i, err := strconv.Atoi(item)
    65  		if err != nil {
    66  			return result, err
    67  		}
    68  		result = append(result, int64(i))
    69  	}
    70  	return result, nil
    71  }
    72  
    73  func SplitUint(str string, sep string) ([]uint, error) {
    74  	items := strings.Split(str, sep)
    75  	result := make([]uint, 0, len(items))
    76  	for _, item := range items {
    77  		i, err := strconv.ParseUint(item, 10, 64)
    78  		if err != nil {
    79  			return result, err
    80  		}
    81  		result = append(result, uint(i))
    82  	}
    83  	return result, nil
    84  }
    85  
    86  func SplitUint8(str string, sep string) ([]uint8, error) {
    87  	items := strings.Split(str, sep)
    88  	result := make([]uint8, 0, len(items))
    89  	for _, item := range items {
    90  		i, err := strconv.ParseUint(item, 10, 64)
    91  		if err != nil {
    92  			return result, err
    93  		}
    94  		result = append(result, uint8(i))
    95  	}
    96  	return result, nil
    97  }
    98  
    99  func SplitUint16(str string, sep string) ([]uint16, error) {
   100  	items := strings.Split(str, sep)
   101  	result := make([]uint16, 0, len(items))
   102  	for _, item := range items {
   103  		i, err := strconv.ParseUint(item, 10, 64)
   104  		if err != nil {
   105  			return result, err
   106  		}
   107  		result = append(result, uint16(i))
   108  	}
   109  	return result, nil
   110  }
   111  
   112  func SplitUint32(str string, sep string) ([]uint32, error) {
   113  	items := strings.Split(str, sep)
   114  	result := make([]uint32, 0, len(items))
   115  	for _, item := range items {
   116  		i, err := strconv.ParseUint(item, 10, 64)
   117  		if err != nil {
   118  			return result, err
   119  		}
   120  		result = append(result, uint32(i))
   121  	}
   122  	return result, nil
   123  }
   124  
   125  func SplitUint64(str string, sep string) ([]uint64, error) {
   126  	items := strings.Split(str, sep)
   127  	result := make([]uint64, 0, len(items))
   128  	for _, item := range items {
   129  		i, err := strconv.ParseUint(item, 10, 64)
   130  		if err != nil {
   131  			return result, err
   132  		}
   133  		result = append(result, i)
   134  	}
   135  	return result, nil
   136  }