SPVM::StringBuffer − String Buffers
The StringBuffer class in SPVM has methods to manipulate string buffers.
use
StringBuffer;
# new
my $buffer = StringBuffer−>new;
my $buffer = StringBuffer−>new("abc");
# push string
$buffer−>push("def");
# Convert to the string
my $string = $buffer−>to_string;
The "string" stored in a StringBuffer object always starts at index 0.
The charactors in the range that is greater than or equal to "length" field and less than "capacity" field are filled with "\0".
|
• |
Cloneable |
|||
|
• |
Comparable |
|||
|
• |
EqualityCheckable |
"has capacity : virtual ro int;"
The capacity. This is the length of the internally reserved characters to extend the length of the string buffer.
"has length : virtual ro int;"
The length of the string buffer.
"has string : mutable string;"
The internal string stored in the StringBuffer object.
"static method new : StringBuffer ($string : string = undef, $capacity : int = −1);"
Creates a new "StringBuffer" object using "new_len".
The passed $length to "new_len" is the length of $string. If the string is undef, it is 0.
$string is copied to the value of the the created string buffer.
"static method new_len : StringBuffer ($length : int, $capacity : int = −1);"
Creates a new "StringBuffer" object with $length and $capacity.
If $capacity is less than 0, $capacity is set to an appropriate value.
If $length is greater than $capacity, $capacity is set to $length.
Exceptions:
$length must be greater than or equal to 0. Otherwise an exception is thrown.
"static method new_ref : StringBuffer ($string : mutable string);"
Creates a new StringBuffer object, and set "string" field to the string $string, and return the new object.
Exceptions:
The array $array must be defined. Otherwise an exception is thrown.
"method push : void ($string : string, $offset : int = 0, $length : int = −1);"
Adds a $string from $offset to the position proceeded by $length after the end of the string in the string buffer.
Exceptions:
$string must be defined. Otherwise an exception is thrown.
$offset must be greater than or equal to 0. Otherwise an exception is thrown.
$offset + $length must be less than or equal to the length of $string. Otherwise an exception is thrown.
"method push_char : void ($char : int);"
Adds Ascii $char after the end of the string in the string buffer.
"method reserve : void ($new_capacity : int);"
Reserves the characters that size is $new_capacity.
If $new_capacity is greater than the capacity of the string buffer, the capacity of the string buffer is extended to $new_capacity.
Exceptions:
$new_capacity must be greater than or equal to 0. Otherwise an exception is thrown.
"method to_string : string ();"
Creates a new string with the length of the buffer and copies all characters in the buffer into the new string, and returns it.
"method get_string : string ();"
Returns the internal "string".
"method set_length : void ($length : int);"
Sets "length" fields.
If the length $length is greater than "length" field, the characters of the exceeding part are filled with "\0".
"method set : void ($string : string);"
Sets the string $string.
"method compare_string : int ($string : string);"
The return value is the same as the return value of "cmp" operator given "$self−>to_string" and $string.
"method equals_string : int ($string : string);"
If the string contained in the StringBuffer instance is equal to the string $string, returns 1, otherwise returns 0.
"method clone ();"
Clones this string buffer and returns it.
This method is a method implementation for Cloneable interface.
"method cmp : int ($a : StringBuffer, $b : StringBuffer);"
Compares two StringBuffer and returns its result.
The return value is the same as the return value of "cmp" operator given "$a−>to_string" or undef(if $a is undef) and "$b−>to_string" or undef(if $b is undef).
This method is a method implementation for Comparable interface.
"method eq : int ($a : StringBuffer, $b : StringBuffer);"
Checks if $a is equal to $b, and returns its result.
Implementation:
If $a and $b is defined, calls "cmp" method. If the return value is 0, returns 1, otherwise 0.
If only $a is not defined, returns 0.
If only $b is not defined, returns 0.
If both $a and $b is not defined, returns 1.
This method is a method implementation for EqualityCheckable interface.
"method substr : string ($offset : int, $length : int = −1, $replacement : string = undef);"
Calls Fn#substr method given the value of "string" field, $offset, $length, and returns its return value.
If the replacement string $replacement is specified, also calls "replace" method.
Exceptions:
The offset $offset + the length $length must be less than or equal to the value of length field. Otherwise an exception is thrown.
Exceptions thrown by Fn#substr method could be thrown.
"method index : int ($substring : string, $begin : int = 0, $end : int = −1);"
Calls Fn#index method given "string" field, $substring, $bigen, $end, and retunrs its return value.
If $end is less than 0, it is set to the value of "length" field.
Exceptions:
The begin $begin must be between 0 and the length of string field. Otherwise an exception is thrown.
The end $end must be less than or equal to the length of string field. Otherwise an exception is thrown.
Exceptions thrown by Fn#index method could be thrown.
"static method contains : int ($substring : string, $begin : int = 0, $end : int = −1);"
Checks if "string" field contains $substring.
Implementation:
The alias for the following code using "index" method.
my $ret = $self−>index($substring, $begin, $end) >= 0;
Exceptions:
Exceptions thrown by "index" method could be thrown.
Copyright (c) 2023 Yuki Kimoto
MIT License