Using utx::string


utx::string is a class (aka a type) that inherits std::string which provides utx::string::cat operations, _convenient data converter, convenient initializers etc.

#include <utxcpp/core.hpp>

int main() {
	utx::string str = "Hello World!";
	std::cout << str << std::endl;
Hello World!
Hello World!

Constructors and Generic Initializers

You don't have to convert different types to string, utx::string will do them for you.

#include <utxcpp/core.hpp>
#include <complex>

int main() {
	utx::string str1{"I have a complex number."};
	utx::string str2{std::complex{3.5, 2.9}};
	utx::string str3{1234};
	utx::string str4{1.2345};

	std::string delim{' '};

	utx::string str = str1 + delim + str2 + delim + str3 + delim + str4 + delim;

I have a complex number. (3.500000,2.900000) 1234 1.234500

utx::string::cat operation

utx::string::cat is used to concat many different type of values together.

#include <utxcpp/core.hpp>
#include <complex>

using namespace std::complex_literals;

int main() {
	utx::string str1;"I have ", 2, " complex number(s): ", std::complex{3.2, 2.5}, " and ", 3.5i);

	utx::string str2 =" ========"); // str2 has its own copy

	// utx::numbers::pi is std::numbers::pi
	utx::string & str3 =' ', utx::numbers::pi); // str3 refs to str2.
I have 2 complex number(s): (3.200000,2.500000) and (0.000000,3.500000)
I have 2 complex number(s): (3.200000,2.500000) and (0.000000,3.500000) ========
I have 2 complex number(s): (3.200000,2.500000) and (0.000000,3.500000) ======== 3.141593

utx::string literal: _us

using namespace utx::string_literals

#include <utxcpp/core.hpp>

using namespace utx::string_literals;

int main() {
	utx::string str1 = "hello"_us;
	utx::print(str1, "world"_us);
	utx::print("hello""world", 2));

	utx::print("", 456, "1/7", "graceful"));
	utx::print("", 456, " 1/7", " graceful"));
hello world
123456 1/7 graceful

Issues and Why

In the previous utx::string implementation, it uses operator+ to do string concat. However when I try to concat string with some different type of values, I find some bugs. So in the later implementation, utx::string does not overload operator+ for strings, but uses utx::string::cat method instead.

This is valid, because utx::string inherits it from std::string:

utx::string str = "Hello"s + "World";

This is invalid, std::string does not provide operator+ for string and integer:

utx::string str = "Hello"s + 123;

This is valid, utx::string::cat provides string and any printable? types concat:

utx::string str = "Hello";

Last revised: May 20, 2022 at 03:59:05 GMT