Convert String to and from std::string
authorArjen Baart <arjen@andromeda.nl>
Fri, 26 Mar 2021 07:33:16 +0000 (08:33 +0100)
committerArjen Baart <arjen@andromeda.nl>
Fri, 26 Mar 2021 07:33:16 +0000 (08:33 +0100)
TODO
doc/string.xml
src/String.h
src/string.cpp
test/string_assign.cpp
test/string_assign.exp
test/string_convert.cpp
test/string_convert.exp

diff --git a/TODO b/TODO
index 168a1d7..09edbb7 100644 (file)
--- a/TODO
+++ b/TODO
@@ -3,7 +3,7 @@ Things to do:
 
 - String: format operator % like in python
 - String: assign fstream objects to read and write a file
-- String: convert to and from std::sstream and std::string
+- String: convert to and from std::sstream
 
 - SuperString: sort and uniq
 - SuperString: find (regex)
index 9958702..fa40493 100644 (file)
@@ -29,6 +29,8 @@ substring expression.
 </item>
 <item tag="String(const String &amp;)">
 </item>
+<item tag="String(const std::string &amp;)">
+</item>
 </description>
 
 <para>
index f352162..1c40f29 100644 (file)
@@ -46,6 +46,7 @@
 #include <stdlib.h>
 #include <iostream>
 #include <vector>
+#include <string>
 #include <string.h>
 
 #include <regex.h>
@@ -99,10 +100,12 @@ public:
    String();                  // String x;
    String(const String &);    // String x = String ...
    String(const substring &); // String x = String(s, l)
+   String(const std::string &); // String x = std::string("abc")
 
    String& operator=(const char);
    String& operator=(const char *);
    String& operator=(const String &);
+   String& operator=(const std::string &);
    ~String();
 
    /*  Numerical conversion */
@@ -155,6 +158,11 @@ public:
       return p->s ? strlen(p->s) : 0;
    }
 
+   int size() const      //  Length of the String
+   {
+      return p->s ? strlen(p->s) : 0;
+   }
+
    operator char*()
    {
       if (p->s && p->s[0])
@@ -170,6 +178,14 @@ public:
          return 0;
    }
 
+   std::string std_string()
+   {
+      if (p->s && p->s[0])
+         return std::string(p->s);
+      else
+         return std::string("");
+   }
+
    operator bool()   //  Test for empty String
    {
       return p->s != 0 && p->s[0] != '\0';
index 9f002bb..1235d4d 100644 (file)
@@ -74,6 +74,16 @@ String::String(const substring &x)
    p->n = 1;
 }
 
+String::String(const std::string &x)
+{
+   p = new srep;
+
+   p->n = 1;
+   p->s = new char[x.size()+1];
+   x.copy(p->s, x.size());
+
+}
+
 String::~String()
 {
    if (--p->n == 0)
@@ -138,6 +148,23 @@ String& String::operator=(const String& x)
    return *this;
 }
 
+String& String::operator=(const std::string &x)
+{
+   if (p->n > 1)
+   {                //    Disconnect self
+      p->n--;
+      p = new srep;
+   }
+   else if (p->n == 1)
+      delete p->s;
+
+   p->n = 1;
+   p->s = new char[x.size()+1];
+   x.copy(p->s, x.size());
+
+   return *this;
+}
+
 /*  Numerical conversion */
 
 String::String(int i)
index 732523d..f7f8adc 100644 (file)
@@ -72,6 +72,28 @@ int main()
    std::cout << "A string initialized with a String object \"lmn\": \"" << s9 << "\"\n";
    assert(~s9 == 3);
 
+   // Contruct and assign from a std::string object
+
+   std::string x10("abc");
+   String  s10(x10);
+
+   std::cout << "A string constructed from std::string object \"" << x10 << "\": \"" << s10 << "\"\n";
+   assert(~s10 == 3);
+
+   std::string x11("def");
+   String  s11 = x11;
+
+   std::cout << "A string constructed from std::string object \"" << x11 << "\": \"" << s11 << "\"\n";
+   assert(~s11 == 3);
+
+   std::string x12("ghi");
+   String  s12;
+
+   s12 = x12;
+
+   std::cout << "A string assigned from std::string object \"" << x12 << "\": \"" << s12 << "\"\n";
+   assert(~s12 == 3);
+
    return 0;
 }
 
index 77a14b8..06140e6 100644 (file)
@@ -8,4 +8,7 @@ A string assigned a literal string "ijk": "ijk"
 A string initialized with a literal string "lmn": "lmn"
 A string assigned a String object "ijk": "ijk"
 A string initialized with a String object "lmn": "lmn"
+A string constructed from std::string object "abc": "abc"
+A string constructed from std::string object "def": "def"
+A string assigned from std::string object "ghi": "ghi"
 PASS string_assign (exit status: 0)
index 7f37906..c37a129 100644 (file)
@@ -49,6 +49,16 @@ int main()
    std::cout << "String " << s5 << " converts into number " << x << "\n";
    assert(x == 0.0);
 
+   //  Convert a String to a std::string
+
+   String s10("This is a String");
+   std::string x10;
+
+   x10 = s10.std_string();
+   std::cout << "String " << s10 << " converts into std::string " << x10 << "\n";
+
+   assert(String(x10) == s10);
+
    return 0;
 }
 
index f825bfd..5a04a21 100644 (file)
@@ -4,4 +4,5 @@ String 42 converts into number 42
 String 3.1415 converts into number 3.1415
 String not a number converts into number 0
 String not a number converts into number 0
+String This is a String converts into std::string This is a String
 PASS string_convert (exit status: 0)