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 macros SLICE_DEF_C_ and SLICE_IMPL_C_ declared and implemented struct. The macros declare and implement also the following functions.

/* init */
cChars chars_c( int64_t s, char const* v );
cChars make_chars_c( char const* beg, char const* end );
cChars empty_chars_c( void );
/* sub */
cChars left_chars_c( cChars chars, int64_t maxLen );
cChars mid_chars_c( cChars chars, int64_t index );
cChars right_chars_c( cChars chars, int64_t maxLen );
cChars sub_chars_c( cChars chars, int64_t begIdx, int64_t endIdx );

cVarChars

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

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

/* init */
cVarChars var_chars_c( int64_t s, char* v );
cVarChars make_var_chars_c( char* beg, char* end );
cVarChars empty_var_chars_c( void );
/* sub */
cVarChars left_var_chars_c( cVarChars chars, int64_t maxLen );
cVarChars mid_var_chars_c( cVarChars chars, int64_t index );
cVarChars right_var_chars_c( cVarChars chars, int64_t maxLen );
cVarChars sub_var_chars_c( cVarChars chars, int64_t begIdx, int64_t endIdx );
/* var slice */
cChars as_chars_c( cVarChars chars );
cVarChars cast_chars_c( cVarChars chars, cChars sub );
int64_t set_var_chars_c( cVarChars dst, cChars src );

cCharChunk

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

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_char_chunk_c( cCharChunk chunk[static 1],
                             int64_t s,
                             cChars chars );
void init_front_char_chunk_c( cCharChunk chunk[static 1],
                              int64_t s,
                              cChars chars );

cVarCharChunk

struct cVarCharChunk
{
   int64_t s;
   char* v;
   int64_t favSize;
   cVarChars slice;
};
typedef struct cVarCharChunk cVarCharChunk;

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_char_chunk_c( cVarCharChunk chunk[static 1],
                                 int64_t s,
                                 cVarChars chars );
void init_front_var_char_chunk_c( cVarCharChunk chunk[static 1],
                                  int64_t s,
                                  cVarChars chars );

cCharWindow

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

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_char_window_c( cCharWindow window[static 1],
                              int64_t s,
                              cChars chars );
void init_front_char_window_c( cCharWindow window[static 1],
                               int64_t s,
                               cChars chars );

cVarCharWindow

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

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_char_window_c( cVarCharWindow window[static 1],
                                  int64_t s,
                                  cVarChars chars );
void init_front_var_char_window_c( cVarCharWindow window[static 1],
                                   int64_t s,
                                   cVarChars chars );

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_chars_c( 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_chars_c( 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_chars_c( 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_chars_c( 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_chars_c( 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.

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.

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_DEF_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_DEF_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_DEF_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_DEF_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_DEF_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_DEF_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_();
}