Example #1
0
	bool Trie::DeleteItem(NodeFrm::POINTER node, const Location& location){

		Location location1 = node->location_;
		if (location1.length() > location.length()){
			return false;
		}

		if (location == location1){
			node->MarkRemove();
			return true;
		}

		Location common = CommonPrefix(location1, location);

		int branch = NextBranch(common, location);
		NodeFrm::POINTER node2 = ChildMayFromDB(node, branch);
		if (node2 == nullptr){
			return false;
		}

		Location location2 = node2->location_;
		Location common2 = CommonPrefix(location2, location);
		if (location2 != common2){
			return false;
		}
		bool ret = DeleteItem(node2, location);
		node->modified_ |= ret;
		return ret;
	}
PRBool
nsAbAutoCompleteSession::CheckEntry(nsAbAutoCompleteSearchString* searchStr,
                                    const PRUnichar* nickName,
                                    const PRUnichar* displayName,
                                    const PRUnichar* firstName,
                                    const PRUnichar* lastName,
                                    const PRUnichar* emailAddress)
{
    const PRUnichar * fullString;
    PRUint32 fullStringLen;
    PRBool isAMatch = PR_FALSE;

    if (searchStr->mFirstPartLen > 0 && searchStr->mSecondPartLen == 0)
    {
        fullString = searchStr->mFirstPart;
        fullStringLen = searchStr->mFirstPartLen;
    }
    else
    {
        fullString = searchStr->mFullString;
        fullStringLen = searchStr->mFullStringLen;
    }

    nsDependentString fullStringStr(fullString, fullStringLen);

    // Compare various properties looking for a match (exact or partial)
    if ( (nickName &&
            fullStringStr.Equals(nsDependentString(nickName), nsCaseInsensitiveStringComparator())) ||
            (displayName &&
             fullStringStr.Equals(nsDependentString(displayName), nsCaseInsensitiveStringComparator())) ||
            (firstName &&
             fullStringStr.Equals(nsDependentString(firstName), nsCaseInsensitiveStringComparator())) ||
            (lastName &&
             fullStringStr.Equals(nsDependentString(lastName), nsCaseInsensitiveStringComparator())) ||
            (emailAddress &&
             fullStringStr.Equals(nsDependentString(emailAddress), nsCaseInsensitiveStringComparator())) ||
            (nickName && CommonPrefix(nickName, fullString, fullStringLen)) ||
            (displayName && CommonPrefix(displayName, fullString, fullStringLen)) ||
            (firstName && CommonPrefix(firstName, fullString, fullStringLen)) ||
            (lastName && CommonPrefix(lastName, fullString, fullStringLen)) ||
            (emailAddress && CommonPrefix(emailAddress, fullString, fullStringLen)) )
        isAMatch = PR_TRUE;
    //If we have a muti-part search string, look for a partial match with first name and last name or reverse
    else if (searchStr->mFirstPartLen && searchStr->mSecondPartLen)
    {
        if (((firstName && CommonPrefix(firstName, searchStr->mFirstPart, searchStr->mFirstPartLen)) &&
                (lastName && CommonPrefix(lastName, searchStr->mSecondPart, searchStr->mSecondPartLen))) ||
                ((lastName && CommonPrefix(lastName, searchStr->mFirstPart, searchStr->mFirstPartLen)) &&
                 (firstName && CommonPrefix(firstName, searchStr->mSecondPart, searchStr->mSecondPartLen))))
            isAMatch = PR_TRUE;
    }

    return isAMatch;
}
nsresult nsAbAutoCompleteSession::SearchPreviousResults(nsAbAutoCompleteSearchString *searchStr, nsIAutoCompleteResults *previousSearchResult, nsIAutoCompleteResults* results)
{
    if (!previousSearchResult)
        return NS_ERROR_NULL_POINTER;

    nsXPIDLString prevSearchString;
    nsresult rv;

    rv = previousSearchResult->GetSearchString(getter_Copies(prevSearchString));
    NS_ENSURE_SUCCESS(rv, rv);

    if (!(const PRUnichar*)prevSearchString || ((const PRUnichar*)prevSearchString)[0] == 0)
        return NS_ERROR_FAILURE;

    PRUint32 prevSearchStrLen = nsCRT::strlen(prevSearchString);
    if (searchStr->mFullStringLen < prevSearchStrLen ||
            CommonPrefix(searchStr->mFullString, prevSearchString, prevSearchStrLen))
        return NS_ERROR_ABORT;

    nsCOMPtr<nsISupportsArray> array;
    rv = previousSearchResult->GetItems(getter_AddRefs(array));
    if (NS_SUCCEEDED(rv))
    {
        PRUint32 nbrOfItems;
        PRUint32 i;
        PRUint32 pos;

        rv = array->Count(&nbrOfItems);
        if (NS_FAILED(rv) || nbrOfItems <= 0)
            return NS_ERROR_FAILURE;

        nsCOMPtr<nsISupports> item;
        nsCOMPtr<nsIAutoCompleteItem> resultItem;
        nsAbAutoCompleteParam *param;

        for (i = 0, pos = 0; i < nbrOfItems; i ++, pos ++)
        {
            rv = array->QueryElementAt(pos, NS_GET_IID(nsIAutoCompleteItem),
                                       getter_AddRefs(resultItem));
            NS_ENSURE_SUCCESS(rv, rv);

            rv = resultItem->GetParam(getter_AddRefs(item));
            NS_ENSURE_SUCCESS(rv, rv);
            if (!item)
                return NS_ERROR_FAILURE;

            param = (nsAbAutoCompleteParam *)(void *)item;

            if (CheckEntry(searchStr, param->mNickName, param->mDisplayName,  param->mFirstName,  param->mLastName, param->mEmailAddress))
                AddToResult(param->mNickName, param->mDisplayName,
                            param->mFirstName, param->mLastName,
                            param->mEmailAddress, param->mNotes,
                            param->mDirName, param->mPopularityIndex, param->mIsMailList, PR_FALSE,
                            results);
        }
        return NS_OK;
    }

    return NS_ERROR_ABORT;
}
Example #4
0
	bool Trie::Exists(NodeFrm::POINTER node, const Location& key) {

		if (node->location_ == key)
			return true;

		auto common = CommonPrefix(node->location_, key);
		int branch = NextBranch(common, key);

		if (node->info_.children(branch).childtype() == protocol::CHILDTYPE::NONE){
			return false;
		}

		Location location2 = node->info_.children(branch).sublocation();

		auto common2 = CommonPrefix(location2, key);
		if (common2 != location2){
			return false;
		}
		auto child = ChildMayFromDB(node, branch);
		return Exists(child, key);
	}
Example #5
0
	protocol::Node Trie::getNode(NodeFrm::POINTER node, const Location& location){
		if (node->location_ == location){
			return node->info_;
		}

		Location common = CommonPrefix(location, node->location_);
		int branch = NextBranch(common, location);

		NodeFrm::POINTER frm = ChildMayFromDB(node, branch);
		if (frm != nullptr){
			return getNode(frm, location);
		}
		else
			return protocol::Node();
	}
Example #6
0
	void Trie::GetAllItem(const Location& node, const Location& location, std::vector<std::string>& result){
		protocol::Node info;
		if (!storage_load(node, info)){
			return;
		}
		Location common = CommonPrefix(node, location);
		if (common == location){
			StorageAssociated(node, result);
			return;
		}

		if (common == node){
			int nextbranch = NextBranch(common, location);
			Location location2 = info.children(nextbranch).sublocation();
			GetAllItem(location2, location, result);
		}

	}
Example #7
0
bool eChatPrefixSpamTester::Check( tString & out, nTimeRolling & timeOut, eChatPrefixSpamType & typeOut )
{
    if ( !ShouldCheckMessage( say_ ) )
        return false;
        
    RemoveTimedOutPrefixes();
    
    // check from known prefixes
    if ( HasKnownPrefix( out, timeOut ) )
    {
        typeOut = eChatPrefixSpamType_Known;
        return true;
    }
        
    
    eChatLastSaid::SaidList & lastSaid = player_->GetLastSaid().lastSaid_;
    
    // Map of Prefix => Data
    std::map< tString, PrefixEntry > foundPrefixes;
        
    for ( eChatLastSaid::SaidList::iterator it = lastSaid.begin(); it != lastSaid.end(); ++it )
    {
        eChatSaidEntry & said = *it;
        
        if ( !ShouldCheckMessage( said ) || say_.Said() == said.Said() )
            continue;
        
        int common = CommonPrefix( say_.Said(), said.Said() );
        
        if ( common > 0 )
        {
            const tString prefix = say_.Said().SubStr(0, common);
            
            // User is talking to a player. Not prefix spam, but we still check the
            // message text excluding the player name.
            // Example: Player 1: grind center. [etc...]
            int nameLen;
            if ( ChatDirectedTowardsPlayer( prefix, nameLen ) )
            {
                said.SetType( eChatMessageType_Public_Direct );
                said.said_ = said.said_.SubStr( nameLen + 1 );
                
                return false;
                
            }
            
            if ( foundPrefixes.find(prefix) == foundPrefixes.end() )
                foundPrefixes[prefix] = PrefixEntry();
            
            PrefixEntry & data = foundPrefixes[prefix];

            data.occurrences += 1;
            CalcScore( data, common, prefix );
            if ( data.score >= se_prefixSpamRequiredScore )
            {
                
#ifdef DEBUG
                con << "Spam prefix found: \"" << se_EscapeColors( prefix ) << "\" with score " << data.score << '\n';
#endif
                nTimeRolling t = player_->GetLastSaid().AddPrefix( prefix, data.score, say_.Time() );
                timeOut = RemainingTime( t );
                
                // We caught the prefix. Don't catch words that start with the prefix.
                RemovePrefixEntries( prefix, said );
                
                out = se_EscapeColors( prefix );
                typeOut = eChatPrefixSpamType_New;
                
                return true;
            }
        }
    }

    return false;
}
Example #8
0
	bool Trie::SetItem(NodeFrm::POINTER node, const Location& location, const std::string &data, int depth){

		node->modified_ = true;
		Location location1 = node->location_;

		if (location1 == location){
			node->SetValue(data);
			return false;
		}

		Location common = CommonPrefix(location1, location);
		int branch = NextBranch(common, location);

		NodeFrm::POINTER node2 = ChildMayFromDB(node, branch);
		protocol::Child child2 = node->info_.children(branch);
		if (node2 == nullptr){
			NodeFrm::POINTER newnode = std::make_shared<NodeFrm>(location);
			newnode->SetValue(data);

			node->SetChild(branch, newnode);
			node->info_.mutable_children(branch)->set_childtype(protocol::LEAF);
			
			return true;
		}

		Location location2 = node2->location_;
		std::string newcommon = CommonPrefix(location, location2);
		if (newcommon == location2){
			return SetItem(node2, location, data, depth + 1);
		}

		if (newcommon == location){
			/*newcommon < node2.key_*/
			/*
				node1
				|
				|
				newnode
				|
				|
				node2
				*/
			NodeFrm::POINTER newnode = std::make_shared< NodeFrm>(location);
			newnode->SetValue(data);
			int b1 = NextBranch(newcommon, location2);
			newnode->SetChild(b1, node2);
			newnode->info_.mutable_children(b1)->CopyFrom(child2);

			node->SetChild(branch, newnode);
			node->info_.mutable_children(branch)->set_childtype(protocol::INNER);
			return true;
		}
		else {

			/************************************************************************/
			/*
						  node1
						  |
						  |
						  mnode
						  / \
						  /   \
						  node2  newnode

						  */
			/************************************************************************/

			NodeFrm::POINTER mnode = std::make_shared< NodeFrm>(newcommon);
			NodeFrm::POINTER newnode = std::make_shared< NodeFrm>(location);
			newnode->SetValue(data);

			int b1 = NextBranch(newcommon, location);
			int b2 = NextBranch(newcommon, location2);
			mnode->SetChild(b1, newnode);
			mnode->SetChild(b2, node2);
			mnode->info_.mutable_children(b2)->CopyFrom(child2);
			node->SetChild(branch, mnode);
			return true;
		}
	}