[Libreoffice-commits] core.git: 6 commits - include/rtl sal/osl sal/rtl

Stephan Bergmann sbergman at redhat.com
Wed Nov 19 23:34:38 PST 2014


 include/rtl/strbuf.h      |    4 +++-
 include/rtl/strbuf.hxx    |   24 ++++++++++++++++++++++++
 include/rtl/ustrbuf.h     |    4 +++-
 include/rtl/ustrbuf.hxx   |   24 ++++++++++++++++++++++++
 sal/osl/unx/nlsupport.cxx |   22 ++++++++++++++--------
 sal/osl/unx/nlsupport.hxx |    4 +++-
 sal/osl/unx/osxlocale.cxx |   25 +++++++++++++++----------
 sal/rtl/math.cxx          |    4 ++++
 sal/rtl/strbuf.cxx        |   16 +++++++---------
 sal/rtl/ustrbuf.cxx       |   17 ++++++++---------
 10 files changed, 105 insertions(+), 39 deletions(-)

New commits:
commit 5252652ac57b3358db6cc0d423cc3d67882b5e90
Author: Stephan Bergmann <sbergman at redhat.com>
Date:   Thu Nov 20 08:32:37 2014 +0100

    Introduce OStringBuffer::appendUninitialized
    
    ...corresponding to the OUStringBuffer couterpart
    
    Change-Id: I3ab03343696e6755cf1ccc470e4decc2f41d2558

diff --git a/include/rtl/strbuf.h b/include/rtl/strbuf.h
index 403d53c..3f682db 100644
--- a/include/rtl/strbuf.h
+++ b/include/rtl/strbuf.h
@@ -101,7 +101,9 @@ SAL_DLLPUBLIC void SAL_CALL rtl_stringbuffer_ensureCapacity(
     @param[in,out]  This        the String to operate on.
     @param[in,out]  capacity    the capacity of the string buffer
     @param[in]      offset      the offset.
-    @param[in]      str         a character array.
+    @param[in]      str         a character array.  Since LibreOffice 4.4, as a
+                                special case, if str is null then the len added
+                                characters are left uninitialized.
     @param[in]      len         the number of characters to append.
  */
 SAL_DLLPUBLIC void SAL_CALL rtl_stringbuffer_insert(
diff --git a/include/rtl/strbuf.hxx b/include/rtl/strbuf.hxx
index c089d61..b135298 100644
--- a/include/rtl/strbuf.hxx
+++ b/include/rtl/strbuf.hxx
@@ -490,6 +490,7 @@ public:
     OStringBuffer & append( const sal_Char * str, sal_Int32 len)
     {
         assert( len >= 0 );
+        assert( len == 0 || str != 0 );
         rtl_stringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
         return *this;
     }
@@ -645,6 +646,28 @@ public:
     }
 
     /**
+       Unsafe way to make space for a fixed amount of characters to be appended
+       into this OStringBuffer.
+
+       A call to this function must immediately be followed by code that
+       completely fills the uninitialized block pointed to by the return value.
+
+       @param length the length of the uninitialized block of char entities;
+       must be non-negative
+
+       @return a pointer to the start of the uninitialized block; only valid
+       until this OStringBuffer's capacity changes
+
+       @since LibreOffice 4.4
+    */
+    char * appendUninitialized(sal_Int32 length) {
+        assert(length >= 0);
+        sal_Int32 n = getLength();
+        rtl_stringbuffer_insert(&pData, &nCapacity, n, 0, length);
+        return pData->buffer + n;
+    }
+
+    /**
         Inserts the string into this string buffer.
 
         The characters of the <code>String</code> argument are inserted, in
@@ -729,6 +752,7 @@ public:
     {
         assert( offset >= 0 && offset <= pData->length );
         assert( len >= 0 );
+        assert( len == 0 || str != 0 );
         rtl_stringbuffer_insert( &pData, &nCapacity, offset, str, len );
         return *this;
     }
diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index ac5a793..d74f502 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -222,6 +222,7 @@ struct StringTraits
                                    sal_Int32 * pOffset, sal_Char const * pChars,
                                    sal_Int32 nLen)
     {
+        assert(pChars != nullptr);
         rtl_stringbuffer_insert(pBuffer, pCapacity, *pOffset, pChars, nLen);
         *pOffset += nLen;
     }
@@ -230,6 +231,7 @@ struct StringTraits
                                    sal_Int32 * pOffset, sal_Char const * pStr,
                                    sal_Int32 nLen)
     {
+        assert(pStr != nullptr);
         rtl_stringbuffer_insert(pBuffer, pCapacity, *pOffset, pStr, nLen);
         *pOffset += nLen;
     }
diff --git a/sal/rtl/strbuf.cxx b/sal/rtl/strbuf.cxx
index f8b1f9c..f75d0cc 100644
--- a/sal/rtl/strbuf.cxx
+++ b/sal/rtl/strbuf.cxx
@@ -114,11 +114,14 @@ void SAL_CALL rtl_stringbuffer_insert( rtl_String ** This,
             memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Char) );
 
         /* insert the new characters */
-        if( len == 1 )
+        if( str != nullptr )
+        {
+            if( len == 1 )
                             /* optimized for 1 character */
-            pBuf[offset] = *str;
-        else
-            memcpy( pBuf + offset, str, len * sizeof(sal_Char) );
+                pBuf[offset] = *str;
+            else
+                memcpy( pBuf + offset, str, len * sizeof(sal_Char) );
+        }
         (*This)->length = nOldLen + len;
         pBuf[ nOldLen + len ] = 0;
     }
commit 04ae3d0cc9b671729deabf33c2cea1031d72e6ae
Author: Stephan Bergmann <sbergman at redhat.com>
Date:   Thu Nov 20 08:23:53 2014 +0100

    len cannot be <= 1 here
    
    Change-Id: I482e6eeca09e7b15a8d95a866ebab35d06f13e9e

diff --git a/sal/rtl/strbuf.cxx b/sal/rtl/strbuf.cxx
index f7a6c99..f8b1f9c 100644
--- a/sal/rtl/strbuf.cxx
+++ b/sal/rtl/strbuf.cxx
@@ -117,7 +117,7 @@ void SAL_CALL rtl_stringbuffer_insert( rtl_String ** This,
         if( len == 1 )
                             /* optimized for 1 character */
             pBuf[offset] = *str;
-        else if( len > 1 )
+        else
             memcpy( pBuf + offset, str, len * sizeof(sal_Char) );
         (*This)->length = nOldLen + len;
         pBuf[ nOldLen + len ] = 0;
diff --git a/sal/rtl/ustrbuf.cxx b/sal/rtl/ustrbuf.cxx
index 38c1b00..2ebae71 100644
--- a/sal/rtl/ustrbuf.cxx
+++ b/sal/rtl/ustrbuf.cxx
@@ -141,7 +141,7 @@ void SAL_CALL rtl_uStringbuffer_insert( rtl_uString ** This,
             if( len == 1 )
                 /* optimized for 1 character */
                 pBuf[offset] = *str;
-            else if( len > 1 )
+            else
                 memcpy( pBuf + offset, str, len * sizeof(sal_Unicode) );
         }
         (*This)->length = nOldLen + len;
commit e7abad5683a28305204977b57357dddefab0e065
Author: Stephan Bergmann <sbergman at redhat.com>
Date:   Thu Nov 20 08:23:16 2014 +0100

    Needless assignment len -> n
    
    Change-Id: I9051284b34e5a6f3f48ee58243d6481b072e0a80

diff --git a/sal/rtl/strbuf.cxx b/sal/rtl/strbuf.cxx
index 18ae7c5..f7a6c99 100644
--- a/sal/rtl/strbuf.cxx
+++ b/sal/rtl/strbuf.cxx
@@ -114,11 +114,10 @@ void SAL_CALL rtl_stringbuffer_insert( rtl_String ** This,
             memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Char) );
 
         /* insert the new characters */
-        n = len;
         if( len == 1 )
                             /* optimized for 1 character */
             pBuf[offset] = *str;
-        else if( n > 1 )
+        else if( len > 1 )
             memcpy( pBuf + offset, str, len * sizeof(sal_Char) );
         (*This)->length = nOldLen + len;
         pBuf[ nOldLen + len ] = 0;
commit c89f15c603cbe59a2a112243ff9f0bf7dff958e1
Author: Stephan Bergmann <sbergman at redhat.com>
Date:   Thu Nov 20 07:47:13 2014 +0100

    Remove comment junk
    
    Change-Id: Ifaed964d084d0b7a2dacbc23a93fb77eb7515ab4

diff --git a/sal/rtl/strbuf.cxx b/sal/rtl/strbuf.cxx
index 5472ceb..18ae7c5 100644
--- a/sal/rtl/strbuf.cxx
+++ b/sal/rtl/strbuf.cxx
@@ -102,10 +102,6 @@ void SAL_CALL rtl_stringbuffer_insert( rtl_String ** This,
         if (*capacity < (*This)->length + len)
             rtl_stringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
 
-        /*
-        if( len == 1 )
-            This->buffer
-        */
         nOldLen = (*This)->length;
         pBuf = (*This)->buffer;
 
diff --git a/sal/rtl/ustrbuf.cxx b/sal/rtl/ustrbuf.cxx
index df8423c..38c1b00 100644
--- a/sal/rtl/ustrbuf.cxx
+++ b/sal/rtl/ustrbuf.cxx
@@ -124,10 +124,6 @@ void SAL_CALL rtl_uStringbuffer_insert( rtl_uString ** This,
         if (*capacity < (*This)->length + len)
             rtl_uStringbuffer_ensureCapacity( This, capacity, (*This)->length + len );
 
-        /*
-        if( len == 1 )
-            This->buffer
-        */
         nOldLen = (*This)->length;
         pBuf = (*This)->buffer;
 
commit 186990395d72a803dd4a9f087fe4e05f49e69ad2
Author: Stephan Bergmann <sbergman at redhat.com>
Date:   Thu Nov 20 07:44:43 2014 +0100

    Clean up Mac _imp_getProcessLocale
    
    Introduces OUStringBuffer::appendUninitialized.
    
    Change-Id: If225ec4d798e0df91cad60130e3f22ee26b88abb

diff --git a/include/rtl/ustrbuf.h b/include/rtl/ustrbuf.h
index e81d40b..adaf92d 100644
--- a/include/rtl/ustrbuf.h
+++ b/include/rtl/ustrbuf.h
@@ -100,7 +100,9 @@ SAL_DLLPUBLIC void SAL_CALL rtl_uStringbuffer_ensureCapacity(
     @param   This        The string, on that the operation should take place
     @param   capacity    the capacity of the string buffer
     @param   offset      the offset.
-    @param   str         a character array.
+    @param   str         a character array.  Since LibreOffice 4.4, as a special
+                         case, if str is null then the len added characters are
+                         left uninitialized.
     @param   len         the number of characters to append.
  */
 SAL_DLLPUBLIC void SAL_CALL rtl_uStringbuffer_insert(
diff --git a/include/rtl/ustrbuf.hxx b/include/rtl/ustrbuf.hxx
index 64c26cf..8bba429 100644
--- a/include/rtl/ustrbuf.hxx
+++ b/include/rtl/ustrbuf.hxx
@@ -474,6 +474,7 @@ public:
     OUStringBuffer & append( const sal_Unicode * str, sal_Int32 len)
     {
         assert( len >= 0 );
+        assert( len == 0 || str != 0 );
         rtl_uStringbuffer_insert( &pData, &nCapacity, getLength(), str, len );
         return *this;
     }
@@ -734,6 +735,28 @@ public:
     }
 
     /**
+       Unsafe way to make space for a fixed amount of characters to be appended
+       into this OUStringBuffer.
+
+       A call to this function must immediately be followed by code that
+       completely fills the uninitialized block pointed to by the return value.
+
+       @param length the length of the uninitialized block of sal_Unicode
+       entities; must be non-negative
+
+       @return a pointer to the start of the uninitialized block; only valid
+       until this OUStringBuffer's capacity changes
+
+       @since LibreOffice 4.4
+    */
+    sal_Unicode * appendUninitialized(sal_Int32 length) {
+        assert(length >= 0);
+        sal_Int32 n = getLength();
+        rtl_uStringbuffer_insert(&pData, &nCapacity, n, 0, length);
+        return pData->buffer + n;
+    }
+
+    /**
         Inserts the string into this string buffer.
 
         The characters of the <code>String</code> argument are inserted, in
@@ -797,6 +820,7 @@ public:
     {
         assert( offset >= 0 && offset <= pData->length );
         assert( len >= 0 );
+        assert( len == 0 || str != 0 );
         rtl_uStringbuffer_insert( &pData, &nCapacity, offset, str, len );
         return *this;
     }
diff --git a/sal/osl/unx/nlsupport.cxx b/sal/osl/unx/nlsupport.cxx
index f6ae33c..8283619 100644
--- a/sal/osl/unx/nlsupport.cxx
+++ b/sal/osl/unx/nlsupport.cxx
@@ -20,6 +20,8 @@
 #include <osl/nlsupport.h>
 #include <osl/diagnose.h>
 #include <osl/process.h>
+#include <rtl/string.hxx>
+#include <rtl/ustring.hxx>
 
 #include "nlsupport.hxx"
 
@@ -844,7 +846,7 @@ rtl_TextEncoding osl_getTextEncodingFromLocale( rtl_Locale * pLocale )
 
 void _imp_getProcessLocale( rtl_Locale ** ppLocale )
 {
-    static char *locale = NULL;
+    static char const *locale = NULL;
 
     /* basic thread safeness */
 //    pthread_mutex_lock( &aLocalMutex );
@@ -852,12 +854,16 @@ void _imp_getProcessLocale( rtl_Locale ** ppLocale )
     /* Only fetch the locale once and cache it */
     if ( NULL == locale )
     {
-
-        locale = (char *)malloc( 128 );
-        if ( locale )
-            macosx_getLocale( locale, 128 );
-        else
-            fprintf( stderr, "nlsupport.c:  locale allocation returned NULL!\n" );
+        rtl::OUString loc16(macosx_getLocale());
+        rtl::OString loc8;
+        if (loc16.convertToString(
+                &loc8, RTL_TEXTENCODING_UTF8,
+                (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
+                 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)))
+        {
+            rtl_string_acquire(loc8.pData); // leak, for setenv
+            locale = loc8.getStr();
+        }
     }
 
     /* handle the case where OS specific method of finding locale fails */
@@ -873,7 +879,7 @@ void _imp_getProcessLocale( rtl_Locale ** ppLocale )
             locale = getenv( "LANG" );
 
         if( NULL == locale )
-            locale = strdup("C");
+            locale = "C";
     }
 
     /* return the locale */
diff --git a/sal/osl/unx/nlsupport.hxx b/sal/osl/unx/nlsupport.hxx
index f7d6ec4..799df2f 100644
--- a/sal/osl/unx/nlsupport.hxx
+++ b/sal/osl/unx/nlsupport.hxx
@@ -24,11 +24,13 @@
 
 #include <rtl/locale.h>
 
+namespace rtl { class OUString; }
+
 void _imp_getProcessLocale( rtl_Locale ** );
 int  _imp_setProcessLocale( rtl_Locale * );
 
 #if defined IOS || defined MACOSX
-void macosx_getLocale(char *locale, sal_uInt32 bufferLen);
+rtl::OUString macosx_getLocale();
 #endif
 
 #endif
diff --git a/sal/osl/unx/osxlocale.cxx b/sal/osl/unx/osxlocale.cxx
index 9bc5742..3fb9664 100644
--- a/sal/osl/unx/osxlocale.cxx
+++ b/sal/osl/unx/osxlocale.cxx
@@ -27,6 +27,8 @@
 #include <CoreFoundation/CoreFoundation.h>
 #include <postmac.h>
 
+#include <rtl/ustrbuf.hxx>
+
 #include <nlsupport.hxx>
 
 namespace
@@ -59,11 +61,17 @@ namespace
 
         return CFLocaleCreateCanonicalLocaleIdentifierFromString(kCFAllocatorDefault, sref);
     }
+
+    void append(rtl::OUStringBuffer & buffer, CFStringRef string) {
+        CFIndex n = CFStringGetLength(string);
+        CFStringGetCharacters(
+            string, CFRangeMake(0, n), buffer.appendUninitialized(n));
+    }
 }
 
 /** Grab current locale from system.
 */
-void macosx_getLocale(char *locale, sal_uInt32 bufferLen)
+rtl::OUString macosx_getLocale()
 {
     CFStringRef sref = getProcessLocale();
     CFStringGuard sGuard(sref);
@@ -75,22 +83,21 @@ void macosx_getLocale(char *locale, sal_uInt32 bufferLen)
     CFArrayRef subs = CFStringCreateArrayBySeparatingStrings(NULL, sref, CFSTR("-"));
     CFArrayGuard arrGuard(subs);
 
-    CFStringRef lang = (CFStringRef)CFArrayGetValueAtIndex(subs, 0);
-    CFStringGetCString(lang, locale, bufferLen, kCFStringEncodingASCII);
+    rtl::OUStringBuffer buf;
+    append(buf, (CFStringRef)CFArrayGetValueAtIndex(subs, 0));
 
     // country also available? Assumption: if the array contains more than one
     // value the second value is always the country!
     if (CFArrayGetCount(subs) > 1)
     {
-        strlcat(locale, "_", bufferLen - strlen(locale));
-
-        CFStringRef country = (CFStringRef)CFArrayGetValueAtIndex(subs, 1);
-        CFStringGetCString(country, locale + strlen(locale), bufferLen - strlen(locale), kCFStringEncodingASCII);
+        buf.append("_");
+        append(buf, (CFStringRef)CFArrayGetValueAtIndex(subs, 1));
     }
     // Append 'UTF-8' to the locale because the Mac OS X file
     // system interface is UTF-8 based and sal tries to determine
     // the file system locale from the locale information
-    strlcat(locale, ".UTF-8", bufferLen - strlen(locale));
+    buf.append(".UTF-8");
+    return buf.makeStringAndClear();
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index e030b9c..ac5a793 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -31,6 +31,7 @@
 #include "sal/types.h"
 
 #include <algorithm>
+#include <cassert>
 #include <float.h>
 #include <limits.h>
 #include <math.h>
@@ -256,6 +257,7 @@ struct UStringTraits
                                    sal_Int32 * pCapacity, sal_Int32 * pOffset,
                                    sal_Unicode const * pChars, sal_Int32 nLen)
     {
+        assert(pChars != nullptr);
         rtl_uStringbuffer_insert(pBuffer, pCapacity, *pOffset, pChars, nLen);
         *pOffset += nLen;
     }
diff --git a/sal/rtl/ustrbuf.cxx b/sal/rtl/ustrbuf.cxx
index 759d05c..df8423c 100644
--- a/sal/rtl/ustrbuf.cxx
+++ b/sal/rtl/ustrbuf.cxx
@@ -140,11 +140,14 @@ void SAL_CALL rtl_uStringbuffer_insert( rtl_uString ** This,
             memmove( pBuf + offset + len, pBuf + offset, n * sizeof(sal_Unicode) );
 
         /* insert the new characters */
-        if( len == 1 )
-            /* optimized for 1 character */
-            pBuf[offset] = *str;
-        else if( len > 1 )
-            memcpy( pBuf + offset, str, len * sizeof(sal_Unicode) );
+        if( str != nullptr )
+        {
+            if( len == 1 )
+                /* optimized for 1 character */
+                pBuf[offset] = *str;
+            else if( len > 1 )
+                memcpy( pBuf + offset, str, len * sizeof(sal_Unicode) );
+        }
         (*This)->length = nOldLen + len;
         pBuf[ nOldLen + len ] = 0;
     }
commit 8cbfce51ff659581a9602b22c36b9af1c69e51f8
Author: Stephan Bergmann <sbergman at redhat.com>
Date:   Wed Nov 19 20:01:28 2014 +0100

    Unused return value
    
    Change-Id: I963d001aee68f1218ba7273562cb41f3cf4c5e20

diff --git a/sal/osl/unx/nlsupport.hxx b/sal/osl/unx/nlsupport.hxx
index 676c00c..f7d6ec4 100644
--- a/sal/osl/unx/nlsupport.hxx
+++ b/sal/osl/unx/nlsupport.hxx
@@ -28,7 +28,7 @@ void _imp_getProcessLocale( rtl_Locale ** );
 int  _imp_setProcessLocale( rtl_Locale * );
 
 #if defined IOS || defined MACOSX
-int macosx_getLocale(char *locale, sal_uInt32 bufferLen);
+void macosx_getLocale(char *locale, sal_uInt32 bufferLen);
 #endif
 
 #endif
diff --git a/sal/osl/unx/osxlocale.cxx b/sal/osl/unx/osxlocale.cxx
index d2fbdb5..9bc5742 100644
--- a/sal/osl/unx/osxlocale.cxx
+++ b/sal/osl/unx/osxlocale.cxx
@@ -63,7 +63,7 @@ namespace
 
 /** Grab current locale from system.
 */
-int macosx_getLocale(char *locale, sal_uInt32 bufferLen)
+void macosx_getLocale(char *locale, sal_uInt32 bufferLen)
 {
     CFStringRef sref = getProcessLocale();
     CFStringGuard sGuard(sref);
@@ -91,8 +91,6 @@ int macosx_getLocale(char *locale, sal_uInt32 bufferLen)
     // system interface is UTF-8 based and sal tries to determine
     // the file system locale from the locale information
     strlcat(locale, ".UTF-8", bufferLen - strlen(locale));
-
-    return noErr;
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list