write_type

Overview

Module with functions to write types in a text format into the recorder.

Important
Check the err value of the recorder if a function returns false.

Functions

basic

write_bool_c

#define write_bool_c_( Rec, Bool )                                             \
   write_bool_c( (Rec), (Bool), "" )
bool write_bool_c( cRecorder rec[static 1],
                   bool val,
                   char const fmt[static 1] );

Writes the bool value in a text format into the recorder. The function supports the following formats:

Table 1. format

l

lower-case representation

true or false

U

upper-case representation

TRUE or FALSE

Cc

camel-case representation

True or False

The function will use \'l' as default format.

Example
#include "clingo/io/write_type.h"
#include "clingo/lang/expect.h"

TEMP_SLICE_C_(
   test,
   {
      bool val;
      char const* fmt;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
   // default
      t_( true, "", "true" ),
      t_( false, "", "false" ),
   // lower case
      t_( true, "l", "true" ),
      t_( false, "l", "false" ),
   // upper case
      t_( true, "U", "TRUE" ),
      t_( false, "U", "FALSE" ),
   // camel case
      t_( true, "Cc", "True" ),
      t_( false, "Cc", "False" )
   );

   for ( int64_t i = 0; i < tests.s; ++i )
   {
      test t = tests.v[i];
      cRecorder* rec = &recorder_c_( 10 );

      bool res = write_bool_c( rec, t.val, t.fmt );
      res &= recorded_is_c( rec, t.exp );

      tap_descf_c( res, "test at index %"PRIi64, i );
   }

   return finish_tap_c_();
}

write_byte_c

#define write_byte_c_( Rec, Byte )                                             \
   write_byte_c( (Rec), (Byte), "" )
bool write_byte_c( cRecorder rec[static 1],
                   cByte byte,
                   char const fmt[static 1] );

Writes the byte value in a text format into the recorder. The function supports the following formats:

Table 2. format

x

lower-case hexadecimal value

0a

X

upper-case hexadecimal value

2D

b

lower-case bit value

oooo1oo1

B

upper-case bit value

11o1oo11

The function will use \'x' as default format.

Example
#include "clingo/io/write_type.h"
#include "clingo/lang/expect.h"

TEMP_SLICE_C_(
   test,
   {
      cByte val;
      char const* fmt;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
   // ""/"x"/"X"
      t_( 0xaF, "", "af" ),
      t_( 0xF, "x", "0f" ),
      t_( 0xaF, "X", "AF" ),
   // "b"/"B"
      t_( 0x7a, "b", "o1111o1o" ),
      t_( 0xa, "B", "00001010" )
   );

   for ( int64_t i = 0; i < tests.s; ++i )
   {
      test t = tests.v[i];
      cRecorder* rec = &recorder_c_( 16 );

      bool res = write_byte_c( rec, t.val, t.fmt );
      res &= recorded_is_c( rec, t.exp );

      tap_descf_c( res, "test at index %"PRIi64, i );
   }

   return finish_tap_c_();
}

write_char_c

#define write_char_c_( Rec, Char )                                             \
   write_char_c( (Rec), (Char), "" )
bool write_char_c( cRecorder rec[static 1],
                   char c,
                   char const fmt[static 1] );

Writes the char value into the recorder. The function supports the following formats:

Table 3. format

c

prints just the char

x

q

single quote the char

\'x'

Q

double quote the char

"x"

The function will use \'c' as default format.

Example
#include "clingo/io/write_type.h"
#include "clingo/lang/expect.h"

#define expect_recorded_( Rec, Exp )                                           \
{                                                                              \
   expect_c_(                                                                  \
      recorded_is_c( (Rec), (Exp) )                                            \
   );                                                                          \
   reset_recorder_c( Rec );                                                    \
}

int main( void )
{
   init_tap_c_();

   cRecorder* rec = &recorder_c_( 128 );

   expect_c_( write_char_c_( rec, 'a' ) );   // ""
   expect_recorded_( rec, "a" );

   expect_c_( write_char_c( rec, 'a', "c" ) );
   expect_recorded_( rec, "a" );

   expect_c_( write_char_c( rec, 'b', "q" ) );
   expect_recorded_( rec, "'b'" );

   expect_c_( write_char_c( rec, 'c', "Q" ) );
   expect_recorded_( rec, "\"c\"" );

   return finish_tap_c_();
}

write_range_c

#define write_range_c_( Rec, Rng )                                             \
   write_range_c( (Rec), (Rng), "" )
bool write_range_c( cRecorder rec[static 1],
                    cRange rng,
                    char const fmt[static 1] );

Writes a range in a text format into the recorder. The format can be "dbg" or a format string with the following syntax:

[format][/delimiter]

The fix format "dbg" writes the struct layout of a cRange into the recorder.

Table 4. format

c

closed range

[12..96]

o

open range

(11..97)

co

closed-open range

[12..97)

oc

open-closed range

(11..96]

The function will use \'c' as default format.

Table 5. delimiter

/,

use comma as delimiter

[12,96]

/;

use semicolon as delimiter

[12;96]

/|

use vertical line as delimiter

[12|96]

/..

use two points as delimiter

[12..96]

The function will use \'/..' as default delimiter.

Example
#include "clingo/io/write_type.h"
#include "clingo/io/write.h"
#include "clingo/lang/expect.h"

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

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
   // "" / "c" / "o" / "co" / "oc"
      t_( "", "[-123..45]" ),
      t_( "c", "[-123..45]" ),
      t_( "o", "(-122..44)" ),
      t_( "co", "[-123..44)" ),
      t_( "oc", "(-122..45]" ),
   // delimiter
      t_( "/,", "[-123,45]" ),
      t_( "co/;", "[-123;44)" ),
      t_( "o/|", "(-122|44)" ),
   // "dbg"
      t_( "dbg", "{ .min=-123, .max=45 }" )
   );

   for ( int64_t i = 0; i < tests.s; ++i )
   {
      test t = tests.v[i];
      cRecorder* rec = &recorder_c_( 128 );
      cRange rng = { .min=-123, .max=45 };

      bool res = write_range_c( rec, rng, t.fmt );
      res &= recorded_is_c( rec, t.exp );

      tap_descf_c( res, "test at index %"PRIi64, i );
   }

   return finish_tap_c_();
}

write_rune_c

#define write_rune_c_( Rec, Rune )                                             \
   write_rune_c( (Rec), (Rune), "" )
bool write_rune_c( cRecorder rec[static 1],
                   cRune rune,
                   char const fmt[static 1] );

Writes the rune value into the recorder. The function supports the following formats:

Table 6. format

s

just the rune

q

single quote the char

\'♘'

Q

double quote the char

"♘"

n

unicode number

U+2658

u8

the UTF-8 hex values

e29998

html

decimal HTML-code

htmlx

hex HTML-code

The function will use \'s' as default format.

Example
#include "clingo/io/write_type.h"
#include "clingo/lang/expect.h"

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

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
   // "" / "s"
      t_( "a", "", "a" ),
      t_( "🪓", "s", "🪓" ),
   // "q" /"Q"
      t_( "Z", "q", "'Z'" ),
      t_( "", "Q", "\"\"" ),
   // "n"
      t_( "Z", "n", "U+005A" ),
      t_( "", "n", "U+2658" ),
      t_( "🪓", "n", "U+1FA93" ),
   // "u8"
      t_( "Z", "u8", "5a" ),
      t_( "", "u8", "e29998" ),
      t_( "🪓", "u8", "f09faa93" ),
   // "html"
      t_( "Z", "html", "&#90;" ),
      t_( "", "html", "&#9816;" ),
      t_( "🪓", "html", "&#129683;" ),
   // "htmlx"
      t_( "Z", "htmlx", "&#x5A;" ),
      t_( "", "htmlx", "&#x2658;" ),
      t_( "🪓", "htmlx", "&#x1FA93;" )
   );

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

      bool res = write_rune_c( rec, rune_c( t->r ), t->fmt );
      res &= recorded_is_c( rec, t->exp );

      tap_descf_c( res, "test '%s' / '%s' / '%s'", t->r, t->fmt, t->exp );
   }

   return finish_tap_c_();
}

float

The format for all float values is "dbg" or follows this prototype:

[sign][type][precision][quote][cell]

The fix format "dbg" writes the sign, exponent and mantissa part of a float as struct.

Table 7. sign

+

Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers.

By default is the flag not set and only negative number are preceded with a \'-' sign.

Table 8. type

f

decimal floating point

392.65

e

lower-case scientific notation (mantissa/exponent)

3.9265e+2

E

upper-case scientific notation (mantissa/exponent)

3.9265E+2

g

use the shortest representation: %e or %f

392.65

G

use the shortest representation: %E or %F

392.65

x

lower-case hexadecimal floating point

-0xc.90fep-2

X

upper-case hexadecimal floating value

-0XC.90FEP-2

The function will use \'f' as default type.

Table 9. precision

.n

this is the number of digits to be printed after the decimal point(.n) or comma(,n)

Table 10. quote

q

single quote the value

\'747.34'

Q

double quote the value

"+123.45"

By default will the quote flag not be set and the value will not be quoted.

The optional cell part defines a cCell with the following Syntax:

([size][align][pad])

All three values are mandatory. The size value must be between 1 and 32767. The align value can be \'l', \'c' and \'r' and represent left, center and right text alignment. The final pad value is the char that will be used to fill up the cell.

write_double_c

#define write_double_c_( Double, Rec )                                         \
   write_double_c( (Double), (Rec), "" )
bool write_double_c( cRecorder rec[static 1],
                     double d,
                     char const fmt[static 1] );

Writes the double value in a text format into the recorder. The default format is "g.9".

Example
#include "clingo/io/write_type.h"
#include "clingo/lang/expect.h"
#include "clingo/lang/locale.h"

TEMP_SLICE_C_(
   test,
   {
      double val;
      char const* fmt;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   set_locale_c( LC_ALL, "C" );

   testSlice tests = slice_c_( test,
      t_( 2.71828, "", "2.71828" ),
      t_( 2.71828, "+E", "+2.718280E+00" ),
      t_( 2.71828, "x.1", "0x1.6p+1" ),
      t_( 2.71828, ".3q(10l.)", "'2.718'..." ),
      t_( 2.71828, "dbg", "{ .sign=0, .exponent=400, .mantissa=5bf0995aaf790 }" ),
   // one
      t_( 1, "e.5", "1.00000e+00" ),
      t_( 1, "f.5", "1.00000" ),
      t_( 1, "g.5", "1" ),
      t_( 1, "x.5", "0x1.00000p+0" ),
   // g conversion and zero suppression
      t_( 400, "g.2", "4e+02" ),
      t_( 40, "g.2", "40" ),
      t_( 4, "g.2", "4" ),
      t_( .4, "g.2", "0.4" ),
      t_( .04, "g.2", "0.04" ),
      t_( .004, "g.2", "0.004" ),
      t_( .0004, "g.2", "0.0004" ),
      t_( .00004, "g.2", "4e-05" ),
      t_( .000004, "g.2", "4e-06" ),
   // zero
      t_( 0, "e.5", "0.00000e+00" ),
      t_( 0, "f.5", "0.00000" ),
      t_( 0, "g", "0" ),
      t_( 0, "g", "0" ),
      t_( 0, "x.5", "0x0.00000p+0" ),
   // -1
      t_( -1, "e.5", "-1.00000e+00" ),
      t_( -1, "f,5", "-1,00000" ),
      t_( -1, "g.5", "-1" ),
   // 12
      t_( 12, "e.5", "1.20000e+01" ),
      t_( 12, "f.5", "12.00000" ),
      t_( 12, "g.5", "12" ),
   // 123456700
      t_( 123456700, "e.5", "1.23457e+08" ),
      t_( 123456700, "f.5", "123456700.00000" ),
      t_( 123456700, "g.5", "1.2346e+08" ),
   // 1.2345e6
      t_( 1.2345e6, "e.5", "1.23450e+06" ),
      t_( 1.2345e6, "f.5", "1234500.00000" ),
      t_( 1.2345e6, "g.5", "1.2345e+06" ),
   // 1e23
      t_( 1e23, "e.17", "9.99999999999999916e+22" ),
      t_( 1e23, "f.17", "99999999999999991611392.00000000000000000" ),
      t_( 1e23, "g.17", "9.9999999999999992e+22" ),
   // NaN & Inf
      t_( NAN, "", "nan" ),
      t_( -NAN, "", "nan" ),
      t_( INFINITY, "+", "+inf" ),
      t_( INFINITY, "", "inf" ),
      t_( -INFINITY, "", "-inf" ),
   // fixed bugs
      t_( 0.9, "f.1", "0.9" ),
      t_( 0.09, "f.1", "0.1" ),
      t_( 0.0999, "f.1", "0.1" ),
      t_( 0.05, "f,1", "0,1" ),
      t_( 0.05, "f.0", "0" ),
      t_( 0.5, "f,1", "0,5" ),
      t_( 0.5, "f.0", "0" ),
      t_( 1.5, "f.0", "2" ),
   // https://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
      t_( 2.2250738585072012e-308, "g.17", "2.2250738585072014e-308" ),
   // https://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
      t_( 2.2250738585072011e-308, "g.16", "2.225073858507201e-308" ),
   // mix
      t_( 383260575764816448.0, "f.0", "383260575764816448" ),
      t_( 383260575764816448.0, "e.16", "3.8326057576481645e+17" ),
      t_( 498484681984085570.0, "f.0", "498484681984085568" ),
      t_( -5.8339553793802237e+23, "e.16", "-5.8339553793802237e+23" ),
   // rounding
      t_( 2.275555555555555, "x", "0x1.23456789abcdep+1" ),
      t_( 2.275555555555555, "x.0", "0x1p+1" ),
      t_( 2.275555555555555, "x.2", "0x1.23p+1" ),
      t_( 2.275555555555555, "x.16", "0x1.23456789abcde000p+1" ),
      t_( 2.275555555555555, "x.21", "0x1.23456789abcde00000000p+1" ),
      t_( 2.2755555510520935, "x", "0x1.2345678p+1" ),
      t_( 2.2755555510520935, "x,6", "0x1,234567p+1" ),
      t_( 2.275555431842804, "x", "0x1.2345668p+1" ),
      t_( 2.275555431842804, "x.6", "0x1.234566p+1" ),
      t_( 3.999969482421875, "x", "0x1.ffffp+1" ),
      t_( 3.999969482421875, "x.4", "0x1.ffffp+1" ),
      t_( 3.999969482421875, "x.3", "0x2.000p+1" ),
      t_( 3.999969482421875, "x.2", "0x2.00p+1" ),
      t_( 3.999969482421875, "x.1", "0x2.0p+1" ),
      t_( 3.999969482421875, "x.1", "0x2.0p+1" )
   );

   for ( int64_t i = 0; i < tests.s; ++i )
   {
      test t = tests.v[i];
      cRecorder* rec = &recorder_c_( 128 );

      bool res = write_double_c( rec, t.val, t.fmt );
      res &= recorded_is_c( rec, t.exp );

      tap_descf_c( res, "fmt: %s / exp: %s / got: %s", t.fmt, t.exp, turn_into_cstr_c( rec ) );
   }

   return finish_tap_c_();
}

write_float_c

#define write_float_c_( Float, Rec )                                           \
   write_float_c( (Float), (Rec), "" )
bool write_float_c( cRecorder rec[static 1],
                    float f,
                    char const fmt[static 1] );

Writes the float value in a text format into the recorder. The default format is "g.17".

Example
#include "clingo/io/write_type.h"
#include "clingo/lang/expect.h"
#include "clingo/lang/locale.h"

TEMP_SLICE_C_(
   test,
   {
      float val;
      char const* fmt;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   set_locale_c( LC_ALL, "C" );

   testSlice tests = slice_c_( test,
   // PI
      t_( 3.14f, "", "3.1400001" ),
      t_( 3.14f, "e", "3.140000e+00" ),
      t_( 3.14f, "X.4", "0X1.91EBP+1" ),
      t_( 3.14f, "+,3q(10r.)", "..'+3,140'" ),
      t_( 3.14f, "dbg", "{ .sign=0, .exponent=80, .mantissa=48f5c3 }" )
   );

   for ( int64_t i = 0; i < tests.s; ++i )
   {
      test t = tests.v[i];
      cRecorder* rec = &recorder_c_( 128 );

      bool res = write_float_c( rec, t.val, t.fmt );
      res &= recorded_is_c( rec, t.exp );

      tap_descf_c( res, "test: %s", turn_into_cstr_c( rec ) );
   }

   return finish_tap_c_();
}

signed

The format for all signed integers follows this prototype:

[sign][type][quote][cell]
Table 11. sign

+

Forces to preceed the result with a plus or minus sign (+ or -) even for positive numbers.

By default is the flag not set and only negative number are preceded with a \'-' sign.

Table 12. type

d

decimal value

x

lower-case hexadecimal value

X

upper-case hexadecimal value

o

octal value

b

lower-case bit value

B

upper-case bit value

0x

memory in lower-case hexadecimal

0X

memory in upper-case hexadecimal

0o

memory in octal

0b

memory in lower-case bit

0B

memory in upper-case bit

The function will use \'d' as default type.

Table 13. quote

q

single quote the value

\'747'

Q

double quote the value

"+123"

By default will the quote flag not be set and the value will not be quoted.

The optional cell part defines a cCell with the following Syntax:

([size][align][pad])

All three values are mandatory. The size value must be between 1 and 32767. The align value can be \'l', \'c' and \'r' and represent left, center and right text alignment. The final pad value is the char that will be used to fill up the cell.

write_int16_c

#define write_int16_c_( Rec, Int16 )                                           \
   write_int16_c( (Rec), (Int16), "" )
bool write_int16_c( cRecorder rec[static 1],
                    int16_t i16,
                    char const fmt[static 1] );

Writes the int16_t value in a text format into the recorder.

Example
#include "clingo/io/write_type.h"
#include "clingo/lang/expect.h"

TEMP_SLICE_C_(
   test,
   {
      int16_t val;
      char const* fmt;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
   // ""/"d"
      t_( 18, "", "18" ),
      t_( 18, "d", "18" ),
   // "x"/"X"/"0x"/"0X"
      t_( 28, "x", "1c" ),
      t_( 28, "0x", "1c" ),
      t_( -28, "X", "-1C" ),
      t_( -28, "0X", "FFE4" ),
   // "o"/"0o"
      t_( 56, "o", "70" ),
      t_( 56, "0o", "70" ),
      t_( -56, "o", "-70" ),
      t_( -56, "0o", "177710" ),
   // "b"/"B"/"0b"/"0B"
      t_( 2821, "b", "1o11""ooooo1o1" ),
      t_( 2821, "0b", "1o11""ooooo1o1" ),
      t_( -2821, "B", "-1011""00000101" ),
      t_( -2821, "0B", "11110100""11111011" ),
   // sign
      t_( 18, "+", "+18" ),
      t_( 18, "+xq", "'+12'" ),
      t_( 18, "+0xq", "'12'" ), // sign does not work for 0? cases
   // quote
      t_( 18, "q", "'18'" ),
      t_( 18, "Q", "\"18\"" ),
   // cell
      t_( 18, "+Q(10c.)", "...\"+18\".." )
   );

   for ( int64_t i = 0; i < tests.s; ++i )
   {
      test t = tests.v[i];
      cRecorder* rec = &recorder_c_( 128 );

      bool res = write_int16_c( rec, t.val, t.fmt );
      res &= recorded_is_c( rec, t.exp );

      tap_descf_c( res, "test at index %"PRIi64, i );
   }

   return finish_tap_c_();
}

write_int32_c

#define write_int32_c_( Rec, Int32  )                                          \
   write_int32_c( (Rec), (Int32), "" )
bool write_int32_c( cRecorder rec[static 1],
                    int32_t i32,
                    char const fmt[static 1] );

Writes the in32_t value in a text format into the recorder.

Example
#include "clingo/io/write_type.h"
#include "clingo/lang/expect.h"

TEMP_SLICE_C_(
   test,
   {
      int32_t val;
      char const* fmt;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      // ""/"d"
      t_( 18, "", "18" ),
      t_( 18, "d", "18" ),
      // "x"/"X"/"0x"/"0X"
      t_( 1234567, "x", "12d687" ),
      t_( 1234567, "0x", "12d687" ),
      t_( -0x12d687, "X", "-12D687" ),
      t_( -0x12d687, "0X", "FFED2979" ),
      // "o"/"0o"
      t_( 123456, "o", "361100" ),
      t_( 123456, "0o", "361100" ),
      t_( -123456, "o", "-361100" ),
      t_( -123456, "0o", "37777416700" ),
      // "b"/"B"/"0b"/"0B"
      t_( 123456789, "b", "111""o1o11o11""11oo11o1""ooo1o1o1" ),
      t_( 123456789, "0b", "111""o1o11o11""11oo11o1""ooo1o1o1" ),
      t_( -123456789, "b", "-111""o1o11o11""11oo11o1""ooo1o1o1" ),
      t_( -123456789, "0B", "11111000""10100100""00110010""11101011" ),
      // sign
      t_( 190, "+", "+190" ),
      t_( -190, "+", "-190" ),
      t_( 190, "+X", "+BE" ),
      t_( -190, "+0X", "FFFFFF42" ), // sign does not work for 0? cases
      // quote
      t_( 1, "+dq", "'+1'" ),
      t_( 42, "+oQ", "\"+52\"" ),
      // cell
      t_( 12, "0x(8r0)", "0000000c" ),
      t_( 18, "+q(10c.)", "...'+18'.." )
   );

   for ( int64_t i = 0; i < tests.s; ++i )
   {
      test t = tests.v[i];
      cRecorder* rec = &recorder_c_( 128 );

      bool res = write_int32_c( rec, t.val, t.fmt );
      res &= recorded_is_c( rec, t.exp );

      tap_descf_c( res, "test at index %"PRIi64, i );
   }

   return finish_tap_c_();
}

write_int64_c

#define write_int64_c_( Rec, Int64 )                                           \
   write_int64_c( (Rec), (Int64), "" )
bool write_int64_c( cRecorder rec[static 1],
                    int64_t i64,
                    char const fmt[static 1] );

Writes the in64_t value in a text format into the recorder.

Example
#include "clingo/io/write_type.h"
#include "clingo/lang/expect.h"

TEMP_SLICE_C_(
   test,
   {
      int64_t val;
      char const* fmt;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
   // ""/"d"
      t_( 18, "", "18" ),
      t_( 18, "d", "18" ),
   // "x", "X"
      t_( 28, "x", "1c" ),
      t_( 0, "0X", "0" ),
      t_( 98765432123456789, "x", "15e""e2a3""21ce""7d15" ),
      t_( -98765432123456789, "X", "-15E""E2A3""21CE""7D15" ),
      t_( -98765432123456789, "0X", "FEA1""1D5C""DE31""82EB" ),
   // "o"
      t_( 56, "o", "70" ),
      t_( 56, "0o", "70" ),
      t_( -98765432123456789, "o", "-5367""05214""41634""76425" ),
      t_( -98765432123456789, "0o", "17""72410""72563""36143""01353" ),
   // "b"/"B"
      t_( 98765432123456789, "b", "1""o1o1111o""111ooo1o""1o1ooo11""oo1oooo1"
                                               "11oo111o""o11111o1""ooo1o1o1" ),
      t_( 98765432123456789, "0b", "1""o1o1111o""111ooo1o""1o1ooo11""oo1oooo1"
                                                "11oo111o""o11111o1""ooo1o1o1" ),
      t_( -98765432123456789, "B", "-1""01011110""11100010""10100011""00100001"
                                                 "11001110""01111101""00010101" ),
      t_( -98765432123456789, "0B", "11111110""10100001""00011101""01011100"
                                    "11011110""00110001""10000010""11101011" ),
   // sign
      t_( 18, "+d", "+18" ),
      t_( 18, "+bq", "'+1oo1o'" ),
      t_( 18, "+0bq", "'1oo1o'" ), // sign does not work for 0? cases
   // quote
      t_( 18, "q", "'18'" ),
      t_( 18, "Q", "\"18\"" ),
   // cell
      t_( 18, "dQ(10c-)","---\"18\"---" )
   );

   for ( int64_t i = 0; i < tests.s; ++i )
   {
      test t = tests.v[i];
      cRecorder* rec = &recorder_c_( 128 );

      bool res = write_int64_c( rec, t.val, t.fmt );
      res &= recorded_is_c( rec, t.exp );

      tap_descf_c( res, "test at index %"PRIi64, i );
   }

   return finish_tap_c_();
}

write_int8_C

#define write_int8_c_( Rec, Int8 )                                             \
   write_int8_c( (Rec), (Int8), "" )
bool write_int8_c( cRecorder rec[static 1],
                   int8_t i8,
                   char const format[static 1] );

Writes the int8_t value in a text format into the recorder.

Example
#include "clingo/io/write_type.h"
#include "clingo/lang/expect.h"

TEMP_SLICE_C_(
   test,
   {
      int8_t val;
      char const* fmt;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
   // ""/"d"
      t_( 18, "", "18" ),
      t_( 18, "d", "18" ),
   // "x"/"X"/"0x"/"0X"
      t_( 42, "x", "2a" ),
      t_( 42, "0x", "2a" ),
      t_( -42, "X", "-2A" ),
      t_( -42, "0X", "D6" ),
   // "o"/"0o"
      t_( 56, "o", "70" ),
      t_( 56, "0o", "70" ),
      t_( -56, "o", "-70" ),
      t_( -56, "0o", "310" ),
   // "b"/"B"/"0b"/"0B"
      t_( 12, "b", "11oo" ),
      t_( 12, "0b", "11oo" ),
      t_( -12, "B", "-1100" ),
      t_( -12, "0B", "11110100" ),
   // sign
      t_( 18, "+d", "+18" ),
      t_( 18, "+xq", "'+12'" ),
      t_( 18, "+0xq", "'12'" ), // sign does not work for 0? cases
   // quote
      t_( 18, "q", "'18'" ),
      t_( 18, "Q", "\"18\"" ),
   // cell
      t_( 18, "Q(10c-)", "---\"18\"---" )
   );

   for ( int64_t i = 0; i < tests.s; ++i )
   {
      test t = tests.v[i];
      cRecorder* rec = &recorder_c_( 128 );

      bool res = write_int8_c( rec, t.val, t.fmt );
      res &= recorded_is_c( rec, t.exp );

      tap_descf_c( res, "test at index %"PRIi64, i );
   }

   return finish_tap_c_();
}

unsigned

The format for all unsigned integers follows this prototype:

[sign-flag][type][quote][cell]
Table 14. sign-flag

+

Forces to preceed the result with a plus.

By default is the flag not set.

Table 15. type

d

decimal value

x

lower-case hexadecimal value

X

upper-case hexadecimal value

o

octal value

b

lower-case bit value

B

upper-case bit value

0x

memory in lower-case hexadecimal

0X

memory in upper-case hexadecimal

0o

memory in octal

0b

memory in lower-case bit

0B

memory in upper-case bit

The function will use \'d' as default type.

Table 16. quote

q

single quote the value

\'747'

Q

double quote the value

"+123"

By default will the quote flag not be set and the value will not be quoted.

The optional cell part defines a cCell with the following Syntax:

([size][align][pad])

All three values are mandatory. The size value must be between 1 and 32767. The align value can be \'l', \'c' and \'r' and represent left, center and right text alignment. The final pad value is the char that will be used to fill up the cell.

write_uint16_c

#define write_uint16_c_( Rec, Uint16 )                                         \
   write_uint16_c( (Rec), (Uint16), "" )
bool write_uint16_c( cRecorder rec[static 1],
                     uint16_t u16,
                     char const fmt[static 1] );

Writes the uint16_t value in a text format into the recorder.

Example
#include "clingo/io/write_type.h"
#include "clingo/lang/expect.h"

TEMP_SLICE_C_(
   test,
   {
      uint16_t val;
      char const* fmt;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
   // ""/"d"
      t_( 168, "", "168" ),
      t_( 168, "d", "168" ),
   // "x"/"X"/"0x"/"0X"
      t_( 1968, "x", "7b0" ),
      t_( 12968, "X", "32A8" ),
   // "o"/"0o"
      t_( 12968, "o", "31250" ),
   // "b"/"B"/"0b"/"0B"
      t_( 12968, "b", "11oo1o""1o1o1ooo" ),
      t_( 15968, "B", "111110""01100000" ),
   // sign
      t_( 534, "+d", "+534" ),
      t_( 15900, "+xq", "'+3e1c'" ),
      t_( 15900, "+0xq", "'3e1c'" ), // sign does not work for 0? cases
   // quote
      t_( 4596, "q", "'4596'" ),
      t_( 4596, "Q", "\"4596\"" ),
   // cell
      t_( 136, "dQ(10c-)", "---\"136\"--" )
   );

   for ( int64_t i = 0; i < tests.s; ++i )
   {
      test t = tests.v[i];
      cRecorder* rec = &recorder_c_( 128 );

      bool res = write_uint16_c( rec, t.val, t.fmt );
      res &= recorded_is_c( rec, t.exp );

      tap_descf_c( res, "test at index %"PRIi64, i );
   }

   return finish_tap_c_();
}

write_uint32_c

#define write_uint32_c_( Rec, Uint32 )                                         \
   write_uint32_c( (Rec), (Uint32), "" )
bool write_uint32_c( cRecorder rec[static 1],
                     uint32_t u32,
                     char const fmt[static 1] );

Writes the uint32_t value in a text format into the recorder.

Example
#include "clingo/lang/expect.h"
#include "clingo/io/write_type.h"

TEMP_SLICE_C_(
   test,
   {
      uint32_t val;
      char const* fmt;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
   // ""/"d"
      t_( 432109, "", "432109" ),
      t_( 432109, "d", "432109" ),
   // "x"/"X"/"0x"/"0X"
      t_( 432109, "x", "697ed" ),
      t_( 0xa7b7c7d7, "X", "A7B7""C7D7" ),
   // "o"/"0o"
      t_( 432109, "o", "15""13755" ),
   // "b"/"B"/"0b"/"0B"
      t_( 0x15ab88df, "b", "1o1o1""1o1o1o11""1ooo1ooo""11o11111" ),
      t_( 0x15ab88df, "B", "10101""10101011""10001000""11011111" ),
   // sign
      t_( 18, "+", "+18" ),
      t_( 18, "+xq", "'+12'" ),
      t_( 18, "+0xq", "'12'" ), // sign does not work for 0? cases
   // quote
      t_( 18, "dq", "'18'" ),
      t_( 18, "Q", "\"18\"" ),
   // cell
      t_( 18081, "Q(10c.)", "..\"18081\"." )
   );

   for ( int64_t i = 0; i < tests.s; ++i )
   {
      test t = tests.v[i];
      cRecorder* rec = &recorder_c_( 128 );

      bool res = write_uint32_c( rec, t.val, t.fmt );
      res &= recorded_is_c( rec, t.exp );

      tap_descf_c( res, "test at index %"PRIi64, i );
   }

   return finish_tap_c_();
}

write_uint64_c

#define write_uint64_c_( Rec, Uint64 )                                         \
   write_uint64_c( (Rec), (Uint64), "" )
bool write_uint64_c( cRecorder rec[static 1],
                     uint64_t u64,
                     char const fmt[static 1] );

Writes the uint64_t value in a text format into the recorder.

Example
#include "clingo/io/write_type.h"
#include "clingo/lang/expect.h"

TEMP_SLICE_C_(
   test,
   {
      uint64_t val;
      char const* fmt;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
   // ""/"d"
      t_( 7700883377, "", "7700883377" ),
      t_( 7700883377, "d", "7700883377" ),
   // "x"/"X"/"0x"/"0X"
      t_( 98765432123456789, "x", "15e""e2a3""21ce""7d15" ),
      t_( 0x15ee2a321ce7d15, "X", "15E""E2A3""21CE""7D15" ),
   // "o"/"0o"
      t_( 98765432123456789, "o", "5367""05214""41634""76425" ),
   // "b"/"B"/"0b"/"0B"
      t_( 0x8edcba9876543210, "b", "1ooo111o""11o111oo""1o111o1o""1oo11ooo"
                                   "o111o11o""o1o1o1oo""oo11oo1o""ooo1oooo" ),
      t_( 0x8e76543210, "B", "10001110""01110110""01010100""00110010""00010000" ),
   // sign
      t_( 839, "+d", "+839" ),
      t_( 839, "+x", "+347" ),
      t_( 839, "+0x", "347" ), // sign does not work for 0? cases
   // quote
      t_( 0xFFFFFFFFFFFFFFFF, "q", "'18446744073709551615'" ),
      t_( 0xF0E1D2C3B, "xQ", "\"f0e1d2c3b\"" ),
   // cell
      t_( 0xabcdef0123, "xq(6r-)", "'abcde" )
   );

   for ( int64_t i = 0; i < tests.s; ++i )
   {
      test t = tests.v[i];
      cRecorder* rec = &recorder_c_( 128 );

      bool res = write_uint64_c( rec, t.val, t.fmt );
      res &= recorded_is_c( rec, t.exp );

      tap_descf_c( res, "test at index %"PRIi64, i );
   }

   return finish_tap_c_();
}

write_uint8_c

#define write_uint8_c_( Rec, Uint8 )                                           \
   write_uint8_c( (Rec), (Uint8), "" )
bool write_uint8_c( cRecorder rec[static 1],
                    uint8_t u8,
                    char const fmt[static 1] );

Writes the uint8_t value in a text format into the recorder.

Example
#include "clingo/io/write_type.h"
#include "clingo/lang/expect.h"

TEMP_SLICE_C_(
   test,
   {
      uint8_t val;
      char const* fmt;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
   // ""/"d"
      t_( 18, "", "18" ),
      t_( 18, "d", "18" ),
   // "x"/"X"/"0x"/"0X"
      t_( 28, "x", "1c" ),
      t_( 0, "X", "0" ),
   // "o"/"0o"
      t_( 56, "o", "70" ),
   // "b"/"B"/"0b"/"0B"
      t_( 237, "b", "111o11o1" ),
      t_( 237, "B", "11101101" ),
   // sign
      t_( 18, "+", "+18" ),
      t_( 18, "+xq", "'+12'" ),
      t_( 18, "+0xq", "'12'" ), // sign does not work for 0? cases
   // quote
      t_( 18, "q", "'18'" ),
      t_( 18, "Q", "\"18\"" ),
   // cell
      t_( 78, "+q(10c )", "   '+78'  " )
   );

   for ( int64_t i = 0; i < tests.s; ++i )
   {
      test t = tests.v[i];
      cRecorder* rec = &recorder_c_( 128 );

      bool res = write_uint8_c( rec, t.val, t.fmt );
      res &= recorded_is_c( rec, t.exp );

      tap_descf_c( res, "test at index %"PRIi64, i );
   }

   return finish_tap_c_();
}

slice

write_bytes_c

#define write_bytes_c_( Rec, Bytes )                                           \
   write_bytes_c( (Rec), (Bytes), "" )
bool write_bytes_c( cRecorder rec[static 1],
                    cBytes bytes,
                    char const fmt[static 1] );

Writes the bytes from the slice in a text format into the recorder. The format string has the following parts:

[format][/[line][count][/chunk]]
Table 17. format

x

lower-case hexadecimal value

0a

X

upper-case hexadecimal value

2D

The function will use \'x' as default format for a byte.

The optional line value defines how much bytes a line should contain, the default value is 24.

Table 18. count

c

appends at the end of the line the number of bytes allready written

By default is the flag not set and no counter will be written.

The chunk value defines after how much bytes in a line a whitespace character should be added, the default value is 1.

Example
#include "clingo/io/write_type.h"
#include "clingo/lang/expect.h"

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

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "", "00 01 02 03 04 05 06 07 08 09 0a 0b "
              "0c 0d 0e 0f 10 11 12 13 14 15 16 17" ),
      t_( "/c", "00 01 02 03 04 05 06 07 08 09 0a 0b "
                "0c 0d 0e 0f 10 11 12 13 14 15 16 17 | 24" ),
      t_( "X/16c/4", "00010203 04050607 08090A0B 0C0D0E0F | 16\n"
                     "10111213 14151617 | 24" ),
      t_( "/20/0", "000102030405060708090a0b0c0d0e0f10111213\n"
                   "14151617" ),
      t_( "//8", "0001020304050607 08090a0b0c0d0e0f 1011121314151617" )
   );

   cBytes slice = slice_c_( cByte,
      0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
      0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
   );

   for ( int64_t i = 0; i < tests.s; ++i )
   {
      test t = tests.v[i];
      cRecorder* rec = &recorder_c_( 128 );

      bool res = write_bytes_c( rec, slice, t.fmt );
      res &= recorded_is_c( rec, t.exp );

      tap_descf_c( res, "test at index %"PRIi64, i );
   }

   return finish_tap_c_();
}

write_chars_c

#define write_chars_c_( Rec, Chars )                                           \
   write_chars_c( (Rec), (Chars), "" )
bool write_chars_c( cRecorder rec[static 1],
                    cChars chars,
                    char const fmt[static 1] );

Writes the char values from the slice into the recorder. The function supports the following formats:

Table 19. format

s

prints just the chars

x

q

single quote the slices

\'x'

Q

double quote the slices

"x"

e

double quoted C-string with escaped characters

"hello\nWorld!"

The function will use \'s' as default format.

Example
#include "clingo/io/write_type.h"
#include "clingo/lang/expect.h"

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

int main( void )
{
   init_tap_c_();

   testSlice tests = slice_c_( test,
      t_( "John", "", "John" ),
      t_( "Paul", "s", "Paul" ),
      t_( "George", "q", "'George'" ),
      t_( "Ringo", "Q", "\"Ringo\"" ),
      t_( "The \"Beatles\":\nJohn\tPaul\tGeorge\tRingo", "e",
          "\"The \\\"Beatles\\\":\\nJohn\\tPaul\\tGeorge\\tRingo\"" )
   );

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

      bool res = write_chars_c( rec, c_c( t->txt ), t->fmt );
      res &= recorded_is_c( rec, t->exp );

      tap_descf_c( res, "test with fmt: \"%s\"", t->fmt );
   }

   return finish_tap_c_();
}

write_cstr_c

#define write_cstr_c_( Rec, Str )                                              \
   write_cstr_c( (Rec), (Str), "" )
bool write_cstr_c( cRecorder rec[static 1],
                   char const str[static 1],
                   char const fmt[static 1] );

Util function that wraps write_chars_c.

write_recorded_c

bool write_recorded_c( cRecorder rec[static 1],
                       cRecorder const src[static 1],
                       char const fmt[static 1] );

Writes the recorded chars or bytes from src into the recorder. If the fmt value is empty or starts with cs will write_chars_c be used, if it starts with bs will write_bytes_c be used. After a / can the format for the concrete write function follow.

Example
#include "clingo/io/write_type.h"
#include "clingo/lang/expect.h"

TEMP_SLICE_C_(
   test,
   {
      cRecorder* src;
      char const* fmt;
      char const* exp;
   }
)
#define t_( ... ) ((test){__VA_ARGS__})

int main( void )
{
   init_tap_c_();

   cRecorder* hi = &recorder_c_( 128 );
   record_chars_c_( hi, "Hi!" );

   testSlice tests = slice_c_( test,
      t_( hi, "", "Hi!" ),
      t_( hi, "cs/q", "'Hi!'" ),
      t_( hi, "bs", "48 69 21" ),
      t_( hi, "bs///2", "4869 21" )
   );

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

      bool res = write_recorded_c( rec, t->src, t->fmt );
      res &= recorded_is_c( rec, t->exp );

      tap_descf_c( res, "test with fmt: \"%s\"", t->fmt );
   }

   return finish_tap_c_();
}

write_unscanned_c

#define write_unscanned_c_( Rec, Sca )                                         \
   write_unscanned_c( (Rec), (Sca), "" )
bool write_unscanned_c( cRecorder rec[static 1],
                        cScanner const sca[static 1],
                        char const fmt[static 1] );

Writes the unscanned chars or bytes from sca into the recorder. If the fmt value is empty or starts with cs will write_chars_c be used, if it starts with bs will write_bytes_c be used. After a / can the format for the concrete write function follow.