...

Source file src/gitlab.hexacode.org/go-libs/hctypes/Buffer.go

Documentation: gitlab.hexacode.org/go-libs/hctypes

     1  /*
     2   * Hexacode Types
     3   * Package: gitlab.hexacode.org/go-libs/hctypes
     4   * Maintainer: Azzis Arswendo <azzis@hexacode.org>
     5   *
     6   * Copyright (C) 2023 Hexacode Teknologi Indonesia
     7   * All Rights Reserved
     8   */
     9  
    10  package hctypes
    11  
    12  import (
    13  	"bytes"
    14  	"encoding/binary"
    15  	"encoding/hex"
    16  	"math/big"
    17  )
    18  
    19  type (
    20  	Buffer  []byte
    21  	Float32 float32
    22  	Float64 float64
    23  	Bool    bool
    24  	Int8    int8
    25  	Int16   int16
    26  	Int32   int32
    27  	Int64   int64
    28  	UInt8   uint8
    29  	UInt16  uint16
    30  	UInt32  uint32
    31  	UInt64  uint64
    32  	BigInt  big.Int
    33  	String  string
    34  )
    35  
    36  func (this Buffer) Equal(buf Buffer) Bool {
    37  	return Bool(bytes.Equal(this, buf))
    38  }
    39  
    40  func (this Buffer) ToString() String {
    41  	return String(this)
    42  }
    43  
    44  func (this Buffer) Encode() String {
    45  	return String(hex.EncodeToString(this))
    46  }
    47  
    48  func (this Buffer) ToFloat32() Float32 {
    49  	var ret Float32
    50  	binary.Read(bytes.NewReader([]byte(this)[:4]), binary.BigEndian, &ret)
    51  	return ret
    52  }
    53  
    54  func (this Buffer) ToFloat64() Float64 {
    55  	var ret Float64
    56  	binary.Read(bytes.NewReader([]byte(this)[:8]), binary.BigEndian, &ret)
    57  	return ret
    58  }
    59  
    60  func (this Buffer) ToBool() Bool {
    61  	i := UInt8([]byte(this)[0])
    62  	if i == 1 {
    63  		return true
    64  	} else {
    65  		return false
    66  	}
    67  }
    68  
    69  func (this Buffer) ToInt8() Int8 {
    70  	return Int8([]byte(this)[0])
    71  }
    72  
    73  func (this Buffer) ToInt16() Int16 {
    74  	var ret Int16
    75  	binary.Read(bytes.NewReader([]byte(this)[:2]), binary.BigEndian, &ret)
    76  	return ret
    77  }
    78  
    79  func (this Buffer) ToInt32() Int32 {
    80  	var ret Int32
    81  	binary.Read(bytes.NewReader([]byte(this)[:4]), binary.BigEndian, &ret)
    82  	return ret
    83  }
    84  
    85  func (this Buffer) ToInt64() Int64 {
    86  	var ret Int64
    87  	binary.Read(bytes.NewReader([]byte(this)[:8]), binary.BigEndian, &ret)
    88  	return ret
    89  }
    90  
    91  // -------
    92  
    93  func (this Buffer) ToUInt8() UInt8 {
    94  	return UInt8([]byte(this)[0])
    95  }
    96  
    97  func (this Buffer) ToUInt16() UInt16 {
    98  	var ret UInt16
    99  	binary.Read(bytes.NewReader([]byte(this)[:2]), binary.BigEndian, &ret)
   100  	return ret
   101  }
   102  
   103  func (this Buffer) ToUInt32() UInt32 {
   104  	var ret UInt32
   105  	binary.Read(bytes.NewReader([]byte(this)[:4]), binary.BigEndian, &ret)
   106  	return ret
   107  }
   108  
   109  func (this Buffer) ToUInt64() UInt64 {
   110  	var ret UInt64
   111  	binary.Read(bytes.NewReader([]byte(this)[:8]), binary.BigEndian, &ret)
   112  	return ret
   113  }
   114  
   115  func (this Buffer) ToBigInt() BigInt {
   116  	var ret = big.Int{}
   117  	ret.SetBytes(this)
   118  	return BigInt(ret)
   119  }
   120  
   121  // -------
   122  
   123  func (this String) ToBuffer() Buffer {
   124  	return Buffer(this)
   125  }
   126  
   127  func (this String) Decode() Buffer {
   128  	data, err := hex.DecodeString(string(this))
   129  	if err != nil {
   130  		return Buffer([]byte{})
   131  	}
   132  
   133  	return Buffer(data)
   134  }
   135  
   136  func (this Float32) ToBuffer() Buffer {
   137  	buf := new(bytes.Buffer)
   138  	binary.Write(buf, binary.BigEndian, this)
   139  	return buf.Bytes()
   140  }
   141  
   142  func (this Float64) ToBuffer() Buffer {
   143  	buf := new(bytes.Buffer)
   144  	binary.Write(buf, binary.BigEndian, this)
   145  	return buf.Bytes()
   146  }
   147  
   148  func (this Bool) ToBuffer() Buffer {
   149  	b := UInt8(0)
   150  	if this {
   151  		b = UInt8(1)
   152  	}
   153  	return b.ToBuffer()
   154  }
   155  
   156  func (this Int8) ToBuffer() Buffer {
   157  	return Buffer{byte(this)}
   158  }
   159  
   160  func (this Int16) ToBuffer() Buffer {
   161  	buf := new(bytes.Buffer)
   162  	binary.Write(buf, binary.BigEndian, this)
   163  	return buf.Bytes()
   164  }
   165  
   166  func (this Int32) ToBuffer() Buffer {
   167  	buf := new(bytes.Buffer)
   168  	binary.Write(buf, binary.BigEndian, this)
   169  	return buf.Bytes()
   170  }
   171  
   172  func (this Int64) ToBuffer() Buffer {
   173  	buf := new(bytes.Buffer)
   174  	binary.Write(buf, binary.BigEndian, this)
   175  	return buf.Bytes()
   176  }
   177  
   178  // ------------
   179  
   180  func (this UInt8) ToBuffer() Buffer {
   181  	return Buffer{byte(this)}
   182  }
   183  
   184  func (this UInt16) ToBuffer() Buffer {
   185  	buf := new(bytes.Buffer)
   186  	binary.Write(buf, binary.BigEndian, this)
   187  	return buf.Bytes()
   188  }
   189  
   190  func (this UInt32) ToBuffer() Buffer {
   191  	buf := new(bytes.Buffer)
   192  	binary.Write(buf, binary.BigEndian, this)
   193  	return buf.Bytes()
   194  }
   195  
   196  func (this UInt64) ToBuffer() Buffer {
   197  	buf := new(bytes.Buffer)
   198  	binary.Write(buf, binary.BigEndian, this)
   199  	return buf.Bytes()
   200  }
   201  
   202  func (this BigInt) ToBuffer() Buffer {
   203  	bi := big.Int(this)
   204  	return bi.Bytes()
   205  }
   206  

View as plain text