From 95c308df724bc9f7cf42e0e10627bace2c331d37 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lubo=C5=A1=20Lu=C5=88=C3=A1k?= <l.lunak@suse.cz>
Date: Tue, 6 Mar 2012 10:44:52 +0100
Subject: [PATCH 1/3] rtl_uString_newFromLiteral() for string literals

Drop the recently introduced rtl_uString_newFromAscii_WithLength()
and replace it with this one. The name fits better and it'll be also
a distinct function that specifically includes embedded \0's
(because that's what OUString supports and if a string literal
explicitly includes it, it makes sense to copy it as such).
---
 sal/inc/rtl/ustring.h                              |    9 +++--
 sal/inc/rtl/ustring.hxx                            |    4 +-
 .../rtl/strings/test_oustring_stringliterals.cxx   |    7 ++++
 sal/rtl/source/ustring.cxx                         |   37 +++++++++++++++++---
 sal/util/sal.map                                   |    2 +-
 5 files changed, 47 insertions(+), 12 deletions(-)

diff --git a/sal/inc/rtl/ustring.h b/sal/inc/rtl/ustring.h
index 80703f8..e432d2c 100644
--- a/sal/inc/rtl/ustring.h
+++ b/sal/inc/rtl/ustring.h
@@ -1243,14 +1243,15 @@ SAL_DLLPUBLIC void SAL_CALL rtl_uString_newFromStr_WithLength(
 SAL_DLLPUBLIC void SAL_CALL rtl_uString_newFromAscii(
         rtl_uString ** newStr, const sal_Char * value ) SAL_THROW_EXTERN_C();
 
-/** Allocate a new string that contains a copy of a character array.
+/** Allocate a new string that contains a copy of a string literal.
 
-    This is equivalent to rtl_uString_newFromAscii(), except that
-    length of the character array is explicitly passed to the function.
+    This is similar to rtl_uString_newFromAscii(), except that
+    length of the string literal is explicitly passed to the function,
+    and embedded \0's are included in the string.
 
     @since 3.6
  */
-SAL_DLLPUBLIC void SAL_CALL rtl_uString_newFromAscii_WithLength(
+SAL_DLLPUBLIC void SAL_CALL rtl_uString_newFromLiteral(
         rtl_uString ** newStr, const sal_Char * value, sal_Int32 len ) SAL_THROW_EXTERN_C();
 
 /** Allocate a new string from an array of Unicode code points.
diff --git a/sal/inc/rtl/ustring.hxx b/sal/inc/rtl/ustring.hxx
index 151e0a2..bb22619 100644
--- a/sal/inc/rtl/ustring.hxx
+++ b/sal/inc/rtl/ustring.hxx
@@ -184,7 +184,7 @@ public:
     OUString( const char (&literal)[ N ] )
     {
         pData = 0;
-        rtl_uString_newFromAscii_WithLength( &pData, literal, N - 1 );
+        rtl_uString_newFromLiteral( &pData, literal, N - 1 );
         if (pData == 0) {
 #if defined EXCEPTIONS_OFF
             SAL_WARN("sal", "std::bad_alloc but EXCEPTIONS_OFF");
@@ -338,7 +338,7 @@ public:
     template< int N >
     OUString& operator=( const char (&literal)[ N ] )
     {
-        rtl_uString_newFromAscii_WithLength( &pData, literal, N - 1 );
+        rtl_uString_newFromLiteral( &pData, literal, N - 1 );
         if (pData == 0) {
 #if defined EXCEPTIONS_OFF
             SAL_WARN("sal", "std::bad_alloc but EXCEPTIONS_OFF");
diff --git a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx
index ce2d22a..1170ff7 100644
--- a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx
+++ b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx
@@ -77,6 +77,13 @@ void test::oustring::StringLiterals::checkCtors()
     const char bad5[][ 6 ] = { "test", "test2" };
 //    CPPUNIT_ASSERT( validConversion( rtl::OUString( bad5[ 0 ] )));
     CPPUNIT_ASSERT( validConversion( rtl::OUString( bad5[ 1 ] )));
+
+// Check that contents are correct and equal to the case when RTL_CONSTASCII_USTRINGPARAM is used.
+// Also check that embedded \0 is included.
+    CPPUNIT_ASSERT( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "" )) == rtl::OUString( "" ));
+    CPPUNIT_ASSERT( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "\0" )) == rtl::OUString( "\0" ));
+    CPPUNIT_ASSERT( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ab" )) == rtl::OUString( "ab" ));
+    CPPUNIT_ASSERT( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "a\0b" )) == rtl::OUString( "a\0b" ));
 }
 
 void test::oustring::StringLiterals::testcall( const char str[] )
diff --git a/sal/rtl/source/ustring.cxx b/sal/rtl/source/ustring.cxx
index 763d9c5..649cb82 100644
--- a/sal/rtl/source/ustring.cxx
+++ b/sal/rtl/source/ustring.cxx
@@ -471,10 +471,37 @@ void SAL_CALL rtl_uString_newFromAscii( rtl_uString** ppThis,
     else
         nLen = 0;
 
-    rtl_uString_newFromAscii_WithLength( ppThis, pCharStr, nLen );
+    if ( !nLen )
+    {
+        IMPL_RTL_STRINGNAME( new )( ppThis );
+        return;
+    }
+
+    if ( *ppThis )
+        IMPL_RTL_STRINGNAME( release )( *ppThis );
+
+    *ppThis = IMPL_RTL_STRINGNAME( ImplAlloc )( nLen );
+    OSL_ASSERT(*ppThis != NULL);
+    if ( (*ppThis) )
+    {
+        IMPL_RTL_STRCODE* pBuffer = (*ppThis)->buffer;
+        do
+        {
+            /* Check ASCII range */
+            SAL_WARN_IF( ((unsigned char)*pCharStr) > 127, "rtl.string",
+                        "rtl_uString_newFromAscii - Found char > 127" );
+
+            *pBuffer = *pCharStr;
+            pBuffer++;
+            pCharStr++;
+        }
+        while ( *pCharStr );
+    }
 }
 
-void SAL_CALL rtl_uString_newFromAscii_WithLength( rtl_uString** ppThis,
+// Used when creating from string literals.
+// Intentionally copies also embedded \0's if present.
+void SAL_CALL rtl_uString_newFromLiteral( rtl_uString** ppThis,
                                         const sal_Char* pCharStr,
                                         sal_Int32 nLen )
     SAL_THROW_EXTERN_C()
@@ -493,17 +520,17 @@ void SAL_CALL rtl_uString_newFromAscii_WithLength( rtl_uString** ppThis,
     if ( (*ppThis) )
     {
         IMPL_RTL_STRCODE* pBuffer = (*ppThis)->buffer;
-        do
+        sal_Int32 nCount;
+        for( nCount = nLen; nCount > 0; --nCount )
         {
             /* Check ASCII range */
             SAL_WARN_IF( ((unsigned char)*pCharStr) > 127, "rtl.string",
-                        "rtl_uString_newFromAscii_WithLength - Found char > 127" );
+                        "rtl_uString_newFromLiteral - Found char > 127" );
 
             *pBuffer = *pCharStr;
             pBuffer++;
             pCharStr++;
         }
-        while ( *pCharStr );
     }
 }
 
diff --git a/sal/util/sal.map b/sal/util/sal.map
index 5ee60b9..a6d9c08 100644
--- a/sal/util/sal.map
+++ b/sal/util/sal.map
@@ -305,7 +305,7 @@ UDK_3_0_0 {
         rtl_uString_newFromStr;
         rtl_uString_newFromStr_WithLength;
         rtl_uString_newFromAscii;
-        rtl_uString_newFromAscii_WithLength;
+        rtl_uString_newFromLiteral;
         rtl_uString_newFromString;
         rtl_uString_newReplace;
         rtl_uString_newReplaceStrAt;
-- 
1.7.3.4

