From: Arjen Baart Date: Fri, 26 Mar 2021 07:33:16 +0000 (+0100) Subject: Convert String to and from std::string X-Git-Url: http://www.andromeda.nl/gitweb/?a=commitdiff_plain;h=7ad7a985cc65bfdb2c6281fc29bd4aa049252b19;p=libacl.git Convert String to and from std::string --- diff --git a/TODO b/TODO index 168a1d7..09edbb7 100644 --- 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) diff --git a/doc/string.xml b/doc/string.xml index 9958702..fa40493 100644 --- a/doc/string.xml +++ b/doc/string.xml @@ -29,6 +29,8 @@ substring expression. + + diff --git a/src/String.h b/src/String.h index f352162..1c40f29 100644 --- a/src/String.h +++ b/src/String.h @@ -46,6 +46,7 @@ #include #include #include +#include #include #include @@ -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'; diff --git a/src/string.cpp b/src/string.cpp index 9f002bb..1235d4d 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -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) diff --git a/test/string_assign.cpp b/test/string_assign.cpp index 732523d..f7f8adc 100644 --- a/test/string_assign.cpp +++ b/test/string_assign.cpp @@ -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; } diff --git a/test/string_assign.exp b/test/string_assign.exp index 77a14b8..06140e6 100644 --- a/test/string_assign.exp +++ b/test/string_assign.exp @@ -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) diff --git a/test/string_convert.cpp b/test/string_convert.cpp index 7f37906..c37a129 100644 --- a/test/string_convert.cpp +++ b/test/string_convert.cpp @@ -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; } diff --git a/test/string_convert.exp b/test/string_convert.exp index f825bfd..5a04a21 100644 --- a/test/string_convert.exp +++ b/test/string_convert.exp @@ -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)