- 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)
</item>
<item tag="String(const String &)">
</item>
+<item tag="String(const std::string &)">
+</item>
</description>
<para>
#include <stdlib.h>
#include <iostream>
#include <vector>
+#include <string>
#include <string.h>
#include <regex.h>
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 */
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])
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';
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)
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)
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;
}
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)
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;
}
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)