Delphi System.Insert -从指定位置开始,将子字符串插入字符串(或将动态数组插入动态数组)

Delphi System.Insert -从指定位置开始,将子字符串插入字符串(或将动态数组插入动态数组)

原型:

procedure  _Insert{ source : ShortString; var s : openstring; index : Integer };
asm
{     ->EAX     Pointer to source string        }
{       EDX     Pointer to destination string   }
{       ECX     Length of destination string    }
{       [ESP+4] Index                   }

        PUSH    EBX
        PUSH    ESI
        PUSH    EDI
        PUSH    ECX
        MOV     ECX,[ESP+16+4]
        SUB     ESP,512         { VAR buf: ARRAY [0..511] of Char       }

        MOV     EBX,EDX         { save pointer to s for later   }
        MOV     ESI,EDX

        XOR     EDX,EDX
        MOV     DL,[ESI]
        INC     ESI

{       limit index to [1 .. Length(s)+1]       }

        INC     EDX
        TEST    ECX,ECX
        JLE     @@smallInx
        CMP     ECX,EDX
        JG      @@bigInx
@@cont1:
        DEC     EDX     { EDX = Length(s)               }
                        { EAX = Pointer to src  }
                        { ESI = EBX = Pointer to s      }
                        { ECX = Index           }

{       copy index-1 chars from s to buf        }

        MOV     EDI,ESP
        DEC     ECX
        SUB     EDX,ECX { EDX = remaining length of s   }
        REP     MOVSB

{       copy Length(src) chars from src to buf  }

        XCHG    EAX,ESI { save pointer into s, point ESI to src         }
        MOV     CL,[ESI]        { ECX = Length(src) (ECX was zero after rep)    }
        INC     ESI
        REP     MOVSB

{       copy remaining chars of s to buf        }

        MOV     ESI,EAX { restore pointer into s                }
        MOV     ECX,EDX { copy remaining bytes of s             }
        REP     MOVSB

{       calculate total chars in buf    }

        SUB     EDI,ESP         { length = bufPtr - buf         }
        MOV     ECX,[ESP+512]   { ECX = Min(length, destLength) }
{       MOV     ECX,[EBP-16]   }{ ECX = Min(length, destLength) }
        CMP     ECX,EDI
        JB      @@1
        MOV     ECX,EDI
@@1:
        MOV     EDI,EBX         { Point EDI to s                }
        MOV     ESI,ESP         { Point ESI to buf              }
        MOV     [EDI],CL        { Store length in s             }
        INC     EDI
        REP     MOVSB           { Copy length chars to s        }
        JMP     @@exit

@@smallInx:
        MOV     ECX,1
        JMP     @@cont1
@@bigInx:
        MOV     ECX,EDX
        JMP     @@cont1

@@exit:
        ADD     ESP,512+4
        POP     EDI
        POP     ESI
        POP     EBX
        RET 4
end;

Delphi:

procedure Insert(
  Substr: String;     //要插入的字符串
  var Dest: String; //源字符串
  Index: Integer    //从第几个字符前开始插入
);

XE:

procedure Insert(
    Source: <string or dynamic array>; //支持动态数组
    var Dest: <string or dynamic array>;   
    Index: Integer
);  

示例1:

var
  s,InStr:string;
begin
  s:='Hello TaoRoy';
  InStr:=',I am ';
  System.Insert(InStr,s,7);
  ShowMessage(s);  //Hello ,I am TaoRoy
end;  

示例2:(XE)

var
  A: array of integer;
begin
  ...
  A:=[1,2,3,4,5];
  Insert(6,A,2); // A 的值: [1,2,6,3,4,5]
  ...
end;

  

  

 

 

创建时间:2022.07.13  更新时间:

posted on 2022-07-13 11:53  滔Roy  阅读(803)  评论(0编辑  收藏  举报

导航