Skip to content

Commit

Permalink
modify string array type detection
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Oct 26, 2024
1 parent e671059 commit 64c447c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
2 changes: 2 additions & 0 deletions VERSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ ChucK VERSIONS log
are distinguished from windows drive letters (e.g., "A:\")
- (fixed) Type.of() now reports more specific array type (e.g., `string[]`) instead of
generic `@array`
- (updated) calling .sort() on an array of strings will sort by string
(instead of Object pointers)
- (updated, internal) dynamic array types are now cached and reused, preventing potential
memory build-up across compilations

Expand Down
34 changes: 19 additions & 15 deletions src/core/chuck_oo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,26 +949,30 @@ static bool ck_compare_string( t_CKUINT lhs, t_CKUINT rhs )
//-----------------------------------------------------------------------------
void Chuck_ArrayInt::sort()
{
if (size() == 0) return;

// if object references
if( m_is_obj ) {
// azaday 10/24/2024
// HACK:
// Chuck Type system reports all object arrays are of type @array
// Somehow this diverges from .typeOf(), which correctly returns "string[]"
// instead we try dynamic_casting the first element to Chuck_String
// to determine if this is a string array
// if this is a string array, sort as strings
if( dynamic_cast<Chuck_String*>((Chuck_Object*)m_vector[0]) ) {
// check size
if( size() == 0 ) return;

// if object references | 1.5.3.5 (azaday) added
if( m_is_obj )
{
// if this is a string[]
if( this->type_ref->array_depth == 1 && this->type_ref->base_name == "string" )
{
// sort as string array
std::sort( m_vector.begin(), m_vector.end(), ck_compare_string );
} else {
}
else // not string object array
{
// sort object pointers as unsigned ints
std::sort( m_vector.begin(), m_vector.end() );
}
}
// if not object references, sort as signed ints
else std::sort( m_vector.begin(), m_vector.end(), ck_compare_sint );
// if not object references
else
{
// sort as signed ints
std::sort( m_vector.begin(), m_vector.end(), ck_compare_sint );
}
}


Expand Down

0 comments on commit 64c447c

Please sign in to comment.