Skip to main content

GetAnyStringLength

Short summary

This functions returns the length (in bytes for Utf-8, in words for Utf-16) and the character count of an AnyString (without null terminator).

Attention: All strings are handled as null-terminated byte streams

  • Return type: UDINT

Parameters

NameTypeCommentKind
stringToCheckANY_STRINGany string that should be processedinput
lengthUDINTlength of the string: in bytes for string, in words for wstringoutput
characterCountUDINTnumber of unicode codepointsoutput

Code

Declaration

FUNCTION GetAnyStringLength :UDINT;
VAR_INPUT
(* any string that should be processed*)
stringToCheck :ANY_STRING;
END_VAR
VAR_OUTPUT
(* length of the string: in bytes for string, in words for wstring *)
length :UDINT;
(* number of unicode codepoints *)
characterCount :UDINT;
END_VAR
VAR
END_VAR

Implementation

GetAnyStringLength := 0;
RETURN((stringToCheck.pValue = 0) OR stringToCheck.diSize <= 0);

CASE stringToCheck.TypeClass OF
__SYSTEM.TYPE_CLASS.TYPE_STRING:
GetUtf8StringLength(stringToCheck.pValue, byteCount => length, characterCount => characterCount);
__SYSTEM.TYPE_CLASS.TYPE_WSTRING:
GetUtf16StringLength(stringToCheck.pValue, wordCount => length, characterCount => characterCount);
ELSE
; // do nothing
END_CASE
GetAnyStringLength := length;