fixes bug 235272 "RFind is broken [was: Thunderbird treats two-letter folder msf files as folders themselves]" r=jst sr=bienvenu

This commit is contained in:
darin%meer.net 2004-03-04 07:54:22 +00:00
Родитель 1dc25eb335
Коммит 6a9b4c1c76
2 изменённых файлов: 71 добавлений и 29 удалений

Просмотреть файл

@ -956,47 +956,49 @@ Find_ComputeSearchRange( PRUint32 bigLen, PRUint32 littleLen, PRInt32& offset, P
/**
* this method changes the meaning of |offset| and |count|:
*
* upon entry,
* |offset| specifies the end point from which to search backwards
* |count| specifies the number of iterations from |offset|
*
* upon return,
* |offset| specifies start of search range
* |count| specifies length of search range
*
*
* EXAMPLE
*
* + -- littleLen=4 -- +
* : :
* |____|____|____|____|____|____|____|____|____|____|____|____|
* : :
* offset=5 bigLen=12
*
* if count = 4, then we expect this function to return offset = 2 and
* count = 7.
*
*/
static void
RFind_ComputeSearchRange( PRUint32 bigLen, PRUint32 littleLen, PRInt32& offset, PRInt32& count )
{
// |count| specifies how many iterations to make from |offset|
if (littleLen > bigLen)
{
offset = 0;
count = 0;
return;
}
PRInt32 maxOffset = PRInt32(bigLen - littleLen);
if (offset < 0)
{
offset = maxOffset;
}
else if (offset > maxOffset)
{
count = 0;
return;
}
offset = bigLen - littleLen;
if (count < 0)
count = offset + 1;
// always do at least one iteration
PRInt32 maxCount = offset + 1;
if (count < 0 || count > maxCount)
{
count = maxCount;
}
else
{
count += littleLen;
if (count > maxCount)
count = maxCount;
}
PRInt32 start = offset - count + 1;
if (start < 0)
start = 0;
offset -= (count - littleLen);
count = offset + littleLen - start;
offset = start;
}
//-----------------------------------------------------------------------------

Просмотреть файл

@ -113,13 +113,39 @@ PRBool test_find()
PRBool test_rfind()
{
const char text[] = "<!DOCTYPE blah blah blah>";
const char term[] = "bLaH";
nsCString src(text);
PRInt32 i = src.RFind("bLaH", PR_TRUE, 3, -1);
if (i == -1)
return PR_TRUE;
PRInt32 i;
printf("i=%d\n", i);
return PR_FALSE;
i = src.RFind(term, PR_TRUE, 3, -1);
if (i != kNotFound)
{
printf("unexpected result searching from offset=3, i=%d\n", i);
return PR_FALSE;
}
i = src.RFind(term, PR_TRUE, -1, -1);
if (i != 20)
{
printf("unexpected result searching from offset=-1, i=%d\n", i);
return PR_FALSE;
}
i = src.RFind(term, PR_TRUE, 13, -1);
if (i != 10)
{
printf("unexpected result searching from offset=13, i=%d\n", i);
return PR_FALSE;
}
i = src.RFind(term, PR_TRUE, 22, 3);
if (i != 20)
{
printf("unexpected result searching from offset=22, i=%d\n", i);
return PR_FALSE;
}
return PR_TRUE;
}
PRBool test_rfind_2()
@ -146,6 +172,19 @@ PRBool test_rfind_3()
return PR_FALSE;
}
PRBool test_rfind_4()
{
nsCString value("a.msf");
PRInt32 i = value.RFind(".msf");
if (i != 1)
{
printf("i=%d\n", i);
return PR_FALSE;
}
return PR_TRUE;
}
PRBool test_distance()
{
const char text[] = "abc-xyz";
@ -444,6 +483,7 @@ tests[] =
{ "test_rfind", test_rfind },
{ "test_rfind_2", test_rfind_2 },
{ "test_rfind_3", test_rfind_3 },
{ "test_rfind_4", test_rfind_4 },
{ "test_distance", test_distance },
{ "test_length", test_length },
{ "test_trim", test_trim },