Skip to content

Commit

Permalink
Merge pull request #956 from ALANVF/map-and-regex-size
Browse files Browse the repository at this point in the history
Get size of map and regex captures
  • Loading branch information
hughsando authored May 31, 2021
2 parents a60f419 + 4df0a7c commit b797ae4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
5 changes: 5 additions & 0 deletions include/hx/StdLibs.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ HXCPP_EXTERN_CLASS_ATTRIBUTES String __hxcpp_utf8_string_to_char_bytes(String &i
#define HX_MAP_THIS_ARG Dynamic &ioHash
#endif

// --- HashRoot ---------------------------------------------------------------------

HXCPP_EXTERN_CLASS_ATTRIBUTES int __root_hash_size(Dynamic *rtHash);

// --- IntHash ----------------------------------------------------------------------

HXCPP_EXTERN_CLASS_ATTRIBUTES inline hx::Object *__int_hash_create() { return 0; }
Expand Down Expand Up @@ -533,6 +537,7 @@ HXCPP_EXTERN_CLASS_ATTRIBUTES Dynamic _hx_regexp_new_options(String s, String op
HXCPP_EXTERN_CLASS_ATTRIBUTES bool _hx_regexp_match(Dynamic handle, String string, int pos, int len);
HXCPP_EXTERN_CLASS_ATTRIBUTES String _hx_regexp_matched(Dynamic handle, int pos);
HXCPP_EXTERN_CLASS_ATTRIBUTES Dynamic _hx_regexp_matched_pos(Dynamic handle, int match);
HXCPP_EXTERN_CLASS_ATTRIBUTES int _hx_regexp_matched_num(Dynamic handle);


// haxe.zip.(Un)Compress.hx -> src/hx/libs/zlib/ZLib.cpp
Expand Down
12 changes: 11 additions & 1 deletion src/hx/Hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
using namespace hx;


// --- HashRoot ---------------------------------------------------

int __root_hash_size(Dynamic &rtHash)
{
HashRoot *hash = static_cast<HashRoot *>(rtHash.GetPtr());
if(!hash)
return 0;
return hash->getSize();
}


// --- IntHash ----------------------------------------------------

namespace
Expand Down Expand Up @@ -549,7 +560,6 @@ void __string_hash_clear(Dynamic &ioHash)




// --- ObjectHash ----------------------------------------------------


Expand Down
22 changes: 14 additions & 8 deletions src/hx/Hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,15 @@ template<typename T> inline void CopyValue(null &, const T &) { }
struct HashRoot : public Object
{
HashStore store;
int size;
int mask;
int bucketCount;

HX_IS_INSTANCE_OF enum { _hx_ClassId = hx::clsIdHash };

virtual void updateAfterGc() = 0;

inline int getSize() { return size; }
};

template<typename KEY>
Expand All @@ -230,6 +235,9 @@ struct HashBase : public HashRoot
HashBase(int inStore)
{
store = (HashStore)inStore;
size = 0;
mask = 0;
bucketCount = 0;
}


Expand Down Expand Up @@ -268,6 +276,11 @@ template<> struct ArrayValueOf<null> { typedef Dynamic Value; };
template<typename ELEMENT>
struct Hash : public HashBase< typename ELEMENT::Key >
{
using HashRoot::size;
using HashRoot::mask;
using HashRoot::bucketCount;
using HashRoot::getSize;

typedef typename ELEMENT::Key Key;
typedef typename ELEMENT::Value Value;
typedef typename ArrayValueOf<Value>::Value ArrayValue;
Expand All @@ -276,22 +289,15 @@ struct Hash : public HashBase< typename ELEMENT::Key >
enum { IgnoreHash = Element::IgnoreHash };


int size;
int mask;
int bucketCount;
ELEMENT **bucket;
ELEMENT **bucket;


Hash() : HashBase<Key>( StoreOf<Value>::store )
{
bucket = 0;
size = 0;
mask = 0;
bucketCount = 0;
if (ELEMENT::WeakKeys && Element::ManageKeys)
RegisterWeakHash(this);
}
inline int getSize() { return size; }

template<typename T>
bool TIsWeakRefValid(T &) { return true; }
Expand Down
15 changes: 15 additions & 0 deletions src/hx/libs/regexp/RegExp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,19 @@ Dynamic _hx_regexp_matched_pos(Dynamic handle, int m)
->setFixed(1,HX_("pos",94,5d,55,00),start);
}

/**
regexp_matched_num : 'regexp -> int
<doc>Return the total number of matched groups, or -1 if the regexp has not
been matched yet</doc>
**/
int _hx_regexp_matched_num(Dynamic handle)
{
pcredata *d = PCRE(handle);

if( !d->string.raw_ptr() )
return -1;
else
return d->nmatchs;
}


0 comments on commit b797ae4

Please sign in to comment.