TrimRightUtf8String
Short summary
This function removes whitespace from the right side (end) of a utf-8 string. It returns the string length in bytes.
Example:
myString :String(10) := 'Test ' ;
newLength :UDINT;
------------------
TrimRightUtf8String(
stringAddress := ADR(myString),
newStringLength => newLength
);
- Return type:
UDINT
Parameters
| Name | Type | Comment | Kind |
|---|---|---|---|
| stringAddress | POINTER TO BYTE | pointer to the utf-8 string | input |
| newStringLength | UDINT | new length of the string in bytes | output |
Code
Declaration
FUNCTION TrimRightUtf8String :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);
TrimRightUtf8String := stringLength;
RETURN (stringLength = 0);
codepoints := __NEW(UnicodeCodepoint, stringLength);
RETURN(codepoints = 0);
GetCodepointsFromUtf8String(
utf8StringAddress := stringAddress,
utf8StringByteCount := stringLength,
codePointBuffer := codepoints,
bufferSize := stringLength*SIZEOF(UnicodeCodepoint),
codePointsCount => codepointCount
);
TrimRightCodepoints(
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);
TrimRightUtf8String := newStringLength;