Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.apax
.env
.vscode
bin
obj
TestResult
Expand Down
562 changes: 297 additions & 265 deletions apax-lock.json

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions src/TRUNC.st
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
NAMESPACE Simatic.Ax.Conversion

/// Truncates a 64-bit floating-point number (LREAL) to an integer.
///
/// This function truncates the input LREAL value to the nearest integer toward zero.
/// It handles both positive and negative values.
///
/// @param Value The LREAL value to truncate.
/// @return The truncated integer value.
FUNCTION TRUNC : DINT
VAR_INPUT
Value : LREAL;
Expand All @@ -16,6 +23,13 @@ NAMESPACE Simatic.Ax.Conversion
END_IF;
END_FUNCTION

/// Truncates a 32-bit floating-point number (REAL) to an integer.
///
/// This function truncates the input REAL value to the nearest integer toward zero.
/// It handles both positive and negative values.
///
/// @param Value The REAL value to truncate.
/// @return The truncated integer value.
FUNCTION TRUNC : DINT
VAR_INPUT
Value : REAL;
Expand All @@ -31,4 +45,5 @@ NAMESPACE Simatic.Ax.Conversion
END_IF;
END_IF;
END_FUNCTION

END_NAMESPACE
20 changes: 20 additions & 0 deletions src/strings/AnyIntToString.st
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ USING System.Strings;
USING System.Math;

NAMESPACE Simatic.Ax.Conversion

/// Converts a signed 64-bit integer (LINT) to its string representation.
///
/// This function converts a signed 64-bit integer to a string, optionally adding a sign
/// based on the specified conversion mode. It handles negative values and ensures the
/// correct number of digits are included in the output.
///
/// @param value The signed 64-bit integer to convert.
/// @param mode The conversion mode (e.g., force sign).
/// @return A string representation of the input integer.
FUNCTION INTERNAL AnyIntToString : STRING
VAR_INPUT
value : LINT;
Expand Down Expand Up @@ -42,6 +52,15 @@ NAMESPACE Simatic.Ax.Conversion
END_FOR;
END_FUNCTION

/// Converts an unsigned 64-bit integer (ULINT) to its string representation.
///
/// This function converts an unsigned 64-bit integer to a string, optionally adding a sign
/// based on the specified conversion mode. It ensures the correct number of digits are
/// included in the output.
///
/// @param value The unsigned 64-bit integer to convert.
/// @param mode The conversion mode (e.g., force sign).
/// @return A string representation of the input integer.
FUNCTION INTERNAL ULintToString : STRING
VAR_INPUT
value : ULINT;
Expand Down Expand Up @@ -80,4 +99,5 @@ NAMESPACE Simatic.Ax.Conversion
ULintToString := Concat(ULintToString, s);
END_FOR;
END_FUNCTION

END_NAMESPACE
21 changes: 21 additions & 0 deletions src/strings/ArrayToString.st
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
USING System.Strings;

NAMESPACE Simatic.Ax.Conversion.Arrays

/// Converts a subset of a character array to a string.
///
/// This function takes a character array and converts a specified range of elements
/// (from `startIdx` to `endIdx`) into a string. If the range is invalid or exceeds
/// the array bounds, an empty string is returned. The maximum string length is limited
/// to `MAX_STR_LEN`.
///
/// @param arr The input character array.
/// @param startIdx The starting index of the range to convert.
/// @param endIdx The ending index of the range to convert.
/// @return A string representation of the specified range of the array.
FUNCTION ToString : STRING
VAR_INPUT
arr : ARRAY[*] OF CHAR;
Expand Down Expand Up @@ -47,6 +59,14 @@ NAMESPACE Simatic.Ax.Conversion.Arrays

END_FUNCTION

/// Converts an entire character array to a string.
///
/// This function takes a character array and converts all its elements into a string.
/// If the array length exceeds `MAX_STR_LEN`, the resulting string is truncated to
/// the maximum allowed length.
///
/// @param arr The input character array.
/// @return A string representation of the entire array.
FUNCTION ToString : STRING
VAR_INPUT
arr : ARRAY[*] OF CHAR;
Expand Down Expand Up @@ -74,4 +94,5 @@ NAMESPACE Simatic.Ax.Conversion.Arrays
END_FOR;

END_FUNCTION

END_NAMESPACE
7 changes: 7 additions & 0 deletions src/strings/ConversionMode.st
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
NAMESPACE Simatic.Ax.Conversion

/// Represents the conversion mode for integer-to-string operations.
///
/// This type defines the available modes for formatting integer-to-string conversions:
/// - `NONE`: No special formatting is applied.
/// - `FORCE_SIGN`: Forces the inclusion of a '+' sign for positive numbers.
TYPE
ConversionMode : WORD (NONE := WORD#16#0000, FORCE_SIGN := WORD#16#0001) := NONE;
END_TYPE

END_NAMESPACE
18 changes: 18 additions & 0 deletions src/strings/CountDigits.st
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
NAMESPACE Simatic.Ax.Conversion

/// Counts the number of digits in a signed 64-bit integer (LINT).
///
/// This function calculates the number of decimal digits in the given signed 64-bit integer.
/// It iteratively checks the range of the value against increasing powers of 10.
/// The maximum number of digits it can handle is defined by `MAX_DIGITS`.
///
/// @param value The signed 64-bit integer whose digits are to be counted.
/// @return The number of decimal digits in the input value.
FUNCTION CountDigits : INT
VAR_INPUT
value : LINT;
Expand All @@ -21,6 +30,14 @@ NAMESPACE Simatic.Ax.Conversion
CountDigits := 19;
END_FUNCTION

/// Counts the number of digits in an unsigned 64-bit integer (ULINT).
///
/// This function calculates the number of decimal digits in the given unsigned 64-bit integer.
/// It iteratively checks the range of the value against increasing powers of 10.
/// The maximum number of digits it can handle is defined by `MAX_DIGITS`.
///
/// @param value The unsigned 64-bit integer whose digits are to be counted.
/// @return The number of decimal digits in the input value.
FUNCTION CountDigits : INT
VAR_INPUT
value : ULINT;
Expand All @@ -42,4 +59,5 @@ NAMESPACE Simatic.Ax.Conversion
END_FOR;
CountDigits := 20;
END_FUNCTION

END_NAMESPACE
17 changes: 16 additions & 1 deletion src/strings/HexToString.st
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ USING System;
USING System.Strings;

NAMESPACE Simatic.Ax.Conversion


/// Converts a BYTE value to its hexadecimal string representation.
///
/// This function takes a BYTE value and converts it into a two-character hexadecimal string.
/// It splits the BYTE into two nibbles, converts each nibble to its hexadecimal character,
/// and concatenates the results.
///
/// @param b The BYTE value to convert.
/// @return A two-character string representing the hexadecimal value of the input BYTE.
FUNCTION PUBLIC ByteToString : STRING
VAR_INPUT
b : BYTE;
Expand All @@ -18,6 +26,13 @@ NAMESPACE Simatic.Ax.Conversion

END_FUNCTION

/// Converts a nibble (4 bits) to its hexadecimal character representation.
///
/// This function takes a nibble (a value between 0 and 15) and returns the corresponding
/// hexadecimal character (0-9, A-F) using a lookup table.
///
/// @param nibble The nibble value to convert (0-15).
/// @return The hexadecimal character corresponding to the input nibble.
FUNCTION INTERNAL LookUpHEX : CHAR
VAR_INPUT
nibble : BYTE;
Expand Down
67 changes: 67 additions & 0 deletions src/strings/IntToString.st
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
USING System.Strings;

NAMESPACE Simatic.Ax.Conversion.Integer

/// Converts a signed 8-bit integer (SINT) to a string.
///
/// This function converts a signed 8-bit integer to its string representation.
/// The conversion supports an optional mode parameter to customize the output format.
///
/// @param value The signed 8-bit integer to convert.
/// @param mode The conversion mode (e.g., force sign).
/// @return A string representation of the input integer.
FUNCTION ToString : STRING[4]
VAR_INPUT
value : SINT;
Expand All @@ -10,6 +20,14 @@ NAMESPACE Simatic.Ax.Conversion.Integer
ToString := AnyIntToString(value := value, mode := mode);
END_FUNCTION

/// Converts an unsigned 8-bit integer (USINT) to a string.
///
/// This function converts an unsigned 8-bit integer to its string representation.
/// The conversion supports an optional mode parameter to customize the output format.
///
/// @param value The unsigned 8-bit integer to convert.
/// @param mode The conversion mode (e.g., force sign).
/// @return A string representation of the input integer.
FUNCTION ToString : STRING[4]
VAR_INPUT
value : USINT;
Expand All @@ -20,6 +38,14 @@ NAMESPACE Simatic.Ax.Conversion.Integer
ToString := AnyIntToString(value := value, mode := mode);
END_FUNCTION

/// Converts a signed 16-bit integer (INT) to a string.
///
/// This function converts a signed 16-bit integer to its string representation.
/// The conversion supports an optional mode parameter to customize the output format.
///
/// @param value The signed 16-bit integer to convert.
/// @param mode The conversion mode (e.g., force sign).
/// @return A string representation of the input integer.
FUNCTION ToString : STRING[6]
VAR_INPUT
value : INT;
Expand All @@ -30,6 +56,14 @@ NAMESPACE Simatic.Ax.Conversion.Integer
ToString := AnyIntToString(value := value, mode := mode);
END_FUNCTION

/// Converts an unsigned 16-bit integer (UINT) to a string.
///
/// This function converts an unsigned 16-bit integer to its string representation.
/// The conversion supports an optional mode parameter to customize the output format.
///
/// @param value The unsigned 16-bit integer to convert.
/// @param mode The conversion mode (e.g., force sign).
/// @return A string representation of the input integer.
FUNCTION ToString : STRING[6]
VAR_INPUT
value : UINT;
Expand All @@ -41,6 +75,14 @@ NAMESPACE Simatic.Ax.Conversion.Integer
END_FUNCTION


/// Converts a signed 32-bit integer (DINT) to a string.
///
/// This function converts a signed 32-bit integer to its string representation.
/// The conversion supports an optional mode parameter to customize the output format.
///
/// @param value The signed 32-bit integer to convert.
/// @param mode The conversion mode (e.g., force sign).
/// @return A string representation of the input integer.
FUNCTION ToString : STRING[11]
VAR_INPUT
value : DINT;
Expand All @@ -51,6 +93,14 @@ NAMESPACE Simatic.Ax.Conversion.Integer
ToString := AnyIntToString(value := value, mode := mode);
END_FUNCTION

/// Converts an unsigned 32-bit integer (UDINT) to a string.
///
/// This function converts an unsigned 32-bit integer to its string representation.
/// The conversion supports an optional mode parameter to customize the output format.
///
/// @param value The unsigned 32-bit integer to convert.
/// @param mode The conversion mode (e.g., force sign).
/// @return A string representation of the input integer.
FUNCTION ToString : STRING[11]
VAR_INPUT
value : UDINT;
Expand All @@ -61,6 +111,14 @@ NAMESPACE Simatic.Ax.Conversion.Integer
ToString := AnyIntToString(value := value, mode := mode);
END_FUNCTION

/// Converts a signed 64-bit integer (LINT) to a string.
///
/// This function converts a signed 64-bit integer to its string representation.
/// The conversion supports an optional mode parameter to customize the output format.
///
/// @param value The signed 64-bit integer to convert.
/// @param mode The conversion mode (e.g., force sign).
/// @return A string representation of the input integer.
FUNCTION ToString : STRING[20]
VAR_INPUT
value : LINT;
Expand All @@ -71,6 +129,14 @@ NAMESPACE Simatic.Ax.Conversion.Integer
ToString := AnyIntToString(value := value, mode := mode);
END_FUNCTION

/// Converts an unsigned 64-bit integer (ULINT) to a string.
///
/// This function converts an unsigned 64-bit integer to its string representation.
/// The conversion supports an optional mode parameter to customize the output format.
///
/// @param value The unsigned 64-bit integer to convert.
/// @param mode The conversion mode (e.g., force sign).
/// @return A string representation of the input integer.
FUNCTION ToString : STRING[21]
VAR_INPUT
value : ULINT;
Expand All @@ -80,4 +146,5 @@ NAMESPACE Simatic.Ax.Conversion.Integer
END_VAR
ToString := ULintToString(value := value, mode := mode);
END_FUNCTION

END_NAMESPACE
17 changes: 17 additions & 0 deletions src/strings/StringToAnyInt.st
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
USING System.Strings;

NAMESPACE Simatic.Ax.Conversion

/// Converts a string to a signed 64-bit integer (LINT).
///
/// This function parses a string and converts it to a signed 64-bit integer. It handles
/// optional '+' or '-' signs and checks for overflow. If the string is invalid, the
/// function returns FALSE and sets the output to 0.
///
/// @param str The input string to convert.
/// @return TRUE if the conversion is successful, FALSE otherwise.
FUNCTION StringToAnyInt : BOOL
VAR_INPUT
str : STRING;
Expand Down Expand Up @@ -80,6 +89,14 @@ NAMESPACE Simatic.Ax.Conversion

END_FUNCTION

/// Converts a string to an unsigned 64-bit integer (ULINT).
///
/// This function parses a string and converts it to an unsigned 64-bit integer. It handles
/// optional '+' signs and checks for overflow. If the string is invalid, the function
/// returns FALSE and sets the output to 0.
///
/// @param str The input string to convert.
/// @return TRUE if the conversion is successful, FALSE otherwise.
FUNCTION INTERNAL StringToULint: BOOL
VAR_INPUT
str : STRING;
Expand Down
11 changes: 11 additions & 0 deletions src/strings/StringToArrayOfCharCount.st
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
USING System.Strings;

NAMESPACE Simatic.Ax.Conversion.Strings.ToArray

/// Converts a string to an array of CHAR values.
///
/// This function copies characters from the input string into the provided array of CHAR.
/// It returns the number of characters copied. If the array is smaller than the string,
/// the output is truncated to fit the array size.
///
/// @param str The input string to convert.
/// @param arr The output array to store the CHAR values.
/// @return The number of characters copied to the array.
FUNCTION OfCharCount : DINT
VAR_INPUT
str : STRING;
Expand All @@ -27,4 +37,5 @@ NAMESPACE Simatic.Ax.Conversion.Strings.ToArray
END_FOR;
OfCharCount := i;
END_FUNCTION

END_NAMESPACE
Loading
Loading