Skip to main content

TrimLeftUtf8String

Short summary

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

Example:

myString :String(10) := ' Test' ;
newLength :UDINT;
------------------
TrimLeftUtf8String(
stringAddress := ADR(myString),
newStringLength => newLength
);
  • Return type: UDINT

Parameters

NameTypeCommentKind
stringAddressPOINTER TO BYTEpointer to the utf-8 stringinput
newStringLengthUDINTnew length of the string in bytesoutput

Code

Declaration

FUNCTION TrimLeftUtf8String :UDINT
VAR_INPUT
(*pointer to the utf-8 string*)
stringAddress :POINTER TO BYTE;
END_VAR
VAR_OUTPUT
(* new length of the string in bytes *)
newStringLength :UDINT := 0;
END_VAR
VAR
stringLength :UDINT;
codepoints :POINTER TO UnicodeCodePoint;
codepointCount :UDINT;
END_VAR

Implementation

stringLength := GetUtf8StringLength(stringAddress);
TrimLeftUtf8String := stringLength;
RETURN (stringLength = 0);
codepoints := __NEW(UnicodeCodepoint, stringLength);
RETURN(codepoints = 0);

GetCodepointsFromUtf8String(
utf8StringAddress := stringAddress,
utf8StringByteCount := stringLength,
codePointBuffer := codepoints,
bufferSize := stringLength*SIZEOF(UnicodeCodepoint),
codePointsCount => codepointCount
);

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

Tc2_System.MEMSET(stringAddress, 0, stringLength);

GetUtf8StringFromCodepoints(
addressOfCodePoints := codepoints,
codePointCount := codepointCount,
utf8StringBuffer := stringAddress,
bufferSize := stringLength + 1, // stringlength + NULL terminator
utf8StringByteCount => newStringLength
);

__DELETE(codepoints);
TrimLeftUtf8String := newStringLength;