Struct SharedString

Struct Documentation

struct SharedString

A string type used by the Slint run-time.

SharedString uses implicit data sharing to make it efficient to pass around copies. When copying, a reference to the data is cloned, not the data itself.

The class provides constructors from std::string_view as well as the automatic conversion to a std::string_view.

For convenience, it’s also possible to convert a number to a string using SharedString::from_number(double).

Under the hood the string data is UTF-8 encoded and it is always terminated with a null character.

Public Functions

inline SharedString()

Creates an empty default constructed string.

inline SharedString(std::string_view s)

Creates a new SharedString from the string view s. The underlying string data is copied.

inline SharedString(const char *s)

Creates a new SharedString from the null-terminated string pointer s. The underlying string data is copied. It is assumed that the string is UTF-8 encoded.

inline SharedString(const SharedString &other)

Creates a new SharedString from other.

inline ~SharedString()

Destroys this SharedString and frees the memory if this is the last instance referencing it.

inline SharedString &operator=(const SharedString &other)

Assigns other to this string and returns a reference to this string.

inline SharedString &operator=(std::string_view s)

Assigns the string view s to this string and returns a reference to this string. The underlying string data is copied. It is assumed that the string is UTF-8 encoded.

inline SharedString &operator=(const char *s)

Assigns null-terminated string pointer s to this string and returns a reference to this string. The underlying string data is copied. It is assumed that the string is UTF-8 encoded.

inline SharedString &operator=(SharedString &&other)

Move-assigns other to this SharedString instance.

inline operator std::string_view() const

Provides a view to the string data. The returned view is only valid as long as at least this SharedString exists.

inline auto data() const -> const char*

Provides a raw pointer to the string data. The returned pointer is only valid as long as at least this SharedString exists.

inline const char *begin() const

Returns a pointer to the first character. It is only safe to dereference the pointer if the string contains at least one character.

inline const char *end() const

Returns a point past the last character of the string. It is not safe to dereference the pointer, but it is suitable for comparison.

inline bool empty() const
Returns

true if the string contains no characters; false otherwise.

inline bool starts_with(std::string_view prefix) const
Returns

true if the string starts with the specified prefix string; false otherwise

inline bool ends_with(std::string_view prefix) const
Returns

true if the string ends with the specified prefix string; false otherwise

inline SharedString &operator+=(std::string_view other)

Appends other to this string and returns a reference to this.

Public Static Functions

static inline SharedString from_number(double n)

Creates a new SharedString from the given number n. The string representation of the number uses a minimal formatting scheme: If n has no fractional part, the number will be formatted as an integer.

For example:

auto str = slint::SharedString::from_number(42); // creates "42"
auto str2 = slint::SharedString::from_number(100.5) // creates "100.5"

Friends

inline friend bool operator==(const SharedString &a, const SharedString &b)

Returns true if a is equal to b; otherwise returns false.

inline friend bool operator!=(const SharedString &a, const SharedString &b)

Returns true if a is not equal to b; otherwise returns false.

inline friend bool operator<(const SharedString &a, const SharedString &b)

Returns true if a is lexicographically less than b; false otherwise.

inline friend bool operator<=(const SharedString &a, const SharedString &b)

Returns true if a is lexicographically less or equal than b; false otherwise.

inline friend bool operator>(const SharedString &a, const SharedString &b)

Returns true if a is lexicographically greater than b; false otherwise.

inline friend bool operator>=(const SharedString &a, const SharedString &b)

Returns true if a is lexicographically greater or equal than b; false otherwise.

inline friend std::ostream &operator<<(std::ostream &stream, const SharedString &shared_string)

Writes the shared_string to the specified stream and returns a reference to the stream.

inline friend SharedString operator+(const SharedString &a, std::string_view b)

Concatenates a and and returns the result as a new SharedString.

inline friend SharedString operator+(SharedString &&a, std::string_view b)

Move-concatenates b to and returns a reference to a.