At 12:00am -0800 01-02-12, Palm Developer Forum digest wrote:
>Subject: StrCaselessStr () - please help
>From: Paul Nevai <[EMAIL PROTECTED]>
>Date: Sun, 11 Feb 2001 16:22:38 -0500 (EST)
>X-Message-Number: 18
>
>This StrCaselessStr () works well BUT is MUCH slower than StrStr (). Could
>you recommend me a faster one? Generally speaking, StrLen (strP) is very
>large.
>
>Note. I must be able to search for partial words too so that the standard
>Palm OS functions do not seem to help.
The slightly faster generic approach is to lower-case the first
character of the token string, and then compare that to lower-cased
characters from the source (to be searched) string. Something like:
UInt16 len = StrLen(tokenP);
// Verify that the source string is at least as long as the target.
// Otherwise there's no chance for a match.
for (UInt16 i = 0; i < len; i++)
{
if (strP[i] == chrNull)
{
return(NULL);
}
}
WChar targetChar;
TxtGetNextChar(tokenP, 0, &targetChar);
targetChar = TxtGlueLowerChar(targetChar);
while (*strP)
{
WChar srcChar;
UInt16 charSize = TxtGetNextChar(strP, 0, &srcChar);
if (TxtGlueToLower(srcChar) == targetChar)
{
if (TxtCaselessCompare(strP, len, NULL,
tokenP, len, NULL) == 0)
{
return(strP);
}
}
strP += charSize;
}
If you know that you're running on a device that uses a single-byte
character encoding, then you could directly use the lower-casing
table returned by GetCharCaselessValue(). This would be even faster.
To check if you're running with a single-byte character encoding,
you'd do something like this:
Boolean
IsSingleByteEncoding(void)
{
UInt32 flags;
UInt32 encoding;
if (FtrGet(sysFtrCreator, sysFtrNumCharEncodingFlags, &flags)
== errNone)
{
return((flags & charEncodingOnlySingleByte) != 0);
}
else if (FtrGet(sysFtrCreator, sysFtrNumCharEncoding,
&encoding) == errNone)
{
return(encoding == charEncodingPalmLatin);
}
else
{
// Must be an older version of Palm OS, thus has to be Latin.
return(true);
}
}
-- Ken
Ken Krugler
TransPac Software, Inc.
<http://www.transpac.com>
+1 530-470-9200
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/tech/support/forums/