isEqualTo
Short summary
This assertion method checks if the current string stringToCheck is equal to expected.
Processed strings must be in UTF-8 encoding
Attention: All strings are handled as null-terminated byte streams
Parameters
| Name | Type | Comment | Kind |
|---|---|---|---|
| stringToCheck | POINTER TO BYTE | current string to check | input |
| expected | POINTER TO BYTE | stringToCheck must be equal to expected | input |
| ignoreCases | BOOL | TRUE means ignore cases; FALSE means that cases must be equal too | input |
| message | AssertMessage | message if the assertion is false | input |
| normalizeStrings | BOOL | normalize both strings for check | input |
Code
Declaration
METHOD isEqualTo
VAR_INPUT
(* current string to check *)
stringToCheck :POINTER TO BYTE;
(* ``stringToCheck`` must be equal to expected *)
expected :POINTER TO BYTE;
(* ``TRUE`` means ignore cases; ``FALSE`` means that cases must be equal too *)
ignoreCases :BOOL := FALSE;
(* message if the assertion is false *)
message :AssertMessage;
(*normalize both strings for check*)
normalizeStrings :BOOL := TRUE;
END_VAR
VAR
(* length of current string to check in bytes *)
stringToCheckByteCount :UDINT;
(* length of current string to check in characters *)
stringToCheckCodePointCount :UDINT;
(* length of ``expected`` in bytes *)
expectedStringByteCount :UDINT;
(* length of ``expected`` in characters *)
expectedStringCodePointCount :UDINT;
(* DWORD array we use to check *)
usedCodePointsToCheck,normalizedUsedCodePointsToCheck :POINTER TO CNM_UnicodeUtilities.UnicodeCodePoint;
usedCodePointToCheckCount :UDINT;
(* DWORD array we use to compare *)
usedExpectedCodePoints, normalizedUsedExpectedCodePoints :POINTER TO CNM_UnicodeUtilities.UnicodeCodePoint;
usedExpectedCodePointCount :UDINT;
END_VAR
VAR CONSTANT
(*After decomposition one codepoint can extend up to 4 codepoints*)
NORMALIZATION_FACTOR :UDINT := 4;
END_VAR
Implementation
CNM_UnicodeUtilities.GetUtf8StringLength(stringAddress := stringToCheck, byteCount => stringToCheckByteCount);
CNM_UnicodeUtilities.GetUtf8StringLength(stringAddress := expected, byteCount => expectedStringByteCount);
IF ((stringToCheckByteCount <> expectedStringByteCount) AND_THEN NOT normalizeStrings) THEN
THIS^.assertionWasWrong(message, THIS^.getDebugInfo('isEqualTo'));
RETURN;
END_IF
RETURN ((
NOT normalizeStrings
) AND_THEN (
NOT THIS^.isContainsCheckNecessary(
stringToCheck := stringToCheck,
searchString := expected,
lengthStringToCheck := stringToCheckByteCount,
lengthSearchString := expectedStringByteCount,
additionalText := THIS^.getDebugInfo('isEqualTo'),
message := message
)
)
);
usedCodePointsToCheck := __NEW(CNM_UnicodeUtilities.UnicodeCodePoint, stringToCheckByteCount);
IF (usedCodePointsToCheck = 0) THEN
THIS^.assertionWasWrong(message, THIS^.getDebugInfo('isEqualTo, malloc failed'));
RETURN;
END_IF
usedExpectedCodePoints := __NEW(CNM_UnicodeUtilities.UnicodeCodePoint, expectedStringByteCount);
IF (usedExpectedCodePoints = 0) THEN
__DELETE(usedCodePointsToCheck);
THIS^.assertionWasWrong(message, THIS^.getDebugInfo('isEqualTo, malloc failed'));
RETURN;
END_IF
(* convert ``stringToCheck`` to code points *)
CNM_UnicodeUtilities.GetCodepointsFromUtf8String(
utf8StringAddress := stringToCheck,
utf8StringByteCount := stringToCheckByteCount,
codePointBuffer := usedCodePointsToCheck,
bufferSize := SIZEOF(CNM_UnicodeUtilities.UnicodeCodePoint) * stringToCheckByteCount,
codePointsCount => usedCodePointToCheckCount);
(* convert ``searchString`` to code points *)
CNM_UnicodeUtilities.GetCodepointsFromUtf8String(
utf8StringAddress := expected,
utf8StringByteCount := expectedStringByteCount,
codePointBuffer := usedExpectedCodePoints,
bufferSize := SIZEOF(CNM_UnicodeUtilities.UnicodeCodePoint) * expectedStringByteCount,
codePointsCount => usedExpectedCodePointCount);
IF normalizeStrings THEN
normalizedUsedCodePointsToCheck := __NEW(CNM_UnicodeUtilities.UnicodeCodePoint, (stringToCheckByteCount * NORMALIZATION_FACTOR));
IF (normalizedUsedCodePointsToCheck = 0) THEN
THIS^.assertionWasWrong(message, THIS^.getDebugInfo('isEqualTo'));
__DELETE(usedCodePointsToCheck);
__DELETE(usedExpectedCodePoints);
RETURN;
END_IF
normalizedUsedExpectedCodePoints := __NEW(CNM_UnicodeUtilities.UnicodeCodePoint, (expectedStringByteCount * NORMALIZATION_FACTOR));
IF (normalizedUsedExpectedCodePoints = 0) THEN
THIS^.assertionWasWrong(message, THIS^.getDebugInfo('isEqualTo'));
__DELETE(usedCodePointsToCheck);
__DELETE(usedExpectedCodePoints);
__DELETE(normalizedUsedCodePointsToCheck);
RETURN;
END_IF
CNM_UnicodeUtilities.NormalizeCodepointsFormD(
codePoints := usedCodePointsToCheck,
codePointsCount := usedCodePointToCheckCount,
normalizedCodepoints := normalizedUsedCodePointsToCheck,
bufferSize := (stringToCheckByteCount * NORMALIZATION_FACTOR) * SIZEOF(CNM_UnicodeUtilities.UnicodeCodePoint),
normalizedCodepointsCount => usedCodePointToCheckCount
);
__DELETE(usedCodePointsToCheck);
usedCodePointsToCheck := normalizedUsedCodePointsToCheck;
CNM_UnicodeUtilities.NormalizeCodepointsFormD(
codePoints := usedExpectedCodePoints,
codePointsCount := usedExpectedCodePointCount,
normalizedCodepoints := normalizedUsedExpectedCodePoints,
bufferSize := (expectedStringByteCount * NORMALIZATION_FACTOR)*SIZEOF(CNM_UnicodeUtilities.UnicodeCodePoint),
normalizedCodepointsCount => usedExpectedCodePointCount
);
__DELETE(usedExpectedCodePoints);
usedExpectedCodePoints := normalizedUsedExpectedCodePoints;
IF (usedCodePointToCheckCount <> usedExpectedCodePointCount) THEN
THIS^.assertionWasWrong(message, THIS^.getDebugInfo('isEqualTo'));
__DELETE(usedCodePointsToCheck);
__DELETE(usedExpectedCodePoints);
RETURN;
END_IF
END_IF
IF (ignoreCases) THEN
CNM_UnicodeUtilities.GetUpperCaseForCodepoint(usedCodePointsToCheck, usedCodePointToCheckCount);
CNM_UnicodeUtilities.GetUpperCaseForCodepoint(usedExpectedCodePoints, usedExpectedCodePointCount);
END_IF
IF (Tc2_System.MEMCMP(
usedCodePointsToCheck,
usedExpectedCodePoints,
usedExpectedCodePointCount*SIZEOF(CNM_UnicodeUtilities.UnicodeCodePoint)
) <> CNM_ReturnTypes.ComparationResult.EQUAL
) THEN
THIS^.assertionWasWrong(message, THIS^.getDebugInfo('isEqualTo'));
END_IF;
__DELETE(usedCodePointsToCheck);
__DELETE(usedExpectedCodePoints);