GetUtf8StringFromCodepoints
Short summary
This functions creates an UTF-8 string from a code points array and stores it to the given string buffer. If the buffer size is lesser then the required string size, the string will be cut after bufferSize-1 bytes.
Example:
myString :String(10) := 'Test';
myCodepoints :Array[0..2] OF UnicodeCodepoint := [16#0041, 16#0308, 16#0051];
codepointCount :UDINT := 3;
------------------
GetUtf8StringFromCodepoints(
addressOfCodePoints := ADR(myCodepoints),
codePointsCount := codepointCount,
utf8StringBuffer := ADR(myString),
bufferSize := SIZEOF(myString),
utf8StringByteCount =>
);
- Return type: UDINT
Parameters
| Name | Type | Comment | Kind |
|---|---|---|---|
| addressOfCodePoints | POINTER TO UnicodeCodePoint | pointer to the codepoint sequence | input |
| codePointCount | UDINT | number of codepoints | input |
| utf8StringBuffer | POINTER TO BYTE | pointer to the string buffer, e.g. ADR(myUtf8String) | input |
| bufferSize | UDINT | size of the given utf-8 string buffer in bytes, e.g. SIZEOF(myUtf8String) | input |
| utf8StringByteCount | UDINT | length of the stored utf-8 string | output |
Code
Declaration
FUNCTION GetUtf8StringFromCodepoints
VAR_INPUT
(*pointer to the codepoint sequence*)
addressOfCodePoints :POINTER TO UnicodeCodePoint;
(* number of codepoints *)
codePointCount :UDINT;
(* pointer to the string buffer, e.g. ADR(myUtf8String) *)
utf8StringBuffer :POINTER TO BYTE;
(* size of the given utf-8 string buffer in bytes, e.g. SIZEOF(myUtf8String) *)
bufferSize :UDINT;
END_VAR
VAR_OUTPUT
(* length of the stored utf-8 string *)
utf8StringByteCount :UDINT := 0;
END_VAR
VAR CONSTANT
END_OF_STRING :BYTE := 16#00;
END_VAR
VAR
codePointIndex :UDINT;
currentCodePoint :UnicodeCodePoint;
octetCount :BYTE(0..4);
END_VAR
Implementation
RETURN((addressOfCodePoints = 0) OR_ELSE (utf8StringBuffer = 0));
WHILE (codePointIndex < codePointCount)
AND_THEN (addressOfCodePoints[codePointIndex] > 0)
AND_THEN (utf8StringByteCount < bufferSize)
DO
currentCodePoint := addressOfCodePoints[codePointIndex];
octetCount := GetOctectCountByCodepoint(currentCodePoint);
IF (octetCount = 0) THEN
RETURN;
END_IF
EncodeCodePointToOctet(currentCodePoint, octetCount, ADR(utf8StringBuffer[utf8StringByteCount]));
codePointIndex := codePointIndex + 1;
utf8StringByteCount := utf8StringByteCount + octetCount;
END_WHILE
IF ( utf8StringByteCount < bufferSize-1 ) THEN
(* add null terminator*)
utf8StringBuffer[utf8StringByteCount] := END_OF_STRING;
ELSE
(* add null terminator*)
utf8StringBuffer[bufferSize-1] := END_OF_STRING;
END_IF