cBytes

Overview

It is recommended to use cBytes and cVarBytes for data buffer.

Types and Definitions

Generated

cBytes

struct cBytes
{
   int64_t s;
   cByte const* v;
};
typedef struct cBytes cBytes;

Via the macro SLICES_C_ generated struct.

cVarBytes

struct cVarBytes
{
   int64_t s;
   cByte* v;
};
typedef struct cVarBytes cVarBytes;

Via the macro SLICES_C_ generated struct.

Functions

overall

byte_buffer_c_

#define byte_buffer_c_( Size )

Macro function that initalizes a cVarBytes that can store Size values.

bit

get_bytes_bit_c

cByte get_bytes_bit_c( cBytes slice, int64_t pos );

Returns the bit at pos in the byte slice.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/cBytes.h"

int main( void )
{
   init_tap_c_();

   cBytes slice = slice_c_( cByte, 0xa2, 0xdb );

   // a - 1010
   expect_c_( get_bytes_bit_c( slice,  0 ) == 1 );
   expect_c_( get_bytes_bit_c( slice,  1 ) == 0 );
   expect_c_( get_bytes_bit_c( slice,  2 ) == 1 );
   expect_c_( get_bytes_bit_c( slice,  3 ) == 0 );
   // 2 - 0010
   expect_c_( get_bytes_bit_c( slice,  4 ) == 0 );
   expect_c_( get_bytes_bit_c( slice,  5 ) == 0 );
   expect_c_( get_bytes_bit_c( slice,  6 ) == 1 );
   expect_c_( get_bytes_bit_c( slice,  7 ) == 0 );
   // d - 1101
   expect_c_( get_bytes_bit_c( slice,  8 ) == 1 );
   expect_c_( get_bytes_bit_c( slice,  9 ) == 1 );
   expect_c_( get_bytes_bit_c( slice, 10 ) == 0 );
   expect_c_( get_bytes_bit_c( slice, 11 ) == 1 );
   // b - 1011
   expect_c_( get_bytes_bit_c( slice, 12 ) == 1 );
   expect_c_( get_bytes_bit_c( slice, 13 ) == 0 );
   expect_c_( get_bytes_bit_c( slice, 14 ) == 1 );
   expect_c_( get_bytes_bit_c( slice, 15 ) == 1 );

   return finish_tap_c_();
}

set_bytes_bit_c

void set_bytes_bit_c( cVarBytes slice, int64_t pos, cByte bit );

Sets the bit at pos in the byte slice.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/cBytes.h"

int main( void )
{
   init_tap_c_();

   cVarBytes slice = slice_c_( cByte, 0x00, 0xff );

   set_bytes_bit_c( slice, 0, 1 );
   set_bytes_bit_c( slice, 2, 1 );
   set_bytes_bit_c( slice, 6, 1 );

   set_bytes_bit_c( slice, 10, 0 );
   set_bytes_bit_c( slice, 13, 0 );

   expect_c_( slice.v[0] == 0xa2 ); // 0-7   -> 1010 0010
   expect_c_( slice.v[1] == 0xdb ); // 8-15  -> 1101 1011

   return finish_tap_c_();
}

set_bytes_bits_c

void set_bytes_bits_c( cVarBytes slice, cRange range, cByte bit );

Sets all bits inside the range with the bit value.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/cBytes.h"

int main( void )
{
   init_tap_c_();

   cVarBytes slice = slice_c_( cByte, 0x0f, 0xf0 );

   set_bytes_bits_c( slice, closed_range_c_( 5, 9 ), 0 );
   expect_c_( slice.v[0] == 0x08 ); // 0-7    -> 0000 1000
   expect_c_( slice.v[1] == 0x30 ); // 8-15   -> 0011 0000

   set_bytes_bits_c( slice, closed_range_c_( 2, 5 ), 1 );
   expect_c_( slice.v[0] == 0x3c ); // 0-7    -> 0011 1100
   expect_c_( slice.v[1] == 0x30 ); // 8-15   -> 0011 0000

   return finish_tap_c_();
}

set_odd_byte_c

void set_odd_byte_c( cVarBytes slice, int64_t pos, int64_t bitOffset, cByte byte );

Sets a byte value that overlaps with two bytes in the byte slice.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/cBytes.h"

#define expect_bytes_( Slice, ... )                                            \
   expect_eq_c_(                                                               \
      cmp_bytes_c(                                                             \
         as_c_( cBytes, Slice ),                                               \
         (cBytes){ (Slice).s, (cByte[]){ __VA_ARGS__ } }                       \
      )                                                                        \
   )

int main( void )
{
   init_tap_c_();

   // 11110001 11110011 11110101 11110111 11110000
   cVarBytes bytes = slice_c_( cByte, 0xf1, 0xf3, 0xf5, 0xf7, 0xf0 );

   set_odd_byte_c( bytes, 2, 4, 0x44 ); // 01000100
   // 11110001 11110011 11110100 01000111 11110000
   expect_bytes_( bytes, 0xf1, 0xf3, 0xf4, 0x47, 0xf0 );

   set_odd_byte_c( bytes, 0, 0, 0x12 ); // 00010010
   // 00010010 11110011 11110100 01000111 11110000
   expect_bytes_( bytes, 0x12, 0xf3, 0xf4, 0x47, 0xf0 );

   set_odd_byte_c( bytes, 4, 7, 0xff ); // 11111111
   // 00010010 11110011 11110100 01000111 11110001
   expect_bytes_( bytes, 0x12, 0xf3, 0xf4, 0x47, 0xf1 );

   return finish_tap_c_();
}

shift_bytes_bits_c

void shift_bytes_bits_c( cVarBytes slice, int64_t offset, cByte fillValue );

Shifts the bits in a byte slice. A negative offset shifts the bits to the left, a positive offset shifts the bits to the right.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/cBytes.h"

#define expect_bytes_( Slice, ... )                                            \
   expect_eq_c_(                                                               \
      cmp_bytes_c(                                                             \
         as_c_( cBytes, Slice ),                                               \
         (cBytes){ (Slice).s, (cByte[]){ __VA_ARGS__ } }                       \
      )                                                                        \
   )

int main( void )
{
   init_tap_c_();

   // ----------------------------------------------------- left shift
   {
      // 00001111 00001111 00001111 00001111 00001111
      cVarBytes bytes = slice_c_( cByte, 0x0f, 0x0f, 0x0f, 0x0f, 0xf );
      shift_bytes_bits_c( bytes, -5, 0 );
      // 11100001 11100001 11100001 11100001 11100000
      expect_bytes_( bytes, 0xe1, 0xe1, 0xe1, 0xe1, 0xe0 );
   }
   {
      // 00000011 10000111 10000111 10000111 10000111
      cVarBytes bytes = slice_c_( cByte, 0x03, 0x87, 0x87, 0x87, 0x87 );
      shift_bytes_bits_c( bytes, -13, 0 );
      // 11110000 11110000 11110000 11100000 00000000
      expect_bytes_( bytes, 0xf0, 0xf0, 0xf0, 0xe0, 0x00 );
   }

   {
      // 00000000 00000000 00000000 00000000 00000001
      cVarBytes bytes = slice_c_( cByte, 0x00, 0x00, 0x00, 0x00, 0x01 );
      shift_bytes_bits_c( bytes, -39, 1 );
      // 11111111 11111111 11111111 11111111 11111111
      expect_bytes_( bytes, 0xff, 0xff, 0xff, 0xff, 0xff );
   }

   // ----------------------------------------------------- right shift
   {
      // 11100001 11100001 11100001 11100001 11100000
      cVarBytes bytes = slice_c_( cByte, 0xe1, 0xe1, 0xe1, 0xe1, 0xe0 );
      shift_bytes_bits_c( bytes, 6, 0 );
      // 00000011 10000111 10000111 10000111 10000111
      expect_bytes_( bytes, 0x03, 0x87, 0x87, 0x87, 0x87 );
   }
   {
      // 11110000 11110000 11110000 11100000 00000000
      cVarBytes bytes = slice_c_( cByte, 0xf0, 0xf0, 0xf0, 0xe0, 0x00 );
      shift_bytes_bits_c( bytes, 16, 0 );
      // 00000000 00000000 11110000 11110000 11110000
      expect_bytes_( bytes, 0x00, 0x00, 0xf0, 0xf0, 0xf0 );
   }
   {
      // 10000000 00000000 00000000 00000000 00000000
      cVarBytes bytes = slice_c_( cByte, 0x80, 0x00, 0x00, 0x00, 0x00 );
      shift_bytes_bits_c( bytes, 39, 1 );
      // 11111111 11111111 11111111 11111111 11111111
      expect_bytes_( bytes, 0xff, 0xff, 0xff, 0xff, 0xff );
   }

   // ----------------------------------------------------- left & right shift
   {
      // 00001111 00001111 00001111 00001111 00001111
      cVarBytes bytes = slice_c_( cByte, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f );

      shift_bytes_bits_c( bytes, -5, 0 );
      // 11100001 11100001 11100001 11100001 11100000
      expect_bytes_( bytes, 0xe1, 0xe1, 0xe1, 0xe1, 0xe0 );

      shift_bytes_bits_c( bytes, 6, 0 );
      // 00000011 10000111 10000111 10000111 10000111
      expect_bytes_( bytes, 0x03, 0x87, 0x87, 0x87, 0x87 );

      shift_bytes_bits_c( bytes, -13, 0 );
      // 11110000 11110000 11110000 11100000 00000000
      expect_bytes_( bytes, 0xf0, 0xf0, 0xf0, 0xe0, 0x00 );

      shift_bytes_bits_c( bytes, 16, 0 );
      // 00000000 00000000 11110000 11110000 11110000
      expect_bytes_( bytes, 0x00, 0x00, 0xf0, 0xf0, 0xf0 );
   }

   return finish_tap_c_();
}

toggle_bytes_bit_c

void toggle_bytes_bit_c( cVarBytes slice, int64_t pos );

Toggles the bit at pos in the byte slice.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/cBytes.h"

int main( void )
{
   init_tap_c_();

   cVarBytes slice = slice_c_( cByte, 0x00, 0xff );

   toggle_bytes_bit_c( slice, 0 );
   toggle_bytes_bit_c( slice, 2 );
   toggle_bytes_bit_c( slice, 6 );

   toggle_bytes_bit_c( slice, 10 );
   toggle_bytes_bit_c( slice, 13 );

   expect_c_( slice.v[ 0 ] == 0xa2 ); // 0-7   -> 1010 0010
   expect_c_( slice.v[ 1 ] == 0xdb ); // 8-15  -> 1101 1011

   return finish_tap_c_();
}

shift

shift_bytes_c

void shift_bytes_c( cVarBytes slice, int64_t offset );

Shifts the bits in a byte slice. A negative offset shifts the bits to the left, a positive offset shifts the bits to the right.

Example
#include "clingo/io/print.h"
#include "clingo/lang/expect.h"
#include "clingo/type/cBytes.h"

TEMP_SLICE_C_(
   test,
   {
      cVarBytes inp;
      int64_t offset;
      cBytes exp;
   }
)
#define t_( ... )((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_(
         slice_c_( cByte, 0x01, 0x02, 0x03, 0x04, 0x05 ),
         -4,
         slice_c_( cByte, 0x05, 0x02, 0x03, 0x04, 0x05 )
      ),
      t_(
         slice_c_( cByte, 0x01, 0x02, 0x03, 0x04, 0x05 ),
         -1,
         slice_c_( cByte, 0x02, 0x03, 0x04, 0x05, 0x05 )
      ),
      t_(
         slice_c_( cByte, 0x01, 0x02, 0x03, 0x04, 0x05 ),
         -5,
         slice_c_( cByte, 0x01, 0x02, 0x03, 0x04, 0x05 )
      ),
      t_(
         slice_c_( cByte, 0x01, 0x02, 0x03, 0x04, 0x05 ),
         4,
         slice_c_( cByte, 0x01, 0x02, 0x03, 0x04, 0x01 )
      ),
      t_(
         slice_c_( cByte, 0x01, 0x02, 0x03, 0x04, 0x05 ),
         1,
         slice_c_( cByte, 0x01, 0x01, 0x02, 0x03, 0x04 )
      ),
      t_(
         slice_c_( cByte, 0x01, 0x02, 0x03, 0x04, 0x05 ),
         5,
         slice_c_( cByte, 0x01, 0x02, 0x03, 0x04, 0x05 )
      )
   );

   for_each_c_( test const*, t, tests )
   {
      shift_bytes_c( t->inp, t->offset );
      bool res = eq_c( cmp_bytes_c( as_c_( cBytes, t->inp ), t->exp ) );
      expect_c_( res );
   }

   return finish_tap_c_();
}

logic

bitand_bytes_c

bool bitand_bytes_c( cBytes a, cBytes b, cVarBytes result );

Combines the bits of a and b with a logical and. Returns false if the slices have different s values.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/cBytes.h"

int main( void )
{
   init_tap_c_();

   cBytes a = slice_c_( cByte, 0x0f, 0xc3, 0x5d, 0x10 );
   cBytes b = slice_c_( cByte, 0x10, 0x5d, 0x0f, 0xc3 );
   cVarBytes result = scalars_c_( 4, cByte );

   bitand_bytes_c( a, b, result );

   expect_c_( result.v[ 0 ] == 0x00 );
   expect_c_( result.v[ 1 ] == 0x41 );
   expect_c_( result.v[ 2 ] == 0x0d );
   expect_c_( result.v[ 3 ] == 0x00 );

   return finish_tap_c_();
}

bitor_bytes_c

bool bitor_bytes_c( cBytes a, cBytes b, cVarBytes result );

Combines the bits of a and b with a logical or. Returns false if the slices have different s values.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/cBytes.h"

int main( void )
{
   init_tap_c_();

   cBytes a = slice_c_( cByte, 0x0f, 0xc3, 0x5d, 0x10 );
   cBytes b = slice_c_( cByte, 0x10, 0x5d, 0x0f, 0xc3 );
   cVarBytes result = scalars_c_( 4, cByte );

   bitor_bytes_c( a, b, result );

   expect_c_( result.v[ 0 ] == 0x1f );
   expect_c_( result.v[ 1 ] == 0xdf );
   expect_c_( result.v[ 2 ] == 0x5f );
   expect_c_( result.v[ 3 ] == 0xd3 );

   return finish_tap_c_();
}

compl_bytes_c

bool compl_bytes_c( cBytes a, cVarBytes result );

Builds the complement of the slice a. Returns false if the slices have different s values.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/cBytes.h"

int main( void )
{
   init_tap_c_();

   cVarBytes slice = slice_c_( cByte, 0x0f, 0xc3, 0x5d, 0x10 );

   compl_bytes_c( as_c_( cBytes, slice ), slice );

   expect_c_( slice.v[ 0 ] == 0xf0 );
   expect_c_( slice.v[ 1 ] == 0x3c );
   expect_c_( slice.v[ 2 ] == 0xa2 );
   expect_c_( slice.v[ 3 ] == 0xef );

   return finish_tap_c_();
}

xor_bytes_c

bool xor_bytes_c( cBytes a, cBytes b, cVarBytes result );

Combine the bits of a and b with a logical xor. Returns false if the slices have different s values.

Example
#include "clingo/lang/expect.h"
#include "clingo/type/cBytes.h"

int main( void )
{
   init_tap_c_();

   cBytes a = slice_c_( cByte, 0x0f, 0xc3, 0x5d, 0x10 );
   cBytes b = slice_c_( cByte, 0x10, 0x5d, 0x0f, 0xc3 );
   cVarBytes result = scalars_c_( 4, cByte );

   xor_bytes_c( a, b, result );

   expect_c_( result.v[ 0 ] == 0x1f );
   expect_c_( result.v[ 1 ] == 0x9e );
   expect_c_( result.v[ 2 ] == 0x52 );
   expect_c_( result.v[ 3 ] == 0xd3 );

   return finish_tap_c_();
}

algo

bytes_ends_with_c

bool bytes_ends_with_c( cBytes slice, cBytes needle );

Via the macro ENDS_WITH_C_ implemented function.

bytes_starts_with_c

bool bytes_starts_with_c( cBytes slice, cBytes needle );

Via the macro STARTS_WITH_C_ implemented function.

cmp_bytes_c

#define cmp_bytes_c_( Slice, ... )                                             \
   cmp_bytes_c( (Slice), ((cBytes)slice_c_( cByte, __VA_ARGS__ )) )
int cmp_bytes_c( cBytes a, cBytes b );

Via the macro CMP_SLICE_C_ implemented function.

count_eq_byte_c

int64_t count_eq_byte_c( cBytes slice, cByte val );

Via the macro COUNT_EQ_C_ implemented function.

find_byte_c

cByte const* find_byte_c( cBytes slice, cByte val );

Via the macro FIND_VAL_C_ implemented function.

index_of_bytes_c

int64_t index_of_bytes_c( cBytes slice, cBytes sub );

Via the macro INDEX_OF_SLICE_C_ implemented function.

insert_byte_c

int64_t insert_byte_c( cVarBytes slice, int64_t index, cByte val );

Via the macro INSERT_VAL_C_ implemented function.

insert_bytes_c

int64_t insert_bytes_c( cVarBytes dst, int64_t index, cBytes src );

Via the macro INSERT_SLICE_C_ implemented function.

remove_byte_c

bool remove_byte_c( cVarBytes bytes[static 1], int64_t pos );

Via the macro REMOVE_C_ implemented function.

take_byte_c

bool take_byte_c( cVarBytes bytes[static 1], int64_t pos, cByte val[static 1] );

Via the macro TAKE_C_ implemented function.