From 7edc96e0a4791e435c837a32ee0e59cd7e88ba4a Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Tue, 28 Jun 2005 13:06:16 +0000 Subject: [PATCH] Added: ReplacePart function --- X2UtStrings.pas | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/X2UtStrings.pas b/X2UtStrings.pas index 1660543..05ff8a5 100644 --- a/X2UtStrings.pas +++ b/X2UtStrings.pas @@ -114,9 +114,13 @@ type function ChildPathEx(var AChild, AParent: String; const AFailIfSame: Boolean = False): Boolean; + function ReplacePart(const ASource: String; const AStart, ALength: Integer; + const AReplace: String): String; + implementation uses - SysUtils; + SysUtils, + Windows; function FormatSize(const ABytes: Int64; AKeepBytes: Boolean = False): String; const @@ -379,5 +383,35 @@ begin (Length(AChild) > Length(AParent))); end; + +function ReplacePart(const ASource: String; const AStart, ALength: Integer; + const AReplace: String): String; +var + iSrcLength: Integer; + iLength: Integer; + iDiff: Integer; + iDest: Integer; + +begin + iSrcLength := Length(ASource); + iLength := Length(AReplace); + iDiff := iLength - ALength; + iDest := 1; + + SetLength(Result, iSrcLength + iDiff); + + // Write first part + CopyMemory(@Result[iDest], @ASource[1], AStart - 1); + Inc(iDest, AStart - 1); + + // Write replacement + CopyMemory(@Result[iDest], @AReplace[1], iLength); + Inc(iDest, iLength); + + // Write last part + CopyMemory(@Result[iDest], @ASource[AStart + ALength], + iSrcLength - AStart - (ALength - 1)); +end; + end.