cCharsSlice

Overview

It is recommended to use cChars and cVarChars as alternative to a C-string.

Types and Definitions

Generated

cCharsSlice

struct cCharsSlice
{
   int64_t s;
   char const* v;
};
typedef struct cCharsSlice cCharsSlice;

Via the macros SLICE_DEF_C_ and SLICE_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.

/* init */
cCharsSlice chars_slice_c( int64_t s, cChars const* v );
cCharsSlice make_chars_slice_c( cChars const* beg, cChars const* end );
cCharsSlice empty_chars_slice_c( void );
/* sub */
cCharsSlice left_chars_slice_c( cCharsSlice slice, int64_t maxLen );
cCharsSlice mid_chars_slice_c( cCharsSlice slice, int64_t index );
cCharsSlice right_chars_slice_c( cCharsSlice slice, int64_t maxLen );
cCharsSlice sub_chars_slice_c( cCharsSlice slice,
                               int64_t begIdx,
                               int64_t endIdx );

cVarCharsSlice

struct cVarCharsSlice
{
   int64_t s;
   char* v;
};
typedef struct cVarCharsSlice cVarCharsSlice;

Via the macros SLICE_DEF_C_ and SLICE_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.

/* init */
cVarCharsSlice var_chars_slice_c( int64_t s, cChars* v );
cVarCharsSlice make_var_chars_slice_c( cChars* beg, cChars* end );
cVarCharsSlice empty_var_chars_slice_c( void );
/* sub */
cVarCharsSlice left_var_chars_slice_c( cVarCharsSlice slice, int64_t maxLen );
cVarCharsSlice mid_var_chars_slice_c( cVarCharsSlice slice, int64_t index );
cVarCharsSlice right_var_slice_chars_c( cVarCharsSlice slice, int64_t maxLen );
cVarCharsSlice sub_var_chars_slice_c( cVarCharsSlice slice,
                                      int64_t begIdx,
                                      int64_t endIdx );
/* var slice */
cCharsSlice as_chars_slice_c( cVarCharsSlice slice );
cVarCharsSlice cast_chars_slice_c( cVarCharsSlice slice, cCharsSlice sub );
int64_t set_var_chars_slice_c( cVarCharsSlice dst, cChars src );

cCharsChunk

struct cCharsChunk
{
   int64_t s;
   cChars const* v;
   int64_t favSize;
   cCharsSlice slice;
};
typedef struct cCharsChunk cCharsChunk;

Via the macros CHUNK_DEF_C_ and CHUNK_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.

void init_back_chars_chunk_c( cCharsChunk chunk[static 1],
                              int64_t s,
                              cCharsSlice slice );
void init_front_chars_chunk_c( cCharsChunk chunk[static 1],
                               int64_t s,
                               cCharsSlice slice );

cVarCharsChunk

struct cVarCharsChunk
{
   int64_t s;
   cChars* v;
   int64_t favSize;
   cVarCharsSlice slice;
};
typedef struct cVarCharsChunk cVarCharsChunk;

Via the macros CHUNK_DEF_C_ and CHUNK_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.

void init_back_var_chars_chunk_c( cVarCharsChunk chunk[static 1],
                                  int64_t s,
                                  cVarCharsSlice slice );
void init_front_var_chars_chunk_c( cVarCharsChunk chunk[static 1],
                                   int64_t s,
                                   cVarCharsSlice slice );

cCharsWindow

struct cCharsWindow
{
   int64_t s;
   cChars const* v;
   cCharsSlice slice;
};
typedef struct cCharsWindow cCharsWindow;

Via the macros WINDOW_DEF_C_ and WINDOW_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.

void init_back_chars_window_c( cCharsWindow window[static 1],
                               int64_t s,
                               cCharsSlice slice );
void init_front_chars_window_c( cCharsWindow window[static 1],
                                int64_t s,
                                cCharsSlice slice );

cVarCharsWindow

struct cVarCharsWindow
{
   int64_t s;
   cChars* v;
   cVarCharsSlice slice;
};
typedef struct cVarCharsWindow cVarCharsWindow;

Via the macros WINDOW_DEF_C_ and WINDOW_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.

void init_back_var_chars_window_c( cVarCharsWindow window[static 1],
                                   int64_t s,
                                   cVarCharsSlice slice );
void init_front_var_chars_window_c( cVarCharsWindow window[static 1],
                                    int64_t s,
                                    cVarCharsSlice slice );

Functions

overall

cs_c_

#define cs_c_( ... )

Macro function that creates a cCharsSlice value with the C-string values in .

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

int main( void )
{
   init_tap_c_();

   cCharsSlice slc = cs_c_( "abc", "def", "gol" );

   expect_c_( slc.s == 3 );
   expect_c_( chars_is_c( slc.v[0], "abc" ) );
   expect_c_( chars_is_c( slc.v[1], "def" ) );
   expect_c_( chars_is_c( slc.v[2], "gol" ) );

   return finish_tap_c_();
}

init_chars_slice_c

cCharsSlice init_chars_slice_c( cVarCharsSlice slice,
                                int64_t n,
                                char const* arr[static 1] );

Inits a slice with the n C-string values in the array. If the n is greater as the space in the slice returns the function an empty slice.

algo

count_eq_chars_c

int64_t count_eq_chars_c( cCharsSlice slice, cChars val );

Via the macro COUNT_EQ_C_ implemented function.

find_chars_c

cChars const* find_chars_c( cCharsSlice slice, cChars val );

Via the macro FIND_VAL_C_ implemented function.

qsort_chars_slice_c

void qsort_chars_slice_c( cVarCharsSlice slice );

Via the macro QSORT_C_ implemented function.

prop

count_chars_slice_c

int64_t count_chars_slice_c( cCharsSlice slice );

Returns the sum of all chars in the slice.

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

int main( void )
{
   init_tap_c_();

   cVarCharsSlice slice = structs_c_( 4, cChars );
   slice.v[0] = c_c( "simple" );
   slice.v[1] = c_c( "ascii text" );
   slice.v[2] = c_c( "" );
   slice.v[3] = c_c( "" );

   expect_c_( count_chars_slice_c( as_chars_slice_c( slice ) ) == 16 );

   slice.v[2] = c_c( "€uro" ); // 6
   expect_c_( count_chars_slice_c( as_chars_slice_c( slice ) ) == 22 );

   slice.v[3] = c_c( "🚀" ); // 4
   expect_c_( count_chars_slice_c( as_chars_slice_c( slice ) ) == 26 );

   return finish_tap_c_();
}

count_chars_slice_runes_c

int64_t count_chars_slice_runes_c( cCharsSlice slice );

Returns the sum of all runes in the slice. Returns -1 if one value in the slice is invalid.

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

int main( void )
{
   init_tap_c_();

   cVarCharsSlice slice = structs_c_( 4, cChars );
   slice.v[0] = c_c( "simple" );
   slice.v[1] = c_c( "ascii text" );
   slice.v[2] = c_c( "" );
   slice.v[3] = c_c( "" );

   expect_c_( count_chars_slice_runes_c( as_chars_slice_c( slice ) ) == 16 );

   slice.v[2] = c_c( "€uro" ); // 4
   expect_c_( count_chars_slice_runes_c( as_chars_slice_c( slice ) ) == 20 );

   slice.v[3] = c_c( "🚀" ); // 1
   expect_c_( count_chars_slice_runes_c( as_chars_slice_c( slice ) ) == 21 );

   return finish_tap_c_();
}

util

chars_ends_with_any_c

#define chars_ends_with_any_c_( Chars, ... )                                   \
   chars_ends_with_any_c( (Chars), cs_c_( __VA_ARGS__ ) )
bool chars_ends_with_any_c( cChars chars, cCharsSlice slice );

Returns true if chars ends with any of the values in slice, otherwise false.

chars_is_any_c

#define chars_is_any_c_( Chars, ... )                                          \
   chars_is_any_c( (Chars), css_c_( __VA_ARGS__ ) )
bool chars_is_any_c( cChars chars, cCharsSlice slice );

Returns true if chars equals any of the chars in the slice, otherwise false.

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

int main( void )
{
   init_tap_c_();

   cChars nephew = c_c( "Tick" );

   expect_c_( chars_is_any_c_( nephew, "Tick", "Trick", "Track" ) );
   expect_c_( not chars_is_any_c_( nephew, "Trick", "Track" ) );

   return finish_tap_c_();
}

chars_starts_with_any_c

#define chars_starts_with_any_c_( Chars, ... )                                 \
   chars_starts_with_any_c( (Chars), cs_c_( __VA_ARGS__ ) )
bool chars_starts_with_any_c( cChars chars, cCharsSlice slice );

Returns true if chars starts with any of the values in slice, otherwise false.

index_of_any_chars_c

#define index_of_any_chars_c_( Chars, ... )                                    \
   index_of_any_chars_c( (Chars), cs_c_( __VA_ARGS__ ) )
int64_t index_of_any_chars_c( cChars chars, cCharsSlice slice );

Returns a positive index if any value in the slice exist in chars, otherwise returns the function -1.

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

int main( void )
{
   init_tap_c_();

   cChars chars = c_c( "abcdefgh" );
   int64_t idx = 0;

   idx = index_of_any_chars_c_( chars, "def", "fgh", "abc" ); // ----------- abc
   expect_c_( idx == 0 );

   idx = index_of_any_chars_c_( chars, "def", "fgh" ); // ------------------ def
   expect_c_( idx == 3 );

   idx = index_of_any_chars_c_( chars, "fght", "adcg" ); // --------------------
   expect_c_( idx == -1 );

   return finish_tap_c_();
}

join_chars_slice_c

cChars join_chars_slice_c( cCharsSlice slice, cChars sep, cVarChars buf );

Fills buf with all chars in the list. Each element will be separated by the given separator (sep).

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

#define expect_( Res, Exp )                                                    \
   expect_c_( chars_is_c( (Res), (Exp) ) )

int main( void )
{
   init_tap_c_();

   cVarChars buf = char_buffer_c_( 128 );
   cCharsSlice slice = cs_c_( "abc", "def", "gh", "ij", "klmn", "opq" );

   expect_( join_chars_slice_c_( slice, "", buf ),
            "abcdefghijklmnopq" );
   expect_( join_chars_slice_c_( slice, "-", buf ),
            "abc-def-gh-ij-klmn-opq" );
   expect_( join_chars_slice_c_( slice, " - ", buf ),
            "abc - def - gh - ij - klmn - opq" );

   return finish_tap_c_();
}