summaryrefslogtreecommitdiff
path: root/sal
diff options
context:
space:
mode:
authorLuboš Luňák <l.lunak@suse.cz>2012-03-06 10:44:52 +0100
committerLuboš Luňák <l.lunak@suse.cz>2012-03-12 13:35:56 +0100
commit2f5f802bcf197c5c65aa4453ad2097d5642b80aa (patch)
treede73d7917814113d7e593f9c42e7a0242465f447 /sal
parentf2d0fcc26be481c2f872056fb3b8402169d124d8 (diff)
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).
Diffstat (limited to 'sal')
-rw-r--r--sal/inc/rtl/ustring.h9
-rw-r--r--sal/inc/rtl/ustring.hxx4
-rw-r--r--sal/qa/rtl/strings/test_oustring_stringliterals.cxx7
-rw-r--r--sal/rtl/source/ustring.cxx37
-rw-r--r--sal/util/sal.map2
5 files changed, 47 insertions, 12 deletions
diff --git a/sal/inc/rtl/ustring.h b/sal/inc/rtl/ustring.h
index 80703f826423..e432d2c831fe 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 151e0a22c3ff..bb22619b6c69 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 ce2d22a530e1..1170ff7e2ed8 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 763d9c51e573..649cb82b9013 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 920bda9413c7..fb7f00112e19 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;