diff --git a/src/Examples/StringExample.cpp b/src/Examples/StringExample.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1297f521fa7f849fbdb20e80cae987c84c316759 --- /dev/null +++ b/src/Examples/StringExample.cpp @@ -0,0 +1,112 @@ +#include <iostream> + +using namespace TNL + +int main() +{ + // constructors + String str1; + String str2( "xxstringxxx", 2, 3 ); + String str3( str2 ); // copy constructor + String str4( 28.4 ); // converts to string + + cout << "str1:" << str1 << endl; + cout << "str2:" << str2 << endl; + cout << "str3:" << str3 << endl; + cout << "str4:" << str4 << endl; + + // functions + int size = str3.getSize(); + cout << "size of string:" << size << "bytes" << endl; + + int alloc_size = str3.getAllocatedSize(); + cout << "alloc_size:" << alloc_size << endl; + + int memory = str1.setSize( 256 ); + cout << "memory:" << memory << endl; + + String str + setter = str.setString( "Something new" ); + cout << "setter:" << setter << endl; + + int getter = str4.getString(); + cout << "getter:" << getter << endl; + + String word( computer ) ; + third_letter = word[2]; + cout << "third_letter:" << third_letter << endl; + + // Operators for C Strings + String a( "hello" ); + a = "bye"; + cout << "a:" << a << endl; + + String b( "see" ); + b += " you"; + cout << "b:" << b << endl; + + String c; + c = b + " soon"; + cout << "c:" << c << endl; + + String name( "Jack" ); + if ( name == "Jack" ) cout << "Names are the same." << endl; + + String surname( "Sparrow" ); + if ( surname != "Jones" ) cout << "Surnames are different." << endl; + + // Operators for Strings + String d1( "Cheese" ); + d = d1; + cout << "d:" << d << endl; + + String e( "Mac&" ); + e += d; + cout << "e:" << e << endl; + + String f; + String f1("Tim likes "); + f = f1 + e; + cout << "f:" << f << endl; + + String num1( "one" ); + String num2( "Anyone", 3); + if ( num1 == num2 ) cout << "Numbers are the same." << endl; + + String eq1( "a + b" ); + String eq2( "a" ); + if ( eq1 != eq2 ) cout << "Equations are different." << endl; + + // Operators for single characters + String g; + g = 'y'; + cout << "g:" << g << endl; + + String h( "x" ); + h += g; + cout << "h:" << h << endl; + + String i; + i = 'a' + 'b'; + cout << "i:" << i << endl; + + String letter1( "u" ); + if ( letter1 == "u" ) cout << "Letters are the same." << endl; + + String letter2( "v" ); + if ( letter2 != "w" ) cout << "Letters are different." << endl; + + //Cast to bool operators + String full( "string" ); + if ( full ) cout << "String is not empty." << endl; + + String empty; + if ( !empty ) cout << "String is empty." << endl; + + //replace + String phrase( "Hakuna matata" ); + new_phrase = phrase.replace( "a", "u", 2 ); + cout << "new_phrase:" << new_phrase << endl; + + +} diff --git a/src/Examples/StringExample.out b/src/Examples/StringExample.out new file mode 100644 index 0000000000000000000000000000000000000000..4bc752b22d760cee6405c3f6b866994d10c8b375 --- /dev/null +++ b/src/Examples/StringExample.out @@ -0,0 +1,40 @@ +//constructors +str1: +str2: string +str3: string +str4: 28.4 //string type + +//functions +size of string: 6 bytes +alloc_size: 256 +memory: 512 +setter: Something new +getter: 28.4 +third_letter: m + +// Operators for C Strings +a: bye +b: see you +c: see you soon +Names are the same. +Surnames are different. + +// Operators for Strings +d: Cheese +e: Mac&Cheese +f: Tim likes Mac&Cheese +Numbers are the same. +Equations are different. +// Operators for single characters +g: y +h: xy +i: ab +Letters are the same. +Letters are different. + +//Cast to bool operators +String is not empty. +String is empty. + +//replace +new_phrase: Hukunu matata diff --git a/src/TNL/String.h b/src/TNL/String.h index 9625554ad2a29ac3f7acb3db2b66adf7ca441381..6d50116f7a338579c093fd542154376c1dc21567 100644 --- a/src/TNL/String.h +++ b/src/TNL/String.h @@ -96,7 +96,7 @@ class String ///// /// Reserves space for given \e size. - /// Requests to allocate storage for given \e size. + /// Requests to allocate storage space of given \e size to avoid memory reallocation. /// It allocates one more byte for the terminating 0. /// @param size Number of characters. void setSize( int size ); @@ -256,7 +256,7 @@ class String bool load( File& file ); - // !!! Mozem dat prec??? + // !!! Mozem dat prec??? // Broadcast to other nodes in MPI cluster // void MPIBcast( int root, MPI_Comm mpi_comm = MPI_COMM_WORLD );