CStringList
Overview
Module that defines the CStringList type and the associated functions.
#include "clingo/lang/expect.h"
#include "clingo/string/CStringList.h"
#define expect_str_( Str, Exp ) \
expect_c_( string_is_c( (Str), (Exp) ) )
int main( void )
{
init_tap_c_();
CStringList* list = retain_c( make_string_list_c( 1 ) );
cVecInfo const* info = info_of_string_list_c( list );
expect_c_( vec_is_empty_c( info ) );
expect_c_( info->cap == 1 );
expect_c_( info->count == 0 );
add_cstr_to_string_list_c( list, "abc" );
add_to_string_list_c( list, lit_c( "def" ) );
expect_c_( not vec_is_empty_c( info ) );
expect_c_( info->cap > 1 );
expect_c_( info->count == 2 );
CString* arr[3]= { lit_c( "bang" ), lit_c( "bang" ), lit_c( "bing" ) };
add_array_to_string_list_c( list, 3, arr );
expect_c_( info->count == 5 );
expect_str_( get_from_string_list_c( list, 0 ), "abc" );
expect_str_( get_from_string_list_c( list, 2 ), "bang" );
expect_str_( get_from_string_list_c( list, 4 ), "bing" );
set_cstr_on_string_list_c( list, 4, "!?" );
expect_str_( get_from_string_list_c( list, 4 ), "!?" );
insert_cstr_into_string_list_c( list, 3, "BOOM" );
expect_c_( info->count == 6 );
CString* joined = join_string_list_c_( list, " " );
expect_str_( joined, "abc def bang BOOM bang !?" );
resize_string_list_c( list, info->count );
expect_c_( info->cap == info->count );
release_all_c_( list, joined );
return finish_tap_c_();
}
Types and Definitions
Generated
CStringList
struct CStringList;
typedef struct CStringList CStringList;
Via the macros OBJ_VEC_DEF_C_ and OBJ_VEC_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.
/* create */
CStringList* make_string_list_c( int64_t cap );
CStringList* new_string_list_c( void );
/* data */
CString* const* data_of_string_list_c( CStringList const* vec );
CString** var_data_of_string_list_c( CStringList* vec );
/* resize */
bool resize_string_list_c( CStringList* vec, int64_t cap );
/* info */
cVecInfo const* info_of_string_list_c( CStringList const* list );
/* use */
bool add_to_string_list_c( CStringList* vec, CString* val );
bool add_array_to_string_list_c( CStringList* vec, int64_t n, CString* const* arr );
CString* get_from_string_list_c( CStringList const* vec, int64_t pos );
bool insert_into_string_list_c( CStringList* vec, int64_t pos, CString* val );
bool remove_from_string_list_c( CStringList* vec, int64_t pos );
void set_on_string_list_c( CStringList* vec, int64_t pos, CString* val );
Functions
overall
add_chars_slice_to_string_list_c
bool add_chars_slice_to_string_list_c( CStringList* list, cCharsSlice slice );
Adds all chars as new strings to the string list.
from_chars_slice_c
CStringList* from_chars_slice_c( cCharsSlice slice );
Creates a new string list and adds all chars as new strings to the string list.
#include "clingo/lang/expect.h"
#include "clingo/string/CStringList.h"
int main( void )
{
init_tap_c_();
cCharsSlice slice = cs_c_( "abc", "def", "gh", "ij", "klmn", "opq" );
CStringList* list = from_chars_slice_c( slice );
CString* str = join_string_list_c_( list, "." );
expect_c_( string_is_c( str, "abc.def.gh.ij.klmn.opq" ) );
release_all_c_( list, str );
return finish_tap_c_();
}
wrap
add_chars_to_string_list_c
bool add_chars_to_string_list_c( CStringList* list, cChars chars );
Util function that wraps add_to_string_list_c.
add_cstr_to_string_list_c
bool add_cstr_to_string_list_c( CStringList* list, char const cstr[static 1] );
Util function that wraps add_to_string_list_c.
insert_chars_into_string_list_c
bool insert_chars_into_string_list_c( CStringList* list,
int64_t pos,
cChars val );
Util function that wraps insert_into_string_list_c.
insert_cstr_into_string_list_c
bool insert_cstr_into_string_list_c( CStringList* list,
int64_t pos,
char const cstr[static 1] );
Util function that wraps insert_into_string_list_c.
set_chars_on_string_list_c
bool set_chars_on_string_list_c( CStringList* list, int64_t pos, cChars val );
Util function that wraps set_on_string_list_c.
set_cstr_on_string_list_c
bool set_cstr_on_string_list_c( CStringList* list,
int64_t pos,
char const cstr[static 1] );
Util function that wraps set_on_string_list_c.
#include "clingo/lang/expect.h"
#include "clingo/string/CStringList.h"
#define expect_str_( Str, Exp ) \
expect_c_( string_is_c( (Str), (Exp) ) )
int main( void )
{
init_tap_c_();
CStringList* list = new_string_list_c();
retain_c( list );
// ------------------------------------------------------------------ prepare
add_cstr_to_string_list_c( list, "..." );
add_cstr_to_string_list_c( list, "A" );
add_cstr_to_string_list_c( list, "BB" );
add_cstr_to_string_list_c( list, "A" );
add_cstr_to_string_list_c( list, "---" );
CString* initStr = join_string_list_c_( list, "" );
expect_str_( initStr, "...ABBA---" );
// --------------------------------------------------------------------- test
CString* str;
set_cstr_on_string_list_c( list, 0, "-+" );
str = join_string_list_c_( list, "" );
expect_str_( str, "-+ABBA---" );
release_c( str );
set_cstr_on_string_list_c( list, 4, "+-" );
str = join_string_list_c_( list, "" );
expect_str_( str, "-+ABBA+-" );
release_c( str );
set_cstr_on_string_list_c( list, 2, "-+-" );
str = join_string_list_c_( list, "" );
expect_str_( str, "-+A-+-A+-" );
release_c( str );
release_c( list );
return finish_tap_c_();
}
prop
count_string_list_chars_c
int64_t count_string_list_chars_c( CStringList const* list );
Returns the sum of all chars in the list.
#include "clingo/lang/expect.h"
#include "clingo/string/CStringList.h"
int main( void )
{
init_tap_c_();
CStringList* list = retain_c( new_string_list_c() );
add_cstr_to_string_list_c( list, "simple" ); // 6
add_cstr_to_string_list_c( list, "ascii text" ); // 10
expect_c_( count_string_list_chars_c( list ) == 16 );
add_cstr_to_string_list_c( list, "€uro" ); // 6
expect_c_( count_string_list_chars_c( list ) == 22 );
add_cstr_to_string_list_c( list, "🚀" ); // 4
expect_c_( count_string_list_chars_c( list ) == 26 );
release_c( list );
return finish_tap_c_();
}
count_string_list_runes_c
int64_t count_string_list_runes_c( CStringList const* list );
Returns the sum of all runes in the list.
#include "clingo/lang/expect.h"
#include "clingo/string/CStringList.h"
int main( void )
{
init_tap_c_();
CStringList* list = new_string_list_c();
retain_c( list );
add_to_string_list_c( list, new_string_c( "simple" ) ); // 6
add_to_string_list_c( list, new_string_c( "ascii text" ) ); // 10
expect_c_( 16 == count_string_list_runes_c( list ) );
add_to_string_list_c( list, new_string_c( "€uro" ) ); // 4
expect_c_( 20 == count_string_list_runes_c( list ) );
add_to_string_list_c( list, new_string_c( "🚀" ) ); // 1
expect_c_( 21 == count_string_list_runes_c( list ) );
release_c( list );
return finish_tap_c_();
}
parts
join_string_list_c
#define join_string_list_c_( List, CStr ) \
join_string_list_c( (List), c_c( CStr ) )
CString* join_string_list_c( CStringList const* list, cChars sep );
Joins all strings in the list into a single string with each element separated by the given separator (sep).
#include "clingo/lang/expect.h"
#include "clingo/string/CStringList.h"
#define expect_str_( Str, Exp ) \
expect_c_( string_is_c( (Str), (Exp) ) )
int main( void )
{
init_tap_c_();
CStringList* list = retain_c( new_string_list_c() );
// ------------------------------------------------------------------ prepare
add_cstr_to_string_list_c( list, "abc" );
add_cstr_to_string_list_c( list, "def" );
add_cstr_to_string_list_c( list, "ghijkl" );
add_cstr_to_string_list_c( list, "mnopqr" );
add_cstr_to_string_list_c( list, "stuv" );
add_cstr_to_string_list_c( list, "wxyz" );
// --------------------------------------------------------------------- test
CString* str;
str = join_string_list_c_( list, "" );
expect_str_( str, "abcdefghijklmnopqrstuvwxyz" );
release_c( str );
str = join_string_list_c_( list, "-" );
expect_str_( str, "abc-def-ghijkl-mnopqr-stuv-wxyz" );
release_c( str );
str = join_string_list_c_( list, " - " );
expect_str_( str, "abc - def - ghijkl - mnopqr - stuv - wxyz" );
release_c( str );
release_c( list );
return finish_tap_c_();
}