Esempio n. 1
0
bool
UniString::EndsWith(const UniString& other, size_t length /* = OpDataFullLength */) const
{
	length = MIN(length, other.Length());
	if (length > Length())
		return false;
	if (!length)
		return true;
	return m_data.EndsWith(other.m_data, UNICODE_SIZE(length));
}
Esempio n. 2
0
bool
UniString::StartsWithCase(const UniString& other, size_t length /* = OpDataFullLength */) const
{
	length = MIN(length, other.Length());
	if (length > Length())
		return false;
	if (!length)
		return true;

	UniStringFragmentIterator this_itr(*this);
	UniStringFragmentIterator other_itr(other);
	size_t this_length = this_itr.GetLength();
	const uni_char* this_data = this_itr.GetData();
	size_t this_str_len = uni_strnlen(this_data, this_length);
	size_t other_length = other_itr.GetLength();
	const uni_char* other_data = other_itr.GetData();
	size_t other_str_len = uni_strnlen(other_data, other_length);
	while (length)
	{
		OP_ASSERT(this_itr && this_data && this_length);
		OP_ASSERT(other_itr && other_data && other_length);

		size_t to_compare = MIN(this_length, length);
		if (to_compare > other_length) to_compare = other_length;
		if (to_compare > this_str_len)
		{
			OP_ASSERT(this_str_len != this_length);
			to_compare = this_str_len;
		}
		if (to_compare > other_str_len)
		{
			OP_ASSERT(other_str_len != other_length);
			to_compare = other_str_len;
		}

		// compare the first part of both fragments
		if (to_compare == 0)
		{
			OP_ASSERT(other_length > 0 && this_length > 0 &&
					  other_str_len != other_length && this_str_len != this_length);
			if (this_str_len != 0 && other_str_len != 0)
				return false;
			OP_ASSERT(this_data[0] == '\0' && other_data[0] == '\0');
			to_compare++;
		}
		else if (0 != uni_strnicmp(this_data, other_data, to_compare))
			return false;

		// advance to the next part to compare:
		length -= to_compare;

		if (length > 0)
		{
			// advance data of this
			this_length -= to_compare;
			if (this_length > 0)
			{
				this_data += to_compare;
				if (this_str_len == 0)
					this_str_len = uni_strnlen(this_data, this_length);
				else
					this_str_len -= to_compare;
			}
			else
			{
				++this_itr;
				this_length = this_itr.GetLength();
				this_data = this_itr.GetData();
				this_str_len = uni_strnlen(this_data, this_length);
			}

			// advance data of other
			other_length -= to_compare;
			if (other_length > 0)
			{
				other_data += to_compare;
				if (other_str_len == 0)
					other_str_len = uni_strnlen(other_data, other_length);
				else
					other_str_len -= to_compare;
			}
			else
			{
				++other_itr;
				other_length = other_itr.GetLength();
				other_data = other_itr.GetData();
				other_str_len = uni_strnlen(other_data, other_length);
			}
		}
	}

	return true;
}
OP_STATUS HistoryAutocompleteModelItem::ShortenUrlQuery(UniString& text, const OpStringC& preserve, int max_length)
{
	int input_length = text.Length();

	UniString uni_out;

	if (text.FindFirst(UNI_L("&")) == OpDataNotFound || max_length <= SHORTENING_VISIBLE_LENGTH)
	{
		if (max_length > SHORTENING_VISIBLE_LENGTH)
		{
			text.Trunc(max_length - SHORTENING_VISIBLE_LENGTH);
			RETURN_IF_ERROR(text.AppendConstData(UNI_L(SHORTENING),sizeof(SHORTENING)-1));
		}
		else
		{
			text.Trunc(max_length);
		}
	}

	bool add_delimiter = true;

	OpAutoPtr< OtlCountedList<UniString> > parts(text.Split('&'));

	int count = parts->Count();
	for (OtlList<UniString>::Iterator part = parts->Begin(); part != parts->End(); ++part)
	{
		count--;
		if (part->IsEmpty())
			continue;
			
		if (max_length - uni_out.Length() - SHORTENING_VISIBLE_LENGTH <= 0)
			break;

		if (count == 0)
		{
			if (add_delimiter)
				RETURN_IF_ERROR(uni_out.AppendConstData(UNI_L("&"),1));
			RETURN_IF_ERROR(uni_out.Append(*part));
			continue;
		}

		if ((!add_delimiter || part->Length() > SHORTENING_VISIBLE_LENGTH ) &&
			(!preserve.CStr() || part->FindFirst(preserve) == OpDataNotFound) && input_length > (max_length - (int)uni_out.Length()))
		{
			if (!add_delimiter)
			{
				input_length -= part->Length();
				continue;
			}

			RETURN_IF_ERROR(uni_out.AppendConstData(UNI_L(SHORTENING),sizeof(SHORTENING)-1));
			RETURN_IF_ERROR(uni_out.AppendConstData(UNI_L("&"),1));
			
			add_delimiter=false;
			input_length -= part->Length() + 1;
		}
		else
		{
			RETURN_IF_ERROR(uni_out.Append(*part));
			RETURN_IF_ERROR(uni_out.AppendConstData(UNI_L("&"),1));
			
			add_delimiter = true;
			input_length -= part->Length() + 1;
		}
	}

	text = uni_out;

	return OpStatus::OK;
}