Skip to main content

SubstringOfUtf16String

Short summary

This function edits a utf-16 string, to be a substring of itself. The substring will start at index start with length length. start should not be greater than the length of string.

It returns the new size of the string in words

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

Example:

myString 			:WString(10) := "TestTest";
lengthOfMyString :UDINT := 8;
------------------
SubstringOfUtf16String(
stringAdress := ADR(myString),
start := 3,
length := 3,
newStringLength => lengthOfMyString
); // myString will be "stT" with length 3
  • Return type: UDINT

Parameters

NameTypeCommentKind
stringAddressPOINTER TO WORDpointer to the utf-16 stringinput
startUDINTposition of first character of the substring (first char in string has position 1)input
lengthUDINTcount of characters in in new substringinput
normalizeStringBOOLnormalize the string before creating subtringinput
newStringLengthUDINTnew length of the string in bytesoutput

Code

Declaration

FUNCTION SubstringOfUtf16String :UDINT
VAR_INPUT
(* pointer to the utf-16 string *)
stringAddress :POINTER TO WORD;
(* position of first character of the substring (first char in string has position 1)*)
start :UDINT;
(* count of characters in in new substring *)
length :UDINT;
(* normalize the string before creating subtring *)
normalizeString :BOOL := TRUE;
END_VAR
VAR_OUTPUT
(* new length of the string in bytes *)
newStringLength :UDINT;
END_VAR
VAR
(* word count of string *)
wordsOfString :UDINT;
(* codepoints of string *)
codepoints :POINTER TO UnicodeCodePoint;
(* codepoints of string after normalization*)
normalizedCodepoints :POINTER TO UnicodeCodePoint;
(* codepoint count of string *)
codepointCountOfString :UDINT;
(* codepoint count of normalized string *)
normalizedCodepointCountOfString :UDINT;
(* codepoint count of substring *)
newCodepointCountOfString :UDINT;
n :UDINT;
END_VAR
VAR CONSTANT
NULL :USINT := 0;
END_VAR

Implementation

RETURN(stringAddress = 0);
RETURN(start = 0);

wordsOfString := GetUtf16StringLength(stringAddress);
// if string is empty or length is 0: substring is empty string
IF wordsOfString = 0 OR length = 0 THEN
Tc2_System.MEMSET(
destAddr := stringAddress,
fillByte := NULL,
n := SIZEOF(WORD)
);
SubstringOfUtf16String := 0;
newStringLength := 0;
RETURN;
END_IF

codepoints := __NEW(UnicodeCodePoint, (wordsOfString + 1));
IF (codepoints = 0) THEN
RETURN;
END_IF

GetCodepointsFromUtf16String(
utf16StringAddress := stringAddress,
utf16StringWordCount := wordsOfString,
codePointBuffer := codepoints,
bufferSize := (wordsOfString + 1) * SIZEOF(UnicodeCodepoint),
codePointCount => codepointCountOfString
);

IF (normalizeString) THEN
normalizedCodepoints := __NEW(UnicodeCodePoint, (wordsOfString*4));
IF (normalizedCodepoints = 0) THEN
__DELETE(codepoints);
RETURN;
END_IF

NormalizeCodepointsFormC(
codePoints := codepoints,
codePointsCount := codepointCountOfString,
normalizedCodepoints := normalizedCodepoints,
bufferSize := (wordsOfString*4) * SIZEOF(UnicodeCodePoint),
normalizedCodepointsCount => normalizedCodepointCountOfString
);
__DELETE(codepoints);
codepoints := normalizedCodepoints;
codepointCountOfString := normalizedCodepointCountOfString;
END_IF

IF start > codepointCountOfString THEN
__DELETE(codepoints);
RETURN;
END_IF

IF start + length > codepointCountOfString THEN
newCodePointCountOfString := codepointCountOfString - start + 1;
ELSE
newCodePointCountOfString := length;
END_IF
n := newCodePointCountOfString * SIZEOF(UnicodeCodePoint);

IF Tc2_System.MEMMOVE(
destAddr := codepoints,
srcAddr := codepoints + (start - 1) * SIZEOF(UnicodeCodePoint),
n := n
) <> n
THEN
__DELETE(codepoints);
RETURN;
END_IF

GetUtf16StringFromCodepoints(
addressOfCodePoints := codepoints,
codePointCount := newCodepointCountOfString,
stringBuffer := stringAddress,
bufferSize := (wordsOfString + 1) * SIZEOF(WORD),
utf16StringWordCount => newStringLength
);

SubstringOfUtf16String := newStringLength;
__DELETE(codepoints);