Skip to content

String natives

IllidanS4 edited this page Apr 19, 2019 · 22 revisions

Construction and destruction

str_new

native String:str_new(const str[], str_create_mode:mode=str_preserve);

Creates a new (local) dynamic string from a string buffer (packed or unpacked). The mode parameter specifies the combination of operations that should be performed on the string after creation. If str_truncate is set, the function will trim the cells to single bytes. If str_no_nulls is set, it will replace all occurences of the null character with 0x00FFFF00 (the highest byte is 0 so that it is not recognized as a packed string, but it is not a null character as understood by amx_StrLen.

str_new_arr

native String:str_new_arr(const arr[], size=sizeof(arr), str_create_mode:mode=str_preserve);

Creates a new (local) dynamic string from an array of cells. mode works the same way as for str_new.

str_new_static

native String:str_new_static(const str[], str_create_mode:mode=str_preserve, size=sizeof(str));

This function is intended to be used for "static" strings, i.e. string literals ("abc") or auto-sized const strings (new const str[] = "abc";). It uses the size of the array to determine the length of the string. Thanks to this, null characters in the string are correctly included in the new string. This works for packed strings too, but null characters at the end may not be recognized.

By default, this native has an alias @ (if PP_NO_ALIASES is not defined).

str_new_buf

native String:str_new_buf(size);

Creates a new string constructed from the null character repeated size times. Intended for use as an output buffer.

str_addr

native AmxString:str_addr(StringTag:str);
native ConstAmxString:str_addr_const(ConstStringTag:str) = str_addr;

This function is called by every assignment to an AmxString or ConstAmxString variable. It obtains the address of the string object relative to the data section of the AMX machine, so that it will be rejected by amx_GetAddr and handled by its hook.

str_buf_addr

native AmxStringBuffer:str_buf_addr(StringTag:str);

Like str_addr, but returns the address of the character data of the string instead of the string instance itself. Useful for SA-MP native functions that do not call amx_GetAddr to obtain the address. Automatically called when a conversion to AmxStringBuffer occurs.

str_acquire

native String:str_acquire(StringTag:str);

Increases the reference count stored in the string instance. If the reference count was 0 previously (which it is for newly created strings), the string instance is moved to the global pool and will not be collected automatically. See garbage collection and resource management.

str_release

native String:str_release(StringTag:str);

Decreases the reference count stored in the string instance. If the reference count was 1 previously, the string is moved to the local pool, making it eligible for automatic collection. If the reference count was 0, an error is raised. See garbage collection and resource management.

str_delete

native str_delete(StringTag:str);

Frees the memory containing the string object. Not recommended to use if you pass the string to other functions that may store the reference, since if the string isn't acquired, it will be collected eventually.

str_valid

native bool:str_valid(ConstStringTag:str);

Returns true if the string pointer points to a valid string. Shouldn't be necessary to use.

str_clone

native String:str_clone(ConstStringTag:str);

Creates a new string with the copy of characters from str.

str_val

native String:str_val(AnyTag:value, TagTag:tag_id=tagof(value));

Converts a value to a string. The tag of the value determines its format. This function has an alias @@, enabled if PP_SYNTAX_@@ is defined.

str_val_arr

native String:str_val_arr(AnyTag:value[], size=sizeof(value), TagTag:tag_id=tagof(value));

Converts an array to a string. The tag of the array determines its format.

Properties

str_len

native str_len(ConstStringTag:str);

Returns the length of the string (in cells).

str_get

native str_get(ConstStringTag:str, buffer[], size=sizeof(buffer), start=0, end=cellmax);

Copies the cells from a dynamic string to a buffer. Start and end index in the string may be specified to copy only a part of the string. Returns the number of characters copied. start and end may be negative, meaning the offset from the end of the string.

str_getc

native str_getc(ConstStringTag:str, pos);

Returns the cell/character at the specified position in the string, or INVALID_CHAR if pos is invalid.

str_setc

native str_setc(StringTag:str, pos, value);

Sets the cell/character at the specified position. Returns the original character, or INVALID_CHAR on failure.

str_cmp

native str_cmp(ConstStringTag:str1, ConstStringTag:str2);

Compares two strings for (case-sensitive) equality. Returns a negative value if the first string precedes the second, a positive number if the first string follows the second, or 0 if they are equal. Used by the == operator.

str_empty

native bool:str_empty(ConstStringTag:str);

Returns true if the string is empty (i.e. its length is zero).

str_eq

native bool:str_eq(ConstStringTag:str1, ConstStringTag:str2);

Return true if the two strings are equal (if they contain the same characters). Used by operator==(StringTag:,StringTag:).

str_findc

native str_findc(ConstStringTag:str, value, offset=0);

Attempts to find a character in the string. Returns the first position of the character (starting from offset), or -1 on failure.

str_find

native str_find(ConstStringTag:str, ConstStringTag:value, offset=0);

Attempts to find a substring in the string. Returns the first position of the substring (starting from offset), or -1 on failure.

Immutable operations

These functions create a new string every time they are used.

str_cat

native String:str_cat(ConstStringTag:str1, ConstStringTag:str2);

Concatenates two strings together, always producing a new string. Used by the + operator.

str_sub

native String:str_sub(ConstStringTag:str, start=0, end=cellmax);

Returns a substring from the specified string.

str_split

native List:str_split(ConstStringTag:str, const delims[]);
native List:str_split_s(ConstStringTag:str, ConstStringTag:delims);

Separates the input string using delimiter characters. The result is a list of strings (non-dynamic).

str_join

native String:str_join(List:list, const delim[]);
native String:str_join_s(List:list, ConstStringTag:delim);

Joins a list of values with a delimiter. The values may be any type, in which case str_val is used on them.

str_to_lower

native String:str_to_lower(ConstStringTag:str);

Creates a copy of the string with all characters converted to lowercase. For ANSI characters, this function takes into account the current locale (if pp_locale was used).

str_to_upper

native String:str_to_upper(ConstStringTag:str);

Creates a copy of the string with all characters converted to upperase. For ANSI characters, this function takes into account the current locale (if pp_locale was used).

Mutable operations

These functions return the target string, without creating a new one.

str_set

native String:str_set(StringTag:target, StringTag:other);

Modifies the characters of the first string to match the second string.

str_append

native String:str_append(StringTag:target, StringTag:other);

Appends the characters of the second string to the first one.

str_ins

native String:str_ins(StringTag:target, StringTag:other, pos);

Inserts a string into another string on the specified position.

str_del

native String:str_del(StringTag:target, start=0, end=cellmax);

Deletes a part of the string as [start, end). If end is smaller than start, it leaves the specified range and deletes the surrounding parts.

str_clear

native String:str_clear(StringTag:str);

Removes all characters from the string. Equivalent to str_del with the default values.

str_resize

native String:str_resize(StringTag:str, size, padding=0);

Sets the length of the string. If the string will be longer, padding will fill the new cells.

str_set_to_lower

native String:str_set_to_lower(StringTag:str);

Converts all characters in the string to lowercase. For ANSI characters, this function takes into account the current locale (if pp_locale was used).

str_set_to_upper

native String:str_set_to_upper(StringTag:str);

Converts all characters in the string to uppercase. For ANSI characters, this function takes into account the current locale (if pp_locale was used).

Other

str_format

native String:str_format(const format[], AnyTag:...);
native String:str_format_s(ConstStringTag:format, AnyTag:...);

Creates a new string with the specified format with its components provided in the variable number of arguments. More in string formatting.

str_set_format

native String:str_set_format(StringTag:target, const format[], AnyTag:...);
native String:str_set_format_s(StringTag:target, ConstStringTag:format, AnyTag:...);

Sets the contents of target to be in the specified format with its components provided in the variable number of arguments. More in string formatting.

str_match

native bool:str_match(ConstStringTag:str, const pattern[], &pos=0, regex_options:options=regex_default);
native bool:str_match_s(ConstStringTag:str, ConstStringTag:pattern, &pos=0, regex_options:options=regex_default);

Performs a regular expression match on str with the specified pattern. More in regular expressions.

str_extract

native List:str_extract(ConstStringTag:str, const pattern[], &pos=0, regex_options:options=regex_default);
native List:str_extract_s(ConstStringTag:str, ConstStringTag:pattern, &pos=0, regex_options:options=regex_default);

Performs a regular expression match on str with the specified pattern, and returns a list of all captured groups in the string (or List:0 if the pattern was not found). More in regular expressions.

str_replace

native String:str_replace(ConstStringTag:str, const pattern[], const replacement[], &pos=0, regex_options:options=regex_default);
native String:str_replace_s(ConstStringTag:str, ConstStringTag:pattern, ConstStringTag:replacement, &pos=0, regex_options:options=regex_default);

Replaces a portion of the string that matches pattern with replacement. Variables like $1, $2 etc. in the replacement can be used to refer to captured groups. More in regular expressions.

str_replace_list

native String:str_replace_list(ConstStringTag:str, const pattern[], List:replacement, &pos=0, regex_options:options=regex_default);
native String:str_replace_list_s(ConstStringTag:str, ConstStringTag:pattern, List:replacement, &pos=0, regex_options:options=regex_default);

Replaces a portion of the string that matches pattern with a string in replacement. The replacement is selected based on the index of the first successfully matched group, i.e. a pattern (a)|(b) will select replacement[0] if a was matched, or replacement[1] if b was matched. Variables like $1, $2 etc. in the replacement can be used to refer to captured groups that begin at the first matched one. More in regular expressions.

str_replace_func

native String:str_replace_func(ConstStringTag:str, const pattern[], const function[], &pos=0, regex_options:options=regex_default, const additional_format[]="", AnyTag:...);
native String:str_replace_func_s(ConstStringTag:str, ConstStringTag:pattern, const function[], &pos=0, regex_options:options=regex_default, const additional_format[]="", AnyTag:...);

Calls function for each matched occurence of pattern in the string. The public function shall receive the contents of all groups in the match as sequential arguments in the form of group[], group_length for all groups, matched or unmatched (group_length will be 0 then). Moreover, the function is expected to return a String-tagged value containing the replacement. More in regular expressions.

str_set_replace

native String:str_set_replace(StringTag:target, ConstStringTag:str, const pattern[], const replacement[], &pos=0, regex_options:options=regex_default);
native String:str_set_replace_s(StringTag:target, ConstStringTag:str, ConstStringTag:pattern, ConstStringTag:replacement, &pos=0, regex_options:options=regex_default);

Like str_replace, but modifies the contents of target instead of creating a new string.

str_set_replace_list

native String:str_set_replace_list(StringTag:target, ConstStringTag:str, const pattern[], List:replacement, &pos=0, regex_options:options=regex_default);
native String:str_set_replace_list_s(StringTag:target, ConstStringTag:str, ConstStringTag:pattern, List:replacement, &pos=0, regex_options:options=regex_default);

Like str_replace_list, but modifies the contents of target instead of creating a new string.

str_set_replace_func

native String:str_set_replace_func(StringTag:target, ConstStringTag:str, const pattern[], const function[], &pos=0, regex_options:options=regex_default, const additional_format[]="", AnyTag:...);
native String:str_set_replace_func_s(StringTag:target, ConstStringTag:str, ConstStringTag:pattern, const function[], &pos=0, regex_options:options=regex_default, const additional_format[]="", AnyTag:...);

Like str_replace_func, but modifies the contents of target instead of creating a new string.

Clone this wiki locally