zorba-coders team mailing list archive
-
zorba-coders team
-
Mailing list archive
-
Message #25751
[Merge] lp:~paul-lucas/zorba/pjl-misc into lp:zorba
Paul J. Lucas has proposed merging lp:~paul-lucas/zorba/pjl-misc into lp:zorba.
Commit message:
Added more functionality to utf8_string. (Needed for other branches.)
Requested reviews:
Paul J. Lucas (paul-lucas)
For more details, see:
https://code.launchpad.net/~paul-lucas/zorba/pjl-misc/+merge/186402
Added more functionality to utf8_string. (Needed for other branches.)
--
https://code.launchpad.net/~paul-lucas/zorba/pjl-misc/+merge/186402
Your team Zorba Coders is subscribed to branch lp:zorba.
=== modified file 'src/util/utf8_string.h'
--- src/util/utf8_string.h 2013-04-01 21:01:28 +0000
+++ src/util/utf8_string.h 2013-09-18 17:42:32 +0000
@@ -230,6 +230,30 @@
////////// constructors & destructor ////////////////////////////////////////
/**
+ * Default contructor wrapped around no string. To wrap around a string at
+ * a later time, use wrap().
+ */
+ utf8_string() : s_( nullptr ), delete_( false ) {
+ }
+
+ /**
+ * Copy constructor.
+ *
+ * @param s The %utf8_string to copy from. If \a s does not have ownership
+ * of its wrapped string, then this %utf8_string shall wrap the same string;
+ * if \a s does have ownership, then the wrapped string is duplicated.
+ */
+ utf8_string( utf8_string const &s ) {
+ if ( &s != this && s.s_ ) {
+ delete_ = s.delete_;
+ s_ = delete_ ? new StringType( *s.s_ ) : s.s_;
+ } else {
+ s_ = nullptr;
+ delete_ = false;
+ }
+ }
+
+ /**
* Constructs a %utf8_string around the given string.
*
* @param s The string to wrap. Ownership is not taken.
@@ -253,7 +277,7 @@
/**
* Constructs a %utf8_string around a C string.
*
- * @param s The C string.
+ * @param s The C string to wrap. Ownership is taken.
*/
explicit utf8_string( const_storage_pointer s ) :
s_( new StringType( s ) ), delete_( true )
@@ -264,7 +288,7 @@
/**
* Constructs a %utf8_string around a C string.
*
- * @param s The C string.
+ * @param s The C string wrap. Ownership is taken.
*/
explicit utf8_string( storage_pointer s ) :
s_( new StringType( s ) ), delete_( true )
@@ -276,8 +300,59 @@
* Destructs this %utf8_string.
*/
~utf8_string() {
- if ( delete_ )
- delete s_;
+ free();
+ }
+
+ ////////// wrapping /////////////////////////////////////////////////////////
+
+ /**
+ * Wraps this %utf8_string around the given string. (Any previously wrapped
+ * string is unwrapped first.)
+ *
+ * @param s The string to wrap. Ownership is not taken.
+ * @return Returns a reference to this %utf8_string.
+ */
+ utf8_string& wrap( StringType &s ) {
+ free();
+ s_ = &s;
+ delete_ = false;
+ return *this;
+ }
+
+ /**
+ * Wraps this %utf8_string around the given string. (Any previously wrapped
+ * string is unwrapped first.)
+ *
+ * @param s The string to wrap. Ownership is taken.
+ * @return Returns a reference to this %utf8_string.
+ */
+ utf8_string& wrap( StringType *s ) {
+ free();
+ s_ = s;
+ delete_ = true;
+ return *this;
+ }
+
+ /**
+ * Wraps this %utf8_string around the given C string. (Any previously
+ * wrapped string is unwrapped first.)
+ *
+ * @param s The C string to wrap. Ownership is taken.
+ * @return Returns a reference to this %utf8_string.
+ */
+ utf8_string& wrap( const_storage_pointer s ) {
+ return wrap( new StringType( s ) );
+ }
+
+ /**
+ * Wraps this %utf8_string around the given C string. (Any previously
+ * wrapped string is unwrapped first.)
+ *
+ * @param s The C string to wrap. Ownership is taken.
+ * @return Returns a reference to this %utf8_string.
+ */
+ utf8_string& wrap( storage_pointer s ) {
+ return wrap( new StringType( s ) );
}
////////// assignment ///////////////////////////////////////////////////////
@@ -1170,6 +1245,14 @@
const_storage_pointer dup( size_type c_n, value_type c );
/**
+ * Deletes the wrapped string but only if ownership was taken.
+ */
+ void free() {
+ if ( delete_ )
+ delete s_;
+ }
+
+ /**
* A helper class to convert character position information into its
* corresponding byte position information.
*/
@@ -1264,7 +1347,7 @@
};
StringType *s_;
- bool const delete_;
+ bool delete_;
};
////////// inline utf8_string members /////////////////////////////////////////
References