Fun POS function than fastpos

xiaoxiao2021-03-06  51

The most used in the string processing is the POS function. However, if you want to search for a sub-string of the second or third occurrence in a string, there is no ready-made Delphi standard function. So I wrote one. At the same time, Faststrings.SmartPos () and JVCL.NPOS () are compared and compatible with Unicode (WideString / Widechar).

Note: The code may feel uncomfortable, but as the most common string function, I think it is still worth it.

Function QuickPos (Const Substr, S: WideString; Matchesindex: Integer = 1): Integer; Function QuickPosback (Const Substr, S: WideString; MatchesReverseIndex: Integer = 1): Integer;

code show as below:

// Compares a Substring with a string. * For inline use "// c: 2004-07-05 | M: 2004-07-05Function _inlineCompareText (const Substr, s: wideString; StartIndex: integer = 1; lenofsubstr: integer = -1; lenofs: integer = -1): boolean; var i: integer; begin if lenofsusetr = -1 Then Lenofsubstr: = Length (Substr); if lenofs = -1 Then Lenofs: = Length (s); if Lenofsubstr> Lenofs the begin result: = false; exit; end; for i: = 1 to Lenofstr Do IF Substr [i] <> s [i startIndex - 1] the beginning: = false; exit; end; result: = true; ;

// Returns the 1. INDEX OF A SUBSTRING WITHINIIN A STRING Start at a certain index.// C: 2004-07-05 | M: 2004-07-05 | P: 1.0 function _POSFORWARD (Const Substr, S: WideString , STARTINDEX: INTEGER; Lenofsubstr: Integer = -1; lenofs: integer = -1): integer; var i: integer; begin result: = 0; case lenofsustr of 0: exit; -1: lenofsubstr: = Length (Substr) ; End; if lenofs = -1 Then Lenofs: = Length (s);

For i: = startIndex to Lenofs Do Begin if (s [i] = substr [1]) and _inlinecomparetext (Substr, S, I, Lenofsubstr, Lenofs) THEN Begin Result: = i;

// returns the 1. index of a substring within a string.// Note: Searching Time Will Inrease When Matchesindex Increased.// C: 2004-04-09 | M: 2004-07-05 | P: 1.0 Function QuickPos (const Substr, s: widestring; matchesindex: integer = 1): Integer; var lenofs, lenofsubstr: integer; begin result: = pOS {POS} (SUBSTR, S); if (matchesindex = 1) OR (Result = 0) Lenofs: = Length (s); lenofsubstr: = Length (SUBSTR);

While (matchesindex> 1) and (result> 0) Do Begin Result: = _POSFORWARD {POS} (Substr, S, Result 1, Lenofstr, Lenofs); // Tip !! Do Not use func.copy !! if result = 0 THEN EXIT; DEC (Matchesindex); end;

// Returns the last index of a substring within a string.// Todo: using asm to shutrite this function. The asm-code loocks very like func.pos! // C: 2004-04-09 | M: 2004-07 -03 | P: N / AFunction _Posback (const SUBSTR, S: WIDESTRINDEX: INTEGER = -1; lenofsubstr: integer = -1): integer = -1): Integer; var i: integer; begin result: = 0; Case Lenofsuser OF 0: EXIT; -1: Lenofsubstr: = Length (Substr); End; if stopindex = -1 Then StopIndex: = Length (s); for i: = stopindex - lenofsubstr 1 Downto 1 Do Begin if (s [i] = SUBSTR [1]) And _inlineCompareText (Substr, S, I, Lenofsustr) The begin result: = i; EXIT;

// Returns the last index of a substring within a String.// C: 2004-04-09 | M: 2004-07-03 | P: N / Afunction QuickPosback (const Substr, s: wideString; matchesreverseIndex: integer = 1 : Integer; Var Lenofsubstr: Integer; Begin Result: = _POSBACK {POS} (SUBSTR, S);

IF (MatchesreverseIndex = 1) or (result = 0) THEN EXIT; LENOFSUBSTR: = Length (Substr);

转载请注明原文地址:https://www.9cbs.com/read-117505.html

New Post(0)