Date: prev next · Thread: first prev next last
2011 Archives by date, by thread · List index


Hi all,

As has recently been discussed on #libreoffice-dev, its probably a good idea to remove the rather unhelpful conversion operators

  rtl::OString::operator sal_Char const * ()
  rtl::OStringBuffer::operator sal_Char const * ()
  rtl::OUString::operator sal_Unicode const * ()
  rtl::OUStringBuffer::operator sal_Unicode const * ()

(The reason why this came up on IRC this time was that the expression "aStr = aStr + cSep" with aStr of type rtl::OString and cSep of type sal_Char had a rather non-intuitive meaning, implicitly converting aStr into a sal_Char const *, adding the integral value of cSep to that pointer, and then converting that modified sal_Char const * back into an rtl::OString via the rtl::OString(sal_Char const *) constructor.)

The change will be source-code incompatible, but that is probably acceptable:

- The change is ABI compatible (involving only inline C++ functions).

- The ~only legitimate use case for those conversion operators was in indexing expressions like aStr[n]. To continue those expressions to work, appropriate operator [](sal_Int) member functions will be added to the four classes.

- All other uses of the conversion operators should use the getStr() member functions. (Also note that there might be cases where translation between, say, rtl::OString and char const * via naive application of rtl::OString::getStr() is not appropriate as it potentially looses information, when the given rtl::OString instance contains NUL characters. Having the calls to getStr explicitly in the code helps auditing in this case.)

My plan is as follows:

- I just pushed <http://cgit.freedesktop.org/libreoffice/core/commit/?id=6671fa81db0ecea4ada005bb79f55f08fb440ad4> "Removed uses of rtl::O[U]String[Buffer]::operator sal_{char|Unicode} const *()." and <http://cgit.freedesktop.org/libreoffice/binfilter/commit/?id=34c2ec87db4b6962ded661157056c58163e39821> "Removed uses of rtl::O[U]String[Buffer]::operator sal_{char|Unicode} const *()." That clears the current master of all dubious (i.e., non-[]) uses of the conversion operators for a Linux x86_64 --enable-binfilter --enable-dbgutil "make check" build.

- A few more builds (e.g., Windows, Mac OS X) should be done with the below patch to ensure that as many dubious uses in platform-/configuration-switch-specific code as possible have been fixed. If someone wants to give me a hand here, that would be great.

- The attached no-string-conversion.patch will remove the conversion operators and introduce the operator [] replacements. It can be committed to master once the above step is done, in the 3.5 time frame. (If anybody has objections to this, esp. as it nominally breaks backwards compatibility at the source level, raise your hands.)

-Stephan
diff --git a/sal/inc/rtl/strbuf.hxx b/sal/inc/rtl/strbuf.hxx
index 63ae448..e485e7d 100644
--- a/sal/inc/rtl/strbuf.hxx
+++ b/sal/inc/rtl/strbuf.hxx
@@ -287,12 +287,18 @@ public:
     /**
         Return a null terminated character array.
      */
-    operator        const sal_Char *() const { return pData->buffer; }
+    const sal_Char* getStr() const { return pData->buffer; }
 
     /**
-        Return a null terminated character array.
-     */
-    const sal_Char* getStr() const { return pData->buffer; }
+      Access to individual characters.
+
+      @param index must be non-negative and less than length.
+
+      @return the character at the given index.
+
+      @since LibreOffice 3.5
+    */
+    sal_Char operator [](sal_Int32 index) const { return getStr()[index]; }
 
     /**
         Return a OString instance reflecting the current content
diff --git a/sal/inc/rtl/string.hxx b/sal/inc/rtl/string.hxx
index 96ca406..22de6dd 100644
--- a/sal/inc/rtl/string.hxx
+++ b/sal/inc/rtl/string.hxx
@@ -251,19 +251,6 @@ public:
     /**
       Returns a pointer to the characters of this string.
 
-      <p>The returned pointer is not guaranteed to point to a null-terminated
-      byte string.  Note that this string object may contain embedded null
-      characters, which will thus also be embedded in the returned byte
-      string.</p>
-
-      @return a pointer to a (not necessarily null-terminated) byte string
-      representing the characters of this string object.
-    */
-    operator const sal_Char *() const SAL_THROW(()) { return pData->buffer; }
-
-    /**
-      Returns a pointer to the characters of this string.
-
       <p>The returned pointer is guaranteed to point to a null-terminated byte
       string.  But note that this string object may contain embedded null
       characters, which will thus also be embedded in the returned
@@ -275,6 +262,17 @@ public:
     const sal_Char * getStr() const SAL_THROW(()) { return pData->buffer; }
 
     /**
+      Access to individual characters.
+
+      @param index must be non-negative and less than length.
+
+      @return the character at the given index.
+
+      @since LibreOffice 3.5
+    */
+    sal_Char operator [](sal_Int32 index) const { return getStr()[index]; }
+
+    /**
       Compares two strings.
 
       The comparison is based on the numeric value of each character in
diff --git a/sal/inc/rtl/ustrbuf.hxx b/sal/inc/rtl/ustrbuf.hxx
index a356309..a995dcd 100644
--- a/sal/inc/rtl/ustrbuf.hxx
+++ b/sal/inc/rtl/ustrbuf.hxx
@@ -267,12 +267,18 @@ public:
     /**
         Return a null terminated unicode character array.
      */
-    operator        const sal_Unicode *() const { return pData->buffer; }
+    const sal_Unicode*  getStr() const { return pData->buffer; }
 
     /**
-        Return a null terminated unicode character array.
-     */
-    const sal_Unicode*  getStr() const { return pData->buffer; }
+      Access to individual characters.
+
+      @param index must be non-negative and less than length.
+
+      @return the character at the given index.
+
+      @since LibreOffice 3.5
+    */
+    sal_Unicode operator [](sal_Int32 index) const { return getStr()[index]; }
 
     /**
         Return a OUString instance reflecting the current content
diff --git a/sal/inc/rtl/ustring.hxx b/sal/inc/rtl/ustring.hxx
index ed268d9..e104f0d 100644
--- a/sal/inc/rtl/ustring.hxx
+++ b/sal/inc/rtl/ustring.hxx
@@ -300,16 +300,18 @@ public:
 
       @return   a pointer to the Unicode characters buffer from this object.
     */
-    operator const sal_Unicode *() const SAL_THROW(()) { return pData->buffer; }
+    const sal_Unicode * getStr() const SAL_THROW(()) { return pData->buffer; }
 
     /**
-      Returns a pointer to the Unicode character buffer from this string.
+      Access to individual characters.
 
-      It isn't necessarily NULL terminated.
+      @param index must be non-negative and less than length.
 
-      @return   a pointer to the Unicode characters buffer from this object.
+      @return the character at the given index.
+
+      @since LibreOffice 3.5
     */
-    const sal_Unicode * getStr() const SAL_THROW(()) { return pData->buffer; }
+    sal_Unicode operator [](sal_Int32 index) const { return getStr()[index]; }
 
     /**
       Compares two strings.

Context


Privacy Policy | Impressum (Legal Info) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Creative Commons Attribution-Share Alike 3.0 License. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License (MPLv2). "LibreOffice" and "The Document Foundation" are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy.