Skip to main content

TrimLeftUtf16String

Short summary

This function removes whitespace from the left side (start) of a utf-16 string. It returns the string length in words

Example:

myString :WString(10) := " Test" ;
newLength :UDINT;
------------------
TrimLeftUtf8String(
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 TrimLeftUtf16String :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, stringLengthUpperCase :UDINT;
codepoints :POINTER TO UnicodeCodePoint;
codepointCount :UDINT;
END_VAR

Implementation

stringLength := GetUtf16StringLength(stringAddress);
newStringLength := 0;
TrimLeftUtf16String := 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
);

TrimLeftCodepoints(
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
);

TrimLeftUtf16String := newStringLength;
__DELETE(codepoints);