Skip to main content

contains

Short summary

This assertion method checks if the current string stringToCheck contains the search string searchString. Processed strings must be in Windows-1252 or Windows-1251 encoding

Attention: All strings are handled as null-terminated byte streams

Parameters

NameTypeCommentKind
stringToCheckPOINTER TO BYTEcurrent string to checkinput
searchStringPOINTER TO BYTEstring must be found in stringToCheckinput
ignoreCasesBOOLTRUE means ignore cases; FALSE means that cases must be equal tooinput
messageAssertMessagemessage if the assertion is falseinput
sbcsTypeTc2_Utilities.E_SBCSTypeused Single Byte Character Set (SBCS), is set in Tc2_Utilities.GLOBAL_SBCS_TABLEinput

Code

Declaration

METHOD contains
VAR_INPUT
(* current string to check *)
stringToCheck :POINTER TO BYTE;
(* string must be found in ``stringToCheck`` *)
searchString :POINTER TO BYTE;
(* ``TRUE`` means ignore cases; ``FALSE`` means that cases must be equal too *)
ignoreCases :BOOL;
(* message if the assertion is false *)
message :AssertMessage;
(* used Single Byte Character Set (SBCS), is set in Tc2_Utilities.GLOBAL_SBCS_TABLE *)
sbcsType :Tc2_Utilities.E_SBCSType := Tc2_Utilities.E_SBCSType.eSBCS_WesternEuropean;
END_VAR
VAR
(* length of current string to check *)
lengthStringToCheck :UDINT;
(* length of ``searchString`` *)
lengthSearchString :UDINT;
(* difference between string length and search string length *)
maxSearchIndex :UDINT;
(* string address we use to compare, if it's case sensitive it's equal to ``stringToCheck`` *)
usedStringToCheck :POINTER TO BYTE;
(* seacrch string address we use to compare, if it's case sensitive it's equal to ``searchString`` *)
usedSearchString :POINTER TO BYTE;
(* character index for the comperation *)
character :UDINT;
END_VAR

Implementation

lengthStringToCheck := THIS^.getStringLength(stringToCheck);
lengthSearchString := THIS^.getStringLength(searchString);

IF (
NOT THIS^.isContainsCheckNecessary(
stringToCheck := stringToCheck,
searchString := searchString,
lengthStringToCheck := lengthStringToCheck,
lengthSearchString := lengthSearchString,
additionalText := THIS^.getDebugInfo('contains'),
message := message
)
) THEN
RETURN;
END_IF

usedStringToCheck := THIS^.getNewUpperCaseString(stringToCheck, lengthStringToCheck, ignoreCases, sbcsType := sbcsType);
usedSearchString := THIS^.getNewUpperCaseString(searchString, lengthSearchString, ignoreCases, sbcsType := sbcsType);
IF ignoreCases AND_THEN ((usedStringToCheck = 0) OR_ELSE (usedSearchString = 0)) THEN
THIS^.assertionWasWrong(message := message, additionalText := THIS^.getDebugInfo('contains, malloc failed'));
RETURN;
END_IF

maxSearchIndex := lengthStringToCheck - lengthSearchString;
FOR (character := 0) TO (maxSearchIndex) DO
IF (Tc2_System.MEMCMP(
ADR(usedStringToCheck[character]),
usedSearchString,
lengthSearchString
) = CNM_ReturnTypes.ComparationResult.EQUAL
) THEN
THIS^.freeMemory(usedStringToCheck, ignoreCases);
THIS^.freeMemory(usedSearchString, ignoreCases);
RETURN; //assert is true
END_IF;
END_FOR

THIS^.assertionWasWrong(message, THIS^.getDebugInfo('contains'));
THIS^.freeMemory(usedStringToCheck, ignoreCases);
THIS^.freeMemory(usedSearchString, ignoreCases);