Request : UTF8ReverseString, UTF8RPos
Original Reporter info from Mantis: bmy92
-
Reporter name: Bernard Marcelly
Original Reporter info from Mantis: bmy92
- Reporter name: Bernard Marcelly
Description:
The RTL offers Pos() and RPos() functions but they do not handle UTF8 strings.
Unit LazUTF8 offers UTF8Pos(), but it lacks UTF8RPos().
AnsiReverseString does not handle UTF8 strings, and there is no UTF8ReverseString.
Since ordinary strings are UTF8 strings in Lazarus 1.6, these routines would come in handy.
Additional information:
// code proposal (tested OK with strings with national characters)
function UTF8ReverseString(p: PChar; const ByteCount: LongInt): string;
var
CharLen, rBytePos: LongInt;
begin
setlength(Result,ByteCount);
rBytePos:= ByteCount+1;
while (rBytePos > 1) do
begin
CharLen:=UTF8CharacterLength(p);
dec(rBytePos, CharLen);
Move(p^, Result[rBytePos], CharLen);
inc(p,CharLen);
end;
end;
function UTF8ReverseString(const AText: string): string; inline;
begin
Result:= UTF8ReverseString(PChar(AText), length(AText));
end;
function UTF8RPos(const Substr, Source: string): integer;
var
RevSubstr, RevSource: string; pRev: integer;
begin
if Pos(Substr, Source) = 0
then
Result:= 0
else
begin
RevSubstr:= UTF8ReverseString(Substr);
RevSource:= UTF8ReverseString(Source);
pRev:= UTF8Pos(RevSubstr, RevSource);
Result:= UTF8Length(Source) -pRev -UTF8Length(Substr) +2;
end;
end;
Mantis conversion info:
- Mantis ID: 29817
- OS: Windows 7 Home
- OS Build: SP1
- Platform: PC
- Version: 1.6
- Fixed in version: 1.8
- Fixed in revision: r51927 (#23f36140)
- Target version: 1.8