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;
   cChars const* v;
};
typedef struct cCharsSlice cCharsSlice;

Via the macro SLICES_C_ generated struct.

cVarCharsSlice

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

Via the macro SLICES_C_ generated struct.

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_c_( cCharsSlice, slice ) ) == 16 );

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

   slice.v[3] = c_c( "🚀" ); // 4
   expect_c_( count_chars_slice_c( as_c_( cCharsSlice, 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_c_( cCharsSlice, slice ) ) == 16 );

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

   slice.v[3] = c_c( "🚀" ); // 1
   expect_c_( count_chars_slice_runes_c( as_c_( cCharsSlice, 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_();
}