From d36432a82b6bf2a1e8441c857fefd33ccbd702a0 Mon Sep 17 00:00:00 2001
From: Nina Dzugasova <dzugasova.nina@gmail.com>
Date: Mon, 29 Oct 2018 13:57:58 +0100
Subject: [PATCH] String documentation.

---
 src/Examples/StringExample.cpp | 112 +++++++++++++++++++++++++++++++++
 src/Examples/StringExample.out |  40 ++++++++++++
 src/TNL/String.h               |   4 +-
 3 files changed, 154 insertions(+), 2 deletions(-)
 create mode 100644 src/Examples/StringExample.cpp
 create mode 100644 src/Examples/StringExample.out

diff --git a/src/Examples/StringExample.cpp b/src/Examples/StringExample.cpp
new file mode 100644
index 0000000000..1297f521fa
--- /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 0000000000..4bc752b22d
--- /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 23ee67d53a..52d77bdcd9 100644
--- a/src/TNL/String.h
+++ b/src/TNL/String.h
@@ -91,7 +91,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 );
@@ -251,7 +251,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 );
 
-- 
GitLab