Dealing with dynamic strings (xstring) in PM123 Plugins

Overview

A xstring is a dynamically allocated string of unlimited length. Different instances of xstrings may share the same storage for the string data. xstrings can be NULL.

The xstring API reference is accessible through the PLUGIN_CONTEXT parameter of the plugin_init call.

In case you have C++ you will automatically have a more convenient implementation. You should prefer the C++ syntax where available, because its implementation provides better performance. Note that you must link against the utils/cpputil_plugin library for this API to work properly within plugins. The utils/cpputil library may cause undefined behavior in plugin code!

Function Description
xstring_create Create a new instance of a xstring from a C string.
xstring_free Release a xstring and set it to NULL.
xstring_length Return the length of a xstring.
xstring_equal Test two strings for equality.
xstring_compare Compare two xstrings.
xstring_copy Assign a xstring with another instance.
xstring_copy_safe Same as xstring_copy but access to the source string is atomic.
xstring_assign Assign a xstring with the value of a C string.
xstring_cmpassign Assign a xstring with a new string only if they are different.
xstring_append Append the value of a C string to a xstring.
xstring_allocate Allocate new storage for a xstring instance.
xstring_sprintf
xstring_vsprintf
Like sprintf/vsprintf into a xstring.
xstring_deduplicate Deduplicate value of xstring instance in memory.

Guidelines

There are some guidelines you should follow when dealing with xstrings:

Furthermore there are guarantees:

Using C++

If you are compiling with a C++ compiler the type xstring is automatically replaced by a binary compatible class that provides overloaded operators for the most common operations and ensures all of the guidelines. The xstring class implicitly ensures all of the above guidelines. For this to work a global variable PLUGIN_CONTEXT Ctx; must exist and initialized by the argument ctx of plugin_init.

There is one restriction with the C++ API of xstring: You must not assign anything to a xstring instance in a static initializer, more precisely before the plugin_init call. Only calling the default constructor is valid within this context.

The xstring objects are binary compatible in C and C++. So xstring instances may be shared between C and C++ code.

Task Code example Remarks
Create a xstring with initial value NULL.
xstring str;

Create a new instance of a xstring from a C string.
xstring str("bla");

Release a xstring and set it to NULL.
str.reset();
This is implicitly done by the destructor.
Return the length of a xstring.
str.size();

Test two strings for equality.
str1 == str2

Compare two xstrings.
str1.compareTo(str2)

Assign a xstring with another instance.
str1 = str2;

Assign a xstring from a shared instance.
volatile xstring shared;
str1 = shared;
This assignment provides strong thread safety.
Assign a xstring with the value of a C string.
str1 = "text";

Assign a xstring with a new string only if they are different.
bool changed = 
str1.cmpassign(str2);

Append the value of a C string to a xstring.
str1 += "append this";

Allocate new storage for a xstring instance.
char* cp = str1.allocate(7);
See xstring_allocate.
Like sprintf/vsprintf into a xstring.
str1.sprintf("%i", 5);
xstring().sprintf("%i", 5)
The latter form creates a temporary, e.g. as function argument.