CStringBuilder

Overview

Module that defines the CStringBuilder and functions to build a string.

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

int main( void )
{
   init_tap_c_();

   CStringBuilder* builder = retain_c( make_string_builder_c( 10 ) );
   cRecorder* rec = &recorder_c_( 128 );

   expect_c_( string_builder_cap_c( builder ) == 10 );
   expect_c_( string_builder_space_c( builder ) == 10 );

   expect_c_( append_cstr_c( builder, "a " ) );
   expect_c_( append_rune_c( builder, rune_c( "" ) ) );
   expect_c_( append_cstr_c( builder, " with " ) );

   write_int8_c_( rec, 2 );
   expect_c_( append_recorded_c( builder, rec ) );
   expect_c_( append_cstr_c( builder, " Windows" ) );
   expect_c_( append_char_c( builder, '!' ) );

   cChars built = built_chars_c( builder );
   expect_c_( chars_is_c( built, "a ⌂ with 2 Windows!" ) );

   CString* str = turn_into_string_c( builder );
   expect_c_( string_is_c( str, "a ⌂ with 2 Windows!" ) );
   expect_c_( string_builder_cap_c( builder ) == 0 );
   expect_c_( string_builder_byte_length_c( builder ) == 0 );

   release_all_c_( builder, str );

   return finish_tap_c_();
}

Types and Definitions

C_StringBuilderMeta

cMeta const C_StringBuilderMeta;

Stores the cMeta instance that all CStringBuilder instances reference.

CStringBuilder

struct CStringBuilder
typedef struct CStringBuilder CStringBuilder

Abstract type that represents a string builder.

Functions

create

make_string_builder_c

CStringBuilder* make_string_builder_c( int64_t cap );

Creates a string builder instance. The cap value defines how much the initiale capacity should be.

new_string_builder_c

CStringBuilder* new_string_builder_c();

Creates a string builder instance.

manage

reset_string_builder_c

void reset_string_builder_c( CStringBuilder* builder );

Resets the builder.

resize_string_builder_c

bool resize_string_builder_c( CStringBuilder* builder, int64_t cap );

Allows to grow or reduce the capacity of a string builder.

string_builder_byte_length_c

int64_t string_builder_byte_length_c( CStringBuilder* builder );

Returns the byte length of the built string without the '\0' character at the end.

string_builder_cap_c

int64_t string_builder_cap_c( CStringBuilder* builder );

Returns the capacity of the string builder.

string_builder_length_c

int64_t string_builder_length_c( CStringBuilder* builder );

Returns the number of runes of the built string without the '\0' character at the end.

string_builder_space_c

int64_ string_builder_space_c( CStringBuilder* builder );

Returns the number of bytes that are not allready used.

access

built_chars_c

cChars built_chars_c( CStringBuilder const* builder );

Returns the built string as chars.

build_cstr_c

char const* built_cstr_c( CStringBuilder const* builder );

Returns the built string as C string.

turn_into_string_c

CString* turn_into_string_c( CStringBuilder* builder );

Converts the built string to a CString instance. The builder transfers the ownership of the internal C string to the CString and the builder will have 0 capacity.

append

append_char_c

bool append_char_c( CStringBuilder* builder, char c );

Appends a char to the string builder.

append_chars_c

bool append_chars_c( CStringBuilder* builder, cChars chars );

Appends chars to the string builder.

append_cstr_c

bool append_cstr_c( CStringBuilder* builder, char const cstr[static 1] );

Appends a C string to the string builder.

append_recorded_c

bool append_recorded_c( CStringBuilder* builder, cRecorder rec[static 1] );

Appends the recorded chars to the string builder.

append_rune_c

bool append_rune_c( CStringBuilder* builder, cRune r );

Appends a rune to the string builder.

append_string_c

bool append_string_c( CStringBuilder* builder, CString const* str );

Appends a string to the string builder.