String matching: Difference between revisions
Content added Content deleted
m (Added Nimrod code) |
No edit summary |
||
Line 413: | Line 413: | ||
console.log matchLocations "xxx", "x" # [0,1,2] |
console.log matchLocations "xxx", "x" # [0,1,2] |
||
</lang> |
</lang> |
||
=={{header|Component Pascal}}== |
|||
BlackBox Component Builder |
|||
<lang oberon2> |
|||
MODULE StringMatch; |
|||
IMPORT StdLog,Strings; |
|||
CONST |
|||
strSize = 1024; |
|||
patSize = 256; |
|||
TYPE |
|||
Matcher* = POINTER TO LIMITED RECORD |
|||
str: ARRAY strSize OF CHAR; |
|||
pat: ARRAY patSize OF CHAR; |
|||
pos: INTEGER |
|||
END; |
|||
PROCEDURE NewMatcher*(IN str: ARRAY OF CHAR): Matcher; |
|||
VAR |
|||
m: Matcher; |
|||
BEGIN |
|||
NEW(m);m.str := str$;m.pos:= 0; |
|||
RETURN m |
|||
END NewMatcher; |
|||
PROCEDURE (m: Matcher) Match*(IN pat: ARRAY OF CHAR): INTEGER,NEW; |
|||
VAR |
|||
pos: INTEGER; |
|||
BEGIN |
|||
m.pat := pat$; |
|||
pos := m.pos; |
|||
Strings.Find(m.str,m.pat,pos,m.pos); |
|||
RETURN m.pos |
|||
END Match; |
|||
PROCEDURE (m: Matcher) Next*(): INTEGER, NEW; |
|||
VAR |
|||
pos: INTEGER; |
|||
BEGIN |
|||
pos := m.pos + LEN(m.pat$); |
|||
Strings.Find(m.str,m.pat,pos,m.pos); |
|||
RETURN m.pos; |
|||
END Next; |
|||
(* Some Helper functions based on Strings module *) |
|||
PROCEDURE StartsWith(IN str: ARRAY OF CHAR;IN pat: ARRAY OF CHAR): BOOLEAN; |
|||
VAR |
|||
pos: INTEGER; |
|||
BEGIN |
|||
Strings.Find(str,pat,0,pos); |
|||
RETURN pos = 0 |
|||
END StartsWith; |
|||
PROCEDURE Contains(IN str: ARRAY OF CHAR;IN pat: ARRAY OF CHAR; OUT pos: INTEGER): BOOLEAN; |
|||
BEGIN |
|||
Strings.Find(str,pat,0,pos); |
|||
RETURN pos >= 0 |
|||
END Contains; |
|||
PROCEDURE EndsWith(IN str: ARRAY OF CHAR;IN pat: ARRAY OF CHAR): BOOLEAN; |
|||
VAR |
|||
pos: INTEGER; |
|||
BEGIN |
|||
Strings.Find(str,pat,0,pos); |
|||
RETURN pos + LEN(pat$) = LEN(str$) |
|||
END EndsWith; |
|||
PROCEDURE Do*; |
|||
CONST |
|||
aStr = "abcdefghijklmnopqrstuvwxyz"; |
|||
VAR |
|||
pat: ARRAY 128 OF CHAR; |
|||
res: BOOLEAN; |
|||
at: INTEGER; |
|||
m: Matcher; |
|||
BEGIN |
|||
(* StartsWith *) |
|||
pat := "abc"; |
|||
StdLog.String(aStr + " startsWith " + pat + " :>");StdLog.Bool(StartsWith(aStr,pat));StdLog.Ln; |
|||
pat := "cba"; |
|||
StdLog.String(aStr + " startsWith " + pat + " :>");StdLog.Bool(StartsWith(aStr,pat));StdLog.Ln; |
|||
pat := "def"; |
|||
StdLog.String(aStr + " startsWith " + pat + " :>");StdLog.Bool(StartsWith(aStr,pat));StdLog.Ln; |
|||
StdLog.Ln; |
|||
(* Contains *) |
|||
pat := 'def'; |
|||
StdLog.String(aStr + " contains " + pat + " :>");StdLog.Bool(Contains(aStr,pat,at)); |
|||
StdLog.String(" at: ");StdLog.Int(at);StdLog.Ln; |
|||
pat := 'efd'; |
|||
StdLog.String(aStr + " contains " + pat + " :>");StdLog.Bool(Contains(aStr,pat,at)); |
|||
StdLog.String(" at: ");StdLog.Int(at);StdLog.Ln; |
|||
pat := 'abc'; |
|||
StdLog.String(aStr + " contains " + pat + " :>");StdLog.Bool(Contains(aStr,pat,at)); |
|||
StdLog.String(" at: ");StdLog.Int(at);StdLog.Ln; |
|||
pat := 'xyz'; |
|||
StdLog.String(aStr + " contains " + pat + " :>");StdLog.Bool(Contains(aStr,pat,at)); |
|||
StdLog.String(" at: ");StdLog.Int(at);StdLog.Ln; |
|||
StdLog.Ln; |
|||
(* EndsWith *) |
|||
pat := 'xyz'; |
|||
StdLog.String(aStr + " endsWith " + pat + " :>");StdLog.Bool(EndsWith(aStr,pat));StdLog.Ln; |
|||
pat := 'zyx'; |
|||
StdLog.String(aStr + " endsWith " + pat + " :>");StdLog.Bool(EndsWith(aStr,pat));StdLog.Ln; |
|||
pat := 'abc'; |
|||
StdLog.String(aStr + " endsWith " + pat + " :>");StdLog.Bool(EndsWith(aStr,pat));StdLog.Ln; |
|||
pat:= 'def'; |
|||
StdLog.String(aStr + " endsWith " + pat + " :>");StdLog.Bool(EndsWith(aStr,pat));StdLog.Ln; |
|||
StdLog.Ln; |
|||
m := NewMatcher("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); |
|||
StdLog.String("Matching 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' against 'abc':> "); |
|||
StdLog.Ln; |
|||
StdLog.String("Match at: ");StdLog.Int(m.Match("abc"));StdLog.Ln; |
|||
StdLog.String("Match at: ");StdLog.Int(m.Next());StdLog.Ln; |
|||
StdLog.String("Match at: ");StdLog.Int(m.Next());StdLog.Ln |
|||
END Do; |
|||
END StringMatch. |
|||
</lang> |
|||
Execute: ^Q StringMatching.Do <br/> |
|||
Output: |
|||
<pre> |
|||
abcdefghijklmnopqrstuvwxyz startsWith abc :> $TRUE |
|||
abcdefghijklmnopqrstuvwxyz startsWith cba :> $FALSE |
|||
abcdefghijklmnopqrstuvwxyz startsWith def :> $FALSE |
|||
abcdefghijklmnopqrstuvwxyz contains def :> $TRUE at: 3 |
|||
abcdefghijklmnopqrstuvwxyz contains efd :> $FALSE at: -1 |
|||
abcdefghijklmnopqrstuvwxyz contains abc :> $TRUE at: 0 |
|||
abcdefghijklmnopqrstuvwxyz contains xyz :> $TRUE at: 23 |
|||
abcdefghijklmnopqrstuvwxyz endsWith xyz :> $TRUE |
|||
abcdefghijklmnopqrstuvwxyz endsWith zyx :> $FALSE |
|||
abcdefghijklmnopqrstuvwxyz endsWith abc :> $FALSE |
|||
abcdefghijklmnopqrstuvwxyz endsWith def :> $FALSE |
|||
Matching 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz' against 'abc':> |
|||
Match at: 0 |
|||
Match at: 26 |
|||
Match at: -1 |
|||
</pre> |
|||
=={{header|D}}== |
=={{header|D}}== |