CString
Overview
Module that defines the CBitVec type and the associated functions.
Types and Definitions
C_StringMeta
cMeta const C_StringMeta;
Stores the cMeta instance that all CString instances reference.
CString
struct CString; typedef struct CString CString;
CString stores a utf8-encoded string. The actuall string is immutable.
Functions
create
adopt_cstr_c
CString* adopt_cstr_c( char cstr[static 1] );
Creates a string that takes ownership of C string.
#include "clingo/lang/expect.h" #include "clingo/string/CString.h" int main( void ) { init_tap_c_(); char* cstr = alloc_c( 4 ); cstr[ 0 ] = 'a'; cstr[ 1 ] = 'b'; cstr[ 2 ] = 'c'; cstr[ 3 ] = '\0'; CString* str = adopt_cstr_c( cstr ); expect_c_( string_is_c( str, "abc" ) ); expect_c_( string_cstr_c( str ) == cstr ); release_c( str ); return finish_tap_c_(); }
empty_string_c
CString* empty_string_c();
Creates a empty string. All empty string instances share the same instance.
#include "clingo/lang/expect.h" #include "clingo/string/CString.h" int main( void ) { init_tap_c_(); CString* e1 = empty_string_c(); CString* e2 = str_c( "" ); CString* e3 = new_string_c( "" ); CString* e4 = make_string_c( empty_chars_c() ); expect_c_( string_is_empty_c( e1 ) ); expect_c_( string_is_empty_c( e2 ) ); expect_c_( string_is_empty_c( e3 ) ); expect_c_( string_is_empty_c( e4 ) ); expect_c_( e1 == e2 ); expect_c_( e2 == e3 ); expect_c_( e3 == e4 ); return finish_tap_c_(); }
make_string_c
CString* make_string_c( cChars chars );
Creates a string with the content of chars.
new_string_c
CString* new_string_c( char const cstr[static 1] );
Creates a string that equals the C string cstr.
str_c
CString* str_c( char const cstr[static 1] );
Creates a string that wraps cstr. The C string will not be freed in this case. It is recomended to use this function for string literals.
#include "clingo/lang/expect.h" #include "clingo/string/CString.h" int main( void ) { init_tap_c_(); char const* cstr = "banjo"; CString* str = str_c( cstr ); expect_c_( string_is_c( str, "banjo" ) ); expect_c_( string_cstr_c( str ) == cstr ); release_c( str ); return finish_tap_c_(); }
prop
is_ascii_string_c
bool is_ascii_string_c( CString const* str );
Returns true if all runes in str represent a ASCII character, otherwise false.
#include "clingo/lang/expect.h" #include "clingo/string/CString.h" int main( void ) { init_tap_c_(); CString* ascii = str_c( "simple ascii text" ); expect_c_( is_ascii_string_c( ascii ) ); release_c( ascii ); CString* utf8 = str_c( "АᏂ 🚀 Ω" ); expect_c_( not is_ascii_string_c( utf8 ) ); release_c( utf8 ); CString* euro = str_c( "€uro" ); expect_c_( not is_ascii_string_c( euro ) ); release_c( euro ); return finish_tap_c_(); }
string_byte_length_c
int64_t string_byte_length_c( CString const* str );
Returns the number of bytes a string requires, without the \0 character at the end.
string_chars_c
cChars string_chars_c( CString const* str );
Returns the internal C string as chars.
#include "clingo/lang/expect.h" #include "clingo/string/CString.h" int main( void ) { init_tap_c_(); CString* s = str_c( "foobar" ); cChars chars = string_chars_c( s ); expect_c_( chars_is_c( chars, "foobar" ) ); release_c( s ); return finish_tap_c_(); }
string_cstr_c
char const* string_cstr_c( CString const* str );
Returns a pointer to the internal C string.
#include "clingo/lang/expect.h" #include "clingo/string/CString.h" int main( void ) { init_tap_c_(); CString* s = new_string_c( "foobar" ); const char* cstr = string_cstr_c( s ); cChars chars = c_c( cstr ); expect_c_( chars_is_c( chars, "foobar" ) ); release_c( s ); return finish_tap_c_(); }
string_is_empty_c
bool string_is_empty_c( CString const* str );
Returns true if the str is empty.
#include "clingo/lang/expect.h" #include "clingo/string/CString.h" int main( void ) { init_tap_c_(); CString* str = str_c( "something" ); CString* empty = empty_string_c(); CString* nothing = new_string_c( "" ); expect_c_( not string_is_empty_c( str ) ); expect_c_( string_is_empty_c( empty ) ); expect_c_( string_is_empty_c( nothing ) ); release_c( str ); return finish_tap_c_(); }
string_length_c
int64_t string_length_c( CString const* str );
Returns the number of runes in str.
#include "clingo/lang/expect.h" #include "clingo/string/CString.h" int main( void ) { init_tap_c_(); CString* ascii = str_c( "simple ascii text" ); expect_c_( string_length_c( ascii ) == 17 ); release_c( ascii ); CString* utf8 = str_c( "АᏂ 🚀 Ω" ); expect_c_( string_length_c( utf8 ) == 6 ); release_c( utf8 ); CString* euro = str_c( "€uro" ); expect_c_( string_length_c( euro ) == 4 ); release_c( euro ); return finish_tap_c_(); }
cmp
cmp_string_c
int cmp_string_c( CString const* a, CString const* b );
Compares two strings and returns the three possible results:
- <0
-
means that a is less compared to b
- 0
-
means that a and b are equal
- >0
-
means that a is greater compared to ba
eq_string_c
bool eq_string_c( CString const* a, CString const* b );
Returns true if both strings are equal, otherwise false.
hash_string_c
uint64_t hash_string_c( CString const* str );
Retuns the hash value of a string.
string_is_c
bool string_is_c( CString const* str, char const cstr[static 1] );
Function that compares the internal C string of a string with cstr.
#include "clingo/lang/expect.h" #include "clingo/string/CString.h" int main( void ) { init_tap_c_(); CString* ascii = str_c( "simple ascii text" ); expect_c_( string_is_c( ascii, "simple ascii text" ) ); expect_c_( !string_is_c( ascii, "simple asci text" ) ); release_c( ascii ); CString* utf8 = str_c( "АᏂ 🚀 Ω" ); expect_c_( string_is_c( utf8, "АᏂ 🚀 Ω" ) ); expect_c_( !string_is_c( utf8, "А 🚀 Ω" ) ); release_c( utf8 ); CString* euro = str_c( "€uro" ); expect_c_( string_is_c( euro, "€uro" ) ); expect_c_( !string_is_c( euro, "Euro" ) ); release_c( euro ); return finish_tap_c_(); }
sub
left_string_c
cChars left_string_c( CString const* s, int64_t maxLen );
Returns the leftmost runes of a string as chars. If maxLen is greater or equal the length of the string, contains chars all runes from the string.
new_left_string_c
CString* new_left_string_c( CString const* s, int64_t maxLen );
Returns the leftmost runes of a string as a new string. If maxLen is greater or equal the length of the string, contains the new string all runes from the string.
mid_string_c
cChars mid_string_c( CString const* s, int64_t index );
Returns a chars starting(including) at a given rune index. If index does not point into chars returns the function an empty slice.
new_mid_string_c
CString* new_mid_string_c( CString const* s, int64_t maxLen );
Returns a new string starting(including) at a given rune index. If index does not point into the slice returns the function an empty string.
right_string_c
cChars right_string_c( CString const* s, int64_t maxLen );
Returns the rightmost runes of a string as chars. If maxLen is greater or equal the length of the string, contains chars all runes from the string.
new_right_string_c
CString* new_right_string_c( CString const* s, int64_t maxLen );
Returns the rightmost runes of a string as a new string. If maxLen is greater or equal the length of the string, contains the new string all runes from the string.
sub_string_c
cChars sub_string_c( CString const* s, int64_t min, int64_t max );
Returns a sub part of the string as chars. The returned chars includes the rune at begIdx(begin index) as first value and the rune at endIdx-1 as last value. If begIdx or endIdx are not valid rune indices returns the function a empty slice.
new_sub_string_c
CString* new_sub_string_c( CString const* s, int64_t min, int64_t max );
Returns a sub part of the string as a new string. The returned string includes the rune at begIdx(begin index) as first value and the rune at endIdx-1 as last value. If begIdx or endIdx are not valid rune indices returns the function a empty string.