Skip to main content

TrimUtf16String

Short summary

This function removes whitespace both from the start and the end of a utf-16 string. It returns the string length in words.

Example:

myString :WString(10) := " Test " ;
newLength :UDINT;
------------------
TrimUtf16String(
stringAddress := ADR(myString),
newStringLength => newLength
);
  • Return type: UDINT

Parameters

NameTypeCommentKind
stringAddressPOINTER TO WORDpointer to the utf-16 stringinput
newStringLengthUDINTnew length of the string in wordsoutput

Code

Declaration

FUNCTION TrimUtf16String :UDINT
VAR_INPUT
(*pointer to the utf-16 string*)
stringAddress :POINTER TO WORD;
END_VAR
VAR_OUTPUT
(* new length of the string in words *)
newStringLength :UDINT := 0;
END_VAR
VAR
stringLength :UDINT;
codepoints :POINTER TO UnicodeCodePoint;
codepointCount :UDINT;
END_VAR

Implementation

stringLength := GetUtf16StringLength(stringAddress);
newStringLength := 0;
TrimUtf16String := 0;
RETURN((stringAddress = 0) OR_ELSE (stringLength = 0));
codepoints := __NEW(UnicodeCodepoint, stringLength);
RETURN(codepoints = 0);

GetCodepointsFromUtf16String(
utf16StringAddress := stringAddress,
utf16StringWordCount := stringLength,
codepointBuffer := codepoints,
bufferSize := stringLength*SIZEOF(UnicodeCodepoint),
codePointCount => codepointCount
);

TrimCodepoints(
codepoints := codepoints,
codepointCount := codepointCount,
newCodepointCount => codepointCount
);

Tc2_System.MEMSET(stringAddress, 0, stringLength*2);

GetUtf16StringFromCodepoints(
addressOfCodePoints := codepoints,
codePointCount := codepointCount,
stringBuffer := stringAddress,
bufferSize := (stringLength + 1)*SIZEOF(WORD), // stringlength + NULL terminator
utf16StringWordCount => newStringLength
);

TrimUtf16String := newStringLength;
__DELETE(codepoints);