Skip to content

Commit

Permalink
add string.appendChar(); op overload reserve array <<
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Dec 3, 2024
1 parent ec6ed83 commit 9dbf5c3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/core/chuck_lang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,12 @@ t_CKBOOL init_class_string( Chuck_Env * env, Chuck_Type * type )
func->doc = "set the character at the specified index.";
if( !type_engine_import_mfun( env, func ) ) goto error;

// add append()
func = make_new_mfun( "string", "appendChar", string_appendChar );
func->add_arg( "int", "theChar" );
func->doc = "append a character by its ASCII code and return a reference to itself; same as using the << operator `STR << INT`";
if( !type_engine_import_mfun( env, func ) ) goto error;

// add substring()
func = make_new_mfun( "string", "substring", string_substring );
func->add_arg("int", "start");
Expand Down Expand Up @@ -1075,7 +1081,7 @@ t_CKBOOL init_class_string( Chuck_Env * env, Chuck_Type * type )

// add toFloat()
func = make_new_mfun( "float", "toFloat", string_toFloat );
func->doc = "Attempt to convert the contents of the string to an float and return the result, or 0 if conversion failed.";
func->doc = "attempt to convert the contents of the string to an float and return the result, or 0 if conversion failed.";
if( !type_engine_import_mfun( env, func ) ) goto error;

// add parent()
Expand Down Expand Up @@ -2908,6 +2914,22 @@ CK_DLL_MFUN(string_setCharAt)
RETURN->v_int = str->str().at(index);
}

CK_DLL_MFUN(string_appendChar)
{
Chuck_String * str = (Chuck_String *)SELF;
t_CKINT the_char = GET_NEXT_INT(ARGS);
char c = (char)the_char;

// get the internal string representation
std::string s = str->str();
// append the character
s += c;
// set it back
str->set( s );
// return the string itself
RETURN->v_string = str;
}

CK_DLL_MFUN(string_substring)
{
Chuck_String * str = (Chuck_String *) SELF;
Expand Down
1 change: 1 addition & 0 deletions src/core/chuck_lang.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ CK_DLL_MFUN( string_set_at );
CK_DLL_MFUN( string_get_at );
CK_DLL_MFUN( string_charAt);
CK_DLL_MFUN( string_setCharAt);
CK_DLL_MFUN( string_appendChar );
CK_DLL_MFUN( string_substring);
CK_DLL_MFUN( string_substringN);
CK_DLL_MFUN( string_insert);
Expand Down
12 changes: 11 additions & 1 deletion src/core/chuck_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7801,7 +7801,17 @@ void type_engine_init_op_overload_builtin( Chuck_Env * env )
registry->reserve( env->ckt_float, ae_op_percent, env->ckt_float );
registry->reserve( env->ckt_time, ae_op_percent, env->ckt_dur );
registry->reserve( env->ckt_dur, ae_op_percent, env->ckt_dur );
// TODO: look into array << int/float/etc. appends
// array append << | 1.5.4.3 (ge) added
registry->reserve( env->ckt_array, ae_op_shift_left, env->ckt_int );
registry->reserve( env->ckt_array, ae_op_shift_left, env->ckt_float );
registry->reserve( env->ckt_array, ae_op_shift_left, env->ckt_dur );
registry->reserve( env->ckt_array, ae_op_shift_left, env->ckt_time );
registry->reserve( env->ckt_array, ae_op_shift_left, env->ckt_complex );
registry->reserve( env->ckt_array, ae_op_shift_left, env->ckt_polar );
registry->reserve( env->ckt_array, ae_op_shift_left, env->ckt_vec2 );
registry->reserve( env->ckt_array, ae_op_shift_left, env->ckt_vec3 );
registry->reserve( env->ckt_array, ae_op_shift_left, env->ckt_vec4 );
registry->reserve( env->ckt_array, ae_op_shift_left, env->ckt_object );

//-------------------------------------------------------------------------
// +=> -=> *=> /=>
Expand Down

0 comments on commit 9dbf5c3

Please sign in to comment.