gitlab.com/evatix-go/core@v1.3.55/coredata/corerange/RangeInt8.go (about)

     1  package corerange
     2  
     3  import (
     4  	"math"
     5  
     6  	"gitlab.com/evatix-go/core/constants"
     7  )
     8  
     9  type RangeInt8 struct {
    10  	*BaseRange
    11  	Start, End int8
    12  }
    13  
    14  func NewRangeInt8MinMax(
    15  	rawString, separator string,
    16  	min, max int8,
    17  ) *RangeInt8 {
    18  	minMax := MinMaxInt{
    19  		Min: int(min),
    20  		Max: int(max),
    21  	}
    22  
    23  	return NewRangeInt(
    24  		rawString,
    25  		separator,
    26  		&minMax).
    27  		CreateRangeInt8()
    28  }
    29  
    30  func NewRangeInt8(
    31  	rawString, separator string,
    32  	minMaxInt8 *MinMaxInt8,
    33  ) *RangeInt8 {
    34  	if minMaxInt8 == nil {
    35  		minMax := &MinMaxInt{
    36  			Min: math.MinInt8,
    37  			Max: math.MaxInt8,
    38  		}
    39  
    40  		rangeInt := NewRangeInt(
    41  			rawString,
    42  			separator,
    43  			minMax)
    44  
    45  		return rangeInt.CreateRangeInt8()
    46  	}
    47  
    48  	minMax := minMaxInt8.CreateMinMaxInt()
    49  
    50  	rangeInt := NewRangeInt(
    51  		rawString,
    52  		separator,
    53  		minMax)
    54  
    55  	return rangeInt.CreateRangeInt8()
    56  }
    57  
    58  func (r *RangeInt8) Difference() int8 {
    59  	return r.End - r.Start
    60  }
    61  
    62  func (r *RangeInt8) DifferenceAbsolute() int8 {
    63  	diff := r.Difference()
    64  
    65  	if diff < 0 {
    66  		return diff
    67  	}
    68  
    69  	return diff
    70  }
    71  
    72  // RangeLength (5 - 3 = 2) + 1
    73  func (r *RangeInt8) RangeLength() int8 {
    74  	return r.DifferenceAbsolute() + 1
    75  }
    76  
    77  // RangesInt8 returns empty ints if IsInvalid
    78  // return range int values
    79  func (r *RangeInt8) RangesInt8() *[]int8 {
    80  	return r.Ranges()
    81  }
    82  
    83  // Ranges returns empty ints if IsInvalid
    84  // return range int values
    85  func (r *RangeInt8) Ranges() *[]int8 {
    86  	if r.IsInvalid() {
    87  		return &[]int8{}
    88  	}
    89  
    90  	length := r.RangeLength()
    91  	start := r.Start
    92  	slice := make([]int8, constants.Zero, length)
    93  
    94  	var i int8
    95  
    96  	for i = 0; i < length; i++ {
    97  		slice[i] = start + i
    98  	}
    99  
   100  	return &slice
   101  }
   102  
   103  func (r *RangeInt8) String() string {
   104  	return r.BaseRange.String(r.Start, r.End)
   105  }
   106  
   107  // IsWithinRange r.Start >= value && value <= r.End
   108  func (r *RangeInt8) IsWithinRange(value int8) bool {
   109  	return r.Start >= value && value <= r.End
   110  }
   111  
   112  // IsValidPlusWithinRange r.IsValid && r.IsWithinRange(value)
   113  func (r *RangeInt8) IsValidPlusWithinRange(value int8) bool {
   114  	return r.IsValid && r.IsWithinRange(value)
   115  }
   116  
   117  // IsInvalidValue !r.IsValid || !r.IsWithinRange(value)
   118  func (r *RangeInt8) IsInvalidValue(value int8) bool {
   119  	return !r.IsValid || !r.IsWithinRange(value)
   120  }