Skip to main content

SubstringOfUtf8String

Short summary

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

It returns the new size of the string in byte.

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

Example:

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

Parameters

NameTypeCommentKind
stringAddressPOINTER TO BYTEpointer to the utf-8 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 SubstringOfUtf8String :UDINT
VAR_INPUT
(* pointer to the utf-8 string *)
stringAddress :POINTER TO BYTE;
(* 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
(* byte count of string *)
bytesOfString :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);

bytesOfString := GetUtf8StringLength(stringAddress);
// if string is empty or length is 0: substring is empty string
IF bytesOfString = 0 OR length = 0 THEN
Tc2_System.MEMSET(
destAddr := stringAddress,
fillByte := NULL,
n := SIZEOF(BYTE)
);
SubstringOfUtf8String := 0;
newStringLength := 0;
RETURN;
END_IF

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

GetCodepointsFromUtf8String(
utf8StringAddress := stringAddress,
utf8StringByteCount := bytesOfString,
codePointBuffer := codepoints,
bufferSize := (bytesOfString + 1) * SIZEOF(UnicodeCodepoint),
codePointsCount => codepointCountOfString
);

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

NormalizeCodepointsFormC(
codePoints := codepoints,
codePointsCount := codepointCountOfString,
normalizedCodepoints := normalizedCodepoints,
bufferSize := (bytesOfString*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

GetUtf8StringFromCodepoints(
addressOfCodePoints := codepoints,
codePointCount := newCodePointCountOfString,
utf8StringBuffer := stringAddress,
bufferSize := bytesOfString + 1,
utf8StringByteCount => newStringLength
);

SubstringOfUtf8String := newStringLength;
__DELETE(codepoints);