cCharsToken

Overview

Module that defines the cCharsToken and functions to tokenize a cChars value.

Types and Definitions

cCharsToken

struct cCharsToken
{
   cChars x;
   cChars tail;
};
typedef struct cCharsToken cCharsToken;

cCharsToken allows to iterate over a cChars value. The x value stores the current token and tail stores the rest that will be tokenize.

Functions

init

init_chars_token_c

void init_chars_token_c( cCharsToken tok[static 1], cChars chars );

Initializes a cCharsToken with chars.

init_cstr_token_c

void init_cstr_token_c( cCharsToken tok[static 1], char const cstr[static 1] );

Initializes a cCharsToken with a C string.

next

next_token_till_any_c

#define next_token_till_any_c_( Tok, ... )                                     \
   next_token_till_any_c( (Tok), cs_c_( __VA_ARGS__ ) )
bool next_token_till_any_c( cCharsToken tok[static 1], cCharsSlice set );

Moves tok to the next token in the text that is split via one of the chars in the set. The function returns false if no other token exist in the text.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/type/cCharsToken.h"

TEMP_SLICE_DEF_C_(
   test,
   {
      char const* text;
      cCharsSlice set;
      bool ignoreEmpty;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

#include "clingo/io/print.h"
#define pln_( ... ) pjotln_c_( xyz, 1024, __VA_ARGS__ )

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "AlBeGaDeEpZeEtThIoKaLaMyNyXiOmPiRhSiTaYpPhChPsOm",
          cs_c_( "De", "Ep", "Xi", "Ta" ), true,
          "AlBeGa.ZeEtThIoKaLaMyNy.OmPiRhSi.YpPhChPsOm" ),
      t_( "AlBeGaDeEpZeEtThIoKaLaMyNyXiOmPiRhSiTaYpPhChPsOm",
          cs_c_( "De", "Ep", "Xi", "Ta" ), false,
          "AlBeGa..ZeEtThIoKaLaMyNy.OmPiRhSi.YpPhChPsOm" )
   );

   for_each_c_( test const*, t, tests )
   {
      cRecorder* rec = &recorder_c_( 128 );

      cCharsToken tok;
      init_cstr_token_c( &tok, t->text );
      while ( next_token_till_any_c( &tok, t->set ) )
      {
         if ( t->ignoreEmpty and is_empty_c_( tok.x ) ) continue;

         record_chars_c( rec, tok.x );
         if ( not is_last_token_c( &tok ) )
         {
            record_char_c( rec, '.' );
         }
      }
      bool res = recorded_is_c( rec, t->exp );
      tap_descf_c( res, "test expected: %s", t->exp );
   }

   return finish_tap_c_();
}

next_token_till_c

#define next_token_till_c_( Tok, Cstr )                                        \
   next_token_till_c( (Tok), c_c( Cstr ) )
bool next_token_till_c( cCharsToken tok[static 1], cChars deli );

Moves tok to the next token in the text that is split via deli value. The function returns false if no other token exist in the text.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/type/cCharsToken.h"

TEMP_SLICE_DEF_C_(
   test,
   {
      char const* text;
      char const* sep;
      bool ignoreEmtpy;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "a...long..text.with..dots", "..", true,
          "a;.long;text.with;dots", )
   );

   for_each_c_( test const*, t, tests )
   {
      cRecorder* rec = &recorder_c_( 128 );

      cCharsToken tok;
      init_cstr_token_c( &tok, t->text );
      while ( next_token_till_c_( &tok, t->sep ) )
      {
         if ( t->ignoreEmtpy and is_empty_c_( tok.x ) ) continue;

         record_chars_c( rec, tok.x );
         if ( not is_last_token_c( &tok ) )
         {
            record_char_c( rec, ';' );
         }
      }
      bool res = recorded_is_c( rec, t->exp );
      tap_descf_c( res, "test expected: %s", t->exp );
   }

   return finish_tap_c_();
}

next_token_till_any_char_c

#define next_token_till_any_char_c_( Tok, Cstr )                               \
   next_token_till_any_char_c( (Tok), c_c( Cstr ) )
bool next_token_till_any_char_c( cCharsToken tok[static 1], cChars set );

Moves tok to the next token in the text that is split via one of the char values in the set. The function returns false if no other token exist in the text.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/type/cCharsToken.h"

TEMP_SLICE_DEF_C_(
   test,
   {
      char const* text;
      char const* set;
      bool ignoreEmpty;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "- This, is a sample string.", " ,.-", true,
          "This;is;a;sample;string;" ),
      t_( "- This, is a sample string.", " ,.-", false,
          ";;This;;is;a;sample;string;" )
   );

   for_each_c_( test const*, t, tests )
   {
      cRecorder* rec = &recorder_c_( 128 );

      cCharsToken tok;
      init_cstr_token_c( &tok, t->text );
      while ( next_token_till_any_char_c_( &tok, t->set ) )
      {
         if ( t->ignoreEmpty and is_empty_c_( tok.x ) ) continue;

         record_chars_c( rec, tok.x );
         if ( not is_last_token_c( &tok ) )
         {
            record_char_c( rec, ';' );
         }
      }
      bool res = recorded_is_c( rec, t->exp );
      tap_descf_c( res, "test expected: %s", t->exp );
   }

   return finish_tap_c_();
}

next_token_till_char_c

bool next_token_till_char_c( cCharsToken tok[static 1], char deli );

Moves tok to the next token in the text that is split via deli value. The function returns false if no other token exist in the text.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/type/cCharsToken.h"

TEMP_SLICE_DEF_C_(
   test,
   {
      char const* text;
      char c;
      bool ignoreEmpty;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "comma,seperated,,values", ',', true, "comma;seperated;values"),
      t_( "comma,seperated,,values,", ',', true, "comma;seperated;values;")
   );

   for_each_c_( test const*, t, tests )
   {
      cRecorder* rec = &recorder_c_( 128 );

      cCharsToken tok;
      init_cstr_token_c( &tok, t->text );
      while ( next_token_till_char_c( &tok, t->c ) )
      {
         if ( t->ignoreEmpty and is_empty_c_( tok.x ) ) continue;

         record_chars_c( rec, tok.x );
         if ( not is_last_token_c( &tok ) )
         {
            record_char_c( rec, ';' );
         }
      }
      bool res = recorded_is_c( rec, t->exp );
      tap_descf_c( res, "test expected: %s", t->exp );
   }

   return finish_tap_c_();
}

next_token_till_any_rune_c

#define next_token_till_any_rune_c_( Tok, Cstr )                               \
   next_token_till_any_rune_c( (Tok), c_c( Cstr ) )
bool next_token_till_any_rune_c( cCharsToken tok[static 1], cChars set );

Moves tok to the next token in the text that is split via one of the runes in the set. The function returns false if no other token exist in the text.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/type/cCharsToken.h"

TEMP_SLICE_DEF_C_(
   test,
   {
      char const* text;
      char const* r;
      bool ignoreEmtpy;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "🤚🤣🖖🤣🤙🤣🤟&🤘🤣🤞", "🤣", true,
          "🤚;🖖;🤙;🤟&🤘;🤞" )
   );

   for_each_c_( test const*, t, tests )
   {
      cRecorder* rec = &recorder_c_( 128 );

      cCharsToken tok;
      init_cstr_token_c( &tok, t->text );
      while ( next_token_till_rune_c_( &tok, t->r ) )
      {
         if ( t->ignoreEmtpy and is_empty_c_( tok.x ) ) continue;

         record_chars_c( rec, tok.x );
         if ( not is_last_token_c( &tok ) )
         {
            record_char_c( rec, ';' );
         }
      }
      bool res = recorded_is_c( rec, t->exp );
      tap_descf_c( res, "test expected: %s", t->exp );
   }

   return finish_tap_c_();
}

next_token_till_rune_c

#define next_token_till_rune_c_( Tok, Cstr )                                   \
   next_token_till_rune_c( (Tok), c_c( Cstr ) )
bool next_token_till_rune_c( cCharsToken tok[static 1], cRune deli );

Moves tok to the next token in the text that is split via deli value. The function returns false if no other token exist in the text.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/type/cCharsToken.h"

TEMP_SLICE_DEF_C_(
   test,
   {
      char const* text;
      char const* r;
      bool ignoreEmtpy;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "🤚🤣🖖🤣🤙🤣🤟&🤘🤣🤞", "🤣", true,
          "🤚;🖖;🤙;🤟&🤘;🤞" )
   );

   for_each_c_( test const*, t, tests )
   {
      cRecorder* rec = &recorder_c_( 128 );

      cCharsToken tok;
      init_cstr_token_c( &tok, t->text );
      while ( next_token_till_rune_c_( &tok, t->r ) )
      {
         if ( t->ignoreEmtpy and is_empty_c_( tok.x ) ) continue;

         record_chars_c( rec, tok.x );
         if ( not is_last_token_c( &tok ) )
         {
            record_char_c( rec, ';' );
         }
      }
      bool res = recorded_is_c( rec, t->exp );
      tap_descf_c( res, "test expected: %s", t->exp );
   }

   return finish_tap_c_();
}

prop

is_last_token_c

bool is_last_token_c( cCharsToken const tok[static 1] );

Returns true if the current token is the last token in the text.

token_delimiter_c

cChars token_delimiter_c( cCharsToken const tok[static 1] );

Returns the current delimiter between the t and the tail value.

Example
#include "clingo/io/cRecorder.h"
#include "clingo/lang/expect.h"
#include "clingo/type/cCharsToken.h"

TEMP_SLICE_DEF_C_(
   test,
   {
      char const* text;
      char const* set;
      bool ignoreEmpty;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "- This, is-a sample string.", " ,.-", false, "- , -  ." )
   );

   for_each_c_( test const*, t, tests )
   {
      cRecorder* rec = &recorder_c_( 128 );

      cCharsToken tok;
      init_cstr_token_c( &tok, t->text );
      while ( next_token_till_any_char_c_( &tok, t->set ) )
      {
         if ( t->ignoreEmpty and is_empty_c_( tok.x ) ) continue;

         record_chars_c( rec, token_delimiter_c( &tok ) );
      }
      bool res = recorded_is_c( rec, t->exp );
      tap_descf_c( res, "test expected: %s", t->exp );
   }

   return finish_tap_c_();
}