cChars

Overview

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

Types and Definitions

Generated

cChars

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

Via the macro SLICES_C_ generated struct.

cVarChars

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

Via the macro SLICES_C_ generated struct.

Functions

overall

c_c

cChars c_c( char const cstr[static 1] );

Function to create a chars value from a C-string. The '\0' from the c string will not be an element of chars.

char_buffer_c_

#define char_buffer_c_( Size )

Macro function that initializes a cVarChars that can store Size values.

chars_is_c

bool chars_is_c( cChars chars, char const cstr[static 1] );

Function that checks the elements with the help of a C-string. The check ignores the '\0' of the c string.

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

int main( void )
{
   init_tap_c_();

   cChars chars = c_c( "clingo" );

   expect_c_(  chars_is_c( chars, "clingo" ) );
   expect_c_( !chars_is_c( chars, "Clingo" ) );

   return finish_tap_c_();
}

cstr_is_c

bool cstr_is_c( char const cstr[static 1], char const exp[static 1] );

Function that checks if the C-string cstr equals exp.

cstr_is_any_char_c

#define cstr_is_any_char_c_( Cstr, Set )                                            \
   cstr_is_any_char_c( (Cstr), c_c( Set ) )
bool cstr_is_any_char_c( char const cstr[static 1], cChars set );

Function checks if the C-string represents any of the characters in the set.

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

int main( void )
{
   init_tap_c_();

   expect_c_( cstr_is_any_char_c_( "Q", "CQ" ) );

   expect_c_( not cstr_is_any_char_c_( "CQ", "CQ" ) );
   expect_c_( not cstr_is_any_char_c_( "a", "xyz" ) );
   expect_c_( not cstr_is_any_char_c_( "", "abc" ) );

   return finish_tap_c_();
}

cstr_is_char_c

bool cstr_is_char_c( char const cstr[static 1], char c );

Function checks if the C-string represents just one char value.

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

int main( void )
{
   init_tap_c_();

   expect_c_( cstr_is_char_c( "Q", 'Q' ) );

   expect_c_( not cstr_is_char_c( "abc", 'a' ) );
   expect_c_( not cstr_is_char_c( "x", 'y' ) );
   expect_c_( not cstr_is_char_c( "", 'c' ) );

   return finish_tap_c_();
}

eq_chars_c

bool eq_chars_c( cChars a, cChars b );

Returns true if both chars are equal, otherwise false.

make_cstr_c

#define make_cstr_c_( Buf, Cstr )                                              \
   make_cstr_c( (Buf), c_c( Cstr ) )
char* make_cstr_c( cVarChars buf, cChars chars );

Creates a C-string in the memory of buf. Returns NULL if buf has not enough space to store the C-string.

algo

chars_ends_with_c

#define chars_ends_with_c_( Chars, Cstr )                                      \
   chars_ends_with_c( (Chars), c_c( Cstr ) )
bool chars_ends_with_c( cChars chars, cChars needle );

Via the macro ENDS_WITH_C_ implemented function.

chars_starts_with_c

#define chars_starts_with_c_( Chars, Cstr )                                    \
   chars_starts_with_c( (Chars), c_c( Cstr ) )
bool chars_starts_with_c( cChars chars, cChars needle );

Via the macro STARTS_WITH_C_ implemented function.

cmp_chars_c

#define cmp_chars_c_( Chars, Cstr )                                            \
   cmp_chars_c( (Chars), c_c( Cstr ) )
int cmp_chars_c( cChars a, cChars b );

Via the macro CMP_SLICE_C_ implemented function.

count_eq_char_c

int64_t count_eq_char_c( cChars chars, char val );

Via the macro COUNT_EQ_C_ implemented function.

find_any_char_c

#define find_any_char_c_( Chars, Cstr )                                        \
   find_any_char_c( (Chars), c_c( Cstr ) )
char const* find_any_char_c( cChars chars, cChars set );

Returns a pointer to the first char that matches any of the characters in the set and NULL if non of the characters exist in the chars.

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

int main( void )
{
   init_tap_c_();

   cChars base = c_c( "- This, string." );
   cChars chars = base;
   cChars set = c_c( " ,.-" );

   char const* ptr = NULL;
   int64_t idx = -1;

   ptr = find_any_char_c( chars, set ); // --------------------------------- '-'
   idx = index_of_c_( base, ptr );
   expect_c_( *ptr == '-' );
   expect_c_( idx == 0 );
   chars = mid_c_( cChars, base, idx+1 );

   ptr = find_any_char_c( chars, set ); // --------------------------------- ' '
   idx = index_of_c_( base, ptr );
   expect_c_( *ptr == ' ' );
   expect_c_( idx == 1 );
   chars = mid_c_( cChars, base, idx+1 );

   ptr = find_any_char_c( chars, set ); // --------------------------------- ','
   idx = index_of_c_( base, ptr );
   expect_c_( *ptr == ',' );
   expect_c_( idx == 6 );
   chars = mid_c_( cChars, base, idx+1 );

   ptr = find_any_char_c( chars, set ); // --------------------------------- ' '
   idx = index_of_c_( base, ptr );
   expect_c_( *ptr == ' ' );
   expect_c_( idx == 7 );
   chars = mid_c_( cChars, base, idx+1 );

   ptr = find_any_char_c( chars, set ); // --------------------------------- '.'
   idx = index_of_c_( base, ptr );
   expect_c_( *ptr == '.' );
   expect_c_( idx == 14 );
   chars = mid_c_( cChars, base, idx+1 );

   return finish_tap_c_();
}

find_char_c

char const* find_char_c( cChars chars, char val );

Via the macro FIND_VAL_C_ implemented function.

index_of_chars_c

#define index_of_chars_c_( Chars, Cstr )                                       \
   index_of_chars_c( (Chars), c_c( Cstr ) )
int64_t index_of_chars_c( cChars chars, cChars sub );

Via the macro INDEX_OF_SLICE_C_ implemented function.

insert_char_c

int64_t insert_char_c( cVarChars chars, int64_t index, char val );

Via the macro INSERT_VAL_C_ implemented function.

insert_chars_c

#define insert_chars_c_( Chars, Index, Cstr )                                  \
   insert_chars_c( (Chars), (Index), c_c( Cstr ) )
int64_t insert_chars_c( cVarChars dst, int64_t index, cChars src );

Via the macro INSERT_SLICE_C_ implemented function.

qsort_chars_c

void qsort_chars_c( cVarChars chars );

Via the macro QSORT_C_ implemented function.

remove_char_c

bool remove_char_c( cVarChars chars[static 1], int64_t pos );

Via the macro REMOVE_C_ implemented function.

reverse_chars_c

void reverse_chars_c( cVarChars chars );

Via the macro REVERSE_C_ implemented function.

rotate_chars_c

void rotate_chars_c( cVarChars chars, int64_t distance );

Via the macro ROTATE_C_ implemented function.

take_char_c

bool take_char_c( cVarChars chars[static 1], int64_t pos, char val[static 1] );

Via the macro TAKE_C_ implemented function.

trim

trim_any_char_c

#define trim_any_char_c_( Chars, Cstr )                                        \
   trim_any_char_c( (Chars), c_c( Cstr ) )
cChars trim_any_char_c( cChars chars, cChars set );

Returns a sub slice without all leading and trailing char values contained in set.

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

TEMP_SLICE_C_(
   test,
   {
      char const* inp;
      char const* set;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "..!Hello, clingo!..", "!.", "Hello, clingo" )
   );

   for_each_c_( test const*, t, tests )
   {
      cChars cs = trim_any_char_c_( c_c( t->inp ), t->set );
      bool res = chars_is_c( cs, t->exp );

      tap_descf_c( res, "'%s' -> '%s' -> '%s'", t->inp, t->set, t->exp );
   }

   return finish_tap_c_();
}

trim_any_char_left_c

#define trim_any_char_left_c_( Chars, Cstr )                                   \
   trim_any_char_left_c( (Chars), c_c( Cstr ) )
cChars trim_any_char_left_c( cChars chars, cChars set );

Returns a sub slice without all leading char values contained in set.

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

TEMP_SLICE_C_(
   test,
   {
      char const* inp;
      char const* set;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "..!Hello, clingo!..", "!.", "Hello, clingo!.." )
   );

   for_each_c_( test const*, t, tests )
   {
      cChars cs = trim_any_char_left_c_( c_c( t->inp ), t->set );
      bool res = chars_is_c( cs, t->exp );

      tap_descf_c( res, "'%s' -> '%s' -> '%s'", t->inp, t->set, t->exp );
   }

   return finish_tap_c_();
}

trim_any_char_right_c

#define trim_any_char_right_c_( Chars, Cstr )                                  \
   trim_any_char_right_c( (Chars), c_c( Cstr ) )
cChars trim_any_char_right_c( cChars chars, cChars set );

Returns a sub slice without all trailing char values contained in set.

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

TEMP_SLICE_C_(
   test,
   {
      char const* inp;
      char const* set;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "..!Hello, clingo!..", "!.", "..!Hello, clingo" ),
      t_( "18:03:12.010", "0", "18:03:12.01" )
   );

   for_each_c_( test const*, t, tests )
   {
      cChars cs = trim_any_char_right_c_( c_c( t->inp ), t->set );
      bool res = chars_is_c( cs, t->exp );

      tap_descf_c( res, "'%s' -> '%s' -> '%s'", t->inp, t->set, t->exp );
   }

   return finish_tap_c_();
}

trim_char_match_c

cChars trim_char_match_c( cChars chars, c_check_char check );

Returns a slice without all leading and trailing char values that pass the check.

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

#include <ctype.h>

TEMP_SLICE_C_(
   test,
   {
      char const* inp;
      c_check_char check;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "..!Hello, clingo!..", ispunct, "Hello, clingo" )
   );

   for_each_c_( test const*, t, tests )
   {
      cChars cs = trim_char_match_c( c_c( t->inp ), t->check );
      bool res = chars_is_c( cs, t->exp );

      tap_descf_c( res, "'%s' -> '%s'", t->inp, t->exp );
   }

   return finish_tap_c_();
}

trim_char_match_left_c

cChars trim_char_match_left_c( cChars chars, c_check_char check );

Returns a slice without all leading char values that pass the check.

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

#include <ctype.h>

TEMP_SLICE_C_(
   test,
   {
      char const* inp;
      c_check_char check;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "..!Hello, clingo!..", ispunct, "Hello, clingo!.." )
   );

   for_each_c_( test const*, t, tests )
   {
      cChars cs = trim_char_match_left_c( c_c( t->inp ), t->check );
      bool res = chars_is_c( cs, t->exp );

      tap_descf_c( res, "'%s' -> '%s'", t->inp, t->exp );
   }

   return finish_tap_c_();
}

trim_char_match_right_c

cChars trim_char_match_right_c( cChars chars, c_check_char check );

Returns a slice without all trailing char values that pass the check.

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

#include "clingo/io/print.h"

#include <ctype.h>

TEMP_SLICE_C_(
   test,
   {
      char const* inp;
      c_check_char check;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "..!Hello, clingo!..", ispunct, "..!Hello, clingo" )
   );

   for_each_c_( test const*, t, tests )
   {
      cChars cs = trim_char_match_right_c( c_c( t->inp ), t->check );
      bool res = chars_is_c( cs, t->exp );
      
      pjotln_c_( xyz, 128, c_c( t->inp ), " ", cs );

      tap_descf_c( res, "'%s' -> '%s'", t->inp, t->exp );
   }

   return finish_tap_c_();
}