tags:

views:

86

answers:

1

While SysUtils.StrScan() takes PWideChar const as the parameter, is there a StrScan() like built-in function for string/Unicodestring type?

Thanks in advance.

+2  A: 

Plain old Pos will work well enough for most cases. The second parameter will simply be a one-character string instead of a Char.

If the string you're searching through will not have embedded null characters, then you can also use StrScan directly; just type-cast the string parameter to PChar. StrScan will stop searching when it reaches the null character.

Rob Kennedy
Yes, but since the plain Pos() was designed to search a substring, its internal code should be not optimal for searching a single character. OK, I think I will still use StrScan() like this: pTempPtr := Pointer(SomeString); Position := StrScan(pTempPtr, AChar) - pTempPtr);
Phantom
What do you think?
Phantom
I mean PChar typecasting versus the bare Pointer typecasting?
Phantom
Is there a downside by using Pointer typecasting instead of PChar typecasting? I see the compiler will generate less code with Pointer typecasting.
Phantom
Always cast to the most specific type available. The function asks for a character pointer, so give it one. Also, type-casting to Pointer will yield a *null* pointer if the string is empty, which is not what the function wants. Finally, it might be worth looking at the `Pos` implementation to see whether it has any optimizations for searching for short strings. (There'd still be a hit for *allocating* the string, though.)
Rob Kennedy
I assume POS is overloaded for single characters.
Marco van de Voort
@Macro: Pos() is not overloaded for single chars but its algorithm searches for the first char in the substring first. So if you have a 1-char substring, it isn't that slow.
Andreas Hausladen
Pos() is overloaded in FPC, but not in Delphi. IMO, Delphi should follow FPC in this case to generate more optimized code.
Phantom