github.com/core-coin/go-core/v2@v2.1.9/mobile/primitives.go (about)

     1  // Copyright 2016 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-core library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Contains various wrappers for primitive types.
    18  
    19  package gocore
    20  
    21  import (
    22  	"errors"
    23  	"fmt"
    24  
    25  	"github.com/core-coin/go-core/v2/common"
    26  )
    27  
    28  // Strings represents s slice of strs.
    29  type Strings struct{ strs []string }
    30  
    31  // Size returns the number of strs in the slice.
    32  func (s *Strings) Size() int {
    33  	return len(s.strs)
    34  }
    35  
    36  // Get returns the string at the given index from the slice.
    37  func (s *Strings) Get(index int) (str string, _ error) {
    38  	if index < 0 || index >= len(s.strs) {
    39  		return "", errors.New("index out of bounds")
    40  	}
    41  	return s.strs[index], nil
    42  }
    43  
    44  // Set sets the string at the given index in the slice.
    45  func (s *Strings) Set(index int, str string) error {
    46  	if index < 0 || index >= len(s.strs) {
    47  		return errors.New("index out of bounds")
    48  	}
    49  	s.strs[index] = str
    50  	return nil
    51  }
    52  
    53  // String implements the Stringer interface.
    54  func (s *Strings) String() string {
    55  	return fmt.Sprintf("%v", s.strs)
    56  }
    57  
    58  // Bools represents a slice of bool.
    59  type Bools struct{ bools []bool }
    60  
    61  // Size returns the number of bool in the slice.
    62  func (bs *Bools) Size() int {
    63  	return len(bs.bools)
    64  }
    65  
    66  // Get returns the bool at the given index from the slice.
    67  func (bs *Bools) Get(index int) (b bool, _ error) {
    68  	if index < 0 || index >= len(bs.bools) {
    69  		return false, errors.New("index out of bounds")
    70  	}
    71  	return bs.bools[index], nil
    72  }
    73  
    74  // Set sets the bool at the given index in the slice.
    75  func (bs *Bools) Set(index int, b bool) error {
    76  	if index < 0 || index >= len(bs.bools) {
    77  		return errors.New("index out of bounds")
    78  	}
    79  	bs.bools[index] = b
    80  	return nil
    81  }
    82  
    83  // String implements the Stringer interface.
    84  func (bs *Bools) String() string {
    85  	return fmt.Sprintf("%v", bs.bools)
    86  }
    87  
    88  // Binaries represents a slice of byte slice
    89  type Binaries struct{ binaries [][]byte }
    90  
    91  // Size returns the number of byte slice in the slice.
    92  func (bs *Binaries) Size() int {
    93  	return len(bs.binaries)
    94  }
    95  
    96  // Get returns the byte slice at the given index from the slice.
    97  func (bs *Binaries) Get(index int) (binary []byte, _ error) {
    98  	if index < 0 || index >= len(bs.binaries) {
    99  		return nil, errors.New("index out of bounds")
   100  	}
   101  	return common.CopyBytes(bs.binaries[index]), nil
   102  }
   103  
   104  // Set sets the byte slice at the given index in the slice.
   105  func (bs *Binaries) Set(index int, binary []byte) error {
   106  	if index < 0 || index >= len(bs.binaries) {
   107  		return errors.New("index out of bounds")
   108  	}
   109  	bs.binaries[index] = common.CopyBytes(binary)
   110  	return nil
   111  }
   112  
   113  // String implements the Stringer interface.
   114  func (bs *Binaries) String() string {
   115  	return fmt.Sprintf("%v", bs.binaries)
   116  }