Name

SPVM::Fn - SPVM Starndard Functions

Usage

use Fn;

my $byte_max = Fn->BYTE_MAX();
my $short_max = Fn->SHORT_MAX();
my $int_max = Fn->INT_MAX();
my $long_max = Fn->LONG_MAX();

# Cut a newline LF
{
  my $ret = Fn->chompr("abc\n");
}

# Copy a string
{
  my $string = "abc";
  my $string_copy = Fn->copy_string ($string);
}

# Search substr
{
  my $found_offset = Fn->index("pppabcde", "bcd", 2);
}

# split a string by the specific separator
my $string = "foo,bar,baz";
my $splited_strs = Fn->split(",", $string);

# Copy a byte array
{
  my $nums = [(byte)1, 2, 3];
  my $nums_copy = Fn->copy_array_byte($nums);
}

# Copy a short array
{
  my $nums = [(short)1, 2, 3];
  my $nums_copy = Fn->copy_array_short($nums);
}

# Copy a int array
{
  my $nums = [1, 2, 3];
  my $nums_copy = Fn->copy_array_int($nums);
}

# Copy a long array
{
  my $nums = [(long)1, 2, 3];
  my $nums_copy = Fn->copy_array_long($nums);
}

# Copy a float array
{
  my $nums = [1.5f, 2.5f, 3.5f];
  my $nums_copy = Fn->copy_array_float($nums);
}

# Copy a double array
{
  my $nums = [1.5, 2.5, 3.5];
  my $nums_copy = Fn->copy_array_double($nums);
}

# Copy a string array
{
  my $strs = ["abc", "def", "ghi"]
  my $strs_copy = Fn->copy_array_string($strs);
}

# Check if the two byte arrays equal
{
  my $nums1 = [(byte)1, 2];
  my $nums2 = [(byte)1, 2];
  my $ret = Fn->equals_array_byte($nums1, $nums2);
}

# Check if the two short arrays equal
{
  my $nums1 = [(short)1, 2];
  my $nums2 = [(short)1, 2];
  my $ret = Fn->equals_array_short($nums1, $nums2);
}

# Check if the two int arrays equal
{
  my $nums1 = [(int)1, 2];
  my $nums2 = [(int)1, 2];
  my $ret = Fn->equals_array_int($nums1, $nums2);
}

# Check if the two long arrays equal
{
  my $nums1 = [(long)1, 2];
  my $nums2 = [(long)1, 2];
  my $ret = Fn->equals_array_long($nums1, $nums2);
}

# Check if the two float arrays equal
{
  my $nums1 = [(float)1, 2];
  my $nums2 = [(float)1, 2];
  my $ret = Fn->equals_array_float($nums1, $nums2);
}

# Check if the two double arrays equal
{
  my $nums1 = [(double)1, 2];
  my $nums2 = [(double)1, 2];
  my $ret = Fn->equals_array_double($nums1, $nums2);
}

# Check if the two string arrays equal
{
  my $strs1 = ["abc", "def"];
  my $strs2 = ["abc", "def"];
  my $ret = Fn->equals_array_string($strs1, $strs2);
}

# Copy object array
my $objects = [(object)Int->new(1), Int->new(2), Int->new(3)];
my $objects_copy = Fn->copy_array_object($objects, method : object ($obj : object) {
  my $int_obj = (Int)$obj;
  my $new_int_obj = Int->new($int_obj->value);
  return $new_int_obj;
});

Description

Fn module provides SPVM Starndard Functions. Fn contains number, string and array utilities.

Class Methods

Class method of Fn module.

DBL_MAX

static method DBL_MAX : double ()

Return the value of DBL_MAX macro defined in float.h header of C language.

DBL_MIN

static method DBL_MIN : double ()

Return the value of DBL_MIN macro defined in float.h header of C language.

FLT_MAX

static method FLT_MAX : float ()

Return the value of FLT_MAX macro defined in float.h header of C language.

FLT_MIN

static method FLT_MIN : float ()

Return the value of FLT_MIN macro defined in float.h header of C language.

INT16_MAX

static method INT16_MAX : short ()

Return 32767. The maximum value of the signed 16bit integer.

INT16_MIN

static method INT16_MIN : short ()

Return -32768. The minimal value of the signed 16bit integer.

INT32_MAX

static method INT32_MAX : int ()

Return 2147483647. The maximum value of the signed 32bit integer.

INT32_MIN

static method INT32_MIN : int ()

Return -2147483648. The minimal value of the signed 32bit integer.

INT64_MAX

static method INT64_MAX : long ()

Return 9223372036854775807. The maximum value of the signed 64bit integer.

INT64_MIN

static method INT64_MIN : long ()

Return -9223372036854775808. The minimal value of the signed 64bit integer.

INT8_MAX

INT8_MAX : byte ()

Return 127. The maximum value of the signed 8bit integer.

INT8_MIN

static method INT8_MIN : byte ()

Return -128. The minimal value of the signed 8bit integer.

UINT16_MAX

static method UINT16_MAX : short ()

Return -1. This represents 0xFFFF in the unsigned 16bit integer in 2's complement.

UINT32_MAX

static method UINT32_MAX : int ()

Return -1. This represents 0xFFFFFFFF in the unsigned 32bit integer in 2's complement.

UINT64_MAX

static method UINT64_MAX : long ()

Return -1. This represents 0xFFFFFFFFFFFFFFFF in the unsigned 64bit integer in 2's complement.

UINT8_MAX

static method UINT8_MAX : byte ()

Return -1. The same bit expression of 0xFF in the unsigned 8bit integer in 2's complement.

DOUBLE_MAX

static method DOUBLE_MAX : double ()

Same as "DBL_MAX".

DOUBLE_MIN

static method DOUBLE_MIN : double ()

Same as "DBL_MIN".

FLOAT_MAX

static method FLOAT_MAX : float ()

Same as "FLT_MAX".

FLOAT_MIN

static method FLOAT_MIN : float()

Same as "FLT_MIN".

SHORT_MAX

static method SHORT_MAX : short ()

Same as "INT16_MAX".

SHORT_MIN

static method SHORT_MIN : short ()

Same as "INT16_MIN".

INT_MAX

static method INT_MAX : int ()

Same as "INT32_MAX".

INT_MIN

static method INT_MIN : int ()

Same as "INT32_MIN".

LONG_MAX

static method LONG_MAX : long ()

Same as "INT64_MAX".

LONG_MIN

static method LONG_MIN : long ()

Same as "INT64_MIN".

INT8_MAX

static method INT8_MAX : byte ()

Same as "INT8_MAX".

BYTE_MIN

static method BYTE_MIN : byte ()

Same as "INT8_MIN".

USHORT_MAX

static method USHORT_MAX : short ()

Same as "UINT16_MAX".

UINT_MAX

static method UINT_MAX : int ()

Same as "UINT32_MAX".

ULONG_MAX

static method ULONG_MAX : long

Same as "UINT64_MAX".

UBYTE_MAX

static method UBYTE_MAX : byte ()

Same as "UINT8_MAX".

abs

static method abs : int ($x : int)

Return the absolute value.

chomp

static method chomp : void ($string : mutable string)

Remove \n of the end of the string. This method changes the given string itself.

If the string is undef or the length is zero, does nothing.

chompr

static method chompr : string ($string : string)

Copy the string and remove "\n" of the end of the string and return it.

chr

static method contains : int ($string : string, $sub_string : string)

Convert Unicode code point to a UTF-8 character. If the Unicode code point is not a Unicode scalar value, return undef.

See also Unicode-is_unicode_scalar_value|SPVM::Unicode/"is_unicode_scalar_value">.

contains

static method index : int ($string : string, $sub_string : string, $start_pos : int)

Search for the substring in the string. If the substring is found, return 1. Otherwise return 0.

copy_string

static method copy_string : string ($string : string)

Copy the value of the string, and return a new string.

If the argument string is undef, return undef.

crand

static method crand : int ($seed : int*);

Get random number(0 <= rundom_number <= Fn->RAND_MAX) with a seed.

The seed is updated.

This method is thread safe different from rand function of C language.

Examples:

use Time;
my $seed = (int)Time->time;
my $crand0 = Fn->crand(\$seed);
my $crand1 = Fn->crand(\$seed);

get_next_code_point

static method get_next_code_point : int ($str : string, $offset_ref : int*)

Get a Unicode codepoint from UTF-8 string with the reference of the offset of the string.

The offset is updated to the position of the next UTF-8 character.

If the offset is over the length of the string, return a negative value.

If an invalid code point is got, return a negative value.

hex

static method hex : int ($hex_string : string)

Convert hex string to int value.

the hex string must be defined. Otherwise an exception will occur.

the hex string must be a valid expression which is represented by a regex "^([0-9a-fA-F]{1,8})$". Otherwise an exception will occur.

index

static method index : int ($string : string, $sub_string : string, $start_pos : int)

Search for the substring in the string from the starting position and return the found position. If the substring is not found, return -1.

index_len

static method index_len : int ($string : string, $sub_string : string, $start_pos : int, $max_string_length : int)

Same as the "index" method except that the max length of the string can be specified.

If the max string length of the argument is greater than the lenght of the string, the max string length become the length of string.

is_alnum

static method is_alnum : int ($code_point : int)

If character is alphanumeric('A'-'Z', 'a'-'z', '0'-'9'), return 1. Otherwise return 0.

is_alpha

static method is_alpha : int ($code_point : int)

If character is alphabetic('A'-'Z', 'a'-'z'), return 1. Otherwise return 0.

is_blank

static method is_blank : int ($code_point : int)

If character is blank(' ', '\t'), return 1. Otherwise return 0.

is_cntrl

static method is_cntrl : int ($code_point : int)

If character is a control character(0x00-0x1F, 0x7F), return 1. Otherwise return 0.

is_digit

static method is_digit : int ($code_point : int)

If character is decimal digit 0-9, return 1. Otherwise return 0.

is_graph

static method is_graph : int ($code_point : int)

If character has graphical representation(0x21-0x7E), return 1. Otherwise return 0.

is_hex_digit

static method is_hex_digit : int ($code_point : int)

If the character is hexadecimal digit 0-9a-fA-F, return 1. Otherwise return 0.

is_lower

static method is_lower : int ($code_point : int)

If character is lowercase letter('a'-'z'), return 1. Otherwise return 0.

is_perl_space

static method is_perl_space : int ($code_point : int)

If character is Perl space character(' ', '\r', '\n', '\t', '\f'), return 1. Otherwise return 0.

is_perl_word

static method is_perl_word : int ($code_point : int)

If character is Perl word character('a'-'z', 'A'-'Z', '_', '0'-'9'), return 1. Otherwise return 0.

is_print

static method is_print : int ($code_point : int)

If character is printable(0x20-0x7E), return 1. Otherwise return 0.

is_punct

static method is_punct : int ($code_point : int)

If character is a punctuation character(0x21-0x2f, 0x3a-0x40, 0x5b-0x60, 0x7b-0x7e), return 1. Otherwise return 0.

is_space

static method is_space : int ($code_point : int)

If character is a white-space(' ', '\t', '\n', '\v', '\f', '\r'), return 1. Otherwise return 0.

is_upper

static method is_upper : int ($code_point : int)

If character is uppercase letter('A'-'Z'), return 1. Otherwise return 0.

is_xdigit

static method is_xdigit : int ($code_point : int)

If character is hexadecimal digit('0'-'9', 'A'-'F', 'a'-'f'), return 1. Otherwise return 0.

is_mulnum_array

static method is_mulnum_array : int ($object : object)

If the object is a multi numeric array, returns 1, otherwise returns 0.

If the object is NULL, returns 0.

is_numeric_array

static method is_numeric_array : int ($object : object)

If the object is a numeric array, returns 1, otherwise returns 0.

If the object is NULL, returns 0.

is_object_array

static method is_object_array : int ($object : object)

If the object is a object array, returns 1, otherwise returns 0.

If the object is NULL, returns 0.

join

static method join : string ($sep : string, $strings : string[])

Join a string array with separater and return it.

If separater is undef, an exception occurs.

If string array is undef, an exception occurs.

labs

static method labs : long ($x : long)

Return the absolute value.

lc

static method lc : string ($string : string)

Convert uppercase string to lowercase string.

lcfirst

static method lcfirst : string ($string : string)

Convert first chracter of string from uppercase to lowercase.

look_next_code_point

static method look_next_code_point : int ($string : string, $offset_ref : int*)

Same as "get_next_code_point", but the offset is not updated.

memcpy

static method memcpy : void ($dest : object, $dest_byte_offset : int, $source : object, $source_byte_offset : int, $byte_length : int);

The destination must be a string type, a numeric type, or a multi numeric type, otherwise an exception is thrown.

If the destination is a read-only string, an exception is thrown.

The source must be a string type, a numeric type, or a multi numeric type, otherwise an exception is thrown.

Copy the source to the destination with the given each offset and the given length by byte unit.

If source data range and destination data overlap, the result is not guaranteed.

The destination must be defined. Otherwise an exception will occur.

The source must be defined. Otherwise an exception will occur.

The length must be more than or equals to 0. Otherwise an exception will occur.

The destination byte offset + byte length must be within the byte range of the destination. Otherwise an exception will occur.

The source byte offset + byte length must be within the byte range of the source. Otherwise an exception will occur.

If source data range and destination data overlap, the result is not guaranteed.

memmove

static method memmove : void ($dest : object, $dest_byte_offset : int, $source : object, $source_byte_offset : int, $byte_length : int);

The destination must be a string type, a numeric type, or a multi numeric type, otherwise an exception is thrown.

If the destination is a read-only string, an exception is thrown.

The source must be a string type, a numeric type, or a multi numeric type, otherwise an exception is thrown.

Copy the source to the destination with the given each offset and the given length by byte unit.

If source data range and destination data overlap, the result is not guaranteed.

The destination must be defined. Otherwise an exception will occur.

The source must be defined. Otherwise an exception will occur.

The length must be more than or equals to 0. Otherwise an exception will occur.

The destination byte offset + byte length must be within the byte range of the destination. Otherwise an exception will occur.

The source byte offset + byte length must be within the byte range of the source. Otherwise an exception will occur.

ord

static method ord : int ($utf8_character : string)

Gets the Unicode code point from the first character of the specified <UTF-8> string.

If the specified UTF-8 character is undef, returns a negative value.

If the length of the specified UTF-8 string is 0, returns a negative value.

If the specified UTF-8 character is invalid UTF-8 character, returns a negative value.

powi

static method powi : int ($x : int, $y : int)

Calculate the exponentiation.

powl

static method powl : long ($x : long, $y : long)

Calculate the exponentiation.

rand

static method rand : double ($seed : int*)

Get random number(0 <= random_number < 1)with a seed.

The seed is updated.

This method is thread safe different from rand function of C language.

Examples:

use Time;
my $seed = (int)Time->time;
my $rand0 = Fn->rand(\$seed);
my $rand1 = Fn->rand(\$seed);

repeat

static method repeat : double ($string : string, $count : int)

Get the repeat string.

If the string is not defined, an exception will be thrown.

If the repeat is not more than 0, an exception will be thrown.

Examples:

# "abcabcabc"
my $repeat_string = Fn->repeat("abc", 3);

rindex

static method rindex : int ($string : string, $sub_string : string, $start_pos : int)

Search for the substring in the string from the starting position to the start of the string. and return the found position. If the substring is not found, return -1.

rindex_len

static method rindex_len : int ($string : string, $sub_string : string, $start_pos : int, $max_string_length : int)

Same as the "rindex" method except that the max length of the string can be specified.

If the max string length of the argument is greater than the lenght of the string, the max string length become the length of string.

shorten

static method shorten : void ($string : mutable string, $length : int32_t)

Shorten the string to the given length.

If the string is null, does nothing.

If the given length is greater than the length of the string, does nothing.

If the given length is lower than 0, the given length become 0.

In the level of native APIs, the charaters of the after the given length are filled with \0.

split

static method split : string[] ($sep : string, $string : string)

Split a string by the specific separator.

sprintf

static method sprintf : string ($format : string, $args : object[]...)

Create a formatted string with the format and the values.

SpecifiersDescriptionsAcceptable Types
%cAn UTF-8 characterByte, Int
%dSigned 32bit integerInt
%uUnsigned 32bit integerInt
%xUnsiged 32 bit integer to a hexadecimal string using 0-9a-zInt
%XUnsiged 32 bit integer to a hexadecimal string using 0-9A-ZInt
%ldSigned 64bit integerLong
%luUnsigned 64bit integerLong
%lxUnsiged 64 bit integer to a hexadecimal string using 0-9a-zLong
%lXUnsiged 64 bit integer to a hexadecimal string using 0-9A-ZLong
%f64bit floating pointDouble, Float
%sStringstring
%UUnicode Code Point to a UTF-8 character

Specifier Options:

Specifier options can be written between % and the character of specifier such as d, f.

Specifier optionsDescriptions
0[DECIMAL_NUMBERS]Zero padding
+Adding a plus sign
-Left justified
.[DECIMAL_NUMBERS]Precision

Examples:

# %d - "123"
my $formatted_string = Fn->sprintf("%d", 123);

# %5d - "  123"
my $formatted_string = Fn->sprintf("%5d", 123);

# %05d - "00123"
my $formatted_string = Fn->sprintf("%05d", 123);

# %+d - "+123"
my $formatted_string = Fn->sprintf("%+d", 123);

# %-5d - "123  "
my $formatted_string = Fn->sprintf("%-5d", 123);

# %d - "x"
my $formatted_string = Fn->sprintf("%c", 'x');

# %c - "あ"
my $formatted_string = Fn->sprintf("%c", Fn->ord("あ"));

# %s - "ABC"
my $formatted_string = Fn->sprintf("%s", "ABC") eq "ABC");

# %u - "4294967295"
my $formatted_string = Fn->sprintf("%u", -1) eq "4294967295");

# %f - "3.141500"
my $formatted_string = Fn->sprintf("%f", 3.1415) eq "3.141500");

# %.2f - "3.14"
my $formatted_string = Fn->sprintf("%.2f", 3.1415) eq "3.14");

# %g - "3.14"
my $formatted_string = Fn->sprintf("%g", 3.14) eq "3.14");

# %x - "ff"
my $formatted_string = Fn->sprintf("%x", 255) eq "ff");
  
# %x - "ffffffff"
my $formatted_string = Fn->sprintf("%x", -1) eq "ffffffff");

substr

static method substr : string ($string : string, $offset : int, $length : int)

Get the substring of the string with the start offset and the length.

to_double

static method to_double : double ($string : string);

Convert the string to float value.

Format is [' ' or '\t' or '\n' or '\v' or '\f' or '\r'][+ or -][zero more than 0-9][.][zero more than 0-9][e or E[+ or -]zero more than 0-9]. Internal of [] is optional.

If the convertion fails, an exception occuer.

my $string = "1.25";
my $num = to_double($string);

to_float

static method to_float : float ($string : string);

Convert the string to float value.

Format is [' ' or '\t' or '\n' or '\v' or '\f' or '\r'][+ or -][zero more than 0-9][.][zero more than 0-9][e or E[+ or -]zero more than 0-9]. Internal of [] is optional.

If the convertion fails, an exception occuer.

my $string = "1.25";
my $num = to_float($string);

to_int

static method to_int : int ($string : string, $digit : int);

Convert the string to a int value. This method is same as to_int_with_base($string, 10).

my $string = "-2147483648";
my $num = to_int($string);

to_int_with_base

static method to_int_with_base : int ($string : string, $digit : int);

Convert the string to a int value with a digit(2, 8, 10, 16).

Format is [' ' or '\t' or '\n' or '\v' or '\f' or '\r'][+ or -][0][x][one more than 0-9]. Internal of [] is optional.

If convertion fails, an exception occuer.

my $string = "-2147483648";
my $num = to_int_with_base($string, 10);

to_long

static method to_long : long ($string : string);

Convert the string to long value. This method is same as to_long($string, 10).

my $string = "-9223372036854775808";
my $num = to_long($string);

to_long_with_base

static method to_long_with_base : long ($string : string, $digit : int);

Convert the string to long value with digit(2, 8, 10, 16).

Format is [' ' or '\t' or '\n' or '\v' or '\f' or '\r'][+ or -][0][x][zero more than 0-9]. Internal of [] is optional.

If the convertion fails, an exception occuer.

my $string = "-9223372036854775808";
my $num = to_long_with_base($string, 10);

to_lower

static method to_lower : int ($code_point : int)

Convert uppercase letter('A'-'Z') to lowercase. If the character is not uppercase letter, return the character.

to_upper

static method to_upper : int ($code_point : int)

Convert lowercase letter('a'-'z') to lowercase. If the character is not uppercase letter, return the character.

trim_ascii_space

static method trim_ascii_space : string ($string : string)

Remove right and left spaces of the string. These spaces is ascii standard spaces which can be checked by is_space method.

If the argument string is undef, return undef.

uc

static method uc : string ($string : string)

Convert a lowercase string to a uppercase string.

If the string is undef, an exception occurs.

ucfirst

static method ucfirst : string ($string : string)

Convert the first character of a string to a uppercase character.

If the string is undef, an exception occurs.