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

2023-10-24 Thread Stephan Bergmann (via logerrit)
 sal/rtl/strtmpl.hxx |4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

New commits:
commit 3d58f3c0c22a972097956f781c51c2822e6d524f
Author: Stephan Bergmann 
AuthorDate: Tue Oct 24 21:43:29 2023 +0200
Commit: Stephan Bergmann 
CommitDate: Tue Oct 24 22:46:25 2023 +0200

Use requires-clauses

Change-Id: I27c990f27023aba5e77c6b406d7dbdcc898054ab
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/158405
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index bb49cfaf0aa4..6414115d4174 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -229,12 +229,12 @@ sal_Int32 compare(S1 s1, S2 s2, Compare, Shorten_t 
shortenedLength)
 }
 
 // take advantage of builtin optimisations
-template  = 0>
+template  requires (sizeof(C) == sizeof(wchar_t))
 sal_Int32 compare(null_terminated s1, null_terminated s2, CompareNormal, 
NoShortening)
 {
 return wcscmp(reinterpret_cast(s1.p), 
reinterpret_cast(s2.p));
 }
-template  = 0>
+template  requires (sizeof(C) == sizeof(char))
 sal_Int32 compare(null_terminated s1, null_terminated s2, CompareNormal, 
NoShortening)
 {
 return strcmp(reinterpret_cast(s1.p), reinterpret_cast(s2.p));


[Libreoffice-commits] core.git: sal/rtl solenv/clang-format

2023-10-21 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx|  259 
 solenv/clang-format/excludelist |1 
 2 files changed, 108 insertions(+), 152 deletions(-)

New commits:
commit 8b327dd75caf3fd3374ad92f84a70c276678a046
Author: Mike Kaganski 
AuthorDate: Sat Oct 21 11:44:17 2023 +0300
Commit: Mike Kaganski 
CommitDate: Sat Oct 21 12:46:44 2023 +0200

clang-format sal/rtl/math.cxx

Change-Id: I0e807118e6a2196d2f2858ed195782a90572a2e9
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/158303
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 32507b619ff7..68068eaf979f 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -102,8 +102,8 @@ static double getN10Exp(int nExp)
 return n10s[nExp - minExp];
 }
 
-namespace {
-
+namespace
+{
 /** If value (passed as absolute value) is an integer representable as double,
 which we handle explicitly at some places.
  */
@@ -113,8 +113,8 @@ bool isRepresentableInteger(double fAbsValue)
   && std::numeric_limits::digits == 53);
 assert(fAbsValue >= 0.0);
 if (fAbsValue >= 0x1p53)
-  return false;
-sal_Int64 nInt = static_cast< sal_Int64 >(fAbsValue);
+return false;
+sal_Int64 nInt = static_cast(fAbsValue);
 return nInt == fAbsValue;
 }
 
@@ -138,7 +138,7 @@ int getBitsInFracPart(double fAbsValue)
 assert(std::isfinite(fAbsValue) && fAbsValue >= 0.0);
 if (fAbsValue == 0.0)
 return 0;
-auto pValParts = reinterpret_cast< const sal_math_Double * >();
+auto pValParts = reinterpret_cast();
 int nExponent = pValParts->inf_parts.exponent - 1023;
 if (nExponent >= 52)
 return 0; // All bits in fraction are in integer part of the number
@@ -156,61 +156,49 @@ int getBitsInFracPart(double fAbsValue)
 
 return std::max(nBitsInFracPart, 0);
 }
-
 }
 
-void SAL_CALL rtl_math_doubleToString(rtl_String ** pResult,
-  sal_Int32 * pResultCapacity,
+void SAL_CALL rtl_math_doubleToString(rtl_String** pResult, sal_Int32* 
pResultCapacity,
   sal_Int32 nResultOffset, double fValue,
-  rtl_math_StringFormat eFormat,
-  sal_Int32 nDecPlaces,
-  char cDecSeparator,
-  sal_Int32 const * pGroups,
-  char cGroupSeparator,
-  sal_Bool bEraseTrailingDecZeros)
+  rtl_math_StringFormat eFormat, sal_Int32 
nDecPlaces,
+  char cDecSeparator, sal_Int32 const* 
pGroups,
+  char cGroupSeparator, sal_Bool 
bEraseTrailingDecZeros)
 SAL_THROW_EXTERN_C()
 {
-rtl::str::doubleToString(
-pResult, pResultCapacity, nResultOffset, fValue, eFormat, nDecPlaces,
-cDecSeparator, pGroups, cGroupSeparator, bEraseTrailingDecZeros);
+rtl::str::doubleToString(pResult, pResultCapacity, nResultOffset, fValue, 
eFormat, nDecPlaces,
+ cDecSeparator, pGroups, cGroupSeparator, 
bEraseTrailingDecZeros);
 }
 
-void SAL_CALL rtl_math_doubleToUString(rtl_uString ** pResult,
-   sal_Int32 * pResultCapacity,
+void SAL_CALL rtl_math_doubleToUString(rtl_uString** pResult, sal_Int32* 
pResultCapacity,
sal_Int32 nResultOffset, double fValue,
-   rtl_math_StringFormat eFormat,
-   sal_Int32 nDecPlaces,
-   sal_Unicode cDecSeparator,
-   sal_Int32 const * pGroups,
-   sal_Unicode cGroupSeparator,
-   sal_Bool bEraseTrailingDecZeros)
+   rtl_math_StringFormat eFormat, 
sal_Int32 nDecPlaces,
+   sal_Unicode cDecSeparator, sal_Int32 
const* pGroups,
+   sal_Unicode cGroupSeparator, sal_Bool 
bEraseTrailingDecZeros)
 SAL_THROW_EXTERN_C()
 {
-rtl::str::doubleToString(
-pResult, pResultCapacity, nResultOffset, fValue, eFormat, nDecPlaces,
-cDecSeparator, pGroups, cGroupSeparator, bEraseTrailingDecZeros);
+rtl::str::doubleToString(pResult, pResultCapacity, nResultOffset, fValue, 
eFormat, nDecPlaces,
+ cDecSeparator, pGroups, cGroupSeparator, 
bEraseTrailingDecZeros);
 }
 
-namespace {
-
-template< typename CharT >
-double stringToDouble(CharT const * pBegin, CharT const * pEnd,
- CharT cDecSeparator, CharT cGroupSeparator,
- rtl_math_ConversionStatus * pStatus,
- CharT 

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

2023-07-20 Thread Vladislav Tarakanov (via logerrit)
 sal/rtl/strtmpl.hxx |8 
 1 file changed, 4 insertions(+), 4 deletions(-)

New commits:
commit 3f96f9cdd542bcc05cdd531da3c35bc6f0c2986b
Author: Vladislav Tarakanov 
AuthorDate: Thu Jul 20 14:45:51 2023 +0400
Commit: Eike Rathke 
CommitDate: Fri Jul 21 00:51:36 2023 +0200

tdf#139306 Incorrect nDecPlaces value after clamping

nDecPlaces clamping diap changed from +-20 to +-309 and buffer is resized 
for new value

Change-Id: Icb2130891598cf02623bbf5bd0273edab529d124
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/153815
Tested-by: Jenkins
Reviewed-by: Eike Rathke 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index eece825ec082..bb49cfaf0aa4 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -1540,7 +1540,7 @@ void doubleToString(rtl_tString** pResult, sal_Int32* 
pResultCapacity, sal_Int32
 // rtl_math_DecimalPlaces_Max was passed with rtl_math_StringFormat_F or
 // others, but we don't want to allocate/deallocate 2GB just to fill it
 // with trailing '0' characters..
-nDecPlaces = std::clamp(nDecPlaces, -20, 20);
+nDecPlaces = std::clamp(nDecPlaces, -309, 309);
 
 sal_Int32 nDigits = nDecPlaces + 1;
 
@@ -1566,9 +1566,9 @@ void doubleToString(rtl_tString** pResult, sal_Int32* 
pResultCapacity, sal_Int32
 sal_Int32 nBuf
 = (nDigits <= 0 ? std::max(nDecPlaces, std::abs(nExp)) : 
nDigits + nDecPlaces)
   + 10 + (pGroups ? std::abs(nDigits) * 2 : 0);
-// max(nDigits) = max(nDecPlaces) + 1 + max(nExp) + 1 = 20 + 1 + 308 + 1 = 
330
-// max(nBuf) = max(nDigits) + max(nDecPlaces) + 10 + max(nDigits) * 2 = 
330 * 3 + 20 + 10 = 1020
-assert(nBuf <= 1024);
+// max(nDigits) = max(nDecPlaces) + 1 + max(nExp) + 1 = 309 + 1 + 308 + 1 
= 619
+// max(nBuf) = max(nDigits) + max(nDecPlaces) + 10 + max(nDigits) * 2 = 
619 * 3 + 309 + 10 = 2176
+assert(nBuf <= 2176);
 auto* const pBuf = static_cast*>(alloca(nBuf * 
sizeof(Char_T)));
 auto* p = pBuf;
 if (bSign)


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

2023-07-12 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx |   32 +++-
 1 file changed, 11 insertions(+), 21 deletions(-)

New commits:
commit d706f5dc6872b94859444f1cacb31d89e0fb840f
Author: Mike Kaganski 
AuthorDate: Wed Jul 12 10:30:28 2023 +0200
Commit: Mike Kaganski 
CommitDate: Wed Jul 12 14:37:45 2023 +0200

Simplify a bit

Change-Id: Idd3e96d99f13b1e87e2a01de9c9392ead0864de7
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/154323
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 0d70e9718bfd..9a8c2614060f 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -112,22 +112,13 @@ namespace {
  */
 bool isRepresentableInteger(double fAbsValue)
 {
+static_assert(std::numeric_limits::is_iec559
+  && std::numeric_limits::digits == 53);
 assert(fAbsValue >= 0.0);
-const sal_Int64 kMaxInt = (static_cast< sal_Int64 >(1) << 53) - 1;
-if (fAbsValue <= static_cast< double >(kMaxInt))
-{
-sal_Int64 nInt = static_cast< sal_Int64 >(fAbsValue);
-// Check the integer range again because double comparison may yield
-// true within the precision range.
-// XXX loplugin:fpcomparison complains about floating-point comparison
-// for static_cast(nInt) == fAbsValue, though we actually want
-// this here.
-if (nInt > kMaxInt)
-return false;
-double fInt = static_cast< double >(nInt);
-return !(fInt < fAbsValue) && !(fInt > fAbsValue);
-}
-return false;
+if (fAbsValue >= 0x1p53)
+  return false;
+sal_Int64 nInt = static_cast< sal_Int64 >(fAbsValue);
+return nInt == fAbsValue;
 }
 
 // Returns 1-based index of least significant bit in a number, or zero if 
number is zero
@@ -724,12 +715,11 @@ double SAL_CALL rtl_math_approxValue( double fValue ) 
SAL_THROW_EXTERN_C()
 bool SAL_CALL rtl_math_approxEqual(double a, double b) SAL_THROW_EXTERN_C()
 {
 static const double e48 = 0x1p-48;
-static const double e44 = 0x1p-44;
 
 if (a == b)
 return true;
 
-if (a == 0.0 || b == 0.0)
+if (a == 0.0 || b == 0.0 || std::signbit(a) != std::signbit(b))
 return false;
 
 const double d = fabs(a - b);
@@ -737,16 +727,16 @@ bool SAL_CALL rtl_math_approxEqual(double a, double b) 
SAL_THROW_EXTERN_C()
 return false;   // Nan or Inf involved
 
 a = fabs(a);
-if (d > (a * e44))
+if (d >= (a * e48))
 return false;
 b = fabs(b);
-if (d > (b * e44))
+if (d >= (b * e48))
 return false;
 
-if (isRepresentableInteger(d) && isRepresentableInteger(a) && 
isRepresentableInteger(b))
+if (isRepresentableInteger(a) && isRepresentableInteger(b))
 return false;   // special case for representable integers.
 
-return (d < a * e48 && d < b * e48);
+return true;
 }
 
 double SAL_CALL rtl_math_expm1(double fValue) SAL_THROW_EXTERN_C()


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

2023-05-05 Thread Mike Kaganski (via logerrit)
 sal/rtl/string.cxx  |6 +++---
 sal/rtl/strtmpl.hxx |   44 +++-
 sal/rtl/ustring.cxx |6 +++---
 3 files changed, 17 insertions(+), 39 deletions(-)

New commits:
commit 4cedea47f9f97ec04e25e36cea38a2fb536bb1bc
Author: Mike Kaganski 
AuthorDate: Fri May 5 20:58:24 2023 +0200
Commit: Mike Kaganski 
CommitDate: Sat May 6 07:24:57 2023 +0200

Simplify rtl_(u)string_newReplace implementation

and unify with *ToAscii(Lower/Upper)Case

Change-Id: I06999b4f5f34abc8da2860b7f9e279608edb40dc
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151381
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index 23196e528682..06cde3c4faad 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -638,19 +638,19 @@ void SAL_CALL 
rtl_string_newReplaceStrAt_WithLength(rtl_String** ppThis, rtl_Str
 void SAL_CALL rtl_string_newReplace(rtl_String** ppThis, rtl_String* pStr, 
char cOld, char cNew)
 SAL_THROW_EXTERN_C()
 {
-rtl::str::newReplace(ppThis, pStr, cOld, cNew);
+rtl::str::newReplaceChars(ppThis, pStr, rtl::str::FromTo(cOld, cNew));
 }
 
 void SAL_CALL rtl_string_newToAsciiLowerCase(rtl_String** ppThis, rtl_String* 
pStr)
 SAL_THROW_EXTERN_C()
 {
-rtl::str::newReplaceChars(ppThis, pStr);
+rtl::str::newReplaceChars(ppThis, pStr, rtl::str::toAsciiLower);
 }
 
 void SAL_CALL rtl_string_newToAsciiUpperCase(rtl_String** ppThis, rtl_String* 
pStr)
 SAL_THROW_EXTERN_C()
 {
-rtl::str::newReplaceChars(ppThis, pStr);
+rtl::str::newReplaceChars(ppThis, pStr, rtl::str::toAsciiUpper);
 }
 
 void SAL_CALL rtl_string_newTrim(rtl_String** ppThis, rtl_String* pStr) 
SAL_THROW_EXTERN_C()
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index ba182f87817e..cbbef030198f 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -87,35 +87,20 @@ template  struct with_length
 auto end() const { return p + len; }
 };
 
-struct ToAsciiLower
+template  
struct CaseReplace
 {
-template  static bool Applicable(C c)
-{
-return rtl::isAsciiUpperCase(UChar(c));
-}
-template  static C Replace(C c)
-{
-return rtl::toAsciiLowerCase(UChar(c));
-}
-} constexpr toAsciiLower;
-
-struct ToAsciiUpper
-{
-template  static bool Applicable(C c)
-{
-return rtl::isAsciiLowerCase(UChar(c));
-}
-template  static C Replace(C c)
-{
-return rtl::toAsciiUpperCase(UChar(c));
-}
-} constexpr toAsciiUpper;
+static auto Applicable() { return [](auto c) { return 
fApplicable(UChar(c)); }; }
+template  static C Replace(C c) { return fReplace(UChar(c)); }
+};
+constexpr CaseReplace 
toAsciiLower;
+constexpr CaseReplace 
toAsciiUpper;
 
 template  struct FromTo
 {
 C from;
 C to;
 FromTo(C cFrom, C cTo) : from(cFrom), to(cTo) {}
+auto Applicable() const { return [this](C c) { return c == from; }; }
 C Replace(C c) const { return c == from ? to : c; }
 };
 
@@ -1112,14 +1097,14 @@ void newReplaceStrAt(rtl_tString** ppThis, rtl_tString* 
pStr, sal_Int32 nIndex,
 
 /* --- */
 
-template 
-void newReplaceChars(rtl_tString** ppThis, rtl_tString* pStr)
+template 
+void newReplaceChars(rtl_tString** ppThis, rtl_tString* pStr, Replacer 
replacer)
 {
 assert(ppThis);
 assert(pStr);
 
 const auto pEnd = pStr->buffer + pStr->length;
-auto pCharStr = std::find_if(pStr->buffer, pEnd, [](auto c) { return 
Traits::Applicable(c); });
+auto pCharStr = std::find_if(pStr->buffer, pEnd, replacer.Applicable());
 if (pCharStr != pEnd)
 {
 rtl_tString* pOrg = *ppThis;
@@ -1133,7 +1118,7 @@ void newReplaceChars(rtl_tString** ppThis, rtl_tString* 
pStr)
 /* replace/copy rest of the string */
 do
 {
-*pNewCharStr = Traits::Replace(*pCharStr);
+*pNewCharStr = replacer.Replace(*pCharStr);
 pNewCharStr++;
 pCharStr++;
 } while (pCharStr != pEnd);
@@ -1367,13 +1352,6 @@ void newReplaceAllFromIndex(S** s, S* s1, CharTypeFrom 
const* from, sal_Int32 fr
 RTL_LOG_STRING_NEW(*s);
 }
 
-template 
-void newReplace(rtl_tString** ppThis, rtl_tString* pStr,
-Char_T cOld, Char_T cNew)
-{
-return newReplaceAllFromIndex(ppThis, pStr, , 1, , 1, 0);
-}
-
 template 
 void newReplaceFirst(rtl_tString** s, rtl_tString* s1, C1 const* from, 
sal_Int32 fromLength,
  C2 const* to, sal_Int32 toLength, sal_Int32& fromIndex)
diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx
index fc23cf37a338..c863be00512b 100644
--- a/sal/rtl/ustring.cxx
+++ b/sal/rtl/ustring.cxx
@@ -1272,19 +1272,19 @@ void SAL_CALL 
rtl_uString_newReplaceStrAtUtf16L(rtl_uString** ppThis, rtl_uStrin
 void SAL_CALL rtl_uString_newReplace(rtl_uString** ppThis, rtl_uString* pStr, 
sal_Unicode cOld,
  

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

2023-04-29 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |  435 +---
 1 file changed, 179 insertions(+), 256 deletions(-)

New commits:
commit 876010cbc4584249e919c694b8b977fd4e83084e
Author: Mike Kaganski 
AuthorDate: Sat Apr 29 11:59:46 2023 +0200
Commit: Mike Kaganski 
CommitDate: Sat Apr 29 13:15:45 2023 +0200

Simplify strtmpl.hxx

Replace bulky and difficult-to-read template argument names with
clearer ones; remove excessive whitespaces; reorder to avoid forward
declarations (these are remnants from the refactor from macros to
templates, when I tried to keep the old structure as close to the
original as possible).

And also a small code simplification in doubleToString.

Change-Id: I901cfb86daf410e85269538f36ebb6b553a4be76
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151125
Tested-by: Mike Kaganski 
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index cbf34fba2ed7..aa126faac1e8 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -49,7 +49,7 @@ void internRelease(rtl_uString*);
 
 namespace rtl::str
 {
-template  auto IMPL_RTL_USTRCODE(C c) { return 
std::make_unsigned_t(c); }
+template  auto UChar(C c) { return std::make_unsigned_t(c); }
 
 // Wrappers around null-terminated/known-length strings, that allow to 
generalize algorithms
 // without overhead (e.g., without need to get length of null-terminated 
strings).
@@ -91,11 +91,11 @@ struct ToAsciiLower
 {
 template  static bool Applicable(C c)
 {
-return rtl::isAsciiUpperCase(IMPL_RTL_USTRCODE(c));
+return rtl::isAsciiUpperCase(UChar(c));
 }
 template  static C Replace(C c)
 {
-return rtl::toAsciiLowerCase(IMPL_RTL_USTRCODE(c));
+return rtl::toAsciiLowerCase(UChar(c));
 }
 } constexpr toAsciiLower;
 
@@ -103,11 +103,11 @@ struct ToAsciiUpper
 {
 template  static bool Applicable(C c)
 {
-return rtl::isAsciiLowerCase(IMPL_RTL_USTRCODE(c));
+return rtl::isAsciiLowerCase(UChar(c));
 }
 template  static C Replace(C c)
 {
-return rtl::toAsciiUpperCase(IMPL_RTL_USTRCODE(c));
+return rtl::toAsciiUpperCase(UChar(c));
 }
 } constexpr toAsciiUpper;
 
@@ -195,8 +195,8 @@ struct CompareNormal
 template  static sal_Int32 compare(C1 c1, C2 c2)
 {
 warnIfOneIsCharAndNotAscii(c1, c2);
-return static_cast(IMPL_RTL_USTRCODE(c1))
-   - static_cast(IMPL_RTL_USTRCODE(c2));
+return static_cast(UChar(c1))
+   - static_cast(UChar(c2));
 }
 };
 
@@ -205,7 +205,7 @@ struct CompareIgnoreAsciiCase
 template  static sal_Int32 compare(C1 c1, C2 c2)
 {
 warnIfOneIsCharAndNotAscii(c1, c2);
-return rtl::compareIgnoreAsciiCase(IMPL_RTL_USTRCODE(c1), 
IMPL_RTL_USTRCODE(c2));
+return rtl::compareIgnoreAsciiCase(UChar(c1), UChar(c2));
 }
 };
 
@@ -294,25 +294,13 @@ sal_Int32 reverseCompare_WithLengths(const C1* pStr1, 
sal_Int32 nStr1Len,
 
 /* --- */
 
-template 
-sal_Int32 hashCode_WithLength(const IMPL_RTL_STRCODE*, sal_Int32);
-
-template  sal_Int32 hashCode( const 
IMPL_RTL_STRCODE* pStr )
-{
-return hashCode_WithLength( pStr, getLength( pStr ) );
-}
-
-/* --- */
-
-template 
-sal_Int32 hashCode_WithLength ( const 
IMPL_RTL_STRCODE* pStr,
-sal_Int32 nLen )
+template  sal_Int32 hashCode_WithLength(const C* pStr, sal_Int32 
nLen)
 {
 assert(nLen >= 0);
 sal_uInt32 h = static_cast(nLen);
 while ( nLen > 0 )
 {
-h = (h*37U) + IMPL_RTL_USTRCODE( *pStr );
+h = (h*37U) + UChar( *pStr );
 pStr++;
 nLen--;
 }
@@ -321,21 +309,26 @@ sal_Int32 hashCode_WithLength 
( const IMPL_RTL_STRCO
 
 /* --- */
 
-template 
-sal_Int32 indexOfChar ( const IMPL_RTL_STRCODE* 
pStr,
-IMPL_RTL_STRCODE c )
+template  sal_Int32 hashCode(const C* pStr)
+{
+return hashCode_WithLength( pStr, getLength( pStr ) );
+}
+
+/* --- */
+
+template  sal_Int32 indexOfChar(const C* pStr, C c)
 {
 assert(pStr);
 if (!c)
 return -1; // Unifies behavior of strchr/wcschr and unoptimized 
algorithm wrt '\0'
 
-if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char))
+if constexpr (sizeof(C) == sizeof(char))
 {
 // take advantage of builtin optimisations
-const IMPL_RTL_STRCODE* p = strchr(pStr, c);
+const C* p = strchr(pStr, c);
 return p ? p - pStr : -1;
 }
-else if constexpr (sizeof(IMPL_RTL_STRCODE) == 

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

2023-04-02 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |   14 +++---
 1 file changed, 3 insertions(+), 11 deletions(-)

New commits:
commit ebf2cc4e9dacdc7afdeb4fcc91482678fa3edc4a
Author: Mike Kaganski 
AuthorDate: Sun Apr 2 15:01:31 2023 +0200
Commit: Mike Kaganski 
CommitDate: Sun Apr 2 16:24:12 2023 +0200

Simplify a bit

Change-Id: Iae784d3ae40cd237c768413c067a2067c608238d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/149885
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index ef425a9b33ab..7ec3af82cf94 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -667,9 +667,9 @@ sal_Int32 valueOfInt   ( 
IMPL_RTL_STRCODE* pStr,
 {
 assert(pStr);
 assert( nRadix >= RTL_STR_MIN_RADIX && nRadix <= RTL_STR_MAX_RADIX );
+const auto* const pStart = pStr;
 charaBuf[maxLen];
 char*   pBuf = aBuf;
-sal_Int32   nLen = 0;
 using uT = std::make_unsigned_t;
 uT nValue;
 
@@ -684,7 +684,6 @@ sal_Int32 valueOfInt   ( 
IMPL_RTL_STRCODE* pStr,
 {
 *pStr = '-';
 pStr++;
-nLen++;
 nValue = n == std::numeric_limits::min() ? static_cast(n) : 
-n;
 }
 else
@@ -707,17 +706,10 @@ sal_Int32 valueOfInt   ( 
IMPL_RTL_STRCODE* pStr,
 while ( nValue > 0 );
 
 /* copy the values in the right direction into the destination buffer */
-do
-{
-pBuf--;
-*pStr = *pBuf;
-pStr++;
-nLen++;
-}
-while ( pBuf != aBuf );
+pStr = std::reverse_copy(aBuf, pBuf, pStr);
 *pStr = 0;
 
-return nLen;
+return pStr - pStart;
 }
 
 /* --- */


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

2023-03-20 Thread Stephan Bergmann (via logerrit)
 sal/rtl/strtmpl.hxx |4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

New commits:
commit a44c9d023041c881f7858ddd9d93809dc1861313
Author: Stephan Bergmann 
AuthorDate: Mon Mar 20 09:35:12 2023 +0100
Commit: Stephan Bergmann 
CommitDate: Mon Mar 20 10:44:40 2023 +

rtl_allocateString takes a size_t parameter

Change-Id: Ibfdc0e7e3af243f157d5d14e3fbe5ab204c0b8df
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/149140
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index ca2800c6a012..ef425a9b33ab 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -835,8 +835,8 @@ template  
IMPL_RTL_STRINGDATA* Alloc( sal_Int32 nL
 {
 constexpr auto fix = offsetof(IMPL_RTL_STRINGDATA, buffer) + sizeof 
IMPL_RTL_STRINGDATA::buffer;
 IMPL_RTL_STRINGDATA * pData
-= (sal::static_int_cast< sal_uInt32 >(nLen)
-   <= ((SAL_MAX_UINT32 - fix)
+= (o3tl::make_unsigned(nLen)
+   <= ((std::numeric_limits::max() - fix)
/ sizeof (STRCODE)))
 ? static_cast(rtl_allocateString(
 fix + nLen * sizeof (STRCODE)))


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

2023-03-20 Thread Stephan Bergmann (via logerrit)
 sal/rtl/strtmpl.hxx |6 --
 1 file changed, 4 insertions(+), 2 deletions(-)

New commits:
commit 926095df6b444866cd7e245b6a7befa6c2e29424
Author: Stephan Bergmann 
AuthorDate: Sun Mar 19 22:50:46 2023 +0100
Commit: Stephan Bergmann 
CommitDate: Mon Mar 20 07:01:25 2023 +

Shrink rtl_[u]String allocation size to what's actually needed

Change-Id: Ib39caf31b5d2fb06cc81cdeb14578794b35d8ed4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/149120
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index a814f4998a43..ca2800c6a012 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -22,6 +22,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -832,12 +833,13 @@ template  using STRINGDATA = typename 
STRINGDATA_::T;
 
 template  IMPL_RTL_STRINGDATA* Alloc( sal_Int32 
nLen )
 {
+constexpr auto fix = offsetof(IMPL_RTL_STRINGDATA, buffer) + sizeof 
IMPL_RTL_STRINGDATA::buffer;
 IMPL_RTL_STRINGDATA * pData
 = (sal::static_int_cast< sal_uInt32 >(nLen)
-   <= ((SAL_MAX_UINT32 - sizeof (IMPL_RTL_STRINGDATA))
+   <= ((SAL_MAX_UINT32 - fix)
/ sizeof (STRCODE)))
 ? static_cast(rtl_allocateString(
-sizeof (IMPL_RTL_STRINGDATA) + nLen * sizeof 
(STRCODE)))
+fix + nLen * sizeof (STRCODE)))
 : nullptr;
 if (pData != nullptr) {
 pData->refCount = 1;


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

2023-02-01 Thread Stephan Bergmann (via logerrit)
 sal/rtl/strtmpl.hxx |   11 +--
 1 file changed, 1 insertion(+), 10 deletions(-)

New commits:
commit 6028e9fda96d0ed5da266b1c54a7755f7ba3408c
Author: Stephan Bergmann 
AuthorDate: Wed Feb 1 09:24:01 2023 +0100
Commit: Stephan Bergmann 
CommitDate: Wed Feb 1 09:51:15 2023 +

Finally drop undocumented rtl_[u]String_newFromStr null argument support

...executing on the TODO left by 4f0c70fb5554325e0cc2129741175bf07de22029 
"Avoid
calling OString ctor with null pointer" in late 2020.

Change-Id: I3db6e2df61ca290948affc5e02ae74757441471d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/146428
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index eb9371219178..a814f4998a43 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -982,16 +982,7 @@ template 
 void newFromStr( IMPL_RTL_STRINGDATA** ppThis,
  const 
STRCODE* pCharStr )
 {
-#if OSL_DEBUG_LEVEL > 0
-//TODO: For now, only abort in non-production debug builds; once all 
places that rely on the
-// undocumented newFromStr behavior of treating a null pCharStr like an 
empty string have been
-// found and fixed, drop support for that behavior and turn this into a 
general assert:
-if (pCharStr == nullptr) {
-std::abort();
-}
-#endif
-
-newFromStr_WithLength(ppThis, pCharStr, pCharStr ? getLength(pCharStr) : 
0);
+newFromStr_WithLength(ppThis, pCharStr, getLength(pCharStr));
 }
 
 /* --- */


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

2022-12-04 Thread Arnaud VERSINI (via logerrit)
 sal/rtl/bootstrap.cxx |   24 
 1 file changed, 12 insertions(+), 12 deletions(-)

New commits:
commit 4e81e3b6c0acad2f3e59564184f8bf70d166557d
Author: Arnaud VERSINI 
AuthorDate: Sat Dec 3 15:44:57 2022 +0100
Commit: Noel Grandin 
CommitDate: Sun Dec 4 13:07:32 2022 +

rtl bootsratp : use enum class

Change-Id: Iea433a2a7be9b62f04b57883dbefaf25586f21d6
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143616
Tested-by: Jenkins
Reviewed-by: Noel Grandin 

diff --git a/sal/rtl/bootstrap.cxx b/sal/rtl/bootstrap.cxx
index 6188406cab5d..422411190ea4 100644
--- a/sal/rtl/bootstrap.cxx
+++ b/sal/rtl/bootstrap.cxx
@@ -85,9 +85,9 @@ bool resolvePathnameUrl(OUString * url)
 return false;
 }
 
-enum LookupMode {
-LOOKUP_MODE_NORMAL, LOOKUP_MODE_URE_BOOTSTRAP,
-LOOKUP_MODE_URE_BOOTSTRAP_EXPANSION };
+enum class LookupMode {
+NORMAL, URE_BOOTSTRAP,
+URE_BOOTSTRAP_EXPANSION };
 
 struct ExpandRequestLink {
 ExpandRequestLink const * next;
@@ -401,7 +401,7 @@ struct FundamentalIniData
 OUString uri;
 ini =
 (get_static_bootstrap_handle()->getValue(
-"URE_BOOTSTRAP", , nullptr, LOOKUP_MODE_NORMAL, 
false,
+"URE_BOOTSTRAP", , nullptr, LookupMode::NORMAL, 
false,
 nullptr)
  && resolvePathnameUrl())
 ? rtl_bootstrap_args_open(uri.pData) : nullptr;
@@ -426,8 +426,8 @@ bool Bootstrap_Impl::getValue(
 LookupMode mode, bool override, ExpandRequestLink const * requestStack)
 const
 {
-if (mode == LOOKUP_MODE_NORMAL && key == "URE_BOOTSTRAP")
-mode = LOOKUP_MODE_URE_BOOTSTRAP;
+if (mode == LookupMode::NORMAL && key == "URE_BOOTSTRAP")
+mode =  LookupMode::URE_BOOTSTRAP;
 
 if (override && getDirectValue(key, value, mode, requestStack))
 return true;
@@ -518,7 +518,7 @@ bool Bootstrap_Impl::getValue(
 if (!override && getDirectValue(key, value, mode, requestStack))
 return true;
 
-if (mode == LOOKUP_MODE_NORMAL)
+if (mode == LookupMode::NORMAL)
 {
 FundamentalIniData const & d = FundamentalIni();
 Bootstrap_Impl const * b = static_cast(d.ini);
@@ -579,12 +579,12 @@ void Bootstrap_Impl::expandValue(
 {
 rtl_uString_assign(
 value,
-(mode == LOOKUP_MODE_URE_BOOTSTRAP && isPathnameUrl(text) ?
+(mode ==  LookupMode::URE_BOOTSTRAP && isPathnameUrl(text) ?
  text :
  recursivelyExpandMacros(
  this, text,
- (mode == LOOKUP_MODE_URE_BOOTSTRAP ?
-  LOOKUP_MODE_URE_BOOTSTRAP_EXPANSION : mode),
+ (mode ==  LookupMode::URE_BOOTSTRAP ?
+   LookupMode::URE_BOOTSTRAP_EXPANSION : mode),
  requestFile, requestKey, requestStack)).pData);
 }
 
@@ -698,7 +698,7 @@ sal_Bool SAL_CALL rtl_bootstrap_get_from_handle(
 handle = get_static_bootstrap_handle();
 
 found = static_cast< Bootstrap_Impl * >(handle)->getValue(
-pName, ppValue, pDefault, LOOKUP_MODE_NORMAL, false, nullptr );
+pName, ppValue, pDefault,  LookupMode::NORMAL, false, nullptr );
 }
 
 return found;
@@ -775,7 +775,7 @@ void SAL_CALL rtl_bootstrap_expandMacros_from_handle(
 
 OUString expanded(expandMacros(static_cast< Bootstrap_Impl * >(handle),
OUString::unacquired(macro),
-   LOOKUP_MODE_NORMAL, nullptr));
+LookupMode::NORMAL, nullptr));
 rtl_uString_assign(macro, expanded.pData);
 }
 


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

2022-09-27 Thread Arnaud VERSINI (via logerrit)
 sal/rtl/uuid.cxx |   12 
 1 file changed, 12 deletions(-)

New commits:
commit 49279212597db3993e1f8f1f70be873a2e22fe2f
Author: Arnaud VERSINI 
AuthorDate: Sat Aug 13 14:42:19 2022 +0200
Commit: Michael Stahl 
CommitDate: Tue Sep 27 10:59:22 2022 +0200

rtl uuid : remove useless endian conversion

Change-Id: I65a4dad0aceb83f2449c86c438cb478937c8b90a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/138229
Tested-by: Jenkins
Reviewed-by: Michael Stahl 

diff --git a/sal/rtl/uuid.cxx b/sal/rtl/uuid.cxx
index 5d348cb8f8e7..22e7f0fe0416 100644
--- a/sal/rtl/uuid.cxx
+++ b/sal/rtl/uuid.cxx
@@ -25,14 +25,6 @@
 #include 
 #include 
 
-#define SWAP_INT32_TO_NETWORK(x)\
-   { sal_uInt32 y = x;\
- sal_uInt8 *p = reinterpret_cast(&(x)); \
- p[0] = static_cast( ( y >> 24 ) & 0xff );\
- p[1] = static_cast( ( y >> 16 ) & 0xff );\
- p[2] = static_cast( ( y >> 8 )  & 0xff );\
- p[3] = static_cast( ( y ) & 0xff);\
-   }
 #define SWAP_INT16_TO_NETWORK(x)\
{ sal_uInt16 y = x;\
  sal_uInt8 *p = reinterpret_cast(&(x)); \
@@ -75,8 +67,6 @@ static void write_v3( sal_uInt8 *pUuid  )
 // copy to avoid alignment problems
 memcpy(, pUuid, 16);
 
-SWAP_NETWORK_TO_INT32(uuid.time_low);
-SWAP_NETWORK_TO_INT16(uuid.time_mid);
 SWAP_NETWORK_TO_INT16(uuid.time_hi_and_version);
 
 /* put in the variant and version bits */
@@ -85,8 +75,6 @@ static void write_v3( sal_uInt8 *pUuid  )
 uuid.clock_seq_hi_and_reserved &= 0x3F;
 uuid.clock_seq_hi_and_reserved |= 0x80;
 
-SWAP_INT32_TO_NETWORK(uuid.time_low);
-SWAP_INT16_TO_NETWORK(uuid.time_mid);
 SWAP_INT16_TO_NETWORK(uuid.time_hi_and_version);
 
 memcpy(pUuid, , 16);


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

2022-08-16 Thread Arnaud VERSINI (via logerrit)
 sal/rtl/uuid.cxx |6 --
 1 file changed, 4 insertions(+), 2 deletions(-)

New commits:
commit 3329bfe698ecb65cbfbe2bf8f38fce45809fd436
Author: Arnaud VERSINI 
AuthorDate: Mon Aug 15 16:54:24 2022 +0200
Commit: Noel Grandin 
CommitDate: Tue Aug 16 09:25:31 2022 +0200

rtl : use a local std::mutex instead of the global mutex

Change-Id: I95a35fe451e459276dcb8c9b90d515d0a34fe36c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/138318
Tested-by: Jenkins
Reviewed-by: Noel Grandin 

diff --git a/sal/rtl/uuid.cxx b/sal/rtl/uuid.cxx
index 130be12456d6..5d348cb8f8e7 100644
--- a/sal/rtl/uuid.cxx
+++ b/sal/rtl/uuid.cxx
@@ -17,10 +17,10 @@
  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
  */
 
+#include 
 #include 
 #include 
 
-#include 
 #include 
 #include 
 #include 
@@ -108,7 +108,9 @@ extern "C" void SAL_CALL rtl_createUuid(sal_uInt8 
*pTargetUUID ,
 return aPool;
 }();
 
-osl::MutexGuard g(osl::Mutex::getGlobalMutex());
+static std::mutex aMutex;
+
+std::scoped_lock g(aMutex);
 if (rtl_random_getBytes(pool, pTargetUUID, 16) != rtl_Random_E_None)
 {
 abort();


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

2022-08-05 Thread Stephan Bergmann (via logerrit)
 sal/rtl/bootstrap.cxx |2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

New commits:
commit fa5ba20b953da85ad387449cf613d7be59c3d5c2
Author: Stephan Bergmann 
AuthorDate: Thu Aug 4 21:10:10 2022 +0200
Commit: Stephan Bergmann 
CommitDate: Fri Aug 5 08:12:52 2022 +0200

Fix typo

...introduced in f59136a2ed1e3eb01cc5b62c5a7da07c34cbdfae "tdf#91794 remove
OSL_DEBUG_LEVEL > 1 conditionals"

Change-Id: I4ead69b3fac4ab51602c3cbd25c8481e749a3e75
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/137835
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann 

diff --git a/sal/rtl/bootstrap.cxx b/sal/rtl/bootstrap.cxx
index 12558d319e44..6188406cab5d 100644
--- a/sal/rtl/bootstrap.cxx
+++ b/sal/rtl/bootstrap.cxx
@@ -761,7 +761,7 @@ void SAL_CALL rtl_bootstrap_set (
 }
 }
 
-SAL_INFO("sal.bootstrap", "explicitly getting: name=" << name << " value=" 

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

2022-06-23 Thread Stephan Bergmann (via logerrit)
 sal/rtl/strtmpl.hxx |4 
 1 file changed, 4 insertions(+)

New commits:
commit ab627d300d0346d1f55b15687b56bdd8df3e2116
Author: Stephan Bergmann 
AuthorDate: Thu Jun 23 22:36:49 2022 +0200
Commit: Stephan Bergmann 
CommitDate: Fri Jun 24 07:56:34 2022 +0200

Let rtl_[u]stringbuffer_insert throw std::bad_alloc on overflow

Prior to a95c585433246813096e8890b7ed6ef4fe30c621 "Pump XInputStream into an
SvMemoryStream rather than an OStringBuffer", this change would have caused

> $ truncate -s 3G test.xml
> $ instdir/program/soffice test.xml

to open an effectively empty draw document (after apparently catching the
std::bad_alloc now thrown from within OrcusFormatDetect::detect; see that
commit's commit message for details) rather than crashing.  This works 
because
rtl_[u]stringbuffer_insert happen not to be decorated with 
SAL_THROW_EXTERN_C().
But even if they were (or when they are called from an external C program), 
it
wouldn't be worse to let the process terminate due to the unexpected C++
std::bad_alloc exception than to let it crash due to an overflown signed 
integer
computation and out-of-bounds memory write.

Change-Id: I21e353367e2b978e8893a2886ac875367a75abd9
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/136352
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index e1389ce6ad1b..eb9371219178 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -26,6 +26,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -1405,6 +1406,9 @@ void stringbuffer_insert(IMPL_RTL_STRINGDATA** ppThis, 
sal_Int32* capacity, sal_
 assert(len >= 0);
 if (len == 0)
 return;
+if (len > std::numeric_limits::max() - (*ppThis)->length) {
+throw std::bad_alloc();
+}
 
 stringbuffer_ensureCapacity(ppThis, capacity, (*ppThis)->length + len);
 


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

2022-05-13 Thread dante (via logerrit)
 sal/rtl/alloc_global.cxx |6 ++
 1 file changed, 2 insertions(+), 4 deletions(-)

New commits:
commit 27a273293618e6dbc2de5b299247b1d9f05864c6
Author: dante 
AuthorDate: Wed May 11 13:19:43 2022 +0200
Commit: Stephan Bergmann 
CommitDate: Fri May 13 11:28:33 2022 +0200

Use calloc instead of malloc and memset

Change-Id: I591ee7f5360a30779f831f6492cdbe71ab94099e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134165
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann 

diff --git a/sal/rtl/alloc_global.cxx b/sal/rtl/alloc_global.cxx
index 9ceba4f4120f..4b7ec6037dc1 100644
--- a/sal/rtl/alloc_global.cxx
+++ b/sal/rtl/alloc_global.cxx
@@ -53,10 +53,8 @@ void SAL_CALL rtl_freeMemory(void * p) SAL_THROW_EXTERN_C()
 
 void * SAL_CALL rtl_allocateZeroMemory(sal_Size n) SAL_THROW_EXTERN_C()
 {
-void * p = rtl_allocateMemory (n);
-if (p)
-memset (p, 0, n);
-return p;
+SAL_WARN_IF( n >= SAL_MAX_INT32, "sal.rtl", "suspicious massive alloc " << 
n);
+return calloc(n, 1);
 }
 
 void SAL_CALL rtl_freeZeroMemory(void * p, sal_Size n) SAL_THROW_EXTERN_C()


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

2022-05-11 Thread Stephan Bergmann (via logerrit)
 sal/rtl/math.cxx |3 ---
 1 file changed, 3 deletions(-)

New commits:
commit 5b9b07dcb3e0065ddb616a0a5b0e69c1c3841fa7
Author: Stephan Bergmann 
AuthorDate: Wed May 11 11:45:13 2022 +0200
Commit: Stephan Bergmann 
CommitDate: Wed May 11 13:59:35 2022 +0200

Revert "Disable -fsanitize=float-divide-by-zero in rtl_math_atanh"

This reverts commit 6a4504bba84dcbaeb71869ec5c9ed6dfdc090619, which is no 
longer
necessary after 6f75ec6115f0152517be634070607bc61bf96dd0 "tdf#148430 Use 
atanh
from  instead of our own" changed the implementation of 
rtl_math_atanh.

Change-Id: I11094f8c9fb0bb7b635541e7360e460816d3c171
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134161
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index b53cbec144b3..0d70e9718bfd 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -765,9 +765,6 @@ double SAL_CALL rtl_math_log1p(double fValue) 
SAL_THROW_EXTERN_C()
 }
 
 double SAL_CALL rtl_math_atanh(double fValue) SAL_THROW_EXTERN_C()
-#if defined __clang__
-__attribute__((no_sanitize("float-divide-by-zero"))) // atahn(1) -> inf
-#endif
 {
 return ::atanh(fValue);
 }


[Libreoffice-commits] core.git: sal/rtl sc/source

2022-05-11 Thread offtkp (via logerrit)
 sal/rtl/math.cxx |2 +-
 sc/source/core/tool/interpr1.cxx |3 ++-
 sc/source/core/tool/interpr3.cxx |3 ++-
 3 files changed, 5 insertions(+), 3 deletions(-)

New commits:
commit 6f75ec6115f0152517be634070607bc61bf96dd0
Author: offtkp 
AuthorDate: Sat May 7 01:20:33 2022 +0300
Commit: Bartosz Kosiorek 
CommitDate: Wed May 11 10:32:30 2022 +0200

tdf#148430 Use atanh from  instead of our own

Change wrapper in rtl::math::atanh to use atanh
from . Also changed all occurrences of rtl::math::atanh
on files that use this function to directly use the standard
atanh instead.

Change-Id: Idc5c456f67291f5816756f023b61afde844b5ceb
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/133965
Tested-by: Jenkins
Reviewed-by: Bartosz Kosiorek 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index a4775585ace4..b53cbec144b3 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -769,7 +769,7 @@ double SAL_CALL rtl_math_atanh(double fValue) 
SAL_THROW_EXTERN_C()
 __attribute__((no_sanitize("float-divide-by-zero"))) // atahn(1) -> inf
 #endif
 {
-   return 0.5 * rtl_math_log1p(2.0 * fValue / (1.0-fValue));
+return ::atanh(fValue);
 }
 
 /** Parent error function (erf) */
diff --git a/sc/source/core/tool/interpr1.cxx b/sc/source/core/tool/interpr1.cxx
index f6dbbffa2d3f..f71c1941b1a2 100644
--- a/sc/source/core/tool/interpr1.cxx
+++ b/sc/source/core/tool/interpr1.cxx
@@ -75,6 +75,7 @@
 #include 
 #include 
 #include 
+#include 
 
 const sal_uInt64 n2power48 = SAL_CONST_UINT64( 281474976710656); // 2^48
 
@@ -1919,7 +1920,7 @@ void ScInterpreter::ScArcTanHyp()
 if (fabs(fVal) >= 1.0)
 PushIllegalArgument();
 else
-PushDouble( ::rtl::math::atanh( fVal));
+PushDouble(::atanh(fVal));
 }
 
 void ScInterpreter::ScArcCotHyp()
diff --git a/sc/source/core/tool/interpr3.cxx b/sc/source/core/tool/interpr3.cxx
index 25b5f44038f2..e5ae61962014 100644
--- a/sc/source/core/tool/interpr3.cxx
+++ b/sc/source/core/tool/interpr3.cxx
@@ -28,6 +28,7 @@
 #include 
 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -1152,7 +1153,7 @@ void ScInterpreter::ScFisher()
 if (std::abs(fVal) >= 1.0)
 PushIllegalArgument();
 else
-PushDouble( ::rtl::math::atanh( fVal));
+PushDouble(::atanh(fVal));
 }
 
 void ScInterpreter::ScFisherInv()


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

2022-04-10 Thread Mike Kaganski (via logerrit)
 sal/rtl/strimp.cxx  |   30 --
 sal/rtl/strimp.hxx  |4 
 sal/rtl/strtmpl.hxx |   38 ++
 3 files changed, 34 insertions(+), 38 deletions(-)

New commits:
commit 57f22d9b1a4e1cd161a35c8e4c390661db981d2c
Author: Mike Kaganski 
AuthorDate: Sun Apr 10 09:31:16 2022 +0100
Commit: Mike Kaganski 
CommitDate: Sun Apr 10 18:40:52 2022 +0200

Move impl functions to the only place they are used

Change-Id: I6871bfc0ae3d2427a4cd135cb37aad76dac0d7a4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132706
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strimp.cxx b/sal/rtl/strimp.cxx
index 51a1a94bd705..44b56023d0d4 100644
--- a/sal/rtl/strimp.cxx
+++ b/sal/rtl/strimp.cxx
@@ -27,36 +27,6 @@
 #include "alloc_impl.hxx"
 #include "alloc_arena.hxx"
 
-sal_Int16 rtl_ImplGetDigit( sal_Unicode ch, sal_Int16 nRadix )
-{
-sal_Int16 n = -1;
-if ( (ch >= '0') && (ch <= '9') )
-n = ch-'0';
-else if ( (ch >= 'a') && (ch <= 'z') )
-n = ch-'a'+10;
-else if ( (ch >= 'A') && (ch <= 'Z') )
-n = ch-'A'+10;
-return (n < nRadix) ? n : -1;
-}
-
-bool rtl_ImplIsWhitespace( sal_Unicode c )
-{
-/* Space or Control character? */
-if ( (c <= 32) && c )
-return true;
-
-/* Only in the General Punctuation area Space or Control characters are 
included? */
-if ( (c < 0x2000) || (c > 0x206F) )
-return false;
-
-if ( ((c >= 0x2000) && (c <= 0x200B)) ||/* All Spaces   */
- (c == 0x2028) ||   /* LINE SEPARATOR   */
- (c == 0x2029) )/* PARAGRAPH SEPARATOR  */
-return true;
-
-return false;
-}
-
 /*
  * TODO: add a slower, more awful, but more space efficient
  * custom allocator for the pre-init phase. Existing slab
diff --git a/sal/rtl/strimp.hxx b/sal/rtl/strimp.hxx
index f3516f7999bd..69404e0aa390 100644
--- a/sal/rtl/strimp.hxx
+++ b/sal/rtl/strimp.hxx
@@ -46,10 +46,6 @@
 #define SAL_STRING_IS_INTERN(a) ((a)->refCount & SAL_STRING_INTERN_FLAG)
 #define SAL_STRING_IS_STATIC(a) ((a)->refCount & SAL_STRING_STATIC_FLAG)
 
-sal_Int16 rtl_ImplGetDigit( sal_Unicode ch, sal_Int16 nRadix );
-
-bool rtl_ImplIsWhitespace( sal_Unicode c );
-
 rtl_uString* rtl_uString_ImplAlloc( sal_Int32 nLen );
 
 rtl_String* rtl_string_ImplAlloc( sal_Int32 nLen );
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 341c6717f39e..e1389ce6ad1b 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -139,6 +139,36 @@ inline void Copy(sal_Unicode* _pDest, const char* _pSrc, 
sal_Int32 _nCount)
});
 }
 
+inline sal_Int16 implGetDigit(sal_Unicode ch, sal_Int16 nRadix)
+{
+sal_Int16 n = -1;
+if ((ch >= '0') && (ch <= '9'))
+n = ch - '0';
+else if ((ch >= 'a') && (ch <= 'z'))
+n = ch - 'a' + 10;
+else if ((ch >= 'A') && (ch <= 'Z'))
+n = ch - 'A' + 10;
+return (n < nRadix) ? n : -1;
+}
+
+inline bool implIsWhitespace(sal_Unicode c)
+{
+/* Space or Control character? */
+if ((c <= 32) && c)
+return true;
+
+/* Only in the General Punctuation area Space or Control characters are 
included? */
+if ((c < 0x2000) || (c > 0x206F))
+return false;
+
+if (((c >= 0x2000) && (c <= 0x200B)) ||/* All Spaces   */
+(c == 0x2028) ||   /* LINE SEPARATOR   */
+(c == 0x2029)) /* PARAGRAPH SEPARATOR  */
+return true;
+
+return false;
+}
+
 /* === */
 /* C-String functions which could be used without the String-Class */
 /* === */
@@ -549,10 +579,10 @@ std::basic_string_view trimView( 
IMPL_RTL_STRCODE* pStr, sal_I
 sal_Int32 nPostSpaces   = 0;
 sal_Int32 nIndex= nLen-1;
 
-while ( (nPreSpaces < nLen) && rtl_ImplIsWhitespace( 
IMPL_RTL_USTRCODE(*(pStr+nPreSpaces)) ) )
+while ( (nPreSpaces < nLen) && implIsWhitespace( 
IMPL_RTL_USTRCODE(*(pStr+nPreSpaces)) ) )
 nPreSpaces++;
 
-while ( (nIndex > nPreSpaces) && rtl_ImplIsWhitespace( 
IMPL_RTL_USTRCODE(*(pStr+nIndex)) ) )
+while ( (nIndex > nPreSpaces) && implIsWhitespace( 
IMPL_RTL_USTRCODE(*(pStr+nIndex)) ) )
 {
 nPostSpaces++;
 nIndex--;
@@ -751,7 +781,7 @@ template  T toInt(S str, sal_Int16 
nRadix)
 const auto end = str.end();
 
 /* Skip whitespaces */
-while (pStr != end && rtl_ImplIsWhitespace(IMPL_RTL_USTRCODE(*pStr)))
+while (pStr != end && implIsWhitespace(IMPL_RTL_USTRCODE(*pStr)))
 pStr++;
 if (pStr == end)
 return 0;
@@ -763,7 +793,7 @@ template  T toInt(S str, sal_Int16 
nRadix)
 std::make_unsigned_t n = 0;
 while (pStr != end)
 {
-sal_Int16 nDigit = rtl_ImplGetDigit(IMPL_RTL_USTRCODE(*pStr), 

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

2022-04-01 Thread Stephan Bergmann (via logerrit)
 sal/rtl/bootstrap.cxx |   38 --
 1 file changed, 20 insertions(+), 18 deletions(-)

New commits:
commit 8ffcc3832ed4685af7211c9c3398c854fcf411ed
Author: Stephan Bergmann 
AuthorDate: Fri Apr 1 12:00:40 2022 +0200
Commit: Stephan Bergmann 
CommitDate: Fri Apr 1 16:03:27 2022 +0200

Use some more string_view

Change-Id: I630c573eedfcc9d8678fda0466ddcc1902fac0f0
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132417
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann 

diff --git a/sal/rtl/bootstrap.cxx b/sal/rtl/bootstrap.cxx
index f2bb6d58a962..88d4bbd59b36 100644
--- a/sal/rtl/bootstrap.cxx
+++ b/sal/rtl/bootstrap.cxx
@@ -40,6 +40,8 @@
 
 #include 
 #include 
+#include 
+#include 
 #include 
 
 #ifdef ANDROID
@@ -92,11 +94,11 @@ struct ExpandRequestLink {
 };
 
 OUString expandMacros(
-Bootstrap_Impl const * file, OUString const & text, LookupMode mode,
+Bootstrap_Impl const * file, std::u16string_view text, LookupMode mode,
 ExpandRequestLink const * requestStack);
 
 OUString recursivelyExpandMacros(
-Bootstrap_Impl const * file, OUString const & text, LookupMode mode,
+Bootstrap_Impl const * file, std::u16string_view text, LookupMode mode,
 Bootstrap_Impl const * requestFile, OUString const & requestKey,
 ExpandRequestLink const * requestStack)
 {
@@ -806,14 +808,14 @@ int hex(sal_Unicode c)
 c >= 'a' && c <= 'f' ? c - 'a' + 10 : -1;
 }
 
-sal_Unicode read(OUString const & text, sal_Int32 * pos, bool * escaped)
+sal_Unicode read(std::u16string_view text, std::size_t * pos, bool * escaped)
 {
-OSL_ASSERT(pos && *pos >= 0 && *pos < text.getLength() && escaped);
+OSL_ASSERT(pos && *pos < text.length() && escaped);
 sal_Unicode c = text[(*pos)++];
 if (c == '\\')
 {
 int n1, n2, n3, n4;
-if (*pos < text.getLength() - 4 && text[*pos] == 'u' &&
+if (*pos < text.length() - 4 && text[*pos] == 'u' &&
 ((n1 = hex(text[*pos + 1])) >= 0) &&
 ((n2 = hex(text[*pos + 2])) >= 0) &&
 ((n3 = hex(text[*pos + 3])) >= 0) &&
@@ -825,7 +827,7 @@ sal_Unicode read(OUString const & text, sal_Int32 * pos, 
bool * escaped)
 (n1 << 12) | (n2 << 8) | (n3 << 4) | n4);
 }
 
-if (*pos < text.getLength())
+if (*pos < text.length())
 {
 *escaped = true;
 return text[(*pos)++];
@@ -847,13 +849,13 @@ OUString lookup(
 }
 
 OUString expandMacros(
-Bootstrap_Impl const * file, OUString const & text, LookupMode mode,
+Bootstrap_Impl const * file, std::u16string_view text, LookupMode mode,
 ExpandRequestLink const * requestStack)
 {
-SAL_INFO("sal.bootstrap", "expandMacros called with: " << text);
+SAL_INFO("sal.bootstrap", "expandMacros called with: " << OUString(text));
 OUStringBuffer buf(2048);
 
-for (sal_Int32 i = 0; i < text.getLength();)
+for (std::size_t i = 0; i < text.length();)
 {
 bool escaped;
 sal_Unicode c = read(text, , );
@@ -863,17 +865,17 @@ OUString expandMacros(
 }
 else
 {
-if (i < text.getLength() && text[i] == '{')
+if (i < text.length() && text[i] == '{')
 {
 ++i;
-sal_Int32 p = i;
+std::size_t p = i;
 sal_Int32 nesting = 0;
 OUString seg[3];
 int n = 0;
 
-while (i < text.getLength())
+while (i < text.length())
 {
-sal_Int32 j = i;
+std::size_t j = i;
 c = read(text, , );
 
 if (!escaped)
@@ -886,7 +888,7 @@ OUString expandMacros(
 case '}':
 if (nesting == 0)
 {
-seg[n++] = text.copy(p, j - p);
+seg[n++] = text.substr(p, j - p);
 goto done;
 }
 else
@@ -897,7 +899,7 @@ OUString expandMacros(
 case ':':
 if (nesting == 0 && n < 2)
 {
-seg[n++] = text.copy(p, j - p);
+seg[n++] = text.substr(p, j - p);
 p = i;
 }
 break;
@@ -953,10 +955,10 @@ OUString expandMacros(
 }
 else
 {
-OUStringBuffer kbuf(text.getLength());
-for (; i < text.getLength();)
+OUStringBuffer kbuf(sal_Int32(text.length()));
+for (; i < text.length();)
 {
-sal_Int32 j = i;
+std::size_t j = i;
   

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

2022-03-22 Thread Deep17 (via logerrit)
 sal/rtl/digest.cxx |4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

New commits:
commit d84ea3c8744aa5a125cec56574c1b943e768b70a
Author: Deep17 
AuthorDate: Mon Mar 21 14:53:34 2022 -0400
Commit: Stephan Bergmann 
CommitDate: Tue Mar 22 08:37:17 2022 +0100

tdf#145614 Convert #define to enum or constexpr

Change-Id: I3090ae3421ae55b7c546d82aeb49a56e93c9f02c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/131913
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann 

diff --git a/sal/rtl/digest.cxx b/sal/rtl/digest.cxx
index d01e82cfcf38..de52ecae5107 100644
--- a/sal/rtl/digest.cxx
+++ b/sal/rtl/digest.cxx
@@ -176,8 +176,8 @@ void SAL_CALL rtl_digest_destroy(rtlDigest Digest) 
SAL_THROW_EXTERN_C()
 pImpl->m_delete(Digest);
 }
 
-constexpr sal_uInt8 DIGEST_CBLOCK_MD2 = 16;
-constexpr sal_uInt8 DIGEST_LBLOCK_MD2 = 16;
+constexpr auto DIGEST_CBLOCK_MD2 = 16;
+constexpr auto DIGEST_LBLOCK_MD2 = 16;
 
 namespace {
 


[Libreoffice-commits] core.git: sal/rtl sw/qa

2022-03-21 Thread Andrea Gelmini (via logerrit)
 sal/rtl/strtmpl.hxx   |1 -
 sw/qa/extras/rtfexport/rtfexport5.cxx |1 -
 2 files changed, 2 deletions(-)

New commits:
commit 5ca3de20e9567810964af008f606e5af1e3221a3
Author: Andrea Gelmini 
AuthorDate: Mon Mar 21 19:06:44 2022 +0100
Commit: Julien Nabet 
CommitDate: Mon Mar 21 21:49:45 2022 +0100

Removed duplicated includes

Change-Id: I22b71580a79500e169ed9cab775501583975d67f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/131911
Tested-by: Jenkins
Reviewed-by: Julien Nabet 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index a3c2b44898ca..341c6717f39e 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -26,7 +26,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
diff --git a/sw/qa/extras/rtfexport/rtfexport5.cxx 
b/sw/qa/extras/rtfexport/rtfexport5.cxx
index c59448af2514..7a7d4b8b2216 100644
--- a/sw/qa/extras/rtfexport/rtfexport5.cxx
+++ b/sw/qa/extras/rtfexport/rtfexport5.cxx
@@ -29,7 +29,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 


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

2022-03-21 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx|  468 
 sal/rtl/string.cxx  |4 
 sal/rtl/strtmpl.hxx |  403 ++--
 sal/rtl/ustring.cxx |4 
 4 files changed, 387 insertions(+), 492 deletions(-)

New commits:
commit bbb7ad2db9bb8525a619c3a329299da31c24bc1b
Author: Mike Kaganski 
AuthorDate: Mon Mar 21 12:40:59 2022 +0300
Commit: Mike Kaganski 
CommitDate: Mon Mar 21 15:12:17 2022 +0100

Move implementation of doubleToString to sal/rtl/strtmpl.hxx

... to reduce inter-relations between compilation units, and drop
*StringTraits from sal/rtl/math.cxx.

Change-Id: I0d7544dead03651dc71ec923cab10580f15cf49a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/131895
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index b9a0a3348757..a4775585ace4 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -35,7 +35,8 @@
 #include 
 #include 
 
-#include 
+#include "strtmpl.hxx"
+
 #include 
 
 constexpr int minExp = -323, maxExp = 308;
@@ -106,80 +107,6 @@ static double getN10Exp(int nExp)
 
 namespace {
 
-struct StringTraits
-{
-typedef char Char;
-
-typedef rtl_String String;
-
-static void createString(rtl_String ** pString,
-char const * pChars, sal_Int32 nLen)
-{
-rtl_string_newFromStr_WithLength(pString, pChars, nLen);
-}
-
-static void createBuffer(rtl_String ** pBuffer,
-const sal_Int32 * pCapacity)
-{
-rtl_string_new_WithLength(pBuffer, *pCapacity);
-}
-
-static void appendChars(rtl_String ** pBuffer, sal_Int32 * pCapacity,
-   sal_Int32 * pOffset, char const * pChars,
-   sal_Int32 nLen)
-{
-assert(pChars);
-rtl_stringbuffer_insert(pBuffer, pCapacity, *pOffset, pChars, nLen);
-*pOffset += nLen;
-}
-
-static void appendAscii(rtl_String ** pBuffer, sal_Int32 * pCapacity,
-   sal_Int32 * pOffset, char const * pStr,
-   sal_Int32 nLen)
-{
-assert(pStr);
-rtl_stringbuffer_insert(pBuffer, pCapacity, *pOffset, pStr, nLen);
-*pOffset += nLen;
-}
-};
-
-struct UStringTraits
-{
-typedef sal_Unicode Char;
-
-typedef rtl_uString String;
-
-static void createString(rtl_uString ** pString,
-sal_Unicode const * pChars, sal_Int32 nLen)
-{
-rtl_uString_newFromStr_WithLength(pString, pChars, nLen);
-}
-
-static void createBuffer(rtl_uString ** pBuffer,
-const sal_Int32 * pCapacity)
-{
-rtl_uString_new_WithLength(pBuffer, *pCapacity);
-}
-
-static void appendChars(rtl_uString ** pBuffer,
-   sal_Int32 * pCapacity, sal_Int32 * pOffset,
-   sal_Unicode const * pChars, sal_Int32 nLen)
-{
-assert(pChars);
-rtl_uStringbuffer_insert(pBuffer, pCapacity, *pOffset, pChars, nLen);
-*pOffset += nLen;
-}
-
-static void appendAscii(rtl_uString ** pBuffer,
-   sal_Int32 * pCapacity, sal_Int32 * pOffset,
-   char const * pStr, sal_Int32 nLen)
-{
-rtl_uStringbuffer_insert_ascii(pBuffer, pCapacity, *pOffset, pStr,
-   nLen);
-*pOffset += nLen;
-}
-};
-
 /** If value (passed as absolute value) is an integer representable as double,
 which we handle explicitly at some places.
  */
@@ -242,393 +169,6 @@ int getBitsInFracPart(double fAbsValue)
 return std::max(nBitsInFracPart, 0);
 }
 
-constexpr sal_uInt64 eX[] = { 10ull,
-  100ull,
-  1000ull,
-  1ull,
-  10ull,
-  100ull,
-  1000ull,
-  1ull,
-  10ull,
-  100ull,
-  1000ull,
-  1ull,
-  10ull,
-  100ull,
-  1000ull,
-  1ull,
-  10ull,
-  100ull,
-  1000ull };
-
-int decimalDigits(sal_uInt64 n)
-{
-return std::distance(std::begin(eX), std::upper_bound(std::begin(eX), 
std::end(eX), n)) + 1;
-}
-
-sal_uInt64 roundToPow10(sal_uInt64 n, int e)
-{
-assert(e > 0 && o3tl::make_unsigned(e) <= std::size(eX));
-

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

2022-03-21 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |  231 ++--
 sal/rtl/ustring.cxx |   25 -
 2 files changed, 117 insertions(+), 139 deletions(-)

New commits:
commit c93d27c9d25e06a884c971fc8dbbea8fe7599578
Author: Mike Kaganski 
AuthorDate: Mon Mar 21 09:54:41 2022 +0300
Commit: Mike Kaganski 
CommitDate: Mon Mar 21 08:46:41 2022 +0100

Reorganize the code a bit to drop some detail::* helper functions

Change-Id: Iee7402f5de7287678e1691b48b6493d3131ef7ee
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/131883
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 9fa05af4526d..f90df2884883 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -34,9 +34,8 @@
 #include 
 #include 
 #include 
-#include 
-#include 
-#include 
+#include 
+#include 
 
 void internRelease(rtl_uString*);
 
@@ -454,6 +453,35 @@ sal_Int32 indexOfStr_WithLength
 ( const IMPL_RTL_STR
 return idx == my_string_view::npos ? -1 : idx;
 }
 
+inline sal_Int32 indexOfStr_WithLength(const sal_Unicode* pStr, sal_Int32 
nStrLen,
+   const char* pSubStr, sal_Int32 nSubLen)
+{
+assert(nStrLen >= 0);
+assert(nSubLen >= 0);
+if (nSubLen > 0 && nSubLen <= nStrLen)
+{
+sal_Unicode const* end = pStr + nStrLen;
+sal_Unicode const* cursor = pStr;
+
+while (cursor < end)
+{
+cursor = std::char_traits::find(cursor, end - cursor, 
*pSubStr);
+if (!cursor || (end - cursor < nSubLen))
+{
+/* no enough left to actually have a match */
+break;
+}
+/* now it is worth trying a full match */
+if (nSubLen == 1 || 
rtl_ustr_asciil_reverseEquals_WithLength(cursor, pSubStr, nSubLen))
+{
+return cursor - pStr;
+}
+cursor += 1;
+}
+}
+return -1;
+}
+
 /* --- */
 
 template 
@@ -1173,21 +1201,6 @@ void newReplaceStrAt( 
IMPL_RTL_STRINGDATA** ppTh
 
 /* --- */
 
-template 
-void newReplaceAllFromIndex(S**, S*, CharTypeFrom const*, sal_Int32, 
CharTypeTo const*, sal_Int32,
-sal_Int32);
-
-template 
-void newReplace( IMPL_RTL_STRINGDATA** ppThis,
- IMPL_RTL_STRINGDATA* pStr,
- STRCODE 
cOld,
- STRCODE 
cNew )
-{
-return newReplaceAllFromIndex(ppThis, pStr, , 1, , 1, 0);
-}
-
-/* --- */
-
 template 
 void newReplaceChars(IMPL_RTL_STRINGDATA** ppThis, IMPL_RTL_STRINGDATA* pStr)
 {
@@ -1289,103 +1302,6 @@ sal_Int32 getToken( 
IMPL_RTL_STRINGDATA** ppThis
 return -1;
 }
 
-namespace detail
-{
-template 
-sal_Int32 indexOf(const CharType1* s, sal_Int32 len, const CharType2* subStr, 
sal_Int32 subLen)
-{
-if constexpr (std::is_same_v)
-return indexOfStr_WithLength(s, len, subStr, subLen);
-else if constexpr (std::is_same_v && 
std::is_same_v)
-return rtl_ustr_indexOfAscii_WithLength(s, len, subStr, subLen);
-}
-
-template 
-void append(S** s, sal_Int32* capacity, const CharType1* s1, sal_Int32 len)
-{
-if constexpr (std::is_same_v && std::is_same_v)
-return rtl_uStringbuffer_insert(s, capacity, (*s)->length, s1, len);
-else if constexpr (std::is_same_v && 
std::is_same_v)
-return rtl_uStringbuffer_insert_ascii(s, capacity, (*s)->length, s1, 
len);
-else if constexpr (std::is_same_v && 
std::is_same_v)
-return rtl_stringbuffer_insert(s, capacity, (*s)->length, s1, len);
-}
-}
-
-template 
-void newReplaceAllFromIndex(S** s, S* s1, CharTypeFrom const* from, sal_Int32 
fromLength,
-CharTypeTo const* to, sal_Int32 toLength, 
sal_Int32 fromIndex)
-{
-assert(s != nullptr);
-assert(s1 != nullptr);
-assert(fromLength >= 0);
-assert(from != nullptr || fromLength == 0);
-assert(toLength >= 0);
-assert(to != nullptr || toLength == 0);
-assert(fromIndex >= 0 && fromIndex <= s1->length);
-sal_Int32 i = detail::indexOf(s1->buffer + fromIndex, s1->length - 
fromIndex, from, fromLength);
-if (i >= 0)
-{
-if (s1->length - fromLength > SAL_MAX_INT32 - toLength)
-std::abort();
-i += fromIndex;
-sal_Int32 nCapacity = s1->length + (toLength - fromLength);
-if (fromLength < toLength)
-{
-// Pre-allocate up to 16 replacements more
-const sal_Int32 nMaxMoreFinds = (s1->length - i - fromLength) / 
fromLength;
-const 

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

2022-03-10 Thread Stephan Bergmann (via logerrit)
 sal/rtl/strtmpl.hxx |7 ---
 1 file changed, 7 deletions(-)

New commits:
commit 52bd453eceea7dfb425e5e58b93244214dad29c2
Author: Stephan Bergmann 
AuthorDate: Fri Mar 11 08:07:05 2022 +0100
Commit: Stephan Bergmann 
CommitDate: Fri Mar 11 08:07:05 2022 +0100

Remove redundant check

The precondition `nIndex >= 0` is already asserted at the top of the 
function.

Change-Id: I7436ce923b19aa81f08d1fb20680308f609d2242

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 07ea6d6cbba8..9fa05af4526d 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -1145,13 +1145,6 @@ void newReplaceStrAt( 
IMPL_RTL_STRINGDATA** ppTh
 return;
 }
 
-/* negative index? */
-if (nIndex < 0)
-{
-nCount -= nIndex;
-nIndex = 0;
-}
-
 /* not more than the String length could be deleted */
 if (nCount >= pStr->length-nIndex)
 {


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

2022-03-05 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |9 +
 1 file changed, 9 insertions(+)

New commits:
commit 713c83c0fc4a0d9950cfa0b598d7c5f4bae15755
Author: Mike Kaganski 
AuthorDate: Sat Mar 5 19:26:38 2022 +0300
Commit: Mike Kaganski 
CommitDate: Sat Mar 5 18:17:55 2022 +0100

Add checks to avoid finding empty substring / zero character

... which changed in commit 281989007fd7dea997ed9a65f513f80b1aff67dd

  Author Noel Grandin 
  Date   Tue Jul 01 13:17:01 2014 +0200

Use standard library optimised routines for OUString/OString

for optimized cases: strchr/wcschr/strrchr/wcsrchr find trailing zero
character, and strstr/wcsstr find empty string; previous/unoptimized
code does not find these. This introduced inconsistency between char
and sal_Unicode functions on non-Windows, and for sal_Unicode between
Windows and non-Windows (because on Windows, optimized code is used
for sal_Unicode, while on other platforms, unoptimized code is used).

Change-Id: I68529c91b26f4113d9bdfc5ac4809349864b
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/131064
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 6fa400c0164f..37f274a69cf2 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -307,6 +307,9 @@ sal_Int32 indexOfChar ( const 
IMPL_RTL_STRCODE* pStr
 IMPL_RTL_STRCODE c )
 {
 assert(pStr);
+if (!c)
+return -1; // Unifies behavior of strchr/wcschr and unoptimized 
algorithm wrt '\0'
+
 if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char))
 {
 // take advantage of builtin optimisations
@@ -361,6 +364,9 @@ sal_Int32 lastIndexOfChar ( 
const IMPL_RTL_STRCODE*
 IMPL_RTL_STRCODE c )
 {
 assert(pStr);
+if (!c)
+return -1; // Unifies behavior of strrchr/wcsrchr and 
lastIndexOfChar_WithLength wrt '\0'
+
 if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char))
 {
 // take advantage of builtin optimisations
@@ -406,6 +412,9 @@ sal_Int32 indexOfStr ( const 
IMPL_RTL_STRCODE* pStr,
 {
 assert(pStr);
 assert(pSubStr);
+/* an empty SubString is always not findable */
+if (*pSubStr == 0)
+return -1;
 if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char))
 {
 // take advantage of builtin optimisations


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

2022-03-03 Thread Mike Kaganski (via logerrit)
 sal/rtl/string.cxx  |   12 ++--
 sal/rtl/strtmpl.hxx |  148 
 sal/rtl/ustring.cxx |   12 ++--
 3 files changed, 47 insertions(+), 125 deletions(-)

New commits:
commit 09a06141c516991865627f78e39593be856f6457
Author: Mike Kaganski 
AuthorDate: Thu Mar 3 08:19:30 2022 +0300
Commit: Mike Kaganski 
CommitDate: Thu Mar 3 17:36:42 2022 +0100

Deduplicate some char replacement functions

Change-Id: Ie65224066941dbab89abb6fc91e6cbfbc45bf201
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130901
Reviewed-by: Noel Grandin 
Reviewed-by: Mike Kaganski 
Tested-by: Jenkins

diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index 9927451c8f9e..7112017826f1 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -441,33 +441,33 @@ sal_Int32 SAL_CALL 
rtl_str_lastIndexOfStr_WithLength(const char* pStr, sal_Int32
 
 void SAL_CALL rtl_str_replaceChar(char* pStr, char cOld, char cNew) 
SAL_THROW_EXTERN_C()
 {
-return rtl::str::replaceChar(pStr, cOld, cNew);
+return rtl::str::replaceChars(rtl::str::null_terminated(pStr), 
rtl::str::FromTo(cOld, cNew));
 }
 
 void SAL_CALL rtl_str_replaceChar_WithLength(char* pStr, sal_Int32 nLen, char 
cOld, char cNew)
 SAL_THROW_EXTERN_C()
 {
-return rtl::str::replaceChar_WithLength(pStr, nLen, cOld, cNew);
+return rtl::str::replaceChars(rtl::str::with_length(pStr, nLen), 
rtl::str::FromTo(cOld, cNew));
 }
 
 void SAL_CALL rtl_str_toAsciiLowerCase(char* pStr) SAL_THROW_EXTERN_C()
 {
-return rtl::str::toAsciiLowerCase(pStr);
+return rtl::str::replaceChars(rtl::str::null_terminated(pStr), 
rtl::str::toAsciiLower);
 }
 
 void SAL_CALL rtl_str_toAsciiLowerCase_WithLength(char* pStr, sal_Int32 nLen) 
SAL_THROW_EXTERN_C()
 {
-return rtl::str::toAsciiLowerCase_WithLength(pStr, nLen);
+return rtl::str::replaceChars(rtl::str::with_length(pStr, nLen), 
rtl::str::toAsciiLower);
 }
 
 void SAL_CALL rtl_str_toAsciiUpperCase(char* pStr) SAL_THROW_EXTERN_C()
 {
-return rtl::str::toAsciiUpperCase(pStr);
+return rtl::str::replaceChars(rtl::str::null_terminated(pStr), 
rtl::str::toAsciiUpper);
 }
 
 void SAL_CALL rtl_str_toAsciiUpperCase_WithLength(char* pStr, sal_Int32 nLen) 
SAL_THROW_EXTERN_C()
 {
-return rtl::str::toAsciiUpperCase_WithLength(pStr, nLen);
+return rtl::str::replaceChars(rtl::str::with_length(pStr, nLen), 
rtl::str::toAsciiUpper);
 }
 
 sal_Int32 SAL_CALL rtl_str_trim(char* pStr) SAL_THROW_EXTERN_C() { return 
rtl::str::trim(pStr); }
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 7234afd39ec1..6fa400c0164f 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -80,6 +80,38 @@ template  struct with_length
 auto end() const { return p + len; }
 };
 
+struct ToAsciiLower
+{
+template  static bool Applicable(C c)
+{
+return rtl::isAsciiUpperCase(IMPL_RTL_USTRCODE(c));
+}
+template  static C Replace(C c)
+{
+return rtl::toAsciiLowerCase(IMPL_RTL_USTRCODE(c));
+}
+} constexpr toAsciiLower;
+
+struct ToAsciiUpper
+{
+template  static bool Applicable(C c)
+{
+return rtl::isAsciiLowerCase(IMPL_RTL_USTRCODE(c));
+}
+template  static C Replace(C c)
+{
+return rtl::toAsciiUpperCase(IMPL_RTL_USTRCODE(c));
+}
+} constexpr toAsciiUpper;
+
+template  struct FromTo
+{
+C from;
+C to;
+FromTo(C cFrom, C cTo) : from(cFrom), to(cTo) {}
+C Replace(C c) const { return c == from ? to : c; }
+};
+
 template  void Copy(C* _pDest, const C* _pSrc, sal_Int32 _nCount)
 {
 // take advantage of builtin optimisations
@@ -450,96 +482,10 @@ sal_Int32 lastIndexOfStr_WithLength   
  ( const IMPL_RTL
 
 /* --- */
 
-template 
-void replaceChar ( IMPL_RTL_STRCODE* pStr,
-   IMPL_RTL_STRCODE cOld,
-   IMPL_RTL_STRCODE cNew )
-{
-assert(pStr);
-while ( *pStr )
-{
-if ( *pStr == cOld )
-*pStr = cNew;
-
-pStr++;
-}
-}
-
-/* --- */
-
-template 
-void replaceChar_WithLength ( IMPL_RTL_STRCODE* 
pStr,
-  sal_Int32 nLen,
-  IMPL_RTL_STRCODE 
cOld,
-  IMPL_RTL_STRCODE 
cNew )
-{
-assert(nLen >= 0);
-while ( nLen > 0 )
-{
-if ( *pStr == cOld )
-*pStr = cNew;
-
-pStr++;
-nLen--;
-}
-}
-
-/* --- */
-
-template  void toAsciiLowerCase( IMPL_RTL_STRCODE* 
pStr )
-{
-assert(pStr);
-while ( *pStr )
-{
-*pStr = 

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

2022-03-03 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |   40 +++-
 1 file changed, 23 insertions(+), 17 deletions(-)

New commits:
commit 04af8ac89ecdc98361f52b792ff0ce1c3ccb4517
Author: Mike Kaganski 
AuthorDate: Thu Mar 3 10:18:20 2022 +0300
Commit: Mike Kaganski 
CommitDate: Thu Mar 3 12:49:11 2022 +0100

Use a more conventional begin() / end() syntax

Also would potentially enable use in std range algorithms
that take different end sentinel type.

Change-Id: Ia75005caa7ce44302fc8612f5717a41d11087622
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130902
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 945aaa54a6e2..7234afd39ec1 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -30,7 +30,6 @@
 
 #include "strimp.hxx"
 
-#include 
 #include 
 #include 
 #include 
@@ -56,8 +55,15 @@ template  struct null_terminated
 {
 assert(pStr);
 }
-auto getIter() const { return p; }
-static auto getEndDetector() { return [](C* iter) { return *iter == 0; }; }
+auto begin() const { return p; }
+struct EndDetector
+{
+friend bool operator==(EndDetector, C* iter) { return *iter == 0; }
+friend bool operator==(C* iter, EndDetector) { return *iter == 0; }
+friend bool operator!=(EndDetector, C* iter) { return *iter != 0; }
+friend bool operator!=(C* iter, EndDetector) { return *iter != 0; }
+};
+static auto end() { return EndDetector{}; }
 };
 
 template  struct with_length
@@ -70,8 +76,8 @@ template  struct with_length
 {
 assert(len >= 0);
 }
-auto getIter() const { return p; }
-auto getEndDetector() const { return [pEnd = p + len](C* iter) { return 
iter == pEnd; }; }
+auto begin() const { return p; }
+auto end() const { return p + len; }
 };
 
 template  void Copy(C* _pDest, const C* _pSrc, sal_Int32 _nCount)
@@ -166,17 +172,17 @@ sal_Int32 compare(S1 s1, S2 s2, Compare, Shorten_t 
shortenedLength)
 {
 static_assert(std::is_same_v || 
std::is_same_v);
 assert(shortenedLength >= 0);
-auto pStr1 = s1.getIter();
-const auto atEnd1 = s1.getEndDetector();
-auto pStr2 = s2.getIter();
-const auto atEnd2 = s2.getEndDetector();
+auto pStr1 = s1.begin();
+const auto end1 = s1.end();
+auto pStr2 = s2.begin();
+const auto end2 = s2.end();
 for (;;)
 {
 if (shortenedLength == 0)
 return 0;
-if (atEnd2(pStr2))
-return atEnd1(pStr1) ? 0 : 1;
-if (atEnd1(pStr1))
+if (pStr2 == end2)
+return pStr1 == end1 ? 0 : 1;
+if (pStr1 == end1)
 return -1;
 if (const sal_Int32 nRet = Compare::compare(*pStr1, *pStr2))
 return nRet;
@@ -753,13 +759,13 @@ template  T toInt(S str, sal_Int16 
nRadix)
 if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) )
 nRadix = 10;
 
-auto pStr = str.getIter();
-const auto atEnd = str.getEndDetector();
+auto pStr = str.begin();
+const auto end = str.end();
 
 /* Skip whitespaces */
-while (!atEnd(pStr) && rtl_ImplIsWhitespace(IMPL_RTL_USTRCODE(*pStr)))
+while (pStr != end && rtl_ImplIsWhitespace(IMPL_RTL_USTRCODE(*pStr)))
 pStr++;
-if (atEnd(pStr))
+if (pStr == end)
 return 0;
 
 const bool bNeg = HandleSignChar(pStr);
@@ -767,7 +773,7 @@ template  T toInt(S str, sal_Int16 
nRadix)
 assert(nDiv > 0);
 
 std::make_unsigned_t n = 0;
-while (!atEnd(pStr))
+while (pStr != end)
 {
 sal_Int16 nDigit = rtl_ImplGetDigit(IMPL_RTL_USTRCODE(*pStr), nRadix);
 if ( nDigit < 0 )


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

2022-03-02 Thread Mike Kaganski (via logerrit)
 sal/rtl/string.cxx  |   41 +--
 sal/rtl/strtmpl.hxx |  185 +++-
 sal/rtl/ustring.cxx |   70 +++
 3 files changed, 131 insertions(+), 165 deletions(-)

New commits:
commit 3cb30369d699a4836043d434833ba4c3d3d257c3
Author: Mike Kaganski 
AuthorDate: Wed Mar 2 13:11:18 2022 +0300
Commit: Mike Kaganski 
CommitDate: Wed Mar 2 22:11:23 2022 +0100

Unify normal/shortened, null-terminated/with-length comparisons

Change-Id: Ie154efd1e0d9b49601200ac896d5d5dd0422d504
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130832
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index c5d535075354..9927451c8f9e 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -325,15 +325,17 @@ sal_Int32 SAL_CALL rtl_str_getLength(const char* pStr) 
SAL_THROW_EXTERN_C()
 
 sal_Int32 SAL_CALL rtl_str_compare(const char* pStr1, const char* pStr2) 
SAL_THROW_EXTERN_C()
 {
-return rtl::str::compare(pStr1, pStr2, rtl::str::CompareNormal());
+return rtl::str::compare(rtl::str::null_terminated(pStr1), 
rtl::str::null_terminated(pStr2),
+ rtl::str::CompareNormal(), 
rtl::str::noShortening);
 }
 
 sal_Int32 SAL_CALL rtl_str_compare_WithLength(const char* pStr1, sal_Int32 
nStr1Len,
   const char* pStr2, sal_Int32 
nStr2Len)
 SAL_THROW_EXTERN_C()
 {
-return rtl::str::compare_WithLengths(pStr1, nStr1Len, pStr2, nStr2Len,
- rtl::str::CompareNormal());
+return rtl::str::compare(rtl::str::with_length(pStr1, nStr1Len),
+ rtl::str::with_length(pStr2, nStr2Len),
+ rtl::str::CompareNormal(), 
rtl::str::noShortening);
 }
 
 sal_Int32 SAL_CALL rtl_str_shortenedCompare_WithLength(const char* pStr1, 
sal_Int32 nStr1Len,
@@ -341,8 +343,9 @@ sal_Int32 SAL_CALL 
rtl_str_shortenedCompare_WithLength(const char* pStr1, sal_In
sal_Int32 
nShortenedLength)
 SAL_THROW_EXTERN_C()
 {
-return rtl::str::shortenedCompare_WithLengths(pStr1, nStr1Len, pStr2, 
nStr2Len,
-  nShortenedLength, 
rtl::str::CompareNormal());
+return rtl::str::compare(rtl::str::with_length(pStr1, nStr1Len),
+ rtl::str::with_length(pStr2, nStr2Len),
+ rtl::str::CompareNormal(), nShortenedLength);
 }
 
 sal_Int32 SAL_CALL rtl_str_reverseCompare_WithLength(const char* pStr1, 
sal_Int32 nStr1Len,
@@ -356,23 +359,26 @@ sal_Int32 SAL_CALL 
rtl_str_reverseCompare_WithLength(const char* pStr1, sal_Int3
 sal_Int32 SAL_CALL rtl_str_compareIgnoreAsciiCase(const char* pStr1, const 
char* pStr2)
 SAL_THROW_EXTERN_C()
 {
-return rtl::str::compare(pStr1, pStr2, rtl::str::CompareIgnoreAsciiCase());
+return rtl::str::compare(rtl::str::null_terminated(pStr1), 
rtl::str::null_terminated(pStr2),
+ rtl::str::CompareIgnoreAsciiCase(), 
rtl::str::noShortening);
 }
 
 sal_Int32 SAL_CALL rtl_str_compareIgnoreAsciiCase_WithLength(const char* 
pStr1, sal_Int32 nStr1Len,
  const char* 
pStr2, sal_Int32 nStr2Len)
 SAL_THROW_EXTERN_C()
 {
-return rtl::str::compare_WithLengths(pStr1, nStr1Len, pStr2, nStr2Len,
- rtl::str::CompareIgnoreAsciiCase());
+return rtl::str::compare(rtl::str::with_length(pStr1, nStr1Len),
+ rtl::str::with_length(pStr2, nStr2Len),
+ rtl::str::CompareIgnoreAsciiCase(), 
rtl::str::noShortening);
 }
 
 sal_Int32 SAL_CALL rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
 const char* pStr1, sal_Int32 nStr1Len, const char* pStr2, sal_Int32 
nStr2Len,
 sal_Int32 nShortenedLength) SAL_THROW_EXTERN_C()
 {
-return rtl::str::shortenedCompare_WithLengths(
-pStr1, nStr1Len, pStr2, nStr2Len, nShortenedLength, 
rtl::str::CompareIgnoreAsciiCase());
+return rtl::str::compare(rtl::str::with_length(pStr1, nStr1Len),
+ rtl::str::with_length(pStr2, nStr2Len),
+ rtl::str::CompareIgnoreAsciiCase(), 
nShortenedLength);
 }
 
 sal_Int32 SAL_CALL rtl_str_hashCode(const char* pStr) SAL_THROW_EXTERN_C()
@@ -506,33 +512,28 @@ sal_Bool SAL_CALL rtl_str_toBoolean(const char* pStr) 
SAL_THROW_EXTERN_C()
 
 sal_Int32 SAL_CALL rtl_str_toInt32(const char* pStr, sal_Int16 nRadix) 
SAL_THROW_EXTERN_C()
 {
-assert(pStr);
-return rtl::str::toInt(pStr, nRadix);
+return rtl::str::toInt(rtl::str::null_terminated(pStr), nRadix);
 }
 
 sal_Int64 SAL_CALL rtl_str_toInt64(const char* pStr, sal_Int16 nRadix) 
SAL_THROW_EXTERN_C()
 {
-assert(pStr);
-return rtl::str::toInt(pStr, nRadix);
+return 

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

2022-03-01 Thread Mike Kaganski (via logerrit)
 sal/rtl/string.cxx  |   15 ++-
 sal/rtl/strtmpl.hxx |   52 
 sal/rtl/ustring.cxx |   15 ++-
 3 files changed, 20 insertions(+), 62 deletions(-)

New commits:
commit 8ae8bea13565012410ecf13332f4696346294c12
Author: Mike Kaganski 
AuthorDate: Wed Mar 2 10:11:23 2022 +0300
Commit: Mike Kaganski 
CommitDate: Wed Mar 2 08:54:56 2022 +0100

Drop useless indirection and unused template

Change-Id: Icbcf56c30c9d901a4436f56a1648a7c8e7504848
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130817
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index 87c65f6069ce..c5d535075354 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -506,28 +506,33 @@ sal_Bool SAL_CALL rtl_str_toBoolean(const char* pStr) 
SAL_THROW_EXTERN_C()
 
 sal_Int32 SAL_CALL rtl_str_toInt32(const char* pStr, sal_Int16 nRadix) 
SAL_THROW_EXTERN_C()
 {
-return rtl::str::toInt32(pStr, nRadix);
+assert(pStr);
+return rtl::str::toInt(pStr, nRadix);
 }
 
 sal_Int64 SAL_CALL rtl_str_toInt64(const char* pStr, sal_Int16 nRadix) 
SAL_THROW_EXTERN_C()
 {
-return rtl::str::toInt64(pStr, nRadix);
+assert(pStr);
+return rtl::str::toInt(pStr, nRadix);
 }
 
 sal_Int64 SAL_CALL rtl_str_toInt64_WithLength(const char* pStr, sal_Int16 
nRadix,
   sal_Int32 nStrLength) 
SAL_THROW_EXTERN_C()
 {
-return rtl::str::toInt64_WithLength(pStr, nRadix, nStrLength);
+assert(pStr || nStrLength == 0);
+return rtl::str::toInt(std::basic_string_view(pStr, 
nStrLength), nRadix);
 }
 
 sal_uInt32 SAL_CALL rtl_str_toUInt32(const char* pStr, sal_Int16 nRadix) 
SAL_THROW_EXTERN_C()
 {
-return rtl::str::toUInt32(pStr, nRadix);
+assert(pStr);
+return rtl::str::toInt(pStr, nRadix);
 }
 
 sal_uInt64 SAL_CALL rtl_str_toUInt64(const char* pStr, sal_Int16 nRadix) 
SAL_THROW_EXTERN_C()
 {
-return rtl::str::toUInt64(pStr, nRadix);
+assert(pStr);
+return rtl::str::toInt(pStr, nRadix);
 }
 
 rtl_String* rtl_string_ImplAlloc(sal_Int32 nLen) { return 
rtl::str::Alloc(nLen); }
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index a6b06ad0256e..6988865e8f11 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -834,58 +834,6 @@ template  T toInt(S str, sal_Int16 
nRadix)
 return static_cast(n);
 }
 
-template 
-sal_Int32 toInt32 ( const IMPL_RTL_STRCODE* pStr,
-sal_Int16 nRadix )
-{
-assert(pStr);
-return toInt(pStr, nRadix);
-}
-
-template 
-sal_Int32 toInt32_WithLength  ( const IMPL_RTL_STRCODE* pStr,
-sal_Int16 nRadix,
-sal_Int32 nStrLength)
-
-{
-assert(pStr);
-return toInt(std::basic_string_view(pStr, nStrLength), nRadix);
-}
-
-template 
-sal_Int64 toInt64 ( const IMPL_RTL_STRCODE* pStr,
-sal_Int16 nRadix )
-{
-assert(pStr);
-return toInt(pStr, nRadix);
-}
-
-template 
-sal_Int64 toInt64_WithLength  ( const IMPL_RTL_STRCODE* pStr,
-sal_Int16 nRadix,
-sal_Int32 nStrLength)
-
-{
-assert(pStr);
-return toInt(std::basic_string_view(pStr, nStrLength), nRadix);
-}
-
-template 
-sal_uInt32 toUInt32 ( const IMPL_RTL_STRCODE* pStr,
-  sal_Int16 nRadix )
-{
-assert(pStr);
-return toInt(pStr, nRadix);
-}
-
-template 
-sal_uInt64 toUInt64 ( const IMPL_RTL_STRCODE* pStr,
-  sal_Int16 nRadix )
-{
-assert(pStr);
-return toInt(pStr, nRadix);
-}
-
 /* === */
 /* Internal String-Class help functions*/
 /* === */
diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx
index 62157a6bafca..4451866e4588 100644
--- a/sal/rtl/ustring.cxx
+++ b/sal/rtl/ustring.cxx
@@ -1171,30 +1171,35 @@ sal_Bool SAL_CALL rtl_ustr_toBoolean(const sal_Unicode* 
pStr) SAL_THROW_EXTERN_C
 
 sal_Int32 SAL_CALL rtl_ustr_toInt32(const sal_Unicode* pStr, sal_Int16 nRadix) 
SAL_THROW_EXTERN_C()
 {
-return rtl::str::toInt32(pStr, nRadix);
+assert(pStr);
+return rtl::str::toInt(pStr, nRadix);
 }
 
 sal_Int64 SAL_CALL rtl_ustr_toInt64(const sal_Unicode* pStr, sal_Int16 nRadix) 
SAL_THROW_EXTERN_C()
 {
-return rtl::str::toInt64(pStr, nRadix);
+assert(pStr);
+return rtl::str::toInt(pStr, nRadix);
 }
 
 sal_Int64 SAL_CALL rtl_ustr_toInt64_WithLength(const sal_Unicode* pStr, 
sal_Int16 nRadix,

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

2022-03-01 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |  145 
 1 file changed, 56 insertions(+), 89 deletions(-)

New commits:
commit 5b03e07dd21b56e99d6b6b60edec1ed2f388bfc2
Author: Mike Kaganski 
AuthorDate: Wed Mar 2 00:36:10 2022 +0300
Commit: Mike Kaganski 
CommitDate: Wed Mar 2 06:22:22 2022 +0100

Unify and deduplicate to[U]Int[_WithLength]

Change-Id: I05e71b36030da0a91c24b3926ee1d069912849f4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130815
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 78375ae07e26..a6b06ad0256e 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -761,58 +761,63 @@ template  sal_Bool toBoolean( 
const IMPL_RTL_STRCODE*
 }
 
 /* --- */
-template 
-T toInt_WithLength  ( const 
IMPL_RTL_STRCODE* pStr,
-  sal_Int16 
nRadix,
-  sal_Int32 
nStrLength )
+
+template  inline bool HandleSignChar(Iter& iter)
+{
+if constexpr (std::numeric_limits::is_signed)
+{
+if (*iter == '-')
+{
+++iter;
+return true;
+}
+}
+if (*iter == '+')
+++iter;
+return false;
+}
+
+template  std::pair DivMod(sal_Int16 nRadix, 
[[maybe_unused]] bool bNeg)
+{
+if constexpr (std::numeric_limits::is_signed)
+if (bNeg)
+return { -(std::numeric_limits::min() / nRadix),
+ -(std::numeric_limits::min() % nRadix) };
+return { std::numeric_limits::max() / nRadix, 
std::numeric_limits::max() % nRadix };
+}
+
+template  auto getIter(const SV& sv) { return sv.begin(); }
+template  auto getIter(const C* pStr) { return pStr; }
+
+template  auto good(typename SV::iterator iter, const SV& sv) { 
return iter != sv.end(); }
+template  auto good(const C* pStr, const C*) { return *pStr != 0; }
+
+template  T toInt(S str, sal_Int16 nRadix)
 {
-static_assert(std::numeric_limits::is_signed, "is signed");
 assert( nRadix >= RTL_STR_MIN_RADIX && nRadix <= RTL_STR_MAX_RADIX );
-assert( nStrLength >= 0 );
-boolbNeg;
-sal_Int16   nDigit;
-U   n = 0;
-const IMPL_RTL_STRCODE* pEnd = pStr + nStrLength;
 
 if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) )
 nRadix = 10;
 
+auto pStr = getIter(str);
+
 /* Skip whitespaces */
-while ( pStr != pEnd && rtl_ImplIsWhitespace( IMPL_RTL_USTRCODE( *pStr ) ) 
)
+while (good(pStr, str) && rtl_ImplIsWhitespace(IMPL_RTL_USTRCODE(*pStr)))
 pStr++;
+if (!good(pStr, str))
+return 0;
 
-if ( *pStr == '-' )
-{
-bNeg = true;
-pStr++;
-}
-else
-{
-if ( *pStr == '+' )
-pStr++;
-bNeg = false;
-}
-
-T nDiv;
-sal_Int16 nMod;
-if ( bNeg )
-{
-nDiv = -(std::numeric_limits::min() / nRadix);
-nMod = -(std::numeric_limits::min() % nRadix);
-}
-else
-{
-nDiv = std::numeric_limits::max() / nRadix;
-nMod = std::numeric_limits::max() % nRadix;
-}
+const bool bNeg = HandleSignChar(pStr);
+const auto& [nDiv, nMod] = DivMod(nRadix, bNeg);
+assert(nDiv > 0);
 
-while ( pStr != pEnd )
+std::make_unsigned_t n = 0;
+while (good(pStr, str))
 {
-nDigit = rtl_ImplGetDigit( IMPL_RTL_USTRCODE( *pStr ), nRadix );
+sal_Int16 nDigit = rtl_ImplGetDigit(IMPL_RTL_USTRCODE(*pStr), nRadix);
 if ( nDigit < 0 )
 break;
-assert(nDiv > 0);
-if( static_cast( nMod < nDigit ? nDiv-1 : nDiv ) < n )
+if (static_cast>(nMod < nDigit ? nDiv - 1 : 
nDiv) < n)
 return 0;
 
 n *= nRadix;
@@ -821,11 +826,12 @@ T toInt_WithLength
  ( const IMPL_RTL
 pStr++;
 }
 
-if ( bNeg )
-return n == static_cast(std::numeric_limits::min())
-? std::numeric_limits::min() : -static_cast(n);
-else
-return static_cast(n);
+if constexpr (std::numeric_limits::is_signed)
+if (bNeg)
+return n == 
static_cast>(std::numeric_limits::min())
+   ? std::numeric_limits::min()
+   : -static_cast(n);
+return static_cast(n);
 }
 
 template 
@@ -833,7 +839,7 @@ sal_Int32 toInt32 ( const 
IMPL_RTL_STRCODE* pStr,
 sal_Int16 nRadix )
 {
 assert(pStr);
-return toInt_WithLength(pStr, nRadix, 
getLength(pStr));
+return toInt(pStr, nRadix);
 }
 
 template 
@@ -843,7 +849,7 @@ sal_Int32 toInt32_WithLength  ( const 
IMPL_RTL_STRCODE* pStr,
 
 {
 assert(pStr);
-return toInt_WithLength(pStr, nRadix, nStrLength);
+   

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

2022-03-01 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

New commits:
commit 66614d9d1c63ecd957440184e696cd1d71fba96a
Author: Mike Kaganski 
AuthorDate: Tue Mar 1 13:50:20 2022 +0300
Commit: Mike Kaganski 
CommitDate: Tue Mar 1 17:39:16 2022 +0100

No need to memset the allocated buffer in new_WithLength

Other places that allocate, specifically newFromStr_WithLength and
ensureCapacity, don't initialize the memory.

Change-Id: Iad5301313312aac24948afefb27925d52fe3c673
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130762
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index db2df9a8ad75..78375ae07e26 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -1026,9 +1026,7 @@ void new_WithLength( IMPL_RTL_STRINGDATA** ppThis, 
sal_Int32 nLen )
 *ppThis = Alloc( nLen );
 assert(*ppThis != nullptr);
 (*ppThis)->length   = 0;
-
-auto* pTempStr = (*ppThis)->buffer;
-memset(pTempStr, 0, nLen*sizeof(*pTempStr));
+(*ppThis)->buffer[0] = 0;
 }
 }
 


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

2022-02-28 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

New commits:
commit 7375401eab02cb5bca37e2ea8c172b3cc9d413d8
Author: Mike Kaganski 
AuthorDate: Mon Feb 28 10:26:38 2022 +0100
Commit: Mike Kaganski 
CommitDate: Mon Feb 28 13:16:35 2022 +0100

Use 'assert' where appropriate

Change-Id: Id7ecf874745fb32406bcd2250a954aaf3a345103
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130667
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 9d14fbf77f23..db2df9a8ad75 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -1024,7 +1024,7 @@ void new_WithLength( IMPL_RTL_STRINGDATA** ppThis, 
sal_Int32 nLen )
 release( *ppThis );
 
 *ppThis = Alloc( nLen );
-OSL_ASSERT(*ppThis != nullptr);
+assert(*ppThis != nullptr);
 (*ppThis)->length   = 0;
 
 auto* pTempStr = (*ppThis)->buffer;
@@ -1402,7 +1402,7 @@ void newReplaceChars(IMPL_RTL_STRINGDATA** ppThis, 
IMPL_RTL_STRINGDATA* pStr)
 {
 IMPL_RTL_STRINGDATA* pOrg = *ppThis;
 *ppThis = Alloc(pStr->length);
-OSL_ASSERT(*ppThis != nullptr);
+assert(*ppThis != nullptr);
 auto* pNewCharStr = (*ppThis)->buffer;
 /* Copy String */
 const sal_Int32 nCount = pCharStr - pStr->buffer;


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

2022-02-28 Thread Mike Kaganski (via logerrit)
 sal/rtl/strbuf.cxx  |  121 +++---
 sal/rtl/string.cxx  |   12 ---
 sal/rtl/strtmpl.hxx |  153 +++-
 sal/rtl/ustrbuf.cxx |  165 +++-
 sal/rtl/ustring.cxx |   14 
 5 files changed, 162 insertions(+), 303 deletions(-)

New commits:
commit 22de5757a2c7c6cd4544a4699e5d4c69969f5961
Author: Mike Kaganski 
AuthorDate: Mon Feb 28 10:52:49 2022 +0300
Commit: Mike Kaganski 
CommitDate: Mon Feb 28 11:49:15 2022 +0100

Deduplicate string buffer code

Change-Id: Ia23974ad3a827c84bb1a5d633a953625aea2576a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130663
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strbuf.cxx b/sal/rtl/strbuf.cxx
index b9f4cd688b05..7ba25ff4a31a 100644
--- a/sal/rtl/strbuf.cxx
+++ b/sal/rtl/strbuf.cxx
@@ -23,6 +23,12 @@
 #include 
 #include "strimp.hxx"
 
+#if USE_SDT_PROBES
+#define RTL_LOG_STRING_BITS 8
+#endif
+
+#include "strtmpl.hxx"
+
 /*
  *  rtl_stringbuffer_newFromStr_WithLength
  */
@@ -30,21 +36,7 @@ void SAL_CALL rtl_stringbuffer_newFromStr_WithLength( 
rtl_String ** newStr,
   const char * value,
   sal_Int32 count )
 {
-assert(newStr);
-assert(count >= 0);
-if (!value)
-{
-rtl_string_new_WithLength( newStr, 16 );
-return;
-}
-
-// use raw alloc to avoid overwriting the buffer twice
-if ( *newStr)
-rtl_string_release( *newStr );
-*newStr = rtl_string_ImplAlloc( count + 16 );
-(*newStr)->length = count;
-memcpy( (*newStr)->buffer, value, count );
-memset( (*newStr)->buffer + count, 0, 16 );
+rtl::str::stringbuffer_newFromStr_WithLength(newStr, value, count);
 }
 
 /*
@@ -54,20 +46,7 @@ sal_Int32 SAL_CALL rtl_stringbuffer_newFromStringBuffer( 
rtl_String ** newStr,
  sal_Int32 capacity,
  rtl_String * oldStr )
 {
-assert(newStr);
-assert(oldStr);
-assert(capacity >= 0);
-sal_Int32 newCapacity = capacity;
-
-if (newCapacity < oldStr->length)
-newCapacity = oldStr->length;
-
-rtl_string_new_WithLength( newStr, newCapacity );
-if (oldStr->length > 0) {
-(*newStr)->length = oldStr->length;
-memcpy( (*newStr)->buffer, oldStr->buffer, oldStr->length );
-}
-return newCapacity;
+return rtl::str::stringbuffer_newFromStringBuffer(newStr, capacity, 
oldStr);
 }
 
 /*
@@ -76,28 +55,7 @@ sal_Int32 SAL_CALL rtl_stringbuffer_newFromStringBuffer( 
rtl_String ** newStr,
 void SAL_CALL rtl_stringbuffer_ensureCapacity
 (rtl_String ** This, sal_Int32* capacity, sal_Int32 minimumCapacity)
 {
-assert(This);
-//assert(capacity && *capacity >= 0);
-//assert(minimumCapacity >= 0);
-if (minimumCapacity <= *capacity)
-return;
-
-rtl_String * pTmp = *This;
-rtl_String * pNew = nullptr;
-auto nLength = (*This)->length;
-*capacity = (nLength + 1) * 2;
-if (minimumCapacity > *capacity)
-/* still lower, set to the minimum capacity */
-*capacity = minimumCapacity;
-
-// use raw alloc to avoid overwriting the buffer twice
-pNew = rtl_string_ImplAlloc( *capacity );
-pNew->length = nLength;
-*This = pNew;
-
-memcpy( (*This)->buffer, pTmp->buffer, nLength );
-memset( (*This)->buffer + nLength, 0, *capacity - nLength );
-rtl_string_release( pTmp );
+rtl::str::stringbuffer_ensureCapacity(This, capacity, minimumCapacity);
 }
 
 /*
@@ -109,41 +67,7 @@ void SAL_CALL rtl_stringbuffer_insert( rtl_String ** This,
const char * str,
sal_Int32 len )
 {
-assert(This);
-assert(capacity && *capacity >= 0);
-assert(offset >= 0 && offset <= (**This).length);
-assert(len >= 0);
-sal_Int32 nOldLen;
-char * pBuf;
-sal_Int32 n;
-if( len == 0 )
-return;
-
-if (*capacity < (*This)->length + len)
-rtl_stringbuffer_ensureCapacity( This, capacity, (*This)->length + len 
);
-
-nOldLen = (*This)->length;
-pBuf = (*This)->buffer;
-
-/* copy the tail */
-n = (nOldLen - offset);
-if( n == 1 )
-/* optimized for 1 character */
-pBuf[offset + len] = pBuf[offset];
-else if( n > 1 )
-memmove( pBuf + offset + len, pBuf + offset, n * sizeof(char) );
-
-/* insert the new characters */
-if( str != nullptr )
-{
-if( len == 1 )
-/* optimized for 1 

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

2022-02-27 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |  132 ++--
 1 file changed, 46 insertions(+), 86 deletions(-)

New commits:
commit a59e62abbd48a9e69dcfea574a84e70bd6983015
Author: Mike Kaganski 
AuthorDate: Sun Feb 27 12:02:20 2022 +0100
Commit: Mike Kaganski 
CommitDate: Sun Feb 27 13:21:16 2022 +0100

More replacement functions deduplication

Change-Id: I700074cddf1be937f5fdaf90e6cc0b533a3c57ad
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130633
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 5a468b8c9d90..f294c9c8b910 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -1218,76 +1218,53 @@ void ensureCapacity( 
IMPL_RTL_STRINGDATA** ppThi
 
 /* --- */
 
-template 
+template 
 void newReplaceStrAt( IMPL_RTL_STRINGDATA** 
ppThis,
   IMPL_RTL_STRINGDATA* 
pStr,
   sal_Int32 nIndex,
   sal_Int32 nCount,
-  IMPL_RTL_STRINGDATA* 
pNewSubStr )
+  const IMPL_RTL_STRCODE* 
pNewSubStr,
+  sal_Int32 nNewSubStrLen )
 {
 assert(ppThis);
 assert(nIndex >= 0 && nIndex <= pStr->length);
 assert(nCount >= 0);
 assert(nCount <= pStr->length - nIndex);
+assert(pNewSubStr != nullptr || nNewSubStrLen == 0);
+assert(nNewSubStrLen >= 0);
 /* Append? */
 if ( nIndex >= pStr->length )
-{
-/* newConcat test, if pNewSubStr is 0 */
-newConcat( ppThis, pStr, pNewSubStr );
-return;
-}
-
-/* negative index? */
-if ( nIndex < 0 )
-{
-nCount -= nIndex;
-nIndex = 0;
-}
+return newConcat(ppThis, pStr, pNewSubStr, nNewSubStrLen);
 
 /* not more than the String length could be deleted */
 if ( nCount >= pStr->length-nIndex )
 {
-nCount = pStr->length-nIndex;
-
 /* Assign of NewSubStr? */
-if ( !nIndex && (nCount >= pStr->length) )
-{
-if ( !pNewSubStr )
-new_( ppThis );
-else
-assign( ppThis, pNewSubStr );
-return;
-}
+if (nIndex == 0)
+return newFromStr_WithLength( ppThis, pNewSubStr, nNewSubStrLen );
+
+nCount = pStr->length - nIndex;
 }
 
 /* Assign of Str? */
-if ( !nCount && (!pNewSubStr || !pNewSubStr->length) )
-{
-assign( ppThis, pStr );
-return;
-}
+if ( !nCount && !nNewSubStrLen )
+return assign(ppThis, pStr);
 
 IMPL_RTL_STRINGDATA*pOrg = *ppThis;
-sal_Int32   nNewLen;
-
-/* Calculate length of the new string */
-nNewLen = pStr->length-nCount;
-if ( pNewSubStr )
-nNewLen += pNewSubStr->length;
 
 /* Alloc New Buffer */
-*ppThis = Alloc( nNewLen );
-OSL_ASSERT(*ppThis != nullptr);
+*ppThis = Alloc(pStr->length - nCount + 
nNewSubStrLen);
+assert(*ppThis != nullptr);
 auto* pBuffer = (*ppThis)->buffer;
 if ( nIndex )
 {
 Copy( pBuffer, pStr->buffer, nIndex );
 pBuffer += nIndex;
 }
-if ( pNewSubStr && pNewSubStr->length )
+if ( nNewSubStrLen )
 {
-Copy( pBuffer, pNewSubStr->buffer, pNewSubStr->length );
-pBuffer += pNewSubStr->length;
+Copy( pBuffer, pNewSubStr, nNewSubStrLen );
+pBuffer += nNewSubStrLen;
 }
 Copy( pBuffer, pStr->buffer+nIndex+nCount, pStr->length-nIndex-nCount );
 
@@ -1299,64 +1276,56 @@ void newReplaceStrAt( 
IMPL_RTL_STRINGDATA** ppTh
 
 /* --- */
 
-template 
+template 
 void newReplaceStrAt( IMPL_RTL_STRINGDATA** 
ppThis,
   IMPL_RTL_STRINGDATA* 
pStr,
   sal_Int32 nIndex,
   sal_Int32 nCount,
-  const IMPL_RTL_STRCODE* 
pNewSubStr,
-  sal_Int32 nNewSubStrLen )
+  IMPL_RTL_STRINGDATA* 
pNewSubStr )
 {
 assert(ppThis);
 assert(nIndex >= 0 && nIndex <= pStr->length);
 assert(nCount >= 0);
 assert(nCount <= pStr->length - nIndex);
-assert(pNewSubStr != nullptr || nNewSubStrLen == 0);
-assert(nNewSubStrLen >= 0);
 /* Append? */
-if ( nIndex >= pStr->length )
-return newConcat(ppThis, pStr, pNewSubStr, nNewSubStrLen);
+if 

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

2022-02-27 Thread Mike Kaganski (via logerrit)
 sal/rtl/string.cxx  |2 
 sal/rtl/strtmpl.hxx |  117 ++--
 sal/rtl/ustring.cxx |2 
 3 files changed, 34 insertions(+), 87 deletions(-)

New commits:
commit 0112fd253483b7b8fcb90e845e0c58ef53ce88ee
Author: Mike Kaganski 
AuthorDate: Sun Feb 27 10:54:00 2022 +0100
Commit: Mike Kaganski 
CommitDate: Sun Feb 27 11:53:12 2022 +0100

Unify creation of character string copy

Change-Id: Ifef89cfd98771f247a308fab58da410a7079ecfc
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130632
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index 92564b494c61..7be5a6e17595 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -598,7 +598,7 @@ void SAL_CALL rtl_string_newFromSubString(rtl_String** 
ppThis, const rtl_String*
 void SAL_CALL rtl_string_newFromLiteral(rtl_String** ppThis, const char* 
pCharStr, sal_Int32 nLen,
 sal_Int32 allocExtra) 
SAL_THROW_EXTERN_C()
 {
-rtl::str::newFromLiteral(ppThis, pCharStr, nLen, allocExtra);
+rtl::str::newFromStr_WithLength(ppThis, pCharStr, nLen, allocExtra);
 }
 
 void SAL_CALL rtl_string_assign(rtl_String** ppThis, rtl_String* pStr) 
SAL_THROW_EXTERN_C()
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index d800dd3c7b84..5a468b8c9d90 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -58,6 +58,7 @@ inline void Copy(sal_Unicode* _pDest, const char* _pSrc, 
sal_Int32 _nCount)
[](char c)
{
assert(rtl::isAscii(static_cast(c)));
+   SAL_WARN_IF(c == '\0', "rtl.string", "Found embedded 
\\0 ASCII character");
return static_cast(c);
});
 }
@@ -1012,8 +1013,35 @@ void new_WithLength( IMPL_RTL_STRINGDATA** ppThis, 
sal_Int32 nLen )
 
 /* --- */
 
-template 
-void newFromStr_WithLength(IMPL_RTL_STRINGDATA**, const 
STRCODE*, sal_Int32);
+template 
+void newFromStr_WithLength(IMPL_RTL_STRINGDATA** ppThis, const C* pCharStr, 
sal_Int32 nLen,
+   sal_Int32 allocExtra = 0)
+{
+assert(ppThis);
+assert(nLen >= 0);
+assert(pCharStr || nLen == 0);
+assert(allocExtra >= 0);
+
+if (nLen + allocExtra == 0)
+return new_(ppThis);
+
+IMPL_RTL_STRINGDATA* pOrg = *ppThis;
+*ppThis = Alloc(nLen + allocExtra);
+assert(*ppThis != nullptr);
+if (nLen > 0)
+Copy((*ppThis)->buffer, pCharStr, nLen);
+if (allocExtra > 0)
+{
+(*ppThis)->length = nLen;
+(*ppThis)->buffer[nLen] = 0;
+}
+
+RTL_LOG_STRING_NEW(*ppThis);
+
+/* must be done last, if pCharStr belongs to *ppThis */
+if (pOrg)
+release(pOrg);
+}
 
 template 
 void newFromString( IMPL_RTL_STRINGDATA** 
ppThis,
@@ -1044,39 +1072,7 @@ void newFromStr( 
IMPL_RTL_STRINGDATA** ppThis,
 
 /* --- */
 
-template 
-void newFromStr_WithLength( 
IMPL_RTL_STRINGDATA** ppThis,
-const 
STRCODE* pCharStr,
-sal_Int32 nLen )
-{
-assert(ppThis);
-
-if ( nLen == 0 )
-{
-new_( ppThis );
-return;
-}
-
-assert(nLen > 0);
-assert(pCharStr);
-
-IMPL_RTL_STRINGDATA* pOrg = *ppThis;
-*ppThis = Alloc( nLen );
-OSL_ASSERT(*ppThis != nullptr);
-Copy( (*ppThis)->buffer, pCharStr, nLen );
-
-RTL_LOG_STRING_NEW( *ppThis );
-
-/* must be done last, if pCharStr == *ppThis */
-if ( pOrg )
-release( pOrg );
-}
-
-/* --- */
-
 template  void assign(IMPL_RTL_STRINGDATA**, 
IMPL_RTL_STRINGDATA*);
-template 
-void newFromLiteral(IMPL_RTL_STRINGDATA**, const char*, sal_Int32, sal_Int32);
 
 template 
 void newFromSubString( IMPL_RTL_STRINGDATA** 
ppThis,
@@ -1086,15 +1082,11 @@ void newFromSubString( 
IMPL_RTL_STRINGDATA** ppT
 {
 assert(ppThis);
 if ( beginIndex == 0 && count == pFrom->length )
-{
-assign( ppThis, const_cast< IMPL_RTL_STRINGDATA * >( pFrom ) );
-return;
-}
+return assign(ppThis, const_cast(pFrom));
 if ( count < 0 || beginIndex < 0 || beginIndex + count > pFrom->length )
 {
 assert(false); // fail fast at least in debug builds
-newFromLiteral( ppThis, "!!br0ken!!", 10, 0 );
-return;
+return newFromStr_WithLength(ppThis, "!!br0ken!!", 10);
 }
 
 newFromStr_WithLength( ppThis, pFrom->buffer + beginIndex, count );
@@ -1102,51 +1094,6 @@ void newFromSubString( 

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

2022-02-27 Thread Mike Kaganski (via logerrit)
 sal/rtl/string.cxx  |4 -
 sal/rtl/strtmpl.hxx |  168 +---
 sal/rtl/ustring.cxx |4 -
 3 files changed, 47 insertions(+), 129 deletions(-)

New commits:
commit 7b8671d7b58a2a70c641f8315dc506542e8266cc
Author: Mike Kaganski 
AuthorDate: Sun Feb 27 09:44:23 2022 +0100
Commit: Mike Kaganski 
CommitDate: Sun Feb 27 10:42:51 2022 +0100

Deduplicate newToAscii*Case

Change-Id: I55f4f43375037ad4247f592bc981d8718c12de68
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130631
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index 4b08b5e2c39f..92564b494c61 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -650,13 +650,13 @@ void SAL_CALL rtl_string_newReplace(rtl_String** ppThis, 
rtl_String* pStr, char
 void SAL_CALL rtl_string_newToAsciiLowerCase(rtl_String** ppThis, rtl_String* 
pStr)
 SAL_THROW_EXTERN_C()
 {
-rtl::str::newToAsciiLowerCase(ppThis, pStr);
+rtl::str::newReplaceChars(ppThis, pStr);
 }
 
 void SAL_CALL rtl_string_newToAsciiUpperCase(rtl_String** ppThis, rtl_String* 
pStr)
 SAL_THROW_EXTERN_C()
 {
-rtl::str::newToAsciiUpperCase(ppThis, pStr);
+rtl::str::newReplaceChars(ppThis, pStr);
 }
 
 void SAL_CALL rtl_string_newTrim(rtl_String** ppThis, rtl_String* pStr) 
SAL_THROW_EXTERN_C()
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 9cbc8148044a..d800dd3c7b84 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -918,13 +918,7 @@ sal_uInt64 toUInt64 ( const 
IMPL_RTL_STRCODE* pStr,
 /* Internal String-Class help functions*/
 /* === */
 
-template  struct STRCODE_DATA
-{
-using type = std::remove_extent_t;
-using unsigned_type = std::make_unsigned_t;
-};
-template  using STRCODE = typename 
STRCODE_DATA::type;
-template  using USTRCODE = typename 
STRCODE_DATA::unsigned_type;
+template  using STRCODE = 
std::remove_extent_t;
 
 template  IMPL_RTL_STRINGDATA* Alloc( sal_Int32 
nLen )
 {
@@ -943,27 +937,6 @@ template  
IMPL_RTL_STRINGDATA* Alloc( sal_Int32 nL
 return pData;
 }
 
-/* --- */
-
-template 
-auto* NewCopy  ( 
IMPL_RTL_STRINGDATA** ppThis,
- 
IMPL_RTL_STRINGDATA* pStr,
- sal_Int32 nCount )
-{
-assert(ppThis);
-assert(pStr);
-assert(nCount >= 0 && nCount <= pStr->length);
-*ppThis = Alloc( pStr->length );
-OSL_ASSERT(*ppThis != nullptr);
-
-auto* pDest   = (*ppThis)->buffer;
-
-Copy(pDest, pStr->buffer, nCount);
-
-RTL_LOG_STRING_NEW( *ppThis );
-return pDest + nCount;
-}
-
 /* === */
 /* String-Class functions  */
 /* === */
@@ -1456,118 +1429,63 @@ void newReplace( 
IMPL_RTL_STRINGDATA** ppThis,
 
 /* --- */
 
-template 
-void newToAsciiLowerCase( 
IMPL_RTL_STRINGDATA** ppThis,
-  IMPL_RTL_STRINGDATA* 
pStr )
+struct ToAsciiLower
 {
-assert(ppThis);
-assert(pStr);
-IMPL_RTL_STRINGDATA*pOrg= *ppThis;
-boolbChanged= false;
-sal_Int32   nLen= pStr->length;
-const auto* pCharStr= pStr->buffer;
-
-while ( nLen > 0 )
+template  static bool Applicable(C c)
 {
-if ( rtl::isAsciiUpperCase(USTRCODE(*pCharStr)) )
-{
-/* Copy String */
-auto* pNewCharStr = NewCopy( ppThis, pStr, pCharStr-pStr->buffer );
-
-/* replace/copy rest of the string */
-if ( pNewCharStr )
-{
-*pNewCharStr = 
rtl::toAsciiLowerCase(USTRCODE(*pCharStr));
-pNewCharStr++;
-pCharStr++;
-nLen--;
-
-while ( nLen > 0 )
-{
-*pNewCharStr = 
rtl::toAsciiLowerCase(USTRCODE(*pCharStr));
-
-pNewCharStr++;
-pCharStr++;
-nLen--;
-}
-}
-
-bChanged = true;
-break;
-}
-
-pCharStr++;
-nLen--;
+return rtl::isAsciiUpperCase(IMPL_RTL_USTRCODE(c));
 }
-
-if ( !bChanged )
+template  static C Replace(C c)
 {
-*ppThis = pStr;
-acquire( pStr );
+return rtl::toAsciiLowerCase(IMPL_RTL_USTRCODE(c));
 }
+};
 
-

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

2022-02-27 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |   19 ++-
 1 file changed, 2 insertions(+), 17 deletions(-)

New commits:
commit bedb7118f5ca2cd19f4bc1eac1b26498ee5ef113
Author: Mike Kaganski 
AuthorDate: Sun Feb 27 09:42:35 2022 +0100
Commit: Mike Kaganski 
CommitDate: Sun Feb 27 10:25:43 2022 +0100

Simplify newTrim

Change-Id: I36e2f8b0f0f4829fba32e637fe61cebe9e67cf91
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130630
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index c5cdc23fbc84..9cbc8148044a 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -1576,28 +1576,13 @@ template 
 void newTrim( IMPL_RTL_STRINGDATA** ppThis,
   IMPL_RTL_STRINGDATA* pStr )
 {
-assert(ppThis);
 assert(pStr);
-IMPL_RTL_STRINGDATA*pOrg= *ppThis;
 const auto view = trimView(pStr->buffer, pStr->length);
 
 if (static_cast(view.size()) == pStr->length)
-{
-*ppThis = pStr;
-acquire( pStr );
-}
+assign(ppThis, pStr);
 else
-{
-sal_Int32 nLen = static_cast(view.size());
-*ppThis = Alloc( nLen );
-assert(*ppThis);
-Copy( (*ppThis)->buffer, view.data(), nLen );
-}
-
-RTL_LOG_STRING_NEW( *ppThis );
-/* must be done last, if pStr == *ppThis */
-if ( pOrg )
-release( pOrg );
+newFromStr_WithLength(ppThis, view.data(), view.size());
 }
 
 /* --- */


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

2022-02-26 Thread Mike Kaganski (via logerrit)
 sal/rtl/string.cxx  |   20 ++--
 sal/rtl/strtmpl.hxx |  219 
 sal/rtl/ustring.cxx |  147 +-
 3 files changed, 154 insertions(+), 232 deletions(-)

New commits:
commit 2126968ffbceb3c6f05db29c26ec90975e3f6331
Author: Mike Kaganski 
AuthorDate: Sat Feb 26 20:41:55 2022 +0300
Commit: Mike Kaganski 
CommitDate: Sun Feb 27 06:07:21 2022 +0100

Deduplicate more comparison functions

Change-Id: I93aba36b7cde268e358bd96b55183533ddc70b16
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130603
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index 2b118a57aada..4b08b5e2c39f 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -337,14 +337,15 @@ sal_Int32 SAL_CALL rtl_str_getLength(const char* pStr) 
SAL_THROW_EXTERN_C()
 
 sal_Int32 SAL_CALL rtl_str_compare(const char* pStr1, const char* pStr2) 
SAL_THROW_EXTERN_C()
 {
-return rtl::str::compare(pStr1, pStr2);
+return rtl::str::compare(pStr1, pStr2, rtl::str::CompareNormal());
 }
 
 sal_Int32 SAL_CALL rtl_str_compare_WithLength(const char* pStr1, sal_Int32 
nStr1Len,
   const char* pStr2, sal_Int32 
nStr2Len)
 SAL_THROW_EXTERN_C()
 {
-return rtl::str::compare_WithLength(pStr1, nStr1Len, pStr2, nStr2Len);
+return rtl::str::compare_WithLengths(pStr1, nStr1Len, pStr2, nStr2Len,
+ rtl::str::CompareNormal());
 }
 
 sal_Int32 SAL_CALL rtl_str_shortenedCompare_WithLength(const char* pStr1, 
sal_Int32 nStr1Len,
@@ -352,35 +353,38 @@ sal_Int32 SAL_CALL 
rtl_str_shortenedCompare_WithLength(const char* pStr1, sal_In
sal_Int32 
nShortenedLength)
 SAL_THROW_EXTERN_C()
 {
-return rtl::str::shortenedCompare_WithLength(pStr1, nStr1Len, pStr2, 
nStr2Len, nShortenedLength);
+return rtl::str::shortenedCompare_WithLengths(pStr1, nStr1Len, pStr2, 
nStr2Len,
+  nShortenedLength, 
rtl::str::CompareNormal());
 }
 
 sal_Int32 SAL_CALL rtl_str_reverseCompare_WithLength(const char* pStr1, 
sal_Int32 nStr1Len,
  const char* pStr2, 
sal_Int32 nStr2Len)
 SAL_THROW_EXTERN_C()
 {
-return rtl::str::reverseCompare_WithLength(pStr1, nStr1Len, pStr2, 
nStr2Len);
+return rtl::str::reverseCompare_WithLengths(pStr1, nStr1Len, pStr2, 
nStr2Len,
+rtl::str::CompareNormal());
 }
 
 sal_Int32 SAL_CALL rtl_str_compareIgnoreAsciiCase(const char* pStr1, const 
char* pStr2)
 SAL_THROW_EXTERN_C()
 {
-return rtl::str::compareIgnoreAsciiCase(pStr1, pStr2);
+return rtl::str::compare(pStr1, pStr2, rtl::str::CompareIgnoreAsciiCase());
 }
 
 sal_Int32 SAL_CALL rtl_str_compareIgnoreAsciiCase_WithLength(const char* 
pStr1, sal_Int32 nStr1Len,
  const char* 
pStr2, sal_Int32 nStr2Len)
 SAL_THROW_EXTERN_C()
 {
-return rtl::str::compareIgnoreAsciiCase_WithLength(pStr1, nStr1Len, pStr2, 
nStr2Len);
+return rtl::str::compare_WithLengths(pStr1, nStr1Len, pStr2, nStr2Len,
+ rtl::str::CompareIgnoreAsciiCase());
 }
 
 sal_Int32 SAL_CALL rtl_str_shortenedCompareIgnoreAsciiCase_WithLength(
 const char* pStr1, sal_Int32 nStr1Len, const char* pStr2, sal_Int32 
nStr2Len,
 sal_Int32 nShortenedLength) SAL_THROW_EXTERN_C()
 {
-return rtl::str::shortenedCompareIgnoreAsciiCase_WithLength(pStr1, 
nStr1Len, pStr2, nStr2Len,
-  
nShortenedLength);
+return rtl::str::shortenedCompare_WithLengths(
+pStr1, nStr1Len, pStr2, nStr2Len, nShortenedLength, 
rtl::str::CompareIgnoreAsciiCase());
 }
 
 sal_Int32 SAL_CALL rtl_str_hashCode(const char* pStr) SAL_THROW_EXTERN_C()
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 256c0cc99dd0..c5cdc23fbc84 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -98,143 +98,173 @@ template  void 
warnIfOneIsCharAndNotAscii(C1 c1, C2 c2
 }
 }
 
-/* --- */
-
-template  sal_Int32 compare(const C1* pStr1, const 
C2* pStr2)
+struct CompareNormal
 {
-assert(pStr1);
-assert(pStr2);
-if constexpr (sizeof(C1) == sizeof(char) && sizeof(C2) == sizeof(char))
+template  static sal_Int32 compare(C1 c1, C2 c2)
 {
-// take advantage of builtin optimisations
-return strcmp( pStr1, pStr2);
+warnIfOneIsCharAndNotAscii(c1, c2);
+return static_cast(IMPL_RTL_USTRCODE(c1))
+   - static_cast(IMPL_RTL_USTRCODE(c2));
 }
-else if constexpr (sizeof(C1) == sizeof(wchar_t) && sizeof(C2) == 
sizeof(wchar_t))
+};
+
+struct CompareIgnoreAsciiCase
+{
+template  static sal_Int32 

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

2022-02-25 Thread Mike Kaganski (via logerrit)
 sal/rtl/string.cxx  |7 ---
 sal/rtl/strtmpl.hxx |   24 ++--
 2 files changed, 6 insertions(+), 25 deletions(-)

New commits:
commit 6819ff8549d0a247e9e16f98eddbb7abd287d123
Author: Mike Kaganski 
AuthorDate: Fri Feb 25 20:36:09 2022 +0100
Commit: Mike Kaganski 
CommitDate: Sat Feb 26 07:27:17 2022 +0100

Slightly cleanup newReplaceStrAt

Change-Id: I230aa635aa797038055bdbc9453a5daa98412f93
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130465
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index de8cabff4c91..2b118a57aada 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -618,13 +618,6 @@ void SAL_CALL rtl_string_newConcat(rtl_String** ppThis, 
rtl_String* pLeft, rtl_S
 rtl::str::newConcat(ppThis, pLeft, pRight);
 }
 
-static void rtl_string_newConcatL(
-rtl_String ** newString, rtl_String * left, char const * right,
-sal_Int32 rightLength)
-{
-rtl::str::newConcat(newString, left, right, rightLength);
-}
-
 void SAL_CALL rtl_string_ensureCapacity(rtl_String** ppThis, sal_Int32 size) 
SAL_THROW_EXTERN_C()
 {
 rtl::str::ensureCapacity(ppThis, size);
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 348b84fab246..256c0cc99dd0 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -1378,33 +1378,21 @@ void newReplaceStrAt( 
IMPL_RTL_STRINGDATA** ppTh
 assert(nNewSubStrLen >= 0);
 /* Append? */
 if ( nIndex >= pStr->length )
-{
-if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char))
-rtl_string_newConcatL( ppThis, pStr, pNewSubStr, nNewSubStrLen );
-else
-rtl_uString_newConcatUtf16L( ppThis, pStr, pNewSubStr, 
nNewSubStrLen );
-return;
-}
+return newConcat(ppThis, pStr, pNewSubStr, nNewSubStrLen);
 
 /* not more than the String length could be deleted */
 if ( nCount >= pStr->length-nIndex )
 {
-nCount = pStr->length-nIndex;
-
 /* Assign of NewSubStr? */
-if ( !nIndex && (nCount >= pStr->length) )
-{
-newFromStr_WithLength( ppThis, pNewSubStr, nNewSubStrLen );
-return;
-}
+if (nIndex == 0)
+return newFromStr_WithLength( ppThis, pNewSubStr, nNewSubStrLen );
+
+nCount = pStr->length - nIndex;
 }
 
 /* Assign of Str? */
 if ( !nCount && !nNewSubStrLen )
-{
-assign( ppThis, pStr );
-return;
-}
+return assign(ppThis, pStr);
 
 IMPL_RTL_STRINGDATA*pOrg = *ppThis;
 sal_Int32   nNewLen;


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

2022-02-25 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |   12 +++-
 1 file changed, 7 insertions(+), 5 deletions(-)

New commits:
commit cbb1241ebb7488aa584825645d399b7ee83272fb
Author: Mike Kaganski 
AuthorDate: Fri Feb 25 20:38:23 2022 +0100
Commit: Mike Kaganski 
CommitDate: Fri Feb 25 22:56:17 2022 +0100

Slightly optimize newReplaceAllFromIndex

Change-Id: Ie73a5e8c44458123ef03096e3e8f6c9e41c48814
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130466
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 598d214878d6..348b84fab246 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -1695,19 +1695,20 @@ void newReplaceAllFromIndex(S** s, S* s1, CharTypeFrom 
const* from, sal_Int32 fr
 {
 if (s1->length - fromLength > SAL_MAX_INT32 - toLength)
 std::abort();
-acquire(s1); // in case *s == s1
+i += fromIndex;
 sal_Int32 nCapacity = s1->length + (toLength - fromLength);
 if (fromLength < toLength)
 {
 // Pre-allocate up to 16 replacements more
-const sal_Int32 nMaxMoreFinds = (s1->length - fromIndex - i - 
fromLength) / fromLength;
+const sal_Int32 nMaxMoreFinds = (s1->length - i - fromLength) / 
fromLength;
 const sal_Int32 nIncrease = toLength - fromLength;
 const sal_Int32 nMoreReplacements = std::min(
 { nMaxMoreFinds, (SAL_MAX_INT32 - nCapacity) / nIncrease, 
sal_Int32(16) });
 nCapacity += nMoreReplacements * nIncrease;
 }
-new_WithLength(s, nCapacity);
-i += fromIndex;
+const auto pOld = *s;
+*s = Alloc(nCapacity);
+(*s)->length = 0;
 fromIndex = 0;
 do
 {
@@ -1718,7 +1719,8 @@ void newReplaceAllFromIndex(S** s, S* s1, CharTypeFrom 
const* from, sal_Int32 fr
 } while (i >= 0);
 // the rest
 detail::append(s, , s1->buffer + fromIndex, s1->length - 
fromIndex);
-release(s1);
+if (pOld)
+release(pOld); // Must be last in case *s == s1
 }
 else
 assign(s, s1);


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

2022-02-25 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |   34 ++
 sal/rtl/ustring.cxx |  121 +---
 2 files changed, 38 insertions(+), 117 deletions(-)

New commits:
commit ffb2e5520eff008d4e784fa6b15f4e2baa534e7a
Author: Mike Kaganski 
AuthorDate: Fri Feb 25 20:41:15 2022 +0100
Commit: Mike Kaganski 
CommitDate: Fri Feb 25 22:55:05 2022 +0100

Deduplicate rtl_uString_newReplaceFirst*

Change-Id: I6e09fd8e8acc06290423c1c44cc0c175b0c47e0a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130547
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 287e6987e36e..598d214878d6 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -1726,6 +1726,40 @@ void newReplaceAllFromIndex(S** s, S* s1, CharTypeFrom 
const* from, sal_Int32 fr
 RTL_LOG_STRING_NEW(*s);
 }
 
+template 
+void newReplaceFirst(S** s, S* s1, CharTypeFrom const* from, sal_Int32 
fromLength,
+ CharTypeTo const* to, sal_Int32 toLength, sal_Int32& 
fromIndex)
+{
+assert(s != nullptr);
+assert(s1 != nullptr);
+assert(fromLength >= 0);
+assert(from != nullptr || fromLength == 0);
+assert(toLength >= 0);
+assert(to != nullptr || toLength == 0);
+assert(fromIndex >= 0 && fromIndex <= s1->length);
+sal_Int32 i = detail::indexOf(s1->buffer + fromIndex, s1->length - 
fromIndex, from, fromLength);
+if (i >= 0)
+{
+if (s1->length - fromLength > SAL_MAX_INT32 - toLength)
+std::abort();
+i += fromIndex;
+const sal_Int32 n = s1->length + (toLength - fromLength);
+const auto pOld = *s;
+*s = Alloc(n);
+if (i)
+Copy((*s)->buffer, s1->buffer, i);
+if (toLength)
+Copy((*s)->buffer + i, to, toLength);
+Copy((*s)->buffer + i + toLength, s1->buffer + i + fromLength, 
s1->length - i - fromLength);
+if (pOld)
+release(pOld); // Must be last in case *s == s1
+}
+else
+assign(s, s1);
+
+fromIndex = i;
+}
+
 template 
 using doubleToString_t
 = void(SAL_CALL*)(IMPL_RTL_STRINGDATA** pResult, sal_Int32* 
pResultCapacity,
diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx
index 1f81dc3fd902..b289ae2a3f21 100644
--- a/sal/rtl/ustring.cxx
+++ b/sal/rtl/ustring.cxx
@@ -984,43 +984,8 @@ void rtl_uString_newReplaceFirstAsciiLAsciiL(
 sal_Int32 fromLength, char const * to, sal_Int32 toLength,
 sal_Int32 * index) SAL_THROW_EXTERN_C()
 {
-assert(str != nullptr);
 assert(index != nullptr);
-assert(*index >= 0 && *index <= str->length);
-assert(fromLength >= 0);
-assert(to != nullptr);
-assert(toLength >= 0);
-sal_Int32 i = rtl_ustr_indexOfAscii_WithLength(
-str->buffer + *index, str->length - *index, from, fromLength);
-if (i == -1) {
-rtl_uString_assign(newStr, str);
-} else {
-assert(i <= str->length - *index);
-i += *index;
-assert(fromLength <= str->length);
-if (str->length - fromLength > SAL_MAX_INT32 - toLength) {
-std::abort();
-}
-sal_Int32 n = str->length - fromLength + toLength;
-rtl_uString_acquire(str); // in case *newStr == str
-rtl_uString_new_WithLength(newStr, n);
-if (n != 0) {
-(*newStr)->length = n;
-assert(i >= 0 && i < str->length);
-memcpy(
-(*newStr)->buffer, str->buffer, i * sizeof (sal_Unicode));
-for (sal_Int32 j = 0; j != toLength; ++j) {
-assert(static_cast< unsigned char >(to[j]) <= 0x7F);
-(*newStr)->buffer[i + j] = to[j];
-}
-memcpy(
-(*newStr)->buffer + i + toLength,
-str->buffer + i + fromLength,
-(str->length - i - fromLength) * sizeof (sal_Unicode));
-}
-rtl_uString_release(str);
-}
-*index = i;
+rtl::str::newReplaceFirst(newStr, str, from, fromLength, to, toLength, 
*index);
 }
 
 void rtl_uString_newReplaceFirstAsciiLUtf16L(
@@ -1028,28 +993,8 @@ void rtl_uString_newReplaceFirstAsciiLUtf16L(
 sal_Int32 fromLength, sal_Unicode const * to, sal_Int32 toLength,
 sal_Int32 * index) SAL_THROW_EXTERN_C()
 {
-assert(str != nullptr);
 assert(index != nullptr);
-assert(*index >= 0 && *index <= str->length);
-assert(fromLength >= 0);
-assert(to != nullptr || toLength == 0);
-assert(toLength >= 0);
-sal_Int32 i = rtl_ustr_indexOfAscii_WithLength(
-str->buffer + *index, str->length - *index, from, fromLength);
-if (i == -1) {
-rtl_uString_assign(newStr, str);
-} else {
-assert(i <= str->length - *index);
-i += *index;
-assert(fromLength <= str->length);
-if (str->length - fromLength > SAL_MAX_INT32 - toLength) {
-rtl_uString_release(*newStr);
-*newStr = nullptr;
-} else {
-

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

2022-02-25 Thread Mike Kaganski (via logerrit)
 sal/rtl/ustring.cxx |   12 +---
 1 file changed, 1 insertion(+), 11 deletions(-)

New commits:
commit 72f836fb0c0c3daaf0e47832809a352888577905
Author: Mike Kaganski 
AuthorDate: Fri Feb 25 17:24:37 2022 +0300
Commit: Mike Kaganski 
CommitDate: Fri Feb 25 16:31:18 2022 +0100

Don't invent another strlen

Change-Id: I7f7b7029ac03b1f06f4d9c2612b9b008b033baea
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130538
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx
index fb7d45e3ebbd..aa3a7263d1b7 100644
--- a/sal/rtl/ustring.cxx
+++ b/sal/rtl/ustring.cxx
@@ -375,17 +375,7 @@ void SAL_CALL rtl_uString_newFromAscii( rtl_uString** 
ppThis,
 SAL_THROW_EXTERN_C()
 {
 assert(ppThis);
-sal_Int32 nLen;
-
-if ( pCharStr )
-{
-const char* pTempStr = pCharStr;
-while( *pTempStr )
-pTempStr++;
-nLen = pTempStr-pCharStr;
-}
-else
-nLen = 0;
+sal_Int32 nLen = pCharStr ? rtl::str::getLength(pCharStr) : 0;
 
 if ( !nLen )
 {


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

2022-02-25 Thread Mike Kaganski (via logerrit)
 sal/rtl/ustring.cxx |   42 ++
 1 file changed, 10 insertions(+), 32 deletions(-)

New commits:
commit f58e351ff05e95fd42b4c108bf0be496be855b46
Author: Mike Kaganski 
AuthorDate: Fri Feb 25 10:49:21 2022 +0300
Commit: Mike Kaganski 
CommitDate: Fri Feb 25 10:22:36 2022 +0100

Use some rtl functions

Change-Id: I12fe2f5fe447e2bc6567e3d0375f1671e781697e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130515
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx
index ff51a53f61fd..fb7d45e3ebbd 100644
--- a/sal/rtl/ustring.cxx
+++ b/sal/rtl/ustring.cxx
@@ -186,8 +186,7 @@ sal_Int32 SAL_CALL rtl_ustr_ascii_compare_WithLength( const 
sal_Unicode* pStr1,
static_cast(static_cast(*pStr2));
 if (!(nRet == 0 && nStr1Len && *pStr2 ))
 break;
-/* Check ASCII range */
-SAL_WARN_IF( (static_cast(*pStr2)) > 127, "rtl.string",
+SAL_WARN_IF( !rtl::isAscii(static_cast(*pStr2)), 
"rtl.string",
 "rtl_ustr_ascii_compare_WithLength - Found char > 127" );
 pStr1++;
 pStr2++;
@@ -212,8 +211,7 @@ sal_Int32 SAL_CALL 
rtl_ustr_ascii_shortenedCompare_WithLength( const sal_Unicode
 while ( (nShortenedLength > 0) &&
 (pStr1 < pStr1End) && *pStr2 )
 {
-/* Check ASCII range */
-SAL_WARN_IF( (static_cast(*pStr2)) > 127, "rtl.string",
+SAL_WARN_IF( !rtl::isAscii(static_cast(*pStr2)), 
"rtl.string",
 "rtl_ustr_ascii_shortenedCompare_WithLength - Found char > 
127" );
 
 nRet = static_cast(*pStr1)-
@@ -267,8 +265,7 @@ sal_Bool SAL_CALL rtl_ustr_asciil_reverseEquals_WithLength( 
const sal_Unicode* p
 const char* pStr2Run = pStr2+nStrLen;
 while ( pStr1 < pStr1Run )
 {
-/* Check ASCII range */
-SAL_WARN_IF( (static_cast(*pStr2)) > 127, "rtl.string",
+SAL_WARN_IF( !rtl::isAscii(static_cast(*pStr2)), 
"rtl.string",
 "rtl_ustr_asciil_reverseEquals_WithLength - Found char > 
127" );
 pStr1Run--;
 pStr2Run--;
@@ -297,25 +294,16 @@ sal_Int32 SAL_CALL 
rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength( const sal_U
 {
 assert(nStr1Len >= 0);
 assert(pStr2);
-sal_Int32   nRet;
-sal_Int32   c1;
-sal_Int32   c2;
+unsigned char c2;
 do
 {
-/* Check ASCII range */
-SAL_WARN_IF( (static_cast(*pStr2)) > 127, "rtl.string",
+SAL_WARN_IF( !rtl::isAscii(static_cast(*pStr2)), 
"rtl.string",
 "rtl_ustr_ascii_compareIgnoreAsciiCase_WithLength - Found 
char > 127" );
 if ( !nStr1Len )
 return *pStr2 == '\0' ? 0 : -1;
 
-/* If character between 'A' and 'Z', then convert it to lowercase */
-c1 = static_cast(*pStr1);
-c2 = static_cast(static_cast(*pStr2));
-if ( (c1 >= 65) && (c1 <= 90) )
-c1 += 32;
-if ( (c2 >= 65) && (c2 <= 90) )
-c2 += 32;
-nRet = c1-c2;
+c2 = static_cast(*pStr2);
+sal_Int32 nRet = rtl::compareIgnoreAsciiCase(*pStr1, c2);
 if ( nRet != 0 )
 return nRet;
 
@@ -347,23 +335,13 @@ sal_Int32 SAL_CALL 
rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength( co
 assert(nShortenedLength >= 0);
 const sal_Unicode*  pStr1End = pStr1 + nStr1Len;
 sal_Int32   nRet;
-sal_Int32   c1;
-sal_Int32   c2;
 while ( (nShortenedLength > 0) &&
 (pStr1 < pStr1End) && *pStr2 )
 {
-/* Check ASCII range */
-SAL_WARN_IF( (static_cast(*pStr2)) > 127, "rtl.string",
+SAL_WARN_IF( !rtl::isAscii(static_cast(*pStr2)), 
"rtl.string",
 "rtl_ustr_ascii_shortenedCompareIgnoreAsciiCase_WithLength 
- Found char > 127" );
 
-/* If character between 'A' and 'Z', then convert it to lowercase */
-c1 = static_cast(*pStr1);
-c2 = static_cast(static_cast(*pStr2));
-if ( (c1 >= 65) && (c1 <= 90) )
-c1 += 32;
-if ( (c2 >= 65) && (c2 <= 90) )
-c2 += 32;
-nRet = c1-c2;
+nRet = rtl::compareIgnoreAsciiCase(*pStr1, static_cast(*pStr2));
 if ( nRet != 0 )
 return nRet;
 
@@ -842,7 +820,7 @@ void SAL_CALL rtl_uString_internConvert( rtl_uString   ** 
newStr,
 for (i = 0; i < len; i++)
 {
 /* Check ASCII range */
-SAL_WARN_IF( (static_cast(str[i])) > 127, 
"rtl.string",
+SAL_WARN_IF( !rtl::isAscii(static_cast(str[i])), "rtl.string",
 "rtl_ustring_internConvert() - Found char > 127 
and RTL_TEXTENCODING_ASCII_US is specified" );
 pScratch->buffer[i] = str[i];
 }


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

2022-02-24 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |   65 +-
 sal/rtl/ustring.cxx |   87 ++--
 2 files changed, 46 insertions(+), 106 deletions(-)

New commits:
commit 8d7516a0e690fb34b894b8f064a47d705100a56f
Author: Mike Kaganski 
AuthorDate: Thu Feb 24 21:43:50 2022 +0300
Commit: Mike Kaganski 
CommitDate: Fri Feb 25 08:29:35 2022 +0100

Deduplicate some comparison functions

Change-Id: Iffeb4323c99649d45387981ec583fdcff207ec4e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130512
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 90d9547170ec..287e6987e36e 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -82,27 +82,45 @@ template  sal_Int32 getLength( const T* pStr )
 
 /* --- */
 
-template 
-sal_Int32 compare ( const IMPL_RTL_STRCODE* pStr1,
-const IMPL_RTL_STRCODE* pStr2 )
+template  void warnIfCharAndNotAscii(C c)
+{
+if constexpr (sizeof(c) == sizeof(char))
+SAL_WARN_IF(!rtl::isAscii(static_cast(c)), "rtl.string",
+"Found non-ASCII char");
+}
+
+template  void warnIfOneIsCharAndNotAscii(C1 c1, C2 
c2)
+{
+if constexpr (sizeof(c1) != sizeof(c2))
+{
+warnIfCharAndNotAscii(c1);
+warnIfCharAndNotAscii(c2);
+}
+}
+
+/* --- */
+
+template  sal_Int32 compare(const C1* pStr1, const 
C2* pStr2)
 {
 assert(pStr1);
 assert(pStr2);
-if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char))
+if constexpr (sizeof(C1) == sizeof(char) && sizeof(C2) == sizeof(char))
 {
 // take advantage of builtin optimisations
 return strcmp( pStr1, pStr2);
 }
-else if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(wchar_t))
+else if constexpr (sizeof(C1) == sizeof(wchar_t) && sizeof(C2) == 
sizeof(wchar_t))
 {
 // take advantage of builtin optimisations
 return wcscmp(reinterpret_cast(pStr1), 
reinterpret_cast(pStr2));
 }
-else
+else // including C1 != C2
 {
 sal_Int32 nRet;
 for (;;)
 {
+warnIfOneIsCharAndNotAscii(*pStr1, *pStr2);
+
 nRet = static_cast(IMPL_RTL_USTRCODE(*pStr1)) -
static_cast(IMPL_RTL_USTRCODE(*pStr2));
 if (!(nRet == 0 && *pStr2 ))
@@ -146,19 +164,19 @@ sal_Int32 shortenedCompare_WithLength 
( const IMPL_R
 
 /* --- */
 
-template 
-sal_Int32 reverseCompare_WithLength ( const 
IMPL_RTL_STRCODE* pStr1,
-  sal_Int32 
nStr1Len,
-  const 
IMPL_RTL_STRCODE* pStr2,
-  sal_Int32 
nStr2Len )
+template 
+sal_Int32 reverseCompare_WithLength(const C1* pStr1, sal_Int32 nStr1Len,
+const C2* pStr2, sal_Int32 nStr2Len)
 {
 assert(nStr1Len >= 0);
 assert(nStr2Len >= 0);
-const IMPL_RTL_STRCODE* pStr1Run = pStr1+nStr1Len;
-const IMPL_RTL_STRCODE* pStr2Run = pStr2+nStr2Len;
+const C1* pStr1Run = pStr1+nStr1Len;
+const C2* pStr2Run = pStr2+nStr2Len;
 sal_Int32   nRet;
 while ( (pStr1 < pStr1Run) && (pStr2 < pStr2Run) )
 {
+warnIfOneIsCharAndNotAscii(*pStr1, *pStr2);
+
 pStr1Run--;
 pStr2Run--;
 nRet = static_cast(IMPL_RTL_USTRCODE( *pStr1Run ))-
@@ -172,15 +190,16 @@ sal_Int32 reverseCompare_WithLength   
  ( const IMPL_RTL
 
 /* --- */
 
-template 
-sal_Int32 compareIgnoreAsciiCase ( const 
IMPL_RTL_STRCODE* pStr1,
-   const 
IMPL_RTL_STRCODE* pStr2 )
+template 
+sal_Int32 compareIgnoreAsciiCase(const C1* pStr1, const C2* pStr2)
 {
 assert(pStr1);
 assert(pStr2);
 sal_uInt32 c1;
 do
 {
+warnIfOneIsCharAndNotAscii(*pStr1, *pStr2);
+
 c1 = IMPL_RTL_USTRCODE(*pStr1);
 sal_Int32 nRet = rtl::compareIgnoreAsciiCase(
 c1, IMPL_RTL_USTRCODE(*pStr2));
@@ -197,18 +216,18 @@ sal_Int32 compareIgnoreAsciiCase  
   ( const IMPL_RTL_ST
 
 /* --- */
 
-template 
-sal_Int32 compareIgnoreAsciiCase_WithLength ( 
const IMPL_RTL_STRCODE* pStr1,
-  
sal_Int32 nStr1Len,
-

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

2022-02-24 Thread Mike Kaganski (via logerrit)
 sal/rtl/string.cxx  |   24 --
 sal/rtl/strtmpl.hxx |   86 +++-
 sal/rtl/ustring.cxx |   47 +---
 3 files changed, 68 insertions(+), 89 deletions(-)

New commits:
commit 089ce740f9f97f9c7b13e37a31acfc94984e9a3e
Author: Mike Kaganski 
AuthorDate: Thu Feb 24 17:45:18 2022 +0300
Commit: Mike Kaganski 
CommitDate: Thu Feb 24 17:57:14 2022 +0100

Deduplicate rtl_*String_newConcat*L

Change-Id: I9712cd8a2798fe5493dffd557e68239d9db3b7aa
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130501
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index ab59eab78c77..de8cabff4c91 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -622,29 +622,7 @@ static void rtl_string_newConcatL(
 rtl_String ** newString, rtl_String * left, char const * right,
 sal_Int32 rightLength)
 {
-assert(newString != nullptr);
-assert(left != nullptr);
-assert(right != nullptr || rightLength == 0);
-assert(rightLength >= 0);
-if (left->length > std::numeric_limits::max() - rightLength) {
-#if !defined(__COVERITY__)
-throw std::length_error("rtl_string_newConcatL");
-#else
-//coverity doesn't report std::bad_alloc as an unhandled exception when
-//potentially thrown from destructors but does report std::length_error
-throw std::bad_alloc();
-#endif
-}
-sal_Int32 n = left->length + rightLength;
-rtl_string_assign(newString, left);
-rtl_string_ensureCapacity(newString, n);
-if (rightLength != 0) {
-memcpy(
-(*newString)->buffer + (*newString)->length, right,
-rightLength);
-}
-(*newString)->buffer[n] = 0;
-(*newString)->length = n;
+rtl::str::newConcat(newString, left, right, rightLength);
 }
 
 void SAL_CALL rtl_string_ensureCapacity(rtl_String** ppThis, sal_Int32 size) 
SAL_THROW_EXTERN_C()
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 8901f3ace2b2..90d9547170ec 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -52,6 +52,16 @@ void Copy( IMPL_RTL_STRCODE* _pDest,
 memcpy( _pDest, _pSrc, _nCount * sizeof(IMPL_RTL_STRCODE));
 }
 
+inline void Copy(sal_Unicode* _pDest, const char* _pSrc, sal_Int32 _nCount)
+{
+std::transform(_pSrc, _pSrc + _nCount, _pDest,
+   [](char c)
+   {
+   assert(rtl::isAscii(static_cast(c)));
+   return static_cast(c);
+   });
+}
+
 /* === */
 /* C-String functions which could be used without the String-Class */
 /* === */
@@ -1152,38 +1162,44 @@ template  auto* getStr( 
IMPL_RTL_STRINGDATA* pThis
 
 /* --- */
 
-template 
-void newConcat( IMPL_RTL_STRINGDATA** ppThis,
-IMPL_RTL_STRINGDATA* pLeft,
-IMPL_RTL_STRINGDATA* pRight )
+enum ThrowPolicy { NoThrow, Throw };
+
+template 
+void newConcat(IMPL_RTL_STRINGDATA** ppThis, const C1* pLeft, sal_Int32 
nLeftLength,
+   const C2* pRight, sal_Int32 nRightLength)
 {
 assert(ppThis);
+assert(nLeftLength >= 0);
+assert(pLeft || nLeftLength == 0);
+assert(nRightLength >= 0);
+assert(pRight || nRightLength == 0);
 IMPL_RTL_STRINGDATA* pOrg = *ppThis;
 
-/* Test for 0-Pointer - if not, change newReplaceStrAt! */
-if ( !pRight || !pRight->length )
+if (nLeftLength > std::numeric_limits::max() - nRightLength)
 {
-*ppThis = pLeft;
-acquire( pLeft );
-}
-else if ( !pLeft || !pLeft->length )
-{
-*ppThis = pRight;
-acquire( pRight );
-}
-else if (pLeft->length
- > std::numeric_limits::max() - pRight->length)
-{
-*ppThis = nullptr;
+if constexpr (throwPolicy == NoThrow)
+*ppThis = nullptr;
+else
+{
+#if !defined(__COVERITY__)
+throw std::length_error("newConcat");
+#else
+//coverity doesn't report std::bad_alloc as an unhandled exception 
when
+//potentially thrown from destructors but does report 
std::length_error
+throw std::bad_alloc();
+#endif
+}
 }
 else
 {
-auto* pTempStr = Alloc( pLeft->length + 
pRight->length );
+auto* pTempStr = Alloc(nLeftLength + 
nRightLength);
 OSL_ASSERT(pTempStr != nullptr);
 *ppThis = pTempStr;
 if (*ppThis != nullptr) {
-Copy( pTempStr->buffer, pLeft->buffer, pLeft->length );
-Copy( pTempStr->buffer+pLeft->length, pRight->buffer, 
pRight->length );
+if (nLeftLength)
+Copy( 

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

2022-02-24 Thread Mike Kaganski (via logerrit)
 sal/rtl/string.cxx  |   26 ++
 sal/rtl/strtmpl.hxx |   24 
 sal/rtl/ustring.cxx |   26 ++
 3 files changed, 28 insertions(+), 48 deletions(-)

New commits:
commit ed5503ba4ef2952b5fbc1195692076e858597baa
Author: Mike Kaganski 
AuthorDate: Thu Feb 24 12:31:56 2022 +0300
Commit: Mike Kaganski 
CommitDate: Thu Feb 24 15:25:14 2022 +0100

Deduplicate rtl_*str_valueOf{Float,Double}

Change-Id: Ie3254fbf6178f4e623fd8209b58e9f19d2c46d2c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130480
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index 86cfdb1672d9..ab59eab78c77 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -64,35 +64,13 @@ rtl_String rtl::str::EmptyStringImpl::data =
 sal_Int32 SAL_CALL rtl_str_valueOfFloat(char * pStr, float f)
 SAL_THROW_EXTERN_C()
 {
-assert(pStr);
-rtl_String * pResult = nullptr;
-sal_Int32 nLen;
-rtl_math_doubleToString(
-, nullptr, 0, f, rtl_math_StringFormat_G,
-RTL_STR_MAX_VALUEOFFLOAT - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', 
nullptr, 0,
-true);
-nLen = pResult->length;
-OSL_ASSERT(nLen < RTL_STR_MAX_VALUEOFFLOAT);
-memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(char));
-rtl_string_release(pResult);
-return nLen;
+return rtl::str::valueOfFP(pStr, f, 
_math_doubleToString);
 }
 
 sal_Int32 SAL_CALL rtl_str_valueOfDouble(char * pStr, double d)
 SAL_THROW_EXTERN_C()
 {
-assert(pStr);
-rtl_String * pResult = nullptr;
-sal_Int32 nLen;
-rtl_math_doubleToString(
-, nullptr, 0, d, rtl_math_StringFormat_G,
-RTL_STR_MAX_VALUEOFDOUBLE - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', 
nullptr,
-0, true);
-nLen = pResult->length;
-OSL_ASSERT(nLen < RTL_STR_MAX_VALUEOFDOUBLE);
-memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(char));
-rtl_string_release(pResult);
-return nLen;
+return rtl::str::valueOfFP(pStr, d, 
_math_doubleToString);
 }
 
 float SAL_CALL rtl_str_toFloat(char const * pStr) SAL_THROW_EXTERN_C()
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 2e11ffd0161f..8901f3ace2b2 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -1663,6 +1663,30 @@ void newReplaceAllFromIndex(S** s, S* s1, CharTypeFrom 
const* from, sal_Int32 fr
 RTL_LOG_STRING_NEW(*s);
 }
 
+template 
+using doubleToString_t
+= void(SAL_CALL*)(IMPL_RTL_STRINGDATA** pResult, sal_Int32* 
pResultCapacity,
+  sal_Int32 nResultOffset, double fValue, 
rtl_math_StringFormat eFormat,
+  sal_Int32 nDecPlaces, STRCODE 
cDecSeparator,
+  sal_Int32 const* pGroups, STRCODE 
cGroupSeparator,
+  sal_Bool bEraseTrailingDecZeros) SAL_THROW_EXTERN_C();
+
+template 
+sal_Int32 SAL_CALL valueOfFP(STRCODE* pStr, T f,
+ doubleToString_t 
doubleToString)
+{
+assert(pStr);
+IMPL_RTL_STRINGDATA* pResult = nullptr;
+sal_Int32 nLen;
+doubleToString(, nullptr, 0, f, rtl_math_StringFormat_G,
+   maxLen - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', nullptr, 
0, true);
+nLen = pResult->length;
+OSL_ASSERT(nLen < maxLen);
+Copy(pStr, pResult->buffer, nLen + 1);
+release(pResult);
+return nLen;
+}
+
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx
index 2ea2eb142d7f..b5fc3685c48b 100644
--- a/sal/rtl/ustring.cxx
+++ b/sal/rtl/ustring.cxx
@@ -121,35 +121,13 @@ sal_Int32 rtl_ustr_lastIndexOfAscii_WithLength(
 sal_Int32 SAL_CALL rtl_ustr_valueOfFloat(sal_Unicode * pStr, float f)
 SAL_THROW_EXTERN_C()
 {
-assert(pStr);
-rtl_uString * pResult = nullptr;
-sal_Int32 nLen;
-rtl_math_doubleToUString(
-, nullptr, 0, f, rtl_math_StringFormat_G,
-RTL_USTR_MAX_VALUEOFFLOAT - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', 
nullptr,
-0, true);
-nLen = pResult->length;
-OSL_ASSERT(nLen < RTL_USTR_MAX_VALUEOFFLOAT);
-memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(sal_Unicode));
-rtl_uString_release(pResult);
-return nLen;
+return rtl::str::valueOfFP(pStr, f, 
_math_doubleToUString);
 }
 
 sal_Int32 SAL_CALL rtl_ustr_valueOfDouble(sal_Unicode * pStr, double d)
 SAL_THROW_EXTERN_C()
 {
-assert(pStr);
-rtl_uString * pResult = nullptr;
-sal_Int32 nLen;
-rtl_math_doubleToUString(
-, nullptr, 0, d, rtl_math_StringFormat_G,
-RTL_USTR_MAX_VALUEOFDOUBLE - RTL_CONSTASCII_LENGTH("-x.E-xxx"), '.', 
nullptr,
-0, true);
-nLen = pResult->length;
-OSL_ASSERT(nLen < RTL_USTR_MAX_VALUEOFDOUBLE);
-memcpy(pStr, pResult->buffer, (nLen + 1) * sizeof(sal_Unicode));
-rtl_uString_release(pResult);
-return nLen;
+return rtl::str::valueOfFP(pStr, d, 
_math_doubleToUString);
 }
 
 namespace {


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

2022-02-24 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

New commits:
commit c6cbf970943e17da5d95fc730fa1c15637caee05
Author: Mike Kaganski 
AuthorDate: Thu Feb 24 12:21:41 2022 +0100
Commit: Mike Kaganski 
CommitDate: Thu Feb 24 15:24:55 2022 +0100

Fix warning text

Change-Id: I42d0b3a19809a288630cfba6c01bcd5c4fde7702
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130454
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 0cee38057074..2e11ffd0161f 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -1116,7 +1116,7 @@ void newFromLiteral( 
IMPL_RTL_STRINGDATA** ppThi
 assert(rtl::isAscii(static_cast(*pCharStr)));
 }
 SAL_WARN_IF( (static_cast(*pCharStr)) == '\0', 
"rtl.string",
-"rtl_uString_newFromLiteral - Found embedded \\0 
character" );
+"newFromLiteral - Found embedded \\0 character" );
 
 *pBuffer = *pCharStr;
 pBuffer++;


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

2022-02-24 Thread Mike Kaganski (via logerrit)
 sal/rtl/string.cxx  |6 +-
 sal/rtl/strtmpl.hxx |  129 ++--
 sal/rtl/ustring.cxx |6 +-
 3 files changed, 22 insertions(+), 119 deletions(-)

New commits:
commit b927f636a0992b750c6a8de8bb8e9cc5e98a0e33
Author: Mike Kaganski 
AuthorDate: Thu Feb 24 11:37:11 2022 +0300
Commit: Mike Kaganski 
CommitDate: Thu Feb 24 15:24:33 2022 +0100

Deduplicate valueOf*Int*

Change-Id: Ib9da148c371f52ee8da2848581bb8a024f97de96
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130479
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index 3c8c01e98b52..86cfdb1672d9 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -514,19 +514,19 @@ sal_Int32 SAL_CALL rtl_str_valueOfChar(char* pStr, char 
c) SAL_THROW_EXTERN_C()
 sal_Int32 SAL_CALL rtl_str_valueOfInt32(char* pStr, sal_Int32 n, sal_Int16 
nRadix)
 SAL_THROW_EXTERN_C()
 {
-return rtl::str::valueOfInt32(pStr, n, nRadix);
+return rtl::str::valueOfInt(pStr, n, nRadix);
 }
 
 sal_Int32 SAL_CALL rtl_str_valueOfInt64(char* pStr, sal_Int64 n, sal_Int16 
nRadix)
 SAL_THROW_EXTERN_C()
 {
-return rtl::str::valueOfInt64(pStr, n, nRadix);
+return rtl::str::valueOfInt(pStr, n, nRadix);
 }
 
 sal_Int32 SAL_CALL rtl_str_valueOfUInt64(char* pStr, sal_uInt64 n, sal_Int16 
nRadix)
 SAL_THROW_EXTERN_C()
 {
-return rtl::str::valueOfUInt64(pStr, n, nRadix);
+return rtl::str::valueOfInt(pStr, n, nRadix);
 }
 
 sal_Bool SAL_CALL rtl_str_toBoolean(const char* pStr) SAL_THROW_EXTERN_C()
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index d0f8fbeb6d83..0cee38057074 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -623,85 +623,35 @@ sal_Int32 valueOfChar ( 
IMPL_RTL_STRCODE* pStr,
 
 /* --- */
 
-template 
-sal_Int32 valueOfInt32 ( IMPL_RTL_STRCODE* pStr,
- sal_Int32 n,
+template 
+sal_Int32 valueOfInt   ( IMPL_RTL_STRCODE* pStr,
+ T n,
  sal_Int16 nRadix )
 {
 assert(pStr);
 assert( nRadix >= RTL_STR_MIN_RADIX && nRadix <= RTL_STR_MAX_RADIX );
-charaBuf[RTL_STR_MAX_VALUEOFINT32];
+charaBuf[maxLen];
 char*   pBuf = aBuf;
 sal_Int32   nLen = 0;
-sal_uInt32  nValue;
+using uT = std::make_unsigned_t;
+uT nValue;
 
 /* Radix must be valid */
 if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) )
 nRadix = 10;
 
-/* is value negative */
-if ( n < 0 )
-{
-*pStr = '-';
-pStr++;
-nLen++;
-nValue = n == SAL_MIN_INT32 ? static_cast(n) : -n;
-}
-else
-nValue = n;
-
-/* create a recursive buffer with all values, except the last one */
-do
+if constexpr (std::is_signed_v)
 {
-char nDigit = static_cast(nValue % nRadix);
-nValue /= nRadix;
-if ( nDigit > 9 )
-*pBuf = (nDigit-10) + 'a';
+/* is value negative */
+if ( n < 0 )
+{
+*pStr = '-';
+pStr++;
+nLen++;
+nValue = n == std::numeric_limits::min() ? static_cast(n) : 
-n;
+}
 else
-*pBuf = (nDigit + '0' );
-pBuf++;
-}
-while ( nValue > 0 );
-
-/* copy the values in the right direction into the destination buffer */
-do
-{
-pBuf--;
-*pStr = *pBuf;
-pStr++;
-nLen++;
-}
-while ( pBuf != aBuf );
-*pStr = 0;
-
-return nLen;
-}
-
-/* --- */
-
-template 
-sal_Int32 valueOfInt64 ( IMPL_RTL_STRCODE* pStr,
- sal_Int64 n,
- sal_Int16 nRadix )
-{
-assert(pStr);
-assert( nRadix >= RTL_STR_MIN_RADIX && nRadix <= RTL_STR_MAX_RADIX );
-charaBuf[RTL_STR_MAX_VALUEOFINT64];
-char*   pBuf = aBuf;
-sal_Int32   nLen = 0;
-sal_uInt64  nValue;
-
-/* Radix must be valid */
-if ( (nRadix < RTL_STR_MIN_RADIX) || (nRadix > RTL_STR_MAX_RADIX) )
-nRadix = 10;
-
-/* is value negative */
-if ( n < 0 )
-{
-*pStr = '-';
-pStr++;
-nLen++;
-nValue = n == SAL_MIN_INT64 ? static_cast(n) : -n;
+nValue = n;
 }
 else
 nValue = n;
@@ -735,53 +685,6 @@ sal_Int32 valueOfInt64 ( 
IMPL_RTL_STRCODE* pStr,
 
 /* --- */
 
-template 
-sal_Int32 valueOfUInt64 ( IMPL_RTL_STRCODE* pStr,
-

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

2022-02-22 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |   56 
 1 file changed, 9 insertions(+), 47 deletions(-)

New commits:
commit 4f1ffd47812bb0507561e01a2ffca3b28b5c1412
Author: Mike Kaganski 
AuthorDate: Tue Feb 22 19:27:19 2022 +0100
Commit: Mike Kaganski 
CommitDate: Wed Feb 23 06:59:02 2022 +0100

Deduplicate newFromStr*

Change-Id: I01dd9d70affdfa048af6bd7455301e662290e662
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130370
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 54a1efd1a555..eec7010117d8 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -1091,29 +1091,16 @@ void new_WithLength( IMPL_RTL_STRINGDATA** ppThis, 
sal_Int32 nLen )
 
 /* --- */
 
+template 
+void newFromStr_WithLength(IMPL_RTL_STRINGDATA**, const 
STRCODE*, sal_Int32);
+
 template 
 void newFromString( IMPL_RTL_STRINGDATA** 
ppThis,
 const IMPL_RTL_STRINGDATA* 
pStr )
 {
-assert(ppThis);
 assert(pStr);
-IMPL_RTL_STRINGDATA* pOrg;
-
-if ( !pStr->length )
-{
-new_( ppThis );
-return;
-}
-
-pOrg = *ppThis;
-*ppThis = Alloc( pStr->length );
-OSL_ASSERT(*ppThis != nullptr);
-Copy( (*ppThis)->buffer, pStr->buffer, pStr->length );
-RTL_LOG_STRING_NEW( *ppThis );
 
-/* must be done last, if pStr == *ppThis */
-if ( pOrg )
-release( pOrg );
+newFromStr_WithLength(ppThis, pStr->buffer, pStr->length);
 }
 
 /* --- */
@@ -1122,10 +1109,6 @@ template 
 void newFromStr( IMPL_RTL_STRINGDATA** ppThis,
  const 
STRCODE* pCharStr )
 {
-assert(ppThis);
-IMPL_RTL_STRINGDATA*pOrg;
-sal_Int32   nLen;
-
 #if OSL_DEBUG_LEVEL > 0
 //TODO: For now, only abort in non-production debug builds; once all 
places that rely on the
 // undocumented newFromStr behavior of treating a null pCharStr like an 
empty string have been
@@ -1135,28 +1118,7 @@ void newFromStr( 
IMPL_RTL_STRINGDATA** ppThis,
 }
 #endif
 
-if ( pCharStr )
-{
-nLen = getLength( pCharStr );
-}
-else
-nLen = 0;
-
-if ( !nLen )
-{
-new_( ppThis );
-return;
-}
-
-pOrg = *ppThis;
-*ppThis = Alloc( nLen );
-OSL_ASSERT(*ppThis != nullptr);
-Copy( (*ppThis)->buffer, pCharStr, nLen );
-RTL_LOG_STRING_NEW( *ppThis );
-
-/* must be done last, if pCharStr == *ppThis */
-if ( pOrg )
-release( pOrg );
+newFromStr_WithLength(ppThis, pCharStr, pCharStr ? getLength(pCharStr) : 
0);
 }
 
 /* --- */
@@ -1167,9 +1129,6 @@ void newFromStr_WithLength
( IMPL_RTL_STRINGDATA*
 sal_Int32 nLen )
 {
 assert(ppThis);
-assert(pCharStr != nullptr || nLen == 0);
-assert(nLen >= 0);
-IMPL_RTL_STRINGDATA* pOrg;
 
 if ( nLen == 0 )
 {
@@ -1177,7 +1136,10 @@ void newFromStr_WithLength   
 ( IMPL_RTL_STRINGDATA*
 return;
 }
 
-pOrg = *ppThis;
+assert(nLen > 0);
+assert(pCharStr);
+
+IMPL_RTL_STRINGDATA* pOrg = *ppThis;
 *ppThis = Alloc( nLen );
 OSL_ASSERT(*ppThis != nullptr);
 Copy( (*ppThis)->buffer, pCharStr, nLen );


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

2022-02-22 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |   32 
 1 file changed, 4 insertions(+), 28 deletions(-)

New commits:
commit c95d2c8ef7a7d062536e225b843006bdf3d7d65b
Author: Mike Kaganski 
AuthorDate: Tue Feb 22 16:51:09 2022 +0100
Commit: Mike Kaganski 
CommitDate: Wed Feb 23 06:00:22 2022 +0100

Deduplicate shortenedCompare*_WithLength

Change-Id: Ie1ba2787036955976f43217e64af889a314a2e65
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130325
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 271f70bc2e37..54a1efd1a555 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -130,13 +130,8 @@ sal_Int32 shortenedCompare_WithLength  
   ( const IMPL_R
 sal_Int32 
nStr2Len,
 sal_Int32 
nShortenedLength )
 {
-assert(nStr1Len >= 0);
-assert(nStr2Len >= 0);
-assert(nShortenedLength >= 0);
-// take advantage of builtin optimisations
-std::basic_string_view aView1(pStr1, std::min(nStr1Len, 
nShortenedLength));
-std::basic_string_view aView2(pStr2, std::min(nStr2Len, 
nShortenedLength));
-return aView1.compare(aView2);
+return compare_WithLength(pStr1, std::min(nStr1Len, nShortenedLength),
+  pStr2, std::min(nStr2Len, nShortenedLength));
 }
 
 /* --- */
@@ -225,27 +220,8 @@ sal_Int32 shortenedCompareIgnoreAsciiCase_WithLength

sal_Int32 nStr2Len,

sal_Int32 nShortenedLength )
 {
-assert(nStr1Len >= 0);
-assert(nStr2Len >= 0);
-assert(nShortenedLength >= 0);
-const IMPL_RTL_STRCODE* pStr1End = pStr1 + nStr1Len;
-const IMPL_RTL_STRCODE* pStr2End = pStr2 + nStr2Len;
-while ( (nShortenedLength > 0) &&
-(pStr1 < pStr1End) && (pStr2 < pStr2End) )
-{
-sal_Int32 nRet = rtl::compareIgnoreAsciiCase(
-IMPL_RTL_USTRCODE(*pStr1), IMPL_RTL_USTRCODE(*pStr2));
-if ( nRet != 0 )
-return nRet;
-
-nShortenedLength--;
-pStr1++;
-pStr2++;
-}
-
-if ( nShortenedLength <= 0 )
-return 0;
-return nStr1Len - nStr2Len;
+return compareIgnoreAsciiCase_WithLength(pStr1, std::min(nStr1Len, 
nShortenedLength),
+ pStr2, std::min(nStr2Len, 
nShortenedLength));
 }
 
 /* --- */


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

2022-02-22 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |   13 ++---
 1 file changed, 2 insertions(+), 11 deletions(-)

New commits:
commit d5cdedbf2241f9b09bbb5698f37c89f9510b
Author: Mike Kaganski 
AuthorDate: Tue Feb 22 19:13:42 2022 +0100
Commit: Mike Kaganski 
CommitDate: Wed Feb 23 05:59:36 2022 +0100

Drop obsolete code

Change-Id: Ia362d00c7637370f4dc967763892269909f06578
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130369
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index d3da51a60c48..271f70bc2e37 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -867,17 +867,8 @@ T toInt_WithLength 
 ( const IMPL_RTL
 sal_Int16 nMod;
 if ( bNeg )
 {
-nDiv = std::numeric_limits::min() / nRadix;
-nMod = std::numeric_limits::min() % nRadix;
-// Cater for C++03 implementations that round the quotient down
-// instead of truncating towards zero as mandated by C++11:
-if ( nMod > 0 )
-{
---nDiv;
-nMod -= nRadix;
-}
-nDiv = -nDiv;
-nMod = -nMod;
+nDiv = -(std::numeric_limits::min() / nRadix);
+nMod = -(std::numeric_limits::min() % nRadix);
 }
 else
 {


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

2022-02-22 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |   63 ++--
 sal/rtl/ustring.cxx |2 -
 2 files changed, 9 insertions(+), 56 deletions(-)

New commits:
commit 52f692937d479e80182ab01e195359a2df064c30
Author: Mike Kaganski 
AuthorDate: Tue Feb 22 19:39:01 2022 +0100
Commit: Mike Kaganski 
CommitDate: Tue Feb 22 22:23:01 2022 +0100

Deduplicate newReplace

Optimize some places for single character case for that

Change-Id: I99b2abd2fd3c89a98ed1d84f7b70ed4def83da2d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130371
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 51dd4d0e191b..d3da51a60c48 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -416,8 +416,7 @@ sal_Int32 indexOfStr_WithLength 
( const IMPL_RTL_STR
 // take advantage of builtin optimisations
 using my_string_view = std::basic_string_view;
 my_string_view v(pStr, nStrLen);
-my_string_view needle(pSubStr, nSubLen);
-typename my_string_view::size_type idx = v.find(needle);
+auto idx = nSubLen == 1 ? v.find(*pSubStr) : v.find(pSubStr, 0, nSubLen);
 return idx == my_string_view::npos ? -1 : idx;
 }
 
@@ -1543,65 +1542,17 @@ void newReplaceStrAt( 
IMPL_RTL_STRINGDATA** ppTh
 
 /* --- */
 
+template 
+void newReplaceAllFromIndex(S**, S*, CharTypeFrom const*, sal_Int32, 
CharTypeTo const*, sal_Int32,
+sal_Int32);
+
 template 
 void newReplace( IMPL_RTL_STRINGDATA** ppThis,
  IMPL_RTL_STRINGDATA* pStr,
  STRCODE 
cOld,
  STRCODE 
cNew )
 {
-assert(ppThis);
-assert(pStr);
-IMPL_RTL_STRINGDATA*pOrg= *ppThis;
-boolbChanged= false;
-sal_Int32   nLen= pStr->length;
-const auto* pCharStr= pStr->buffer;
-
-while ( nLen > 0 )
-{
-if ( *pCharStr == cOld )
-{
-/* Copy String */
-auto* pNewCharStr = NewCopy( ppThis, pStr, pCharStr-pStr->buffer );
-
-/* replace/copy rest of the string */
-if ( pNewCharStr )
-{
-*pNewCharStr = cNew;
-pNewCharStr++;
-pCharStr++;
-nLen--;
-
-while ( nLen > 0 )
-{
-if ( *pCharStr == cOld )
-*pNewCharStr = cNew;
-else
-*pNewCharStr = *pCharStr;
-
-pNewCharStr++;
-pCharStr++;
-nLen--;
-}
-}
-
-bChanged = true;
-break;
-}
-
-pCharStr++;
-nLen--;
-}
-
-if ( !bChanged )
-{
-*ppThis = pStr;
-acquire( pStr );
-}
-
-RTL_LOG_STRING_NEW( *ppThis );
-/* must be done last, if pStr == *ppThis */
-if ( pOrg )
-release( pOrg );
+return newReplaceAllFromIndex(ppThis, pStr, , 1, , 1, 0);
 }
 
 /* --- */
@@ -1877,6 +1828,8 @@ void newReplaceAllFromIndex(S** s, S* s1, CharTypeFrom 
const* from, sal_Int32 fr
 }
 else
 assign(s, s1);
+
+RTL_LOG_STRING_NEW(*s);
 }
 
 }
diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx
index 5a4f16b88fcb..227894742b4e 100644
--- a/sal/rtl/ustring.cxx
+++ b/sal/rtl/ustring.cxx
@@ -89,7 +89,7 @@ sal_Int32 rtl_ustr_indexOfAscii_WithLength(
 break;
 }
 /* now it is worth trying a full match */
-if (rtl_ustr_asciil_reverseEquals_WithLength(cursor, subStr, 
subLen))
+if (subLen == 1 || 
rtl_ustr_asciil_reverseEquals_WithLength(cursor, subStr, subLen))
 {
 return cursor - str;
 }


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

2022-02-17 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx |   20 
 1 file changed, 12 insertions(+), 8 deletions(-)

New commits:
commit cb56edb177f4db5b9cc4d140543c4b11d41ef1b0
Author: Mike Kaganski 
AuthorDate: Fri Feb 18 01:43:32 2022 +0300
Commit: Mike Kaganski 
CommitDate: Fri Feb 18 05:04:39 2022 +0100

Define policy to optimize to_decimal to our use case

We don't need it to strip trailing zeroes (we do that ourselves anyway);
also we don't need it to handle negatives.

Makes doubleToString ~5% faster in my testing.

Change-Id: Ie3c4e3fec1899364af54fda3f7141678a95359d0
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130120
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index edb894cb7820..b9a0a3348757 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -300,6 +300,9 @@ void doubleToString(typename T::String ** pResult,
 return;
 }
 
+// sign adjustment, instead of testing for fValue<0.0 this will also fetch 
-0.0
+bool bSign = std::signbit(fValue);
+
 if (std::isinf(fValue))
 {
 // #i112652# XMLSchema-2
@@ -311,7 +314,7 @@ void doubleToString(typename T::String ** pResult,
 nResultOffset = 0;
 }
 
-if (std::signbit(fValue))
+if (bSign)
 T::appendAscii(pResult, pResultCapacity, ,
RTL_CONSTASCII_STRINGPARAM("-"));
 
@@ -321,17 +324,18 @@ void doubleToString(typename T::String ** pResult,
 return;
 }
 
-decltype(jkj::dragonbox::to_decimal(fValue)) aParts{};
+if (bSign)
+fValue = -fValue;
+
+decltype(jkj::dragonbox::to_decimal(fValue, 
jkj::dragonbox::policy::sign::ignore,
+
jkj::dragonbox::policy::trailing_zero::ignore)) aParts{};
 if (fValue) // to_decimal is documented to only handle non-zero finite 
numbers
-aParts = jkj::dragonbox::to_decimal(fValue);
-else
-aParts.is_negative = std::signbit(fValue); // Handle -0.0
+aParts = jkj::dragonbox::to_decimal(fValue, 
jkj::dragonbox::policy::sign::ignore,
+
jkj::dragonbox::policy::trailing_zero::ignore);
 
 int nOrigDigits = decimalDigits(aParts.significand);
 int nExp = nOrigDigits + aParts.exponent - 1;
 int nRoundDigits = 15;
-if (aParts.is_negative)
-fValue = -fValue;
 
 // Unfortunately the old rounding below writes 1.79769313486232e+308 for
 // DBL_MAX and 4 subsequent nextafter(...,0).
@@ -461,7 +465,7 @@ void doubleToString(typename T::String ** pResult,
 assert(nBuf <= 1024);
 typename T::Char* pBuf = static_cast(alloca(nBuf * 
sizeof(typename T::Char)));
 typename T::Char * p = pBuf;
-if (aParts.is_negative)
+if (bSign)
 *p++ = '-';
 
 bool bHasDec = false;


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

2022-02-17 Thread Mike Kaganski (via logerrit)
 sal/rtl/string.cxx  |   14 +-
 sal/rtl/strtmpl.hxx |2 +-
 sal/rtl/ustring.cxx |   38 ++
 3 files changed, 4 insertions(+), 50 deletions(-)

New commits:
commit 01645b021c3609f86b8ebca100f54006f467573f
Author: Mike Kaganski 
AuthorDate: Thu Feb 17 08:50:12 2022 +0100
Commit: Mike Kaganski 
CommitDate: Thu Feb 17 21:19:25 2022 +0100

Deduplicate some code

Change-Id: Ib05624739cce31b08fc409f238d447ed8d99bad9
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/130016
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index a2020561a7c7..3c8c01e98b52 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -339,19 +339,7 @@ void rtl_string_newReplaceFirst(
 if (str->length - fromLength > SAL_MAX_INT32 - toLength) {
 std::abort();
 }
-sal_Int32 n = str->length - fromLength + toLength;
-rtl_string_acquire(str); // in case *newStr == str
-rtl_string_new_WithLength(newStr, n);
-if (n != 0) {
-(*newStr)->length = n;
-assert(i >= 0 && i < str->length);
-memcpy((*newStr)->buffer, str->buffer, i);
-memcpy((*newStr)->buffer + i, to, toLength);
-memcpy(
-(*newStr)->buffer + i + toLength, str->buffer + i + fromLength,
-str->length - i - fromLength);
-}
-rtl_string_release(str);
+rtl::str::newReplaceStrAt(newStr, str, i, fromLength, to, toLength);
 }
 *index = i;
 }
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 4f04cc404ee5..51dd4d0e191b 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -1481,7 +1481,7 @@ void newReplaceStrAt( 
IMPL_RTL_STRINGDATA** ppTh
 assert(nIndex >= 0 && nIndex <= pStr->length);
 assert(nCount >= 0);
 assert(nCount <= pStr->length - nIndex);
-assert(pNewSubStr);
+assert(pNewSubStr != nullptr || nNewSubStrLen == 0);
 assert(nNewSubStrLen >= 0);
 /* Append? */
 if ( nIndex >= pStr->length )
diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx
index 70048db58f6e..5a4f16b88fcb 100644
--- a/sal/rtl/ustring.cxx
+++ b/sal/rtl/ustring.cxx
@@ -1213,24 +1213,7 @@ void rtl_uString_newReplaceFirstAsciiLUtf16L(
 rtl_uString_release(*newStr);
 *newStr = nullptr;
 } else {
-sal_Int32 n = str->length - fromLength + toLength;
-rtl_uString_acquire(str); // in case *newStr == str
-rtl_uString_new_WithLength(newStr, n);
-if (n != 0 && /*TODO:*/ *newStr != nullptr) {
-(*newStr)->length = n;
-assert(i >= 0 && i < str->length);
-memcpy(
-(*newStr)->buffer, str->buffer, i * sizeof (sal_Unicode));
-if (toLength != 0) {
-memcpy(
-(*newStr)->buffer + i, to, toLength * sizeof 
(sal_Unicode));
-}
-memcpy(
-(*newStr)->buffer + i + toLength,
-str->buffer + i + fromLength,
-(str->length - i - fromLength) * sizeof (sal_Unicode));
-}
-rtl_uString_release(str);
+rtl::str::newReplaceStrAt(newStr, str, i, fromLength, to, 
toLength);
 }
 }
 *index = i;
@@ -1306,24 +1289,7 @@ void rtl_uString_newReplaceFirstUtf16LUtf16L(
 rtl_uString_release(*newStr);
 *newStr = nullptr;
 } else {
-sal_Int32 n = str->length - fromLength + toLength;
-rtl_uString_acquire(str); // in case *newStr == str
-rtl_uString_new_WithLength(newStr, n);
-if (n != 0 && /*TODO:*/ *newStr != nullptr) {
-(*newStr)->length = n;
-assert(i >= 0 && i < str->length);
-memcpy(
-(*newStr)->buffer, str->buffer, i * sizeof (sal_Unicode));
-if (toLength != 0) {
-memcpy(
-(*newStr)->buffer + i, to, toLength * sizeof 
(sal_Unicode));
-}
-memcpy(
-(*newStr)->buffer + i + toLength,
-str->buffer + i + fromLength,
-(str->length - i - fromLength) * sizeof (sal_Unicode));
-}
-rtl_uString_release(str);
+rtl::str::newReplaceStrAt(newStr, str, i, fromLength, to, 
toLength);
 }
 }
 *index = i;


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

2022-02-16 Thread Mike Kaganski (via logerrit)
 sal/rtl/string.cxx  |9 --
 sal/rtl/strtmpl.hxx |   71 
 sal/rtl/ustring.cxx |   41 ++
 3 files changed, 76 insertions(+), 45 deletions(-)

New commits:
commit 4e4a01302a140d75a49055821b3197a2eda81db5
Author: Mike Kaganski 
AuthorDate: Wed Feb 16 09:45:40 2022 +0100
Commit: Mike Kaganski 
CommitDate: Wed Feb 16 18:15:27 2022 +0100

Related: tdf#147421: optimize O(U)String's replaceAll*

... and underlying low-level C functions.

Change-Id: I78c2dd75e272e6d3cdd2cfae007ca0a0ec1635e1
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129942
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index d67246dc159c..a2020561a7c7 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -361,14 +361,7 @@ void rtl_string_newReplaceAll(
 sal_Int32 fromLength, char const * to, sal_Int32 toLength)
 SAL_THROW_EXTERN_C()
 {
-rtl_string_assign(newStr, str);
-for (sal_Int32 i = 0;; i += toLength) {
-rtl_string_newReplaceFirst(
-newStr, *newStr, from, fromLength, to, toLength, );
-if (i == -1) {
-break;
-}
-}
+rtl::str::newReplaceAllFromIndex(newStr, str, from, fromLength, to, 
toLength, 0);
 }
 
 sal_Int32 SAL_CALL rtl_str_getLength(const char* pStr) SAL_THROW_EXTERN_C()
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 7a909bb5efe3..4f04cc404ee5 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -35,6 +35,9 @@
 #include 
 #include 
 #include 
+#include 
+#include 
+#include 
 
 namespace rtl::str
 {
@@ -1808,6 +1811,74 @@ sal_Int32 getToken( 
IMPL_RTL_STRINGDATA** ppThis
 }
 }
 
+namespace detail
+{
+template 
+sal_Int32 indexOf(const CharType1* s, sal_Int32 len, const CharType2* subStr, 
sal_Int32 subLen)
+{
+if constexpr (std::is_same_v)
+return indexOfStr_WithLength(s, len, subStr, subLen);
+else if constexpr (std::is_same_v && 
std::is_same_v)
+return rtl_ustr_indexOfAscii_WithLength(s, len, subStr, subLen);
+}
+
+template 
+void append(S** s, sal_Int32* capacity, const CharType1* s1, sal_Int32 len)
+{
+if constexpr (std::is_same_v && std::is_same_v)
+return rtl_uStringbuffer_insert(s, capacity, (*s)->length, s1, len);
+else if constexpr (std::is_same_v && 
std::is_same_v)
+return rtl_uStringbuffer_insert_ascii(s, capacity, (*s)->length, s1, 
len);
+else if constexpr (std::is_same_v && 
std::is_same_v)
+return rtl_stringbuffer_insert(s, capacity, (*s)->length, s1, len);
+}
+}
+
+template 
+void newReplaceAllFromIndex(S** s, S* s1, CharTypeFrom const* from, sal_Int32 
fromLength,
+CharTypeTo const* to, sal_Int32 toLength, 
sal_Int32 fromIndex)
+{
+assert(s != nullptr);
+assert(s1 != nullptr);
+assert(fromLength >= 0);
+assert(from != nullptr || fromLength == 0);
+assert(toLength >= 0);
+assert(to != nullptr || toLength == 0);
+assert(fromIndex >= 0 && fromIndex <= s1->length);
+sal_Int32 i = detail::indexOf(s1->buffer + fromIndex, s1->length - 
fromIndex, from, fromLength);
+if (i >= 0)
+{
+if (s1->length - fromLength > SAL_MAX_INT32 - toLength)
+std::abort();
+acquire(s1); // in case *s == s1
+sal_Int32 nCapacity = s1->length + (toLength - fromLength);
+if (fromLength < toLength)
+{
+// Pre-allocate up to 16 replacements more
+const sal_Int32 nMaxMoreFinds = (s1->length - fromIndex - i - 
fromLength) / fromLength;
+const sal_Int32 nIncrease = toLength - fromLength;
+const sal_Int32 nMoreReplacements = std::min(
+{ nMaxMoreFinds, (SAL_MAX_INT32 - nCapacity) / nIncrease, 
sal_Int32(16) });
+nCapacity += nMoreReplacements * nIncrease;
+}
+new_WithLength(s, nCapacity);
+i += fromIndex;
+fromIndex = 0;
+do
+{
+detail::append(s, , s1->buffer + fromIndex, i);
+detail::append(s, , to, toLength);
+fromIndex += i + fromLength;
+i = detail::indexOf(s1->buffer + fromIndex, s1->length - 
fromIndex, from, fromLength);
+} while (i >= 0);
+// the rest
+detail::append(s, , s1->buffer + fromIndex, s1->length - 
fromIndex);
+release(s1);
+}
+else
+assign(s, s1);
+}
+
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx
index c550c99c419e..70048db58f6e 100644
--- a/sal/rtl/ustring.cxx
+++ b/sal/rtl/ustring.cxx
@@ -1367,15 +1367,7 @@ void rtl_uString_newReplaceAllAsciiLAsciiL(
 sal_Int32 fromLength, char const * to, sal_Int32 toLength)
 SAL_THROW_EXTERN_C()
 {
-assert(toLength >= 0);
-rtl_uString_assign(newStr, str);
-for (sal_Int32 i = 0;; i += toLength) {
-

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

2022-02-16 Thread Mike Kaganski (via logerrit)
 sal/rtl/ustring.cxx |  135 +++-
 1 file changed, 10 insertions(+), 125 deletions(-)

New commits:
commit acdf524e12d302b4bf9367a8b25408746737e123
Author: Mike Kaganski 
AuthorDate: Wed Feb 16 10:48:56 2022 +0100
Commit: Mike Kaganski 
CommitDate: Wed Feb 16 18:14:57 2022 +0100

De-duplicate some code

Change-Id: I355fd800e09cef0776408375bd4d6053048d7eb8
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129943
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx
index 64f13cabc7ee..c550c99c419e 100644
--- a/sal/rtl/ustring.cxx
+++ b/sal/rtl/ustring.cxx
@@ -1120,41 +1120,10 @@ void rtl_uString_newReplaceFirst(
 rtl_uString ** newStr, rtl_uString * str, rtl_uString const * from,
 rtl_uString const * to, sal_Int32 * index) SAL_THROW_EXTERN_C()
 {
-assert(str != nullptr);
-assert(index != nullptr);
-assert(*index >= 0 && *index <= str->length);
 assert(from != nullptr);
 assert(to != nullptr);
-sal_Int32 i = rtl_ustr_indexOfStr_WithLength(
-str->buffer + *index, str->length - *index, from->buffer, 
from->length);
-if (i == -1) {
-rtl_uString_assign(newStr, str);
-} else {
-assert(i <= str->length - *index);
-i += *index;
-assert(from->length <= str->length);
-if (str->length - from->length > SAL_MAX_INT32 - to->length) {
-std::abort();
-}
-sal_Int32 n = str->length - from->length + to->length;
-rtl_uString_acquire(str); // in case *newStr == str
-rtl_uString_new_WithLength(newStr, n);
-if (n != 0) {
-(*newStr)->length = n;
-assert(i >= 0 && i < str->length);
-memcpy(
-(*newStr)->buffer, str->buffer, i * sizeof (sal_Unicode));
-memcpy(
-(*newStr)->buffer + i, to->buffer,
-to->length * sizeof (sal_Unicode));
-memcpy(
-(*newStr)->buffer + i + to->length,
-str->buffer + i + from->length,
-(str->length - i - from->length) * sizeof (sal_Unicode));
-}
-rtl_uString_release(str);
-}
-*index = i;
+rtl_uString_newReplaceFirstUtf16LUtf16L(newStr, str, from->buffer, 
from->length, to->buffer,
+to->length, index);
 }
 
 void rtl_uString_newReplaceFirstAsciiL(
@@ -1162,41 +1131,9 @@ void rtl_uString_newReplaceFirstAsciiL(
 sal_Int32 fromLength, rtl_uString const * to, sal_Int32 * index)
 SAL_THROW_EXTERN_C()
 {
-assert(str != nullptr);
-assert(index != nullptr);
-assert(*index >= 0 && *index <= str->length);
-assert(fromLength >= 0);
 assert(to != nullptr);
-sal_Int32 i = rtl_ustr_indexOfAscii_WithLength(
-str->buffer + *index, str->length - *index, from, fromLength);
-if (i == -1) {
-rtl_uString_assign(newStr, str);
-} else {
-assert(i <= str->length - *index);
-i += *index;
-assert(fromLength <= str->length);
-if (str->length - fromLength > SAL_MAX_INT32 - to->length) {
-std::abort();
-}
-sal_Int32 n = str->length - fromLength + to->length;
-rtl_uString_acquire(str); // in case *newStr == str
-rtl_uString_new_WithLength(newStr, n);
-if (n != 0) {
-(*newStr)->length = n;
-assert(i >= 0 && i < str->length);
-memcpy(
-(*newStr)->buffer, str->buffer, i * sizeof (sal_Unicode));
-memcpy(
-(*newStr)->buffer + i, to->buffer,
-to->length * sizeof (sal_Unicode));
-memcpy(
-(*newStr)->buffer + i + to->length,
-str->buffer + i + fromLength,
-(str->length - i - fromLength) * sizeof (sal_Unicode));
-}
-rtl_uString_release(str);
-}
-*index = i;
+rtl_uString_newReplaceFirstAsciiLUtf16L(newStr, str, from, fromLength, 
to->buffer, to->length,
+index);
 }
 
 void rtl_uString_newReplaceFirstToAsciiL(
@@ -1204,42 +1141,9 @@ void rtl_uString_newReplaceFirstToAsciiL(
 char const * to, sal_Int32 toLength, sal_Int32 * index)
 SAL_THROW_EXTERN_C()
 {
-assert(str != nullptr);
-assert(index != nullptr);
-assert(*index >= 0 && *index <= str->length);
 assert(from != nullptr);
-assert(toLength >= 0);
-sal_Int32 i = rtl_ustr_indexOfStr_WithLength(
-str->buffer + *index, str->length - *index, from->buffer, 
from->length);
-if (i == -1) {
-rtl_uString_assign(newStr, str);
-} else {
-assert(i <= str->length - *index);
-i += *index;
-assert(from->length <= str->length);
-if (str->length - from->length > SAL_MAX_INT32 - toLength) {
-std::abort();
-}
-sal_Int32 n = str->length - from->length 

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

2021-11-17 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx |   10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

New commits:
commit b254ecc2bbc716343fccf303f58dfb034b487bca
Author: Mike Kaganski 
AuthorDate: Wed Nov 17 16:25:55 2021 +0200
Commit: Mike Kaganski 
CommitDate: Wed Nov 17 19:28:05 2021 +0100

Use C++17 hex-exponent floating point literals

... instead of some calculated/hardcoded decimal literals.
The new syntax represents the power of 2 exponent directly.

Change-Id: I826bda6e06ec77f0706bb48abe2934f7289fddc7
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/125359
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 6acf3a943fb2..bec83c380b32 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -1130,7 +1130,7 @@ double SAL_CALL rtl_math_round(double fValue, int 
nDecPlaces,
 // make sense, do not even try to multiply/divide and introduce inaccuracy.
 // For same reasons, do not attempt to round integers to decimals.
 if (nDecPlaces >= 0
-&& (fValue >= (static_cast(1) << 52)
+&& (fValue >= 0x1p52
 || isRepresentableInteger(fValue)))
 return fOrigValue;
 
@@ -1181,7 +1181,7 @@ double SAL_CALL rtl_math_round(double fValue, int 
nDecPlaces,
 // Round only if not already in distance precision gaps of integers, where
 // for [2^52,2^53) adding 0.5 would even yield the next representable
 // integer.
-if (fValue < (static_cast(1) << 52))
+if (fValue < 0x1p52)
 {
 switch ( eMode )
 {
@@ -1276,7 +1276,7 @@ double SAL_CALL rtl_math_pow10Exp(double fValue, int 
nExp) SAL_THROW_EXTERN_C()
 
 double SAL_CALL rtl_math_approxValue( double fValue ) SAL_THROW_EXTERN_C()
 {
-const double fBigInt = 219902322.0; // 2^41 -> only 11 bits left for 
fractional part, fine as decimal
+const double fBigInt = 0x1p41; // 2^41 -> only 11 bits left for fractional 
part, fine as decimal
 if (fValue == 0.0 || fValue == HUGE_VAL || !std::isfinite( fValue) || 
fValue > fBigInt)
 {
 // We don't handle these conditions.  Bail out.
@@ -1325,8 +1325,8 @@ double SAL_CALL rtl_math_approxValue( double fValue ) 
SAL_THROW_EXTERN_C()
 
 bool SAL_CALL rtl_math_approxEqual(double a, double b) SAL_THROW_EXTERN_C()
 {
-static const double e48 = 1.0 / (16777216.0 * 16777216.0);
-static const double e44 = e48 * 16.0;
+static const double e48 = 0x1p-48;
+static const double e44 = 0x1p-44;
 
 if (a == b)
 return true;


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

2021-11-14 Thread Noel Grandin (via logerrit)
 sal/rtl/alloc_fini.cxx |   29 ++---
 sal/rtl/bootstrap.cxx  |   10 ++
 2 files changed, 24 insertions(+), 15 deletions(-)

New commits:
commit 864cbd8b2216c043db42351beac3538b4d51adcd
Author: Noel Grandin 
AuthorDate: Sun Nov 14 16:37:41 2021 +0200
Commit: Noel Grandin 
CommitDate: Sun Nov 14 21:05:11 2021 +0100

rtl::Static->thread-safe static in sal

Change-Id: I04f8a05aaa078642ee5e55c777b9b259c089695b
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/125197
Tested-by: Jenkins
Reviewed-by: Noel Grandin 

diff --git a/sal/rtl/alloc_fini.cxx b/sal/rtl/alloc_fini.cxx
index 3745e900c86d..ba798452dedf 100644
--- a/sal/rtl/alloc_fini.cxx
+++ b/sal/rtl/alloc_fini.cxx
@@ -17,8 +17,6 @@
  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
  */
 
-#include 
-
 #include 
 
 namespace
@@ -34,13 +32,16 @@ namespace
 rtl_cache_fini();
 }
 };
-class theCacheSingleton
-: public rtl::Static{};
+rtlCacheSingleton& theCacheSingleton()
+{
+static rtlCacheSingleton SINGLETON;
+return SINGLETON;
+}
 }
 
 void ensureCacheSingleton()
 {
-theCacheSingleton::get();
+theCacheSingleton();
 }
 
 namespace
@@ -56,13 +57,16 @@ namespace
 rtl_arena_fini();
 }
 };
-class theArenaSingleton
-: public rtl::Static{};
+rtlArenaSingleton& theArenaSingleton()
+{
+static rtlArenaSingleton SINGLETON;
+return SINGLETON;
+}
 }
 
 void ensureArenaSingleton()
 {
-theArenaSingleton::get();
+theArenaSingleton();
 }
 
 namespace
@@ -78,13 +82,16 @@ namespace
 rtl_locale_fini();
 }
 };
-class theLocaleSingleton
-: public rtl::Static{};
+rtlLocaleSingleton& theLocaleSingleton()
+{
+static rtlLocaleSingleton SINGLETON;
+return SINGLETON;
+}
 }
 
 void ensureLocaleSingleton()
 {
-theLocaleSingleton::get();
+theLocaleSingleton();
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sal/rtl/bootstrap.cxx b/sal/rtl/bootstrap.cxx
index 9feadf874e54..e651a8436363 100644
--- a/sal/rtl/bootstrap.cxx
+++ b/sal/rtl/bootstrap.cxx
@@ -33,7 +33,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -407,8 +406,11 @@ struct FundamentalIniData
 FundamentalIniData& operator=(const FundamentalIniData&) = delete;
 };
 
-struct FundamentalIni: public rtl::Static< FundamentalIniData, FundamentalIni >
-{};
+FundamentalIniData& FundamentalIni()
+{
+static FundamentalIniData SINGLETON;
+return SINGLETON;
+}
 
 }
 
@@ -511,7 +513,7 @@ bool Bootstrap_Impl::getValue(
 
 if (mode == LOOKUP_MODE_NORMAL)
 {
-FundamentalIniData const & d = FundamentalIni::get();
+FundamentalIniData const & d = FundamentalIni();
 Bootstrap_Impl const * b = static_cast(d.ini);
 if (b != nullptr && b != this && b->getDirectValue(key, value, mode, 
requestStack))
 return true;


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

2021-10-18 Thread Emanuel Schorsch (via logerrit)
 sal/rtl/bootstrap.cxx |8 +++-
 1 file changed, 3 insertions(+), 5 deletions(-)

New commits:
commit 191e5fc227e40d18a1fe4563ed145517117596ea
Author: Emanuel Schorsch 
AuthorDate: Sun Oct 17 18:31:30 2021 -0700
Commit: Stephan Bergmann 
CommitDate: Mon Oct 18 09:10:46 2021 +0200

Noop unify style for branches

Change-Id: Ifa96b7f273f7c154fdd267efba7271765ff7ae45
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/123728
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann 

diff --git a/sal/rtl/bootstrap.cxx b/sal/rtl/bootstrap.cxx
index fc551c8ce933..9feadf874e54 100644
--- a/sal/rtl/bootstrap.cxx
+++ b/sal/rtl/bootstrap.cxx
@@ -919,11 +919,9 @@ OUString expandMacros(
 }
 else if (n == 2)
 {
-buf.append(
-lookup(
-static_cast< Bootstrap_Impl * >(
-rtl::Bootstrap(seg[0]).getHandle()),
-mode, false, seg[1], requestStack));
+rtl::Bootstrap b(seg[0]);
+Bootstrap_Impl * f = static_cast< Bootstrap_Impl * 
>(b.getHandle());
+buf.append(lookup(f, mode, false, seg[1], requestStack));
 }
 else if (n == 3 && seg[0] == ".override")
 {


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

2021-10-15 Thread Emanuel Schorsch (via logerrit)
 sal/rtl/bootstrap.cxx |   61 +++---
 1 file changed, 29 insertions(+), 32 deletions(-)

New commits:
commit 88375fd36899d21d3309cf8333712e02a87d3a91
Author: Emanuel Schorsch 
AuthorDate: Sun Oct 3 17:43:39 2021 -0700
Commit: Stephan Bergmann 
CommitDate: Fri Oct 15 09:59:16 2021 +0200

NoOp refactor to make elif clearer

Change-Id: I05be42fde3f2b66d20c337583d8f8f92143bcb58
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/123032
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann 

diff --git a/sal/rtl/bootstrap.cxx b/sal/rtl/bootstrap.cxx
index 4f7a827fd77b..fc551c8ce933 100644
--- a/sal/rtl/bootstrap.cxx
+++ b/sal/rtl/bootstrap.cxx
@@ -905,10 +905,26 @@ OUString expandMacros(
 seg[j] = expandMacros(file, seg[j], mode, requestStack);
 }
 
+if (n == 3 && seg[0] != ".override" && seg[1].isEmpty())
+{
+// For backward compatibility, treat ${file::key} the
+// same as just ${file:key}:
+seg[1] = seg[2];
+n = 2;
+}
+
 if (n == 1)
 {
 buf.append(lookup(file, mode, false, seg[0], 
requestStack));
 }
+else if (n == 2)
+{
+buf.append(
+lookup(
+static_cast< Bootstrap_Impl * >(
+rtl::Bootstrap(seg[0]).getHandle()),
+mode, false, seg[1], requestStack));
+}
 else if (n == 3 && seg[0] == ".override")
 {
 rtl::Bootstrap b(seg[1]);
@@ -917,38 +933,19 @@ OUString expandMacros(
 }
 else
 {
-if (n == 3 && seg[1].isEmpty())
-{
-// For backward compatibility, treat ${file::key} the
-// same as just ${file:key}:
-seg[1] = seg[2];
-n = 2;
-}
-
-if (n == 2)
-{
-buf.append(
-lookup(
-static_cast< Bootstrap_Impl * >(
-rtl::Bootstrap(seg[0]).getHandle()),
-mode, false, seg[1], requestStack));
-}
-else
-{
-// Going through osl::Profile, this code erroneously
-// does not recursively expand macros in the resulting
-// replacement text (and if it did, it would fail to
-// detect cycles that pass through here):
-buf.append(
-OStringToOUString(
-osl::Profile(seg[0]).readString(
-OUStringToOString(
-seg[1], RTL_TEXTENCODING_UTF8),
-OUStringToOString(
-seg[2], RTL_TEXTENCODING_UTF8),
-OString()),
-RTL_TEXTENCODING_UTF8));
-}
+// Going through osl::Profile, this code erroneously
+// does not recursively expand macros in the resulting
+// replacement text (and if it did, it would fail to
+// detect cycles that pass through here):
+buf.append(
+OStringToOUString(
+osl::Profile(seg[0]).readString(
+OUStringToOString(
+seg[1], RTL_TEXTENCODING_UTF8),
+OUStringToOString(
+seg[2], RTL_TEXTENCODING_UTF8),
+OString()),
+RTL_TEXTENCODING_UTF8));
 }
 }
 else


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

2021-10-05 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx |   70 +++
 1 file changed, 60 insertions(+), 10 deletions(-)

New commits:
commit 7e558cd5b90f45c67ffc4d03f1c54564aff54bbb
Author: Mike Kaganski 
AuthorDate: Tue Oct 5 19:09:58 2021 +0200
Commit: Mike Kaganski 
CommitDate: Tue Oct 5 21:19:22 2021 +0200

Optimize the whole range of getN10Exp over IEEE 754 doubles

Needs 5056 bytes of pre-calculated data.

Change-Id: I138d9dc80c176f675a6854fe906e235c98efcbc0
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122947
Tested-by: Jenkins
Reviewed-by: Noel Grandin 
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 4d590089..6acf3a943fb2 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -36,20 +36,70 @@
 
 #include 
 
-constexpr int n10Count = 16;
-constexpr double n10s[n10Count*2+1] = {
-1e-16, 1e-15, 1e-14, 1e-13, 1e-12, 1e-11, 1e-10, 1e-9,
-1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1e0,
-1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8,
-1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16 ,
+constexpr int minExp = -323, maxExp = 308;
+constexpr double n10s[] = {
+1e-323, 1e-322, 1e-321, 1e-320, 1e-319, 1e-318, 1e-317, 1e-316, 1e-315, 
1e-314, 1e-313, 1e-312,
+1e-311, 1e-310, 1e-309, 1e-308, 1e-307, 1e-306, 1e-305, 1e-304, 1e-303, 
1e-302, 1e-301, 1e-300,
+1e-299, 1e-298, 1e-297, 1e-296, 1e-295, 1e-294, 1e-293, 1e-292, 1e-291, 
1e-290, 1e-289, 1e-288,
+1e-287, 1e-286, 1e-285, 1e-284, 1e-283, 1e-282, 1e-281, 1e-280, 1e-279, 
1e-278, 1e-277, 1e-276,
+1e-275, 1e-274, 1e-273, 1e-272, 1e-271, 1e-270, 1e-269, 1e-268, 1e-267, 
1e-266, 1e-265, 1e-264,
+1e-263, 1e-262, 1e-261, 1e-260, 1e-259, 1e-258, 1e-257, 1e-256, 1e-255, 
1e-254, 1e-253, 1e-252,
+1e-251, 1e-250, 1e-249, 1e-248, 1e-247, 1e-246, 1e-245, 1e-244, 1e-243, 
1e-242, 1e-241, 1e-240,
+1e-239, 1e-238, 1e-237, 1e-236, 1e-235, 1e-234, 1e-233, 1e-232, 1e-231, 
1e-230, 1e-229, 1e-228,
+1e-227, 1e-226, 1e-225, 1e-224, 1e-223, 1e-222, 1e-221, 1e-220, 1e-219, 
1e-218, 1e-217, 1e-216,
+1e-215, 1e-214, 1e-213, 1e-212, 1e-211, 1e-210, 1e-209, 1e-208, 1e-207, 
1e-206, 1e-205, 1e-204,
+1e-203, 1e-202, 1e-201, 1e-200, 1e-199, 1e-198, 1e-197, 1e-196, 1e-195, 
1e-194, 1e-193, 1e-192,
+1e-191, 1e-190, 1e-189, 1e-188, 1e-187, 1e-186, 1e-185, 1e-184, 1e-183, 
1e-182, 1e-181, 1e-180,
+1e-179, 1e-178, 1e-177, 1e-176, 1e-175, 1e-174, 1e-173, 1e-172, 1e-171, 
1e-170, 1e-169, 1e-168,
+1e-167, 1e-166, 1e-165, 1e-164, 1e-163, 1e-162, 1e-161, 1e-160, 1e-159, 
1e-158, 1e-157, 1e-156,
+1e-155, 1e-154, 1e-153, 1e-152, 1e-151, 1e-150, 1e-149, 1e-148, 1e-147, 
1e-146, 1e-145, 1e-144,
+1e-143, 1e-142, 1e-141, 1e-140, 1e-139, 1e-138, 1e-137, 1e-136, 1e-135, 
1e-134, 1e-133, 1e-132,
+1e-131, 1e-130, 1e-129, 1e-128, 1e-127, 1e-126, 1e-125, 1e-124, 1e-123, 
1e-122, 1e-121, 1e-120,
+1e-119, 1e-118, 1e-117, 1e-116, 1e-115, 1e-114, 1e-113, 1e-112, 1e-111, 
1e-110, 1e-109, 1e-108,
+1e-107, 1e-106, 1e-105, 1e-104, 1e-103, 1e-102, 1e-101, 1e-100, 1e-99,  
1e-98,  1e-97,  1e-96,
+1e-95,  1e-94,  1e-93,  1e-92,  1e-91,  1e-90,  1e-89,  1e-88,  1e-87,  
1e-86,  1e-85,  1e-84,
+1e-83,  1e-82,  1e-81,  1e-80,  1e-79,  1e-78,  1e-77,  1e-76,  1e-75,  
1e-74,  1e-73,  1e-72,
+1e-71,  1e-70,  1e-69,  1e-68,  1e-67,  1e-66,  1e-65,  1e-64,  1e-63,  
1e-62,  1e-61,  1e-60,
+1e-59,  1e-58,  1e-57,  1e-56,  1e-55,  1e-54,  1e-53,  1e-52,  1e-51,  
1e-50,  1e-49,  1e-48,
+1e-47,  1e-46,  1e-45,  1e-44,  1e-43,  1e-42,  1e-41,  1e-40,  1e-39,  
1e-38,  1e-37,  1e-36,
+1e-35,  1e-34,  1e-33,  1e-32,  1e-31,  1e-30,  1e-29,  1e-28,  1e-27,  
1e-26,  1e-25,  1e-24,
+1e-23,  1e-22,  1e-21,  1e-20,  1e-19,  1e-18,  1e-17,  1e-16,  1e-15,  
1e-14,  1e-13,  1e-12,
+1e-11,  1e-10,  1e-9,   1e-8,   1e-7,   1e-6,   1e-5,   1e-4,   1e-3,   
1e-2,   1e-1,   1e0,
+1e1,1e2,1e3,1e4,1e5,1e6,1e7,1e8,1e9,
1e10,   1e11,   1e12,
+1e13,   1e14,   1e15,   1e16,   1e17,   1e18,   1e19,   1e20,   1e21,   
1e22,   1e23,   1e24,
+1e25,   1e26,   1e27,   1e28,   1e29,   1e30,   1e31,   1e32,   1e33,   
1e34,   1e35,   1e36,
+1e37,   1e38,   1e39,   1e40,   1e41,   1e42,   1e43,   1e44,   1e45,   
1e46,   1e47,   1e48,
+1e49,   1e50,   1e51,   1e52,   1e53,   1e54,   1e55,   1e56,   1e57,   
1e58,   1e59,   1e60,
+1e61,   1e62,   1e63,   1e64,   1e65,   1e66,   1e67,   1e68,   1e69,   
1e70,   1e71,   1e72,
+1e73,   1e74,   1e75,   1e76,   1e77,   1e78,   1e79,   1e80,   1e81,   
1e82,   1e83,   1e84,
+1e85,   1e86,   1e87,   1e88,   1e89,   1e90,   1e91,   1e92,   1e93,   
1e94,   1e95,   1e96,
+1e97,   1e98,   1e99,   1e100,  1e101,  1e102,  1e103,  1e104,  1e105,  
1e106,  1e107,  1e108,
+1e109,  1e110,  1e111,  1e112,  1e113,  1e114,  1e115,  1e116,  1e117,  
1e118,  1e119,  1e120,
+1e121,  1e122,  1e123,  1e124,  1e125,  1e126,  1e127,  

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

2021-10-04 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx |  100 +--
 1 file changed, 47 insertions(+), 53 deletions(-)

New commits:
commit 347e18a3e8a742dc63833bc030dcf224499853b3
Author: Mike Kaganski 
AuthorDate: Mon Oct 4 14:15:06 2021 +0200
Commit: Mike Kaganski 
CommitDate: Mon Oct 4 15:54:06 2021 +0200

Use isRepresentableInteger here

Change-Id: I8ba202232fa42765a8b04113639fdac4b5724aa2
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122941
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index f6a2b15a21d4..4d590089 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -335,71 +335,65 @@ void doubleToString(typename T::String ** pResult,
 
 // Use integer representation for integer values that fit into the
 // mantissa (1.((2^53)-1)) with a precision of 1 for highest accuracy.
-const sal_Int64 kMaxInt = (static_cast< sal_Int64 >(1) << 53) - 1;
 if ((eFormat == rtl_math_StringFormat_Automatic ||
- eFormat == rtl_math_StringFormat_F) && fValue <= static_cast< double 
>(kMaxInt))
+ eFormat == rtl_math_StringFormat_F) && isRepresentableInteger(fValue))
 {
 sal_Int64 nInt = static_cast< sal_Int64 >(fValue);
-// Check the integer range again because double comparison may yield
-// true within the precision range.
-if (nInt <= kMaxInt && static_cast< double >(nInt) == fValue)
-{
-if (nDecPlaces == rtl_math_DecimalPlaces_Max)
-nDecPlaces = 0;
-else
-nDecPlaces = ::std::clamp< sal_Int32 >(nDecPlaces, -15, 15);
+if (nDecPlaces == rtl_math_DecimalPlaces_Max)
+nDecPlaces = 0;
+else
+nDecPlaces = ::std::clamp< sal_Int32 >(nDecPlaces, -15, 15);
 
-if (bEraseTrailingDecZeros && nDecPlaces > 0)
-nDecPlaces = 0;
+if (bEraseTrailingDecZeros && nDecPlaces > 0)
+nDecPlaces = 0;
 
-// Round before decimal position.
-if (nDecPlaces < 0)
-{
-sal_Int64 nRounding = static_cast< sal_Int64 
>(getN10Exp(-nDecPlaces - 1));
-const sal_Int64 nTemp = (nInt / nRounding + 5) / 10;
-nInt = nTemp * 10 * nRounding;
-}
+// Round before decimal position.
+if (nDecPlaces < 0)
+{
+sal_Int64 nRounding = static_cast< sal_Int64 
>(getN10Exp(-nDecPlaces - 1));
+const sal_Int64 nTemp = (nInt / nRounding + 5) / 10;
+nInt = nTemp * 10 * nRounding;
+}
 
-// Max 1 sign, 16 integer digits, 15 group separators, 1 decimal
-// separator, 15 decimals digits.
-typename T::Char aBuf[64];
-typename T::Char* pEnd = aBuf + 40;
-typename T::Char* pStart = pEnd;
+// Max 1 sign, 16 integer digits, 15 group separators, 1 decimal
+// separator, 15 decimals digits.
+typename T::Char aBuf[64];
+typename T::Char* pEnd = aBuf + 40;
+typename T::Char* pStart = pEnd;
 
-// Backward fill.
-sal_Int32 nGrouping = cGroupSeparator && pGroups ? *pGroups : 0;
-sal_Int32 nGroupDigits = 0;
-do
+// Backward fill.
+sal_Int32 nGrouping = cGroupSeparator && pGroups ? *pGroups : 0;
+sal_Int32 nGroupDigits = 0;
+do
+{
+typename T::Char nDigit = nInt % 10;
+nInt /= 10;
+*--pStart = nDigit + '0';
+if (nGrouping && nGrouping == ++nGroupDigits && nInt)
 {
-typename T::Char nDigit = nInt % 10;
-nInt /= 10;
-*--pStart = nDigit + '0';
-if (nGrouping && nGrouping == ++nGroupDigits && nInt)
-{
-*--pStart = cGroupSeparator;
-if (*(pGroups + 1))
-nGrouping = *++pGroups;
-nGroupDigits = 0;
-}
+*--pStart = cGroupSeparator;
+if (*(pGroups + 1))
+nGrouping = *++pGroups;
+nGroupDigits = 0;
 }
-while (nInt);
-if (bSign)
-*--pStart = '-';
+}
+while (nInt);
+if (bSign)
+*--pStart = '-';
 
-// Append decimals.
-if (nDecPlaces > 0)
-{
-*pEnd++ = cDecSeparator;
-pEnd = std::fill_n(pEnd, nDecPlaces, '0');
-}
+// Append decimals.
+if (nDecPlaces > 0)
+{
+*pEnd++ = cDecSeparator;
+pEnd = std::fill_n(pEnd, nDecPlaces, '0');
+}
 
-if (!pResultCapacity)
-T::createString(pResult, pStart, pEnd - pStart);
-else
-T::appendChars(pResult, pResultCapacity, , 
pStart, pEnd - 

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

2021-10-01 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx |   64 ---
 1 file changed, 24 insertions(+), 40 deletions(-)

New commits:
commit de80116e81a5b73215098cd0aa60ac9226905ae5
Author: Mike Kaganski 
AuthorDate: Fri Oct 1 09:17:03 2021 +0200
Commit: Mike Kaganski 
CommitDate: Fri Oct 1 19:36:41 2021 +0200

Simplify the code a bit to clarify the logic

Change-Id: I80201148684f6e297ff0c880c0dbbc346129a557
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122864
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 2d1c1257ef97..f6a2b15a21d4 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -580,64 +580,48 @@ void doubleToString(typename T::String ** pResult,
 
 if (nDigit >= 10)
 {   // after-treatment of up-rounding to the next decade
-sal_Int32 sLen = p - pBuf - 1;
-if (sLen == -1 || (sLen == 0 && bSign))
+typename T::Char* p1 = pBuf;
+// Assert that no one changed the logic we rely on.
+assert(!bSign || *p1 == '-');
+// Do not touch leading minus sign put earlier.
+if (bSign)
+++p1;
+assert(p1 <= p);
+if (p1 == p)
 {
-// Assert that no one changed the logic we rely on.
-assert(!bSign || pBuf[0] == '-');
-p = pBuf;
-if (bSign)
-++p;
-if (eFormat == rtl_math_StringFormat_F)
+*p++ = '1';
+if (eFormat != rtl_math_StringFormat_F)
 {
-*p++ = '1';
-*p++ = '0';
-}
-else
-{
-*p++ = '1';
 *p++ = cDecSeparator;
-*p++ = '0';
 nExp++;
 bHasDec = true;
 }
+*p++ = '0';
 }
 else
 {
-for (sal_Int32 j = sLen; j >= 0; j--)
+for (typename T::Char* p2 = p - 1; p2 >= p1; --p2)
 {
-typename T::Char* p2 = [j];
 typename T::Char cS = *p2;
-if (j == 0 && bSign)
+if (cS == cDecSeparator)
+continue;
+if (cS != '9')
 {
-// Do not touch leading minus sign put earlier.
-assert(cS == '-');
-break;  // for, this is the last character 
backwards.
+++*p2;
+break;
 }
-if (cS != cDecSeparator)
+*p2 = '0';
+if (p2 == p1) // The number consisted of all 9s 
replaced to all 0s
 {
-if (cS != '9')
-{
-*p2 = ++cS;
-j = -1; // break loop
+if (eFormat == rtl_math_StringFormat_F)
+{ // move everything to the right before 
inserting '1'
+std::memmove(p2 + 1, p2, (p++ - p2) * 
sizeof(*p));
 }
 else
 {
-*p2 = '0';
-if (j == 0 || (j == 1 && bSign))
-{
-if (eFormat == rtl_math_StringFormat_F)
-{   // insert '1'
-std::memmove(p2 + 1, p2, (p++ - 
p2) * sizeof(*p));
-*p2 = '1';
-}
-else
-{
-*p2 = '1';
-nExp++;
-}
-}
+nExp++;
 }
+*p2 = '1';
 }
 }
 


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

2021-10-01 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx |2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

New commits:
commit 0471dccf9c81b4851078033d286a215954c488ed
Author: Mike Kaganski 
AuthorDate: Fri Oct 1 15:12:49 2021 +0200
Commit: Mike Kaganski 
CommitDate: Fri Oct 1 18:04:02 2021 +0200

Calculate buffer size correctly

A mistake in eae24a9488814e77254d175c11fc4a138c1dbd30

Change-Id: I0da64366e4c39b3f5559e8a1c757a94d811f041f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122869
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index ce939b907655..2d1c1257ef97 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -506,7 +506,7 @@ void doubleToString(typename T::String ** pResult,
 // max(nDigits) = max(nDecPlaces) + 1 + max(nExp) + 1 = 20 + 1 + 308 + 1 = 
330
 // max(nBuf) = max(nDigits) + max(nDecPlaces) + 10 + max(nDigits) * 2 = 
330 * 3 + 20 + 10 = 1020
 assert(nBuf <= 1024);
-typename T::Char* pBuf = static_cast(alloca(nBuf));
+typename T::Char* pBuf = static_cast(alloca(nBuf * 
sizeof(typename T::Char)));
 typename T::Char * p = pBuf;
 if ( bSign )
 *p++ = '-';


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

2021-10-01 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx |   13 +++--
 1 file changed, 7 insertions(+), 6 deletions(-)

New commits:
commit 6b6fde50ab6069f8f82b4666e6ac1f50db5fcf1d
Author: Mike Kaganski 
AuthorDate: Fri Oct 1 09:07:49 2021 +0200
Commit: Mike Kaganski 
CommitDate: Fri Oct 1 16:01:13 2021 +0200

Consider leading minus

An overlook from commit be8da97976658ff19b4dd010bff328cd3f424c1b

Change-Id: I2f639a9d865b43b38e19ad35a0a9e6b5bc1c1c8f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122863
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 970f2e298e02..ce939b907655 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -606,7 +606,8 @@ void doubleToString(typename T::String ** pResult,
 {
 for (sal_Int32 j = sLen; j >= 0; j--)
 {
-typename T::Char cS = pBuf[j];
+typename T::Char* p2 = [j];
+typename T::Char cS = *p2;
 if (j == 0 && bSign)
 {
 // Do not touch leading minus sign put earlier.
@@ -617,22 +618,22 @@ void doubleToString(typename T::String ** pResult,
 {
 if (cS != '9')
 {
-pBuf[j] = ++cS;
+*p2 = ++cS;
 j = -1; // break loop
 }
 else
 {
-pBuf[j] = '0';
+*p2 = '0';
 if (j == 0 || (j == 1 && bSign))
 {
 if (eFormat == rtl_math_StringFormat_F)
 {   // insert '1'
-std::memmove(pBuf + 1, pBuf, (p++ 
- pBuf) * sizeof(*p));
-pBuf[0] = '1';
+std::memmove(p2 + 1, p2, (p++ - 
p2) * sizeof(*p));
+*p2 = '1';
 }
 else
 {
-pBuf[j] = '1';
+*p2 = '1';
 nExp++;
 }
 }


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

2021-10-01 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx |8 +---
 1 file changed, 1 insertion(+), 7 deletions(-)

New commits:
commit d7bd370e7ec71ae8e511c21aa1a2bb51b6c208a3
Author: Mike Kaganski 
AuthorDate: Fri Oct 1 08:28:25 2021 +0200
Commit: Mike Kaganski 
CommitDate: Fri Oct 1 14:17:17 2021 +0200

Use std::memmove instead of loop

Change-Id: Ia88ceaaad700bf3c2c8db9bb19146b75146dad3d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122861
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 7aefe09a5fa6..970f2e298e02 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -627,13 +627,7 @@ void doubleToString(typename T::String ** pResult,
 {
 if (eFormat == rtl_math_StringFormat_F)
 {   // insert '1'
-typename T::Char * px = p++;
-while (pBuf < px)
-{
-*px = *(px-1);
-px--;
-}
-
+std::memmove(pBuf + 1, pBuf, (p++ 
- pBuf) * sizeof(*p));
 pBuf[0] = '1';
 }
 else


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

2021-10-01 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx |   62 +--
 1 file changed, 29 insertions(+), 33 deletions(-)

New commits:
commit 7a8f7f802a342c2bc301f5d313001829a07a7c92
Author: Mike Kaganski 
AuthorDate: Fri Oct 1 08:56:17 2021 +0200
Commit: Mike Kaganski 
CommitDate: Fri Oct 1 13:46:48 2021 +0200

Drop static_cast syntactic noise

Change-Id: I699f22aac871fdcef9ee9326ac30091239fa02be
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122862
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 7ad1671924ab..7aefe09a5fa6 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -500,7 +500,7 @@ void doubleToString(typename T::String ** pResult,
 }
 }
 
-sal_Int32 nBuf = static_cast< sal_Int32 >
+sal_Int32 nBuf =
 (nDigits <= 0 ? std::max< sal_Int32 >(nDecPlaces, abs(nExp))
   : nDigits + nDecPlaces ) + 10 + (pGroups ? abs(nDigits) * 2 : 0);
 // max(nDigits) = max(nDecPlaces) + 1 + max(nExp) + 1 = 20 + 1 + 308 + 1 = 
330
@@ -509,7 +509,7 @@ void doubleToString(typename T::String ** pResult,
 typename T::Char* pBuf = static_cast(alloca(nBuf));
 typename T::Char * p = pBuf;
 if ( bSign )
-*p++ = static_cast< typename T::Char >('-');
+*p++ = '-';
 
 bool bHasDec = false;
 
@@ -519,7 +519,7 @@ void doubleToString(typename T::String ** pResult,
 {
 if(nExp < 0)
 {
-*p++ = static_cast< typename T::Char >('0');
+*p++ = '0';
 if (nDecPlaces > 0)
 {
 *p++ = cDecSeparator;
@@ -530,7 +530,7 @@ void doubleToString(typename T::String ** pResult,
 
 while((i--) > 0)
 {
-*p++ = static_cast< typename T::Char >('0');
+*p++ = '0';
 }
 
 nDecPos = 0;
@@ -574,30 +574,30 @@ void doubleToString(typename T::String ** pResult,
 {
 int nDigit;
 if (nDigits-1 == 0 && i > 0 && i < 14)
-nDigit = static_cast< int >(floor( fValue + 
nCorrVal[15-i]));
+nDigit = floor( fValue + nCorrVal[15-i]);
 else
-nDigit = static_cast< int >(fValue + 1E-15);
+nDigit = fValue + 1E-15;
 
 if (nDigit >= 10)
 {   // after-treatment of up-rounding to the next decade
-sal_Int32 sLen = static_cast< long >(p-pBuf)-1;
+sal_Int32 sLen = p - pBuf - 1;
 if (sLen == -1 || (sLen == 0 && bSign))
 {
 // Assert that no one changed the logic we rely on.
-assert(!bSign || pBuf[0] == static_cast< typename 
T::Char >('-'));
+assert(!bSign || pBuf[0] == '-');
 p = pBuf;
 if (bSign)
 ++p;
 if (eFormat == rtl_math_StringFormat_F)
 {
-*p++ = static_cast< typename T::Char >('1');
-*p++ = static_cast< typename T::Char >('0');
+*p++ = '1';
+*p++ = '0';
 }
 else
 {
-*p++ = static_cast< typename T::Char >('1');
+*p++ = '1';
 *p++ = cDecSeparator;
-*p++ = static_cast< typename T::Char >('0');
+*p++ = '0';
 nExp++;
 bHasDec = true;
 }
@@ -610,19 +610,19 @@ void doubleToString(typename T::String ** pResult,
 if (j == 0 && bSign)
 {
 // Do not touch leading minus sign put earlier.
-assert(cS == static_cast< typename T::Char 
>('-'));
+assert(cS == '-');
 break;  // for, this is the last character 
backwards.
 }
 if (cS != cDecSeparator)
 {
-if (cS != static_cast< typename T::Char >('9'))
+if (cS != '9')
 {
 pBuf[j] = ++cS;
 j = -1; // break loop
 }
 else
 {
-pBuf[j] = static_cast< typename T::Char 
>('0');
+pBuf[j] = '0';
 if (j == 0 || (j == 1 && bSign))

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

2021-10-01 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx |   24 +---
 1 file changed, 5 insertions(+), 19 deletions(-)

New commits:
commit eae24a9488814e77254d175c11fc4a138c1dbd30
Author: Mike Kaganski 
AuthorDate: Fri Oct 1 08:14:29 2021 +0200
Commit: Mike Kaganski 
CommitDate: Fri Oct 1 13:46:32 2021 +0200

Always use buffer on stack

Change-Id: I39ed2485a67ec7a8b24ab90ea0d69a5982374334
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122860
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 95dd6ae41bc6..7ad1671924ab 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -500,24 +500,13 @@ void doubleToString(typename T::String ** pResult,
 }
 }
 
-static sal_Int32 const nBufMax = 256;
-typename T::Char aBuf[nBufMax];
-typename T::Char * pBuf;
 sal_Int32 nBuf = static_cast< sal_Int32 >
 (nDigits <= 0 ? std::max< sal_Int32 >(nDecPlaces, abs(nExp))
   : nDigits + nDecPlaces ) + 10 + (pGroups ? abs(nDigits) * 2 : 0);
-
-if (nBuf > nBufMax)
-{
-pBuf = static_cast< typename T::Char * >(
-malloc(nBuf * sizeof (typename T::Char)));
-OSL_ENSURE(pBuf, "Out of memory");
-}
-else
-{
-pBuf = aBuf;
-}
-
+// max(nDigits) = max(nDecPlaces) + 1 + max(nExp) + 1 = 20 + 1 + 308 + 1 = 
330
+// max(nBuf) = max(nDigits) + max(nDecPlaces) + 10 + max(nDigits) * 2 = 
330 * 3 + 20 + 10 = 1020
+assert(nBuf <= 1024);
+typename T::Char* pBuf = static_cast(alloca(nBuf));
 typename T::Char * p = pBuf;
 if ( bSign )
 *p++ = static_cast< typename T::Char >('-');
@@ -595,7 +584,7 @@ void doubleToString(typename T::String ** pResult,
 if (sLen == -1 || (sLen == 0 && bSign))
 {
 // Assert that no one changed the logic we rely on.
-assert(!bSign || *pBuf == static_cast< typename 
T::Char >('-'));
+assert(!bSign || pBuf[0] == static_cast< typename 
T::Char >('-'));
 p = pBuf;
 if (bSign)
 ++p;
@@ -761,9 +750,6 @@ void doubleToString(typename T::String ** pResult,
 T::createString(pResult, pBuf, p - pBuf);
 else
 T::appendChars(pResult, pResultCapacity, , pBuf, p - 
pBuf);
-
-if (pBuf != [0])
-free(pBuf);
 }
 
 }


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

2021-09-30 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx |   10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

New commits:
commit 0b81761c86786eeb035f857f549d75f143e491ad
Author: Mike Kaganski 
AuthorDate: Thu Sep 30 09:15:01 2021 +0200
Commit: Mike Kaganski 
CommitDate: Thu Sep 30 10:26:56 2021 +0200

Simplify comparisons in the loop

Change-Id: I9963a527d2323f4df8c3b46c13f5b7993e22f163
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122855
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 0cd53cd3902a..95dd6ae41bc6 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -367,22 +367,22 @@ void doubleToString(typename T::String ** pResult,
 typename T::Char* pStart = pEnd;
 
 // Backward fill.
-size_t nGrouping = 0;
+sal_Int32 nGrouping = cGroupSeparator && pGroups ? *pGroups : 0;
 sal_Int32 nGroupDigits = 0;
 do
 {
 typename T::Char nDigit = nInt % 10;
 nInt /= 10;
 *--pStart = nDigit + '0';
-if (pGroups && pGroups[nGrouping] == ++nGroupDigits && nInt > 
0 && cGroupSeparator)
+if (nGrouping && nGrouping == ++nGroupDigits && nInt)
 {
 *--pStart = cGroupSeparator;
-if (pGroups[nGrouping+1])
-++nGrouping;
+if (*(pGroups + 1))
+nGrouping = *++pGroups;
 nGroupDigits = 0;
 }
 }
-while (nInt > 0);
+while (nInt);
 if (bSign)
 *--pStart = '-';
 


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

2021-09-29 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx |   23 +--
 1 file changed, 9 insertions(+), 14 deletions(-)

New commits:
commit a4d865e4d82d44cd75f24c5385d44de8f4fcc62f
Author: Mike Kaganski 
AuthorDate: Wed Sep 29 23:22:01 2021 +0200
Commit: Mike Kaganski 
CommitDate: Thu Sep 30 07:13:50 2021 +0200

Avoid reversing the buffer

Fill it in the correct order, starting from the middle

Change-Id: Id35475e391d771d6c23252124a92825b24b55e0c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122853
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index b36aef117136..0cd53cd3902a 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -358,14 +358,13 @@ void doubleToString(typename T::String ** pResult,
 sal_Int64 nRounding = static_cast< sal_Int64 
>(getN10Exp(-nDecPlaces - 1));
 const sal_Int64 nTemp = (nInt / nRounding + 5) / 10;
 nInt = nTemp * 10 * nRounding;
-nDecPlaces = 0;
 }
 
 // Max 1 sign, 16 integer digits, 15 group separators, 1 decimal
 // separator, 15 decimals digits.
 typename T::Char aBuf[64];
-typename T::Char * pBuf = aBuf;
-typename T::Char * p = pBuf;
+typename T::Char* pEnd = aBuf + 40;
+typename T::Char* pStart = pEnd;
 
 // Backward fill.
 size_t nGrouping = 0;
@@ -374,10 +373,10 @@ void doubleToString(typename T::String ** pResult,
 {
 typename T::Char nDigit = nInt % 10;
 nInt /= 10;
-*p++ = nDigit + '0';
+*--pStart = nDigit + '0';
 if (pGroups && pGroups[nGrouping] == ++nGroupDigits && nInt > 
0 && cGroupSeparator)
 {
-*p++ = cGroupSeparator;
+*--pStart = cGroupSeparator;
 if (pGroups[nGrouping+1])
 ++nGrouping;
 nGroupDigits = 0;
@@ -385,23 +384,19 @@ void doubleToString(typename T::String ** pResult,
 }
 while (nInt > 0);
 if (bSign)
-*p++ = '-';
-
-// Reverse buffer content.
-std::reverse(pBuf, p);
+*--pStart = '-';
 
 // Append decimals.
 if (nDecPlaces > 0)
 {
-*p++ = cDecSeparator;
-while (nDecPlaces--)
-*p++ = '0';
+*pEnd++ = cDecSeparator;
+pEnd = std::fill_n(pEnd, nDecPlaces, '0');
 }
 
 if (!pResultCapacity)
-T::createString(pResult, pBuf, p - pBuf);
+T::createString(pResult, pStart, pEnd - pStart);
 else
-T::appendChars(pResult, pResultCapacity, , pBuf, 
p - pBuf);
+T::appendChars(pResult, pResultCapacity, , 
pStart, pEnd - pStart);
 
 return;
 }


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

2021-09-29 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx |6 +-
 1 file changed, 1 insertion(+), 5 deletions(-)

New commits:
commit 8d131274e7aca598a89060cb0fbf7afb239b9034
Author: Mike Kaganski 
AuthorDate: Wed Sep 29 15:37:46 2021 +0200
Commit: Mike Kaganski 
CommitDate: Wed Sep 29 16:29:45 2021 +0200

Use std::reverse instead of swapping in a loop

Change-Id: Iee8966eeeaea461e34b5d22b80cb612dfaa57fe4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122750
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 6de4d30383cd..b36aef117136 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -388,11 +388,7 @@ void doubleToString(typename T::String ** pResult,
 *p++ = '-';
 
 // Reverse buffer content.
-sal_Int32 n = (p - pBuf) / 2;
-for (sal_Int32 i=0; i < n; ++i)
-{
-::std::swap( pBuf[i], p[-i-1]);
-}
+std::reverse(pBuf, p);
 
 // Append decimals.
 if (nDecPlaces > 0)


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

2021-09-29 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx |   12 ++--
 1 file changed, 2 insertions(+), 10 deletions(-)

New commits:
commit 0bc7b206c8f6e62313ae2f26211b15cac880125f
Author: Mike Kaganski 
AuthorDate: Wed Sep 29 14:23:37 2021 +0200
Commit: Mike Kaganski 
CommitDate: Wed Sep 29 16:24:46 2021 +0200

Simplify integer rounding

Change-Id: I09ab406a8b7279801ce79b2f9c0a0011f6db05be
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122749
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index feb3a2b1852d..6de4d30383cd 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -356,16 +356,8 @@ void doubleToString(typename T::String ** pResult,
 if (nDecPlaces < 0)
 {
 sal_Int64 nRounding = static_cast< sal_Int64 
>(getN10Exp(-nDecPlaces - 1));
-sal_Int64 nTemp = nInt / nRounding;
-int nDigit = nTemp % 10;
-nTemp /= 10;
-
-if (nDigit >= 5)
-++nTemp;
-
-nTemp *= 10;
-nTemp *= nRounding;
-nInt = nTemp;
+const sal_Int64 nTemp = (nInt / nRounding + 5) / 10;
+nInt = nTemp * 10 * nRounding;
 nDecPlaces = 0;
 }
 


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

2021-09-17 Thread Mike Kaganski (via logerrit)
 sal/rtl/string.cxx  |4 ++--
 sal/rtl/strtmpl.hxx |8 
 sal/rtl/ustring.cxx |4 ++--
 3 files changed, 4 insertions(+), 12 deletions(-)

New commits:
commit 11c8109fa109ef8178679b4de6efde695b99e0a8
Author: Mike Kaganski 
AuthorDate: Fri Sep 17 07:42:31 2021 +0200
Commit: Mike Kaganski 
CommitDate: Fri Sep 17 13:44:20 2021 +0200

Drop this unneeded indirection

Change-Id: I6b40642c7574a1863658854d206ed849517dbd0f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122130
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index 94be8029032a..b8a96d43a07b 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -23,7 +23,6 @@
 #include 
 
 #include 
-#include 
 #include 
 #include 
 
@@ -599,7 +598,8 @@ void SAL_CALL rtl_string_new(rtl_String** ppThis) 
SAL_THROW_EXTERN_C()
 
 rtl_String* SAL_CALL rtl_string_alloc(sal_Int32 nLen) SAL_THROW_EXTERN_C()
 {
-return rtl::str::alloc(nLen);
+assert(nLen >= 0);
+return rtl::str::Alloc(nLen);
 }
 
 void SAL_CALL rtl_string_new_WithLength(rtl_String** ppThis, sal_Int32 nLen) 
SAL_THROW_EXTERN_C()
diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 493184f92efa..f26c21225177 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -1099,14 +1099,6 @@ template  void new_( 
IMPL_RTL_STRINGDATA** ppThis
 
 /* --- */
 
-template  IMPL_RTL_STRINGDATA* alloc( sal_Int32 
nLen )
-{
-assert(nLen >= 0);
-return Alloc( nLen );
-}
-
-/* --- */
-
 template 
 void new_WithLength( IMPL_RTL_STRINGDATA** ppThis, sal_Int32 nLen )
 {
diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx
index 4aac6455..ee84510bcc7f 100644
--- a/sal/rtl/ustring.cxx
+++ b/sal/rtl/ustring.cxx
@@ -27,7 +27,6 @@
 
 #include 
 #include 
-#include 
 #include 
 #include 
 
@@ -1793,7 +1792,8 @@ void SAL_CALL rtl_uString_new(rtl_uString** ppThis) 
SAL_THROW_EXTERN_C()
 
 rtl_uString* SAL_CALL rtl_uString_alloc(sal_Int32 nLen) SAL_THROW_EXTERN_C()
 {
-return rtl::str::alloc(nLen);
+assert(nLen >= 0);
+return rtl::str::Alloc(nLen);
 }
 
 void SAL_CALL rtl_uString_new_WithLength(rtl_uString** ppThis, sal_Int32 nLen) 
SAL_THROW_EXTERN_C()


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

2021-07-04 Thread BaiXiaochun (via logerrit)
 sal/rtl/math.cxx |8 
 1 file changed, 8 deletions(-)

New commits:
commit 420e4544267545217d54e9acbc9dbf321d58740a
Author: BaiXiaochun 
AuthorDate: Fri Jul 2 12:36:06 2021 +0200
Commit: Mike Kaganski 
CommitDate: Sun Jul 4 15:41:40 2021 +0200

Remove unused includes

Change-Id: I243d2998725f09ef533bd06865081d5c415f0d18
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/118282
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 75981d970d74..e9b50a1b4638 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -19,17 +19,9 @@
 
 #include 
 
-#include 
 #include 
-#include 
 #include 
 #include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
 
 #include 
 #include 
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2021-07-02 Thread BaiXiaochun (via logerrit)
 sal/rtl/math.cxx |   23 ---
 1 file changed, 4 insertions(+), 19 deletions(-)

New commits:
commit e5c80bb69a30dfb0a3daf6061ab127d92f8142d6
Author: BaiXiaochun 
AuthorDate: Wed Jun 30 19:01:16 2021 +0200
Commit: Mike Kaganski 
CommitDate: Fri Jul 2 08:41:25 2021 +0200

Purge out setNan from math.cxx

Change-Id: I95f9b37b564bb733f44899a8c6c1ea3c36e35694
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/118196
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 88354763b2b4..75981d970d74 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -865,7 +865,7 @@ double stringToDouble(CharT const * pBegin, CharT const * 
pEnd,
 && (CharT('N') == p[2]))
 {
 p += 3;
-rtl::math::setNan(  );
+fVal = std::numeric_limits::quiet_NaN();
 bDone = true;
 }
 else if ((CharT('I') == p[0]) && (CharT('N') == p[1])
@@ -1012,19 +1012,8 @@ double stringToDouble(CharT const * pBegin, CharT const 
* pEnd,
 {
 // "1.#NAN", "+1.#NAN", "-1.#NAN"
 p += 4;
-rtl::math::setNan(  );
-if (bSign)
-{
-union {
-double sd;
-sal_math_Double md;
-} m;
-
-m.sd = fVal;
-m.md.w32_parts.msw |= 0x8000; // create negative NaN
-fVal = m.sd;
-bSign = false; // don't negate again
-}
+fVal = std::numeric_limits::quiet_NaN();
+// bSign will cause negation of fVal in the end, producing a 
negative NAN.
 
 // Eat any further digits:
 while (p != pEnd && rtl::isAsciiDigit(*p))
@@ -1443,11 +1432,7 @@ double SAL_CALL rtl_math_acosh(double fX) 
SAL_THROW_EXTERN_C()
 {
 volatile double fZ = fX - 1.0;
 if (fX < 1.0)
-{
-double fResult;
-::rtl::math::setNan(  );
-return fResult;
-}
+return std::numeric_limits::quiet_NaN();
 if ( fX == 1.0 )
 return 0.0;
 
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2021-06-29 Thread BaiXiaochun (via logerrit)
 sal/rtl/math.cxx |   29 -
 1 file changed, 8 insertions(+), 21 deletions(-)

New commits:
commit 9c15dea0b2192d231b65175291a7655122c2e24c
Author: BaiXiaochun 
AuthorDate: Sun Jun 27 18:38:10 2021 +0200
Commit: Mike Kaganski 
CommitDate: Tue Jun 29 23:18:47 2021 +0200

Simplify expresion

Reduce operation count by space / speed tradeoff.
This expression is widely used in LO.
Then needs to be fast.

Change-Id: Ic88cf15d451ec95a8ad6da88cd9f601cf2876871
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/117954
Reviewed-by: Mike Kaganski 
Tested-by: Jenkins

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 7db362f35cfe..88354763b2b4 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -44,33 +44,20 @@
 
 #include 
 
-int const n10Count = 16;
-double const n10s[2][n10Count] = {
-{ 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8,
-  1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16 },
-{ 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8,
-  1e-9, 1e-10, 1e-11, 1e-12, 1e-13, 1e-14, 1e-15, 1e-16 }
+constexpr int n10Count = 16;
+constexpr double n10s[n10Count*2+1] = {
+1e-16, 1e-15, 1e-14, 1e-13, 1e-12, 1e-11, 1e-10, 1e-9,
+1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1e0,
+1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8,
+1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16 ,
 };
 
 // return pow(10.0,nExp) optimized for exponents in the interval [-16,16]
 static double getN10Exp(int nExp)
 {
-if (nExp < 0)
-{
-// && -nExp > 0 necessary for std::numeric_limits::min()
-// because -nExp = nExp
-if (-nExp <= n10Count && -nExp > 0)
-return n10s[1][-nExp-1];
+if (nExp < -n10Count || nExp > n10Count)
 return pow(10.0, static_cast(nExp));
-}
-if (nExp > 0)
-{
-if (nExp <= n10Count)
-return n10s[0][nExp-1];
-
-return pow(10.0, static_cast(nExp));
-}
-return 1.0;
+return n10s[nExp + n10Count];
 }
 
 namespace {
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2021-06-21 Thread Noel Grandin (via logerrit)
 sal/rtl/bootstrap.cxx |   33 -
 1 file changed, 24 insertions(+), 9 deletions(-)

New commits:
commit 46d6942bc16fb291f37b0700bb531a3e0d2d11f6
Author: Noel Grandin 
AuthorDate: Sun Jun 20 20:19:28 2021 +0200
Commit: Noel Grandin 
CommitDate: Mon Jun 21 14:27:53 2021 +0200

tdf#135316 add small cache to rtl_bootstrap_args_open

Filesystem access is quite expensive on Windows, so add a small
cache for the filepath/name normalisation.

This takes my load time from 19s to 18s

Change-Id: I4410d066b8a4d2fd2eb746a5dd8f4ee763a8aa3e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/117549
Tested-by: Jenkins
Reviewed-by: Noel Grandin 

diff --git a/sal/rtl/bootstrap.cxx b/sal/rtl/bootstrap.cxx
index 9aa3546a5ca7..afb80dcd66cb 100644
--- a/sal/rtl/bootstrap.cxx
+++ b/sal/rtl/bootstrap.cxx
@@ -37,6 +37,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
@@ -588,21 +589,35 @@ bootstrap_map_t bootstrap_map;
 
 rtlBootstrapHandle SAL_CALL rtl_bootstrap_args_open(rtl_uString * pIniName)
 {
-OUString iniName( pIniName );
+static o3tl::lru_map fileUrlLookupCache(16);
+
+OUString originalIniName( pIniName );
+OUString iniName;
+
+osl::ResettableMutexGuard guard(osl::Mutex::getGlobalMutex());
+auto cacheIt = fileUrlLookupCache.find(originalIniName);
+bool foundInCache = cacheIt != fileUrlLookupCache.end();
+if (foundInCache)
+iniName = cacheIt->second;
+guard.clear();
 
 // normalize path
-FileStatus status(osl_FileStatus_Mask_FileURL);
-DirectoryItem dirItem;
-if (DirectoryItem::get(iniName, dirItem) != DirectoryItem::E_None ||
-dirItem.getFileStatus(status) != DirectoryItem::E_None)
+if (!foundInCache)
 {
-return nullptr;
+FileStatus status(osl_FileStatus_Mask_FileURL);
+DirectoryItem dirItem;
+if (DirectoryItem::get(originalIniName, dirItem) != 
DirectoryItem::E_None ||
+dirItem.getFileStatus(status) != DirectoryItem::E_None)
+{
+return nullptr;
+}
+iniName = status.getFileURL();
 }
 
-iniName = status.getFileURL();
-
+guard.reset();
+if (!foundInCache)
+fileUrlLookupCache.insert({originalIniName, iniName});
 Bootstrap_Impl * that;
-osl::ResettableMutexGuard guard(osl::Mutex::getGlobalMutex());
 auto iFind(bootstrap_map.find(iniName));
 if (iFind == bootstrap_map.end())
 {
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2021-06-21 Thread Noel Grandin (via logerrit)
 sal/rtl/bootstrap.cxx |2 --
 1 file changed, 2 deletions(-)

New commits:
commit a5e9cde9f9abe3ee4e5fa0cf921d53a8cd7cb1d2
Author: Noel Grandin 
AuthorDate: Mon Jun 21 12:50:10 2021 +0200
Commit: Noel Grandin 
CommitDate: Mon Jun 21 13:39:55 2021 +0200

remove now unnecessary comment

Change-Id: I0297f299c42a5ce5485cdcdebfbd1597969c907c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/117573
Tested-by: Jenkins
Reviewed-by: Noel Grandin 

diff --git a/sal/rtl/bootstrap.cxx b/sal/rtl/bootstrap.cxx
index 45c330a56edb..9aa3546a5ca7 100644
--- a/sal/rtl/bootstrap.cxx
+++ b/sal/rtl/bootstrap.cxx
@@ -581,8 +581,6 @@ void Bootstrap_Impl::expandValue(
 
 namespace {
 
-// This map must only be called properly synchronized via some mutex
-// (e.g., osl::Mutex::getGlobalMutex()):
 typedef std::unordered_map< OUString, Bootstrap_Impl * > bootstrap_map_t;
 bootstrap_map_t bootstrap_map;
 
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2021-06-21 Thread Noel Grandin (via logerrit)
 sal/rtl/bootstrap.cxx |   59 +++---
 1 file changed, 13 insertions(+), 46 deletions(-)

New commits:
commit 2e3046cc6fdebb52cfb1cdc114a9c2cd26bcc178
Author: Noel Grandin 
AuthorDate: Sun Jun 20 19:10:50 2021 +0200
Commit: Noel Grandin 
CommitDate: Mon Jun 21 08:47:52 2021 +0200

simplify bootstrap_map (tdf#135316 related)

No need for this indirection

Change-Id: I87c90c9f1a7904f5a506acac631fe5a1f52f9190
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/117521
Tested-by: Jenkins
Reviewed-by: Noel Grandin 

diff --git a/sal/rtl/bootstrap.cxx b/sal/rtl/bootstrap.cxx
index 4f5c2f732dc1..45c330a56edb 100644
--- a/sal/rtl/bootstrap.cxx
+++ b/sal/rtl/bootstrap.cxx
@@ -581,38 +581,10 @@ void Bootstrap_Impl::expandValue(
 
 namespace {
 
-struct bootstrap_map {
-typedef std::unordered_map<
-OUString, Bootstrap_Impl * > t;
-
-bootstrap_map(const bootstrap_map&) = delete;
-bootstrap_map& operator=(const bootstrap_map&) = delete;
-
-// get and release must only be called properly synchronized via some mutex
-// (e.g., osl::Mutex::getGlobalMutex()):
-
-static t * get()
-{
-if (!m_map)
-m_map = new t;
-
-return m_map;
-}
-
-static void release()
-{
-if (m_map != nullptr && m_map->empty())
-{
-delete m_map;
-m_map = nullptr;
-}
-}
-
-private:
-static t * m_map;
-};
-
-bootstrap_map::t * bootstrap_map::m_map = nullptr;
+// This map must only be called properly synchronized via some mutex
+// (e.g., osl::Mutex::getGlobalMutex()):
+typedef std::unordered_map< OUString, Bootstrap_Impl * > bootstrap_map_t;
+bootstrap_map_t bootstrap_map;
 
 }
 
@@ -633,21 +605,18 @@ rtlBootstrapHandle SAL_CALL 
rtl_bootstrap_args_open(rtl_uString * pIniName)
 
 Bootstrap_Impl * that;
 osl::ResettableMutexGuard guard(osl::Mutex::getGlobalMutex());
-bootstrap_map::t* p_bootstrap_map = bootstrap_map::get();
-bootstrap_map::t::const_iterator iFind(p_bootstrap_map->find(iniName));
-if (iFind == p_bootstrap_map->end())
+auto iFind(bootstrap_map.find(iniName));
+if (iFind == bootstrap_map.end())
 {
-bootstrap_map::release();
 guard.clear();
 that = new Bootstrap_Impl(iniName);
 guard.reset();
-p_bootstrap_map = bootstrap_map::get();
-iFind = p_bootstrap_map->find(iniName);
-if (iFind == p_bootstrap_map->end())
+iFind = bootstrap_map.find(iniName);
+if (iFind == bootstrap_map.end())
 {
 ++that->_nRefCount;
-::std::pair< bootstrap_map::t::iterator, bool > insertion(
-p_bootstrap_map->emplace(iniName, that));
+::std::pair< bootstrap_map_t::iterator, bool > insertion(
+bootstrap_map.emplace(iniName, that));
 OSL_ASSERT(insertion.second);
 }
 else
@@ -675,23 +644,21 @@ void SAL_CALL rtl_bootstrap_args_close(rtlBootstrapHandle 
handle) SAL_THROW_EXTE
 Bootstrap_Impl * that = static_cast< Bootstrap_Impl * >( handle );
 
 osl::MutexGuard guard(osl::Mutex::getGlobalMutex());
-bootstrap_map::t* p_bootstrap_map = bootstrap_map::get();
-OSL_ASSERT(p_bootstrap_map->find(that->_iniName)->second == that);
+OSL_ASSERT(bootstrap_map.find(that->_iniName)->second == that);
 --that->_nRefCount;
 
 if (that->_nRefCount != 0)
 return;
 
 std::size_t const nLeaking = 8; // only hold up to 8 files statically
-if (p_bootstrap_map->size() > nLeaking)
+if (bootstrap_map.size() > nLeaking)
 {
-::std::size_t erased = p_bootstrap_map->erase( that->_iniName );
+::std::size_t erased = bootstrap_map.erase( that->_iniName );
 if (erased != 1) {
 OSL_ASSERT( false );
 }
 delete that;
 }
-bootstrap_map::release();
 }
 
 sal_Bool SAL_CALL rtl_bootstrap_get_from_handle(
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2021-06-17 Thread Mike Kaganski (via logerrit)
 sal/rtl/math.cxx |   22 --
 1 file changed, 20 insertions(+), 2 deletions(-)

New commits:
commit 3fc41a91ce3bd9d6756905809e261499a51d9774
Author: Mike Kaganski 
AuthorDate: Thu Jun 17 22:49:30 2021 +0200
Commit: Mike Kaganski 
CommitDate: Fri Jun 18 06:24:33 2021 +0200

Use std::fesetround / std::nearbyint for half-to-even rounding

C++ floating-point environment has thread storage duration, so
std::fesetround call is thread-safe.
std::nearbyint uses half-to-even rounding with FE_TONEAREST,
as specified in ISO/IEC 9899:1999 F.3, and in Appendix to
ANSI/IEEE 854.

Change-Id: I26995842c30e2663b40af5b5314791c1328037d4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/117307
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 698e158df072..7db362f35cfe 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -33,6 +33,8 @@
 
 #include 
 #include 
+#include 
+#include 
 #include 
 #include 
 #include 
@@ -1137,8 +1139,24 @@ double SAL_CALL rtl_math_round(double fValue, int 
nDecPlaces,
 if (fValue == 0.0)
 return fValue;
 
-if ( nDecPlaces == 0 && eMode == rtl_math_RoundingMode_Corrected )
-return std::round( fValue );
+if (nDecPlaces == 0)
+{
+switch (eMode)
+{
+case rtl_math_RoundingMode_Corrected:
+return std::round(fValue);
+case rtl_math_RoundingMode_HalfEven:
+if (const int oldMode = std::fegetround(); 
std::fesetround(FE_TONEAREST) == 0)
+{
+fValue = std::nearbyint(fValue);
+std::fesetround(oldMode);
+return fValue;
+}
+break;
+default:
+break;
+}
+}
 
 const double fOrigValue = fValue;
 
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2021-05-22 Thread Eike Rathke (via logerrit)
 sal/rtl/math.cxx |   41 ++---
 1 file changed, 26 insertions(+), 15 deletions(-)

New commits:
commit 49af7e22e61c2e5d440ad55cd362388983e128ae
Author: Eike Rathke 
AuthorDate: Sat May 22 20:46:35 2021 +0200
Commit: Eike Rathke 
CommitDate: Sun May 23 03:04:56 2021 +0200

Related: tdf#136794 tdf#137063 Unlimit decimals in rtl_math_round()

Change-Id: I7f9fb45824458d578ee302a668386fe0c3c80974
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116010
Reviewed-by: Eike Rathke 
Tested-by: Jenkins

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index a85c8ac6e959..698e158df072 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -1131,8 +1131,6 @@ double SAL_CALL rtl_math_round(double fValue, int 
nDecPlaces,
enum rtl_math_RoundingMode eMode)
 SAL_THROW_EXTERN_C()
 {
-OSL_ASSERT(nDecPlaces >= -20 && nDecPlaces <= 20);
-
 if (!std::isfinite(fValue))
 return fValue;
 
@@ -1142,6 +1140,8 @@ double SAL_CALL rtl_math_round(double fValue, int 
nDecPlaces,
 if ( nDecPlaces == 0 && eMode == rtl_math_RoundingMode_Corrected )
 return std::round( fValue );
 
+const double fOrigValue = fValue;
+
 // sign adjustment
 bool bSign = std::signbit( fValue );
 if (bSign)
@@ -1153,42 +1153,50 @@ double SAL_CALL rtl_math_round(double fValue, int 
nDecPlaces,
 if (nDecPlaces >= 0
 && (fValue >= (static_cast(1) << 52)
 || isRepresentableInteger(fValue)))
-return bSign ? -fValue : fValue;
+return fOrigValue;
 
 double fFac = 0;
 if (nDecPlaces != 0)
 {
-if (nDecPlaces > 1 && fValue > 4294967296.0)
+if (nDecPlaces > 0)
 {
-// 4294967296 is 2^32 with room for at least 20 decimals, checking
-// smaller values is not necessary. Lower the limit if more than 20
-// decimals were to be allowed.
-
 // Determine how many decimals are representable in the precision.
 // Anything greater 2^52 and 0.0 was already ruled out above.
 // Theoretically 0.5, 0.25, 0.125, 0.0625, 0.03125, ...
 const sal_math_Double* pd = reinterpret_cast();
 const sal_Int32 nDec = 52 - (pd->parts.exponent - 1023);
+
+if (nDec <= 0)
+{
+assert(!"Shouldn't this had been caught already as large 
number?");
+return fOrigValue;
+}
+
 if (nDec < nDecPlaces)
 nDecPlaces = nDec;
 }
 
-/* TODO: this was without the inverse factor and determining max
- * possible decimals, it could now be adjusted to be more lenient. */
-// max 20 decimals, we don't have unlimited precision
-// #38810# and no overflow on fValue*=fFac
-if (nDecPlaces < -20 || 20 < nDecPlaces || fValue > (DBL_MAX / 1e20))
-return bSign ? -fValue : fValue;
-
 // Avoid 1e-5 (1.0001e-05) and such inaccurate fractional
 // factors that later when dividing back spoil things. For negative
 // decimals divide first with the inverse, then multiply the rounded
 // value back.
 fFac = getN10Exp(abs(nDecPlaces));
+
+if (fFac == 0.0 || (nDecPlaces < 0 && !std::isfinite(fFac)))
+// Underflow, rounding to that many integer positions would be 0.
+return 0.0;
+
+if (!std::isfinite(fFac))
+// Overflow with very small values and high number of decimals.
+return fOrigValue;
+
 if (nDecPlaces < 0)
 fValue /= fFac;
 else
 fValue *= fFac;
+
+if (!std::isfinite(fValue))
+return fOrigValue;
 }
 
 // Round only if not already in distance precision gaps of integers, where
@@ -1276,6 +1284,9 @@ double SAL_CALL rtl_math_round(double fValue, int 
nDecPlaces,
 fValue /= fFac;
 }
 
+if (!std::isfinite(fValue))
+return fOrigValue;
+
 return bSign ? -fValue : fValue;
 }
 
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2021-04-23 Thread Stephan Bergmann (via logerrit)
 sal/rtl/string.cxx  |7 +++
 sal/rtl/ustring.cxx |7 +++
 2 files changed, 14 insertions(+)

New commits:
commit 8ae3ae4bf75fdd0aaa132c956d9da029baa3adc6
Author: Stephan Bergmann 
AuthorDate: Fri Apr 23 13:28:37 2021 +0200
Commit: Stephan Bergmann 
CommitDate: Fri Apr 23 19:06:05 2021 +0200

Step 1 of removing cargo-cult pragma pack around rtl_[u]String

Following up on f62cb40bdfaf41cf8e989640f9be79f652f30914 "Remove dubious 
#pragma
pack" and 9eba8aa38db3a0dc2f7dfaf24a003c16418aef18 "Remove dubious #pragma 
pack"
for O[U]StringLiteral, which argued that the pragma pack around 
rtl_[u]String
are useless cargo cult (paraphrasing):  "All struct member types involved
(oslInterlockedCount aka sal_Int32, sal_Int32 itself, sal_Unicode, and char)
have size <= 4 resp. < 8, so the member alignment ("on a boundary that's 
either
a multiple of [the pragma pack value], or a multiple of the size of the 
member,
whichever is smaller", according to
) is 
not
affected.  And neither are alignof(rtl_String) and alignof(rtl_uString)
affected, which would remain e.g. 4 on x86-64."

(Curiously, the pragma pack value had always been 8 for rtl_String but 4 for
rtl_uString, ever since at least 9399c662f36c385b0c705eb34e636a9aec450282
"initial import".)

The plan is as follows:  In step 1, add temporary static_asserts that state 
the
current alignof/sizeof values; keep this for a while to see that all 
relevant
Windows builds actually agree on these status-quo values.  In step 2, 
remove the
pragma pack cargo cult; keep the static_asserts for a while to see that the
removal has no impact on any of the relevant Windows builds.  Finally, in
step 3, remove the temporary static_asserts again.

Change-Id: I8059ac300cc5b517db4a575f0eaba48678966537
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/114540
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann 

diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index a649406f68ad..94be8029032a 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -33,6 +33,13 @@
 
 #include 
 
+#if defined _WIN32
+// Temporary check to verify that the #pragma pack around rtl_String is indeed 
cargo cult and can
+// safely be removed:
+static_assert(alignof (rtl_String) == 4);
+static_assert(sizeof (rtl_String) == 12);
+#endif
+
 /* === */
 
 #if USE_SDT_PROBES
diff --git a/sal/rtl/ustring.cxx b/sal/rtl/ustring.cxx
index a5efa62d759c..4aac6455 100644
--- a/sal/rtl/ustring.cxx
+++ b/sal/rtl/ustring.cxx
@@ -42,6 +42,13 @@
 
 #include 
 
+#if defined _WIN32
+// Temporary check to verify that the #pragma pack around rtl_uString is 
indeed cargo cult and can
+// safely be removed:
+static_assert(alignof (rtl_uString) == 4);
+static_assert(sizeof (rtl_uString) == 12);
+#endif
+
 /* === */
 
 #if USE_SDT_PROBES
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2021-04-21 Thread Stephan Bergmann (via logerrit)
 sal/rtl/cmdargs.cxx |   14 --
 1 file changed, 8 insertions(+), 6 deletions(-)

New commits:
commit 97351cfe6adcb87ef9076e69312a7208029d6d1a
Author: Stephan Bergmann 
AuthorDate: Tue Apr 20 16:07:21 2021 +0200
Commit: Stephan Bergmann 
CommitDate: Wed Apr 21 08:34:08 2021 +0200

Simplify some code by using OUString instead of raw rtl_uString

Change-Id: I0d795db2e0fc5f5a74fd8437cac46edaabd1336d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/114342
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann 

diff --git a/sal/rtl/cmdargs.cxx b/sal/rtl/cmdargs.cxx
index 8eb328258dfe..bad833077012 100644
--- a/sal/rtl/cmdargs.cxx
+++ b/sal/rtl/cmdargs.cxx
@@ -59,12 +59,14 @@ void init()
 {
 rtl_uString * pArg = nullptr;
 osl_getCommandArg (i, );
-if ((pArg->buffer[0] == '-' || pArg->buffer[0] == '/') &&
- pArg->buffer[1] == 'e' &&
- pArg->buffer[2] == 'n' &&
- pArg->buffer[3] == 'v' &&
- pArg->buffer[4] == ':' &&
-rtl_ustr_indexOfChar (&(pArg->buffer[5]), '=') >= 0 )
+bool env;
+{
+auto const & arg = OUString::unacquired();
+env = (arg.startsWith("-") || arg.startsWith("/")) &&
+arg.match("env:", 1) &&
+arg.indexOf ('=') >= 0;
+}
+if (env )
 {
 // ignore.
 rtl_uString_release (pArg);
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2021-04-20 Thread Stephan Bergmann (via logerrit)
 sal/rtl/bootstrap.cxx |   18 +++---
 1 file changed, 7 insertions(+), 11 deletions(-)

New commits:
commit 72b9ad0bd1f355b8a1cb6ef6960ec7aaf67e4de5
Author: Stephan Bergmann 
AuthorDate: Tue Apr 20 16:06:51 2021 +0200
Commit: Stephan Bergmann 
CommitDate: Tue Apr 20 21:21:16 2021 +0200

Simplify some code by using OUString instead of raw rtl_uString

Change-Id: I62001c05436efe6a5fb6f19fb733e41837c7d9d7
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/114341
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann 

diff --git a/sal/rtl/bootstrap.cxx b/sal/rtl/bootstrap.cxx
index a8dc87b6fd98..4f5c2f732dc1 100644
--- a/sal/rtl/bootstrap.cxx
+++ b/sal/rtl/bootstrap.cxx
@@ -161,21 +161,18 @@ static bool getFromCommandLineArgs(
 sal_Int32 nArgCount = osl_getCommandArgCount();
 for(sal_Int32 i = 0; i < nArgCount; ++ i)
 {
-rtl_uString *pArg = nullptr;
-osl_getCommandArg( i,  );
-if( (pArg->buffer[0] == '-' || pArg->buffer[0] == '/' ) &&
-pArg->buffer[1] == 'e' &&
-pArg->buffer[2] == 'n' &&
-pArg->buffer[3] == 'v' &&
-pArg->buffer[4] == ':' )
+OUString pArg;
+osl_getCommandArg( i,  );
+if( (pArg.startsWith("-") || pArg.startsWith("/") ) &&
+pArg.match("env:", 1) )
 {
-sal_Int32 nIndex = rtl_ustr_indexOfChar( pArg->buffer, '=' );
+sal_Int32 nIndex = pArg.indexOf( '=' );
 
 if( nIndex >= 0 )
 {
 rtl_bootstrap_NameValue nameValue;
-nameValue.sName = OUString( &(pArg->buffer[5]), nIndex - 5 
 );
-nameValue.sValue = OUString( &(pArg->buffer[nIndex+1]) );
+nameValue.sName = pArg.copy( 5, nIndex - 5  );
+nameValue.sValue = pArg.copy( nIndex+1 );
 
 if( i == nArgCount-1 &&
 nameValue.sValue.getLength() &&
@@ -190,7 +187,6 @@ static bool getFromCommandLineArgs(
 tmp.push_back( nameValue );
 }
 }
-rtl_uString_release( pArg );
 };
 return tmp;
 }();
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2021-04-12 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |   98 +---
 1 file changed, 10 insertions(+), 88 deletions(-)

New commits:
commit 27911b0455d8dcc08a0702372492a6ce00250cb7
Author: Mike Kaganski 
AuthorDate: Mon Apr 12 13:33:03 2021 +0300
Commit: Mike Kaganski 
CommitDate: Mon Apr 12 16:34:15 2021 +0200

Use more std lib

Change-Id: Idc9a619562b278eed513033c7450a16516b7d3a5
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113980
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index bdff4bb44647..fdb26fd15cf1 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -60,22 +60,10 @@ template  sal_Int32 getLength( const T* pStr )
 {
 return pStr->length;
 }
-else if constexpr (sizeof(T) == sizeof(char))
-{
-// take advantage of builtin optimisations
-return strlen( pStr);
-}
-else if constexpr (sizeof(T) == sizeof(wchar_t))
-{
-// take advantage of builtin optimisations
-return wcslen(reinterpret_cast(pStr));
-}
 else
 {
-const T* pTempStr = pStr;
-while( *pTempStr )
-pTempStr++;
-return pTempStr-pStr;
+// take advantage of builtin optimisations
+return std::char_traits::length(pStr);
 }
 }
 
@@ -124,40 +112,10 @@ sal_Int32 compare_WithLength 
( const IMPL_RTL_STRCOD
 {
 assert(nStr1Len >= 0);
 assert(nStr2Len >= 0);
-if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char))
-{
-// take advantage of builtin optimisations
-sal_Int32 nMin = std::min(nStr1Len, nStr2Len);
-sal_Int32 nRet = memcmp(pStr1, pStr2, nMin);
-return nRet == 0 ? nStr1Len - nStr2Len : nRet;
-}
-else if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(wchar_t))
-{
-// take advantage of builtin optimisations
-sal_Int32 nMin = std::min(nStr1Len, nStr2Len);
-sal_Int32 nRet = wmemcmp(reinterpret_cast(pStr1),
-reinterpret_cast(pStr2), nMin);
-return nRet == 0 ? nStr1Len - nStr2Len : nRet;
-}
-else
-{
-sal_Int32 nRet = nStr1Len - nStr2Len;
-int nCount = (nRet <= 0) ? nStr1Len : nStr2Len;
-
-while( --nCount >= 0 ) {
-if (*pStr1 != *pStr2) {
-break;
-}
-++pStr1;
-++pStr2;
-}
-
-if( nCount >= 0 )
-nRet = static_cast(IMPL_RTL_USTRCODE( *pStr1 ))
- - static_cast(IMPL_RTL_USTRCODE( *pStr2 ));
-
-return nRet;
-}
+// take advantage of builtin optimisations
+std::basic_string_view aView1(pStr1, nStr1Len);
+std::basic_string_view aView2(pStr2, nStr2Len);
+return aView1.compare(aView2);
 }
 
 /* --- */
@@ -172,46 +130,10 @@ sal_Int32 shortenedCompare_WithLength 
( const IMPL_R
 assert(nStr1Len >= 0);
 assert(nStr2Len >= 0);
 assert(nShortenedLength >= 0);
-if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char))
-{
-// take advantage of builtin optimisations
-sal_Int32 nMin = std::min(std::min(nStr1Len, nStr2Len), 
nShortenedLength);
-sal_Int32 nRet = memcmp(pStr1, pStr2, nMin);
-if (nRet == 0 && nShortenedLength > std::min(nStr1Len, nStr2Len))
-return nStr1Len - nStr2Len;
-return nRet;
-}
-else if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(wchar_t))
-{
-// take advantage of builtin optimisations
-sal_Int32 nMin = std::min(std::min(nStr1Len, nStr2Len), 
nShortenedLength);
-sal_Int32 nRet = wmemcmp(reinterpret_cast(pStr1), 
reinterpret_cast(pStr2), nMin);
-if (nRet == 0 && nShortenedLength > std::min(nStr1Len, nStr2Len))
-return nStr1Len - nStr2Len;
-return nRet;
-}
-else
-{
-const IMPL_RTL_STRCODE* pStr1End = pStr1 + nStr1Len;
-const IMPL_RTL_STRCODE* pStr2End = pStr2 + nStr2Len;
-sal_Int32   nRet;
-while ( (nShortenedLength > 0) &&
-(pStr1 < pStr1End) && (pStr2 < pStr2End) )
-{
-nRet = static_cast(IMPL_RTL_USTRCODE( *pStr1 ))-
-   static_cast(IMPL_RTL_USTRCODE( *pStr2 ));
-if ( nRet )
-return nRet;
-
-nShortenedLength--;
-pStr1++;
-pStr2++;
-}
-
-if ( nShortenedLength <= 0 )
-return 0;
-return nStr1Len - nStr2Len;
-}
+// take advantage of builtin optimisations
+std::basic_string_view aView1(pStr1, std::min(nStr1Len, 
nShortenedLength));
+std::basic_string_view aView2(pStr2, std::min(nStr2Len, 
nShortenedLength));
+return aView1.compare(aView2);
 }
 
 /* --- */

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

2021-04-12 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |  209 ++--
 1 file changed, 25 insertions(+), 184 deletions(-)

New commits:
commit 334838efc12cd1e4a451a8dd7542b93fdaf30bb9
Author: Mike Kaganski 
AuthorDate: Sun Apr 11 08:46:44 2021 +0300
Commit: Mike Kaganski 
CommitDate: Mon Apr 12 08:00:30 2021 +0200

Unify on std::basic_string_view for indexOfStr

A follow-up for 5ebccaa07589383653dbd65e58204a82dd3cde09 and
5a11edc795d8a3ef1e15fc4e251f594911403131

Change-Id: I58c1f518967cb98bd7edecd655d5be0d4657a1ee
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113938
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index d0f853f62feb..bdff4bb44647 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -392,33 +392,13 @@ sal_Int32 indexOfChar_WithLength  
   ( const IMPL_RTL_ST

IMPL_RTL_STRCODE c )
 {
 //assert(nLen >= 0);
-if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char))
-{
-// take advantage of builtin optimisations
-IMPL_RTL_STRCODE* p = 
static_cast(std::memchr(const_cast(pStr), c, nLen));
-return p ? p - pStr : -1;
-}
-else if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char16_t))
-{
-// take advantage of builtin optimisations
-if (nLen <= 0) // char_traits::find takes an unsigned length
-return -1;
-char16_t const * p = std::char_traits::find(pStr, nLen, c);
-return p ? p - pStr : -1;
-}
-else
-{
-const IMPL_RTL_STRCODE* pTempStr = pStr;
-while ( nLen > 0 )
-{
-if ( *pTempStr == c )
-return pTempStr-pStr;
-
-pTempStr++;
-nLen--;
-}
+if (nLen <= 0)
 return -1;
-}
+// take advantage of builtin optimisations
+using my_string_view = std::basic_string_view;
+my_string_view v(pStr, nLen);
+typename my_string_view::size_type idx = v.find(c);
+return idx == my_string_view::npos ? -1 : idx;
 }
 
 /* --- */
@@ -457,33 +437,11 @@ sal_Int32 lastIndexOfChar_WithLength  
   ( const IMPL_RT

IMPL_RTL_STRCODE c )
 {
 assert(nLen >= 0);
-if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char))
-{
-// take advantage of builtin optimisations
-std::string_view v(pStr, nLen);
-std::string_view::size_type idx = v.rfind(c);
-return idx == std::string_view::npos ? -1 : idx;
-}
-else if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char16_t))
-{
-// take advantage of builtin optimisations
-std::u16string_view v(pStr, nLen);
-std::u16string_view::size_type idx = v.rfind(c);
-return idx == std::string_view::npos ? -1 : idx;
-}
-else
-{
-pStr += nLen;
-while ( nLen > 0 )
-{
-nLen--;
-pStr--;
-
-if ( *pStr == c )
-return nLen;
-}
-return -1;
-}
+// take advantage of builtin optimisations
+using my_string_view = std::basic_string_view;
+my_string_view v(pStr, nLen);
+typename my_string_view::size_type idx = v.rfind(c);
+return idx == my_string_view::npos ? -1 : idx;
 }
 
 /* --- */
@@ -529,75 +487,13 @@ sal_Int32 indexOfStr_WithLength   
  ( const IMPL_RTL_STR
 assert(nSubLen >= 0);
 /* an empty SubString is always not findable */
 if ( nSubLen == 0 )
-;
-else if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char))
-{
-// take advantage of builtin optimisations
-std::string_view v(pStr, nStrLen);
-std::string_view needle(pSubStr, nSubLen);
-std::string_view::size_type idx = v.find(needle);
-return idx == std::string_view::npos ? -1 : idx;
-}
-else if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char16_t))
-{
-// take advantage of builtin optimisations
-std::u16string_view v(pStr, nStrLen);
-std::u16string_view needle(pSubStr, nSubLen);
-std::u16string_view::size_type idx = v.find(needle);
-return idx == std::string_view::npos ? -1 : idx;
-}
-else
-{
-/* faster search for a single character */
-if ( nSubLen == 1 )
-{
-IMPL_RTL_STRCODEc = *pSubStr;
-const IMPL_RTL_STRCODE* pTempStr = pStr;
-while ( nStrLen > 0 )
-{
-if ( *pTempStr == c )
-return pTempStr-pStr;
-
-pTempStr++;
-nStrLen--;
-}
-}
-else
-{
-const 

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

2021-04-10 Thread Noel Grandin (via logerrit)
 sal/rtl/strtmpl.hxx |9 -
 1 file changed, 8 insertions(+), 1 deletion(-)

New commits:
commit 5a11edc795d8a3ef1e15fc4e251f594911403131
Author: Noel Grandin 
AuthorDate: Sat Apr 10 17:00:06 2021 +0200
Commit: Noel Grandin 
CommitDate: Sat Apr 10 22:10:02 2021 +0200

use std lib for sal_Unicode version of indexOfChar_WithLength

if possible, which will probably have a better word-at-a-time
algorithm.

Change-Id: Ia338a0aad81ef450d482701139f131d6d577b737
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113922
Tested-by: Jenkins
Reviewed-by: Noel Grandin 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index a2677f7da0f2..d0f853f62feb 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -398,6 +398,14 @@ sal_Int32 indexOfChar_WithLength   
  ( const IMPL_RTL_ST
 IMPL_RTL_STRCODE* p = 
static_cast(std::memchr(const_cast(pStr), c, nLen));
 return p ? p - pStr : -1;
 }
+else if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char16_t))
+{
+// take advantage of builtin optimisations
+if (nLen <= 0) // char_traits::find takes an unsigned length
+return -1;
+char16_t const * p = std::char_traits::find(pStr, nLen, c);
+return p ? p - pStr : -1;
+}
 else
 {
 const IMPL_RTL_STRCODE* pTempStr = pStr;
@@ -409,7 +417,6 @@ sal_Int32 indexOfChar_WithLength
 ( const IMPL_RTL_ST
 pTempStr++;
 nLen--;
 }
-
 return -1;
 }
 }
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2021-04-10 Thread Noel Grandin (via logerrit)
 sal/rtl/strtmpl.hxx |  176 +---
 1 file changed, 113 insertions(+), 63 deletions(-)

New commits:
commit 5ebccaa07589383653dbd65e58204a82dd3cde09
Author: Noel Grandin 
AuthorDate: Sat Apr 10 17:27:08 2021 +0200
Commit: Noel Grandin 
CommitDate: Sat Apr 10 20:57:16 2021 +0200

use std lib for O[U]String indexOfStr

if possible, which will probably have faster implementations.

Change-Id: I403d4c3c0f5407412a2284a90fd5abc083881d18
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113923
Tested-by: Jenkins
Reviewed-by: Noel Grandin 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index f69aad1ee990..a2677f7da0f2 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -450,17 +450,33 @@ sal_Int32 lastIndexOfChar_WithLength  
   ( const IMPL_RT

IMPL_RTL_STRCODE c )
 {
 assert(nLen >= 0);
-pStr += nLen;
-while ( nLen > 0 )
+if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char))
 {
-nLen--;
-pStr--;
-
-if ( *pStr == c )
-return nLen;
+// take advantage of builtin optimisations
+std::string_view v(pStr, nLen);
+std::string_view::size_type idx = v.rfind(c);
+return idx == std::string_view::npos ? -1 : idx;
+}
+else if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char16_t))
+{
+// take advantage of builtin optimisations
+std::u16string_view v(pStr, nLen);
+std::u16string_view::size_type idx = v.rfind(c);
+return idx == std::string_view::npos ? -1 : idx;
 }
+else
+{
+pStr += nLen;
+while ( nLen > 0 )
+{
+nLen--;
+pStr--;
 
-return -1;
+if ( *pStr == c )
+return nLen;
+}
+return -1;
+}
 }
 
 /* --- */
@@ -504,10 +520,28 @@ sal_Int32 indexOfStr_WithLength   
  ( const IMPL_RTL_STR
 {
 assert(nStrLen >= 0);
 assert(nSubLen >= 0);
-/* faster search for a single character */
-if ( nSubLen < 2 )
+/* an empty SubString is always not findable */
+if ( nSubLen == 0 )
+;
+else if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char))
+{
+// take advantage of builtin optimisations
+std::string_view v(pStr, nStrLen);
+std::string_view needle(pSubStr, nSubLen);
+std::string_view::size_type idx = v.find(needle);
+return idx == std::string_view::npos ? -1 : idx;
+}
+else if constexpr (sizeof(IMPL_RTL_STRCODE) == sizeof(char16_t))
+{
+// take advantage of builtin optimisations
+std::u16string_view v(pStr, nStrLen);
+std::u16string_view needle(pSubStr, nSubLen);
+std::u16string_view::size_type idx = v.find(needle);
+return idx == std::string_view::npos ? -1 : idx;
+}
+else
 {
-/* an empty SubString is always not findable */
+/* faster search for a single character */
 if ( nSubLen == 1 )
 {
 IMPL_RTL_STRCODEc = *pSubStr;
@@ -521,42 +555,41 @@ sal_Int32 indexOfStr_WithLength   
  ( const IMPL_RTL_STR
 nStrLen--;
 }
 }
-}
-else
-{
-const IMPL_RTL_STRCODE* pTempStr = pStr;
-while ( nStrLen > 0 )
+else
 {
-if ( *pTempStr == *pSubStr )
+const IMPL_RTL_STRCODE* pTempStr = pStr;
+while ( nStrLen > 0 )
 {
-/* Compare SubString */
-if ( nSubLen <= nStrLen )
+if ( *pTempStr == *pSubStr )
 {
-const IMPL_RTL_STRCODE* pTempStr1 = pTempStr;
-const IMPL_RTL_STRCODE* pTempStr2 = pSubStr;
-sal_Int32   nTempLen = nSubLen;
-while ( nTempLen )
+/* Compare SubString */
+if ( nSubLen <= nStrLen )
 {
-if ( *pTempStr1 != *pTempStr2 )
-break;
-
-pTempStr1++;
-pTempStr2++;
-nTempLen--;
+const IMPL_RTL_STRCODE* pTempStr1 = pTempStr;
+const IMPL_RTL_STRCODE* pTempStr2 = pSubStr;
+sal_Int32   nTempLen = nSubLen;
+while ( nTempLen )
+{
+if ( *pTempStr1 != *pTempStr2 )
+break;
+
+pTempStr1++;
+pTempStr2++;
+nTempLen--;
+}
+
+if ( !nTempLen )
+ 

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

2021-03-15 Thread Mike Kaganski (via logerrit)
 sal/rtl/strtmpl.hxx |   48 
 1 file changed, 20 insertions(+), 28 deletions(-)

New commits:
commit 760a902a3314b7fde8751b0df67c024b8d5935f3
Author: Mike Kaganski 
AuthorDate: Mon Mar 15 11:55:08 2021 +0100
Commit: Mike Kaganski 
CommitDate: Mon Mar 15 13:55:04 2021 +0100

Deduplicate trim implementations

Change-Id: I9233e9d6f11d9abf90ff27769673ba67555a9dde
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/112430
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index b63d9ff35415..f69aad1ee990 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -28,6 +28,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #include 
@@ -738,7 +739,7 @@ template  sal_Int32 trim( 
IMPL_RTL_STRCODE* pStr )
 /* --- */
 
 template 
-sal_Int32 trim_WithLength( IMPL_RTL_STRCODE* pStr, sal_Int32 nLen )
+std::basic_string_view trimView( IMPL_RTL_STRCODE* pStr, 
sal_Int32 nLen )
 {
 assert(nLen >= 0);
 sal_Int32 nPreSpaces= 0;
@@ -754,18 +755,22 @@ sal_Int32 trim_WithLength( IMPL_RTL_STRCODE* pStr, 
sal_Int32 nLen )
 nIndex--;
 }
 
-if ( nPostSpaces )
-{
-nLen -= nPostSpaces;
-*(pStr+nLen) = 0;
-}
+return { pStr + nPreSpaces, static_cast(nLen - nPostSpaces - 
nPreSpaces) };
+}
+
+/* --- */
+
+template 
+sal_Int32 trim_WithLength( IMPL_RTL_STRCODE* pStr, sal_Int32 nLen )
+{
+const auto view = trimView(pStr, nLen);
 
-if ( nPreSpaces )
+if (static_cast(view.size()) != nLen)
 {
-nLen -= nPreSpaces;
-memmove(pStr, pStr + nPreSpaces, nLen * sizeof(IMPL_RTL_STRCODE));
-pStr += nLen;
-*pStr = 0;
+nLen = static_cast(view.size());
+if (view.data() != pStr)
+memmove(pStr, view.data(), nLen * sizeof(IMPL_RTL_STRCODE));
+*(pStr+nLen) = 0;
 }
 
 return nLen;
@@ -1825,32 +1830,19 @@ void newTrim( 
IMPL_RTL_STRINGDATA** ppThis,
 assert(ppThis);
 assert(pStr);
 IMPL_RTL_STRINGDATA*pOrg= *ppThis;
-const auto* pCharStr= pStr->buffer;
-sal_Int32   nPreSpaces  = 0;
-sal_Int32   nPostSpaces = 0;
-sal_Int32   nLen= pStr->length;
-sal_Int32   nIndex  = nLen-1;
-
-while ( (nPreSpaces < nLen) && rtl_ImplIsWhitespace( 
USTRCODE(*(pCharStr+nPreSpaces)) ) )
-nPreSpaces++;
-
-while ( (nIndex > nPreSpaces) && rtl_ImplIsWhitespace( 
USTRCODE(*(pCharStr+nIndex)) ) )
-{
-nPostSpaces++;
-nIndex--;
-}
+const auto view = trimView(pStr->buffer, pStr->length);
 
-if ( !nPreSpaces && !nPostSpaces )
+if (static_cast(view.size()) == pStr->length)
 {
 *ppThis = pStr;
 acquire( pStr );
 }
 else
 {
-nLen -= nPostSpaces+nPreSpaces;
+sal_Int32 nLen = static_cast(view.size());
 *ppThis = Alloc( nLen );
 assert(*ppThis);
-Copy( (*ppThis)->buffer, pStr->buffer+nPreSpaces, nLen );
+Copy( (*ppThis)->buffer, view.data(), nLen );
 }
 
 RTL_LOG_STRING_NEW( *ppThis );
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2021-01-10 Thread Stephan Bergmann (via logerrit)
 sal/rtl/strtmpl.hxx |2 ++
 1 file changed, 2 insertions(+)

New commits:
commit 91291170dd9688ad3f17e0aad03aa349be81db05
Author: Stephan Bergmann 
AuthorDate: Sun Jan 10 17:14:17 2021 +0100
Commit: Stephan Bergmann 
CommitDate: Sun Jan 10 20:12:22 2021 +0100

Missing include guard in new sal/rtl/strtmpl.hxx

...as causing trouble in ASan/UBSan builds,

> [CXX] jurt/source/pipe/staticsalhack.cxx
> In file included from jurt/source/pipe/staticsalhack.cxx:20:
> In file included from sal/rtl/ustring.cxx:51:
> sal/rtl/strtmpl.hxx:38:28: error: redefinition of 'IMPL_RTL_USTRCODE'
> template  auto IMPL_RTL_USTRCODE(C c) { return 
std::make_unsigned_t(c); }
>^
> sal/rtl/strtmpl.hxx:38:28: note: previous definition is here
> template  auto IMPL_RTL_USTRCODE(C c) { return 
std::make_unsigned_t(c); }
>^
[...]

Change-Id: Ied38a5f96bbb6d1c0d8ae956629ca6be64dc273f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/109055
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann 

diff --git a/sal/rtl/strtmpl.hxx b/sal/rtl/strtmpl.hxx
index 78d2e023e459..b63d9ff35415 100644
--- a/sal/rtl/strtmpl.hxx
+++ b/sal/rtl/strtmpl.hxx
@@ -22,6 +22,8 @@
 /* String-Class*/
 /* === */
 
+#pragma once
+
 #include 
 #include 
 #include 
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


[Libreoffice-commits] core.git: sal/rtl solenv/clang-format

2021-01-10 Thread Mike Kaganski (via logerrit)
 sal/rtl/string.cxx  |  372 +++--
 sal/rtl/strtmpl.hxx |  846 +++-
 sal/rtl/ustring.cxx |  392 --
 solenv/clang-format/excludelist |2 
 4 files changed, 1113 insertions(+), 499 deletions(-)

New commits:
commit c5c0f8df855dfd4478c57170e251e4a9bcff1c10
Author: Mike Kaganski 
AuthorDate: Wed Jan 6 21:17:56 2021 +0300
Commit: Mike Kaganski 
CommitDate: Sun Jan 10 10:05:27 2021 +0100

Replace strtmpl.cxx with actual C++ templates in strtmpl.hxx

This has at least better IDE support, with easier lookup for function
implementations.

Change-Id: I0e4cfe40df036efa796c057852bd5cb4421507f1
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/108931
Tested-by: Jenkins
Reviewed-by: Mike Kaganski 

diff --git a/sal/rtl/string.cxx b/sal/rtl/string.cxx
index aa6e2f58f3ae..a649406f68ad 100644
--- a/sal/rtl/string.cxx
+++ b/sal/rtl/string.cxx
@@ -35,10 +35,17 @@
 
 /* === */
 
+#if USE_SDT_PROBES
+#define RTL_LOG_STRING_BITS 8
+#endif
+
+#include "strtmpl.hxx"
+
 /* static data to be referenced by all empty strings
  * the refCount is predefined to 1 and must never become 0 !
  */
-rtl_String const aImplEmpty_rtl_String =
+template<>
+rtl_String rtl::str::EmptyStringImpl::data =
 {
 SAL_STRING_STATIC_FLAG|1,
 /* sal_Int32refCount;   */
@@ -47,39 +54,6 @@ rtl_String const aImplEmpty_rtl_String =
 };
 
 /* === */
-/* These macros are for the "poor-man templates" included from
- * the strtmpl.cxx just below, used to share code between here and
- * ustring.cxx
- */
-
-#define IMPL_RTL_IS_USTRING 0
-
-#define IMPL_RTL_STRCODEchar
-#define IMPL_RTL_USTRCODE( c )  (static_cast(c))
-#define IMPL_RTL_STRNAME( n )   rtl_str_ ## n
-
-#define IMPL_RTL_STRINGNAME( n )rtl_string_ ## n
-#define IMPL_RTL_STRINGDATA rtl_String
-#define IMPL_RTL_EMPTYSTRINGaImplEmpty_rtl_String
-
-#if USE_SDT_PROBES
-#define RTL_LOG_STRING_BITS 8
-#endif
-
-/* === */
-
-/* Include String/UString template code */
-
-#include "strtmpl.cxx"
-
-#undef IMPL_RTL_EMPTYSTRING
-#undef IMPL_RTL_IS_USTRING
-#undef IMPL_RTL_STRCODE
-#undef IMPL_RTL_STRINGDATA
-#undef IMPL_RTL_STRINGNAME
-#undef IMPL_RTL_STRNAME
-#undef IMPL_RTL_USTRCODE
-#undef RTL_LOG_STRING_BITS
 
 sal_Int32 SAL_CALL rtl_str_valueOfFloat(char * pStr, float f)
 SAL_THROW_EXTERN_C()
@@ -293,7 +267,7 @@ static bool rtl_impl_convertUStringToString(rtl_String ** 
pTarget,
 {
 rtl_String* pTemp2 = rtl_string_ImplAlloc( nDestBytes );
 OSL_ASSERT(pTemp2 != nullptr);
-rtl_str_ImplCopy( pTemp2->buffer, pTemp->buffer, nDestBytes );
+rtl::str::Copy(pTemp2->buffer, pTemp->buffer, nDestBytes);
 rtl_freeString( pTemp );
 pTemp = pTemp2;
 }
@@ -391,4 +365,332 @@ void rtl_string_newReplaceAll(
 }
 }
 
+sal_Int32 SAL_CALL rtl_str_getLength(const char* pStr) SAL_THROW_EXTERN_C()
+{
+return rtl::str::getLength(pStr);
+}
+
+sal_Int32 SAL_CALL rtl_str_compare(const char* pStr1, const char* pStr2) 
SAL_THROW_EXTERN_C()
+{
+return rtl::str::compare(pStr1, pStr2);
+}
+
+sal_Int32 SAL_CALL rtl_str_compare_WithLength(const char* pStr1, sal_Int32 
nStr1Len,
+  const char* pStr2, sal_Int32 
nStr2Len)
+SAL_THROW_EXTERN_C()
+{
+return rtl::str::compare_WithLength(pStr1, nStr1Len, pStr2, nStr2Len);
+}
+
+sal_Int32 SAL_CALL rtl_str_shortenedCompare_WithLength(const char* pStr1, 
sal_Int32 nStr1Len,
+   const char* pStr2, 
sal_Int32 nStr2Len,
+   sal_Int32 
nShortenedLength)
+SAL_THROW_EXTERN_C()
+{
+return rtl::str::shortenedCompare_WithLength(pStr1, nStr1Len, pStr2, 
nStr2Len, nShortenedLength);
+}
+
+sal_Int32 SAL_CALL rtl_str_reverseCompare_WithLength(const char* pStr1, 
sal_Int32 nStr1Len,
+ const char* pStr2, 
sal_Int32 nStr2Len)
+SAL_THROW_EXTERN_C()
+{
+return rtl::str::reverseCompare_WithLength(pStr1, nStr1Len, pStr2, 
nStr2Len);
+}
+
+sal_Int32 SAL_CALL rtl_str_compareIgnoreAsciiCase(const char* pStr1, const 
char* pStr2)
+SAL_THROW_EXTERN_C()
+{
+return rtl::str::compareIgnoreAsciiCase(pStr1, pStr2);
+}
+
+sal_Int32 SAL_CALL rtl_str_compareIgnoreAsciiCase_WithLength(const char* 
pStr1, sal_Int32 nStr1Len,
+ const char* 
pStr2, sal_Int32 nStr2Len)
+SAL_THROW_EXTERN_C()
+{
+return rtl::str::compareIgnoreAsciiCase_WithLength(pStr1, nStr1Len, pStr2, 
nStr2Len);
+}
+
+sal_Int32 SAL_CALL 

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

2020-12-17 Thread Eike Rathke (via logerrit)
 sal/rtl/math.cxx |  128 ---
 1 file changed, 67 insertions(+), 61 deletions(-)

New commits:
commit ecfcd99abd3f7dfe68a306dd8045d2da79e42d74
Author: Eike Rathke 
AuthorDate: Thu Dec 17 23:25:07 2020 +0100
Commit: Eike Rathke 
CommitDate: Fri Dec 18 03:17:21 2020 +0100

Check intermediate for not to be rounded value, tdf#138360 follow-up

Change-Id: I98cc25267e7a10c34179bab50d19f49436e1c48c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/107929
Tested-by: Jenkins
Reviewed-by: Eike Rathke 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 527b508e848c..a85c8ac6e959 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -1191,75 +1191,81 @@ double SAL_CALL rtl_math_round(double fValue, int 
nDecPlaces,
 fValue *= fFac;
 }
 
-switch ( eMode )
+// Round only if not already in distance precision gaps of integers, where
+// for [2^52,2^53) adding 0.5 would even yield the next representable
+// integer.
+if (fValue < (static_cast(1) << 52))
 {
-case rtl_math_RoundingMode_Corrected :
-fValue = rtl::math::approxFloor(fValue + 0.5);
-break;
-case rtl_math_RoundingMode_Down:
-fValue = rtl::math::approxFloor(fValue);
-break;
-case rtl_math_RoundingMode_Up:
-fValue = rtl::math::approxCeil(fValue);
-break;
-case rtl_math_RoundingMode_Floor:
-fValue = bSign ? rtl::math::approxCeil(fValue)
-: rtl::math::approxFloor( fValue );
-break;
-case rtl_math_RoundingMode_Ceiling:
-fValue = bSign ? rtl::math::approxFloor(fValue)
-: rtl::math::approxCeil(fValue);
-break;
-case rtl_math_RoundingMode_HalfDown :
-{
-double f = floor(fValue);
-fValue = ((fValue - f) <= 0.5) ? f : ceil(fValue);
-}
-break;
-case rtl_math_RoundingMode_HalfUp:
+switch ( eMode )
 {
-double f = floor(fValue);
-fValue = ((fValue - f) < 0.5) ? f : ceil(fValue);
-}
-break;
-case rtl_math_RoundingMode_HalfEven:
+case rtl_math_RoundingMode_Corrected :
+fValue = rtl::math::approxFloor(fValue + 0.5);
+break;
+case rtl_math_RoundingMode_Down:
+fValue = rtl::math::approxFloor(fValue);
+break;
+case rtl_math_RoundingMode_Up:
+fValue = rtl::math::approxCeil(fValue);
+break;
+case rtl_math_RoundingMode_Floor:
+fValue = bSign ? rtl::math::approxCeil(fValue)
+: rtl::math::approxFloor( fValue );
+break;
+case rtl_math_RoundingMode_Ceiling:
+fValue = bSign ? rtl::math::approxFloor(fValue)
+: rtl::math::approxCeil(fValue);
+break;
+case rtl_math_RoundingMode_HalfDown :
+{
+double f = floor(fValue);
+fValue = ((fValue - f) <= 0.5) ? f : ceil(fValue);
+}
+break;
+case rtl_math_RoundingMode_HalfUp:
+{
+double f = floor(fValue);
+fValue = ((fValue - f) < 0.5) ? f : ceil(fValue);
+}
+break;
+case rtl_math_RoundingMode_HalfEven:
 #if defined FLT_ROUNDS
-/*
-Use fast version. FLT_ROUNDS may be defined to a function by some 
compilers!
-
-DBL_EPSILON is the smallest fractional number which can be represented,
-its reciprocal is therefore the smallest number that cannot have a
-fractional part. Once you add this reciprocal to `x', its fractional part
-is stripped off. Simply subtracting the reciprocal back out returns `x'
-without its fractional component.
-Simple, clever, and elegant - thanks to Ross Cottrell, the original author,
-who placed it into public domain.
-
-volatile: prevent compiler from being too smart
-*/
-if (FLT_ROUNDS == 1)
-{
-volatile double x = fValue + 1.0 / DBL_EPSILON;
-fValue = x - 1.0 / DBL_EPSILON;
-}
-else
-#endif // FLT_ROUNDS
-{
-double f = floor(fValue);
-if ((fValue - f) != 0.5)
+/*
+   Use fast version. FLT_ROUNDS may be defined to a function 
by some compilers!
+
+   DBL_EPSILON is the smallest fractional number which can be 
represented,
+   its reciprocal is therefore the smallest number that cannot 
have a
+   fractional part. Once you add this reciprocal to `x', its 
fractional part
+   is stripped off. Simply subtracting the reciprocal back out 
returns `x'
+   without its fractional component.
+   

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

2020-12-17 Thread Eike Rathke (via logerrit)
 sal/rtl/math.cxx |7 ---
 1 file changed, 4 insertions(+), 3 deletions(-)

New commits:
commit a10c33fdbe980effc3a14e773d1b94a14be7d428
Author: Eike Rathke 
AuthorDate: Thu Dec 17 22:02:48 2020 +0100
Commit: Eike Rathke 
CommitDate: Fri Dec 18 03:17:03 2020 +0100

Replace log2() call with parts.exponent-1023, tdf#138360 follow-up

... to save some cycles as we anyway need only the integer value
of the exponent and even exactly this value for the number of
possible decimals.

Change-Id: I8d462f53cadde6a95d57d1342d8487fbfa001ae9
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/107928
Tested-by: Jenkins
Reviewed-by: Eike Rathke 

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 46a3e925b95b..527b508e848c 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -1167,9 +1167,10 @@ double SAL_CALL rtl_math_round(double fValue, int 
nDecPlaces,
 // Determine how many decimals are representable in the precision.
 // Anything greater 2^52 and 0.0 was already ruled out above.
 // Theoretically 0.5, 0.25, 0.125, 0.0625, 0.03125, ...
-const double fDec = 52 - log2(fValue) + 1;
-if (fDec < nDecPlaces)
-nDecPlaces = static_cast(fDec);
+const sal_math_Double* pd = reinterpret_cast();
+const sal_Int32 nDec = 52 - (pd->parts.exponent - 1023);
+if (nDec < nDecPlaces)
+nDecPlaces = nDec;
 }
 
 /* TODO: this was without the inverse factor and determining max
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2020-12-08 Thread Stephan Bergmann (via logerrit)
 sal/rtl/strtmpl.cxx |3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

New commits:
commit 1efec9ec21dba32335e311d367b636538e219621
Author: Stephan Bergmann 
AuthorDate: Tue Dec 8 16:25:13 2020 +0100
Commit: Stephan Bergmann 
CommitDate: Tue Dec 8 17:43:30 2020 +0100

Tighten rtl_{string,uString}_newFromStr_WithLength implementation

While the documented interface was already narrow (or at least didn't 
suggest
that the length argument could reasonably be negative), the implementation 
was
somewhat broader:  For one, it allowed the character pointer to be null even
when the length was non-zero, which looks more like a call-site bug than 
like a
useful feature.  And for another, while it did assert that the length is 
non-
negative, it nevertheless then checked "overly defensively" for <= 0 rather 
than
== 0 down the road.

Change-Id: I084148aaa4b9c4aea16729b0ce90b73ccbe73ebe
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/107425
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann 

diff --git a/sal/rtl/strtmpl.cxx b/sal/rtl/strtmpl.cxx
index 082dcd7d3913..bf58db0ba037 100644
--- a/sal/rtl/strtmpl.cxx
+++ b/sal/rtl/strtmpl.cxx
@@ -1356,10 +1356,11 @@ void SAL_CALL IMPL_RTL_STRINGNAME( 
newFromStr_WithLength )( IMPL_RTL_STRINGDATA*
 SAL_THROW_EXTERN_C()
 {
 assert(ppThis);
+assert(pCharStr != nullptr || nLen == 0);
 assert(nLen >= 0);
 IMPL_RTL_STRINGDATA* pOrg;
 
-if ( !pCharStr || (nLen <= 0) )
+if ( nLen == 0 )
 {
 IMPL_RTL_STRINGNAME( new )( ppThis );
 return;
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2020-12-03 Thread Eike Rathke (via logerrit)
 sal/rtl/math.cxx |   14 +++---
 1 file changed, 11 insertions(+), 3 deletions(-)

New commits:
commit 520949f17a91c531ea0c8b3856ffcf3c7ac8a3b2
Author: Eike Rathke 
AuthorDate: Thu Dec 3 18:18:23 2020 +0100
Commit: Eike Rathke 
CommitDate: Thu Dec 3 19:58:43 2020 +0100

Better accuracy in rtl_math_approxValue(), tdf#138360 related

Similar to commit 5abb1890ffafe5a2212076208a1c6e226f1ffa4e for
rtl_math_round() use the reciprocal value in an inverse operation
for negative exponents to not use the inexact 1e-1
0.10001 and so on factors.

Change-Id: I05b852e06f2c31d6e0ce622b07277a81a5690833
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/107172
Reviewed-by: Eike Rathke 
Tested-by: Jenkins

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index a296927635bf..46a3e925b95b 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -1299,16 +1299,24 @@ double SAL_CALL rtl_math_approxValue( double fValue ) 
SAL_THROW_EXTERN_C()
 
 int nExp = static_cast< int >(floor(log10(fValue)));
 nExp = 14 - nExp;
-double fExpValue = getN10Exp(nExp);
+double fExpValue = getN10Exp(abs(nExp));
+
+if (nExp < 0)
+fValue /= fExpValue;
+else
+fValue *= fExpValue;
 
-fValue *= fExpValue;
 // If the original value was near DBL_MIN we got an overflow. Restore and
 // bail out.
 if (!std::isfinite(fValue))
 return fOrigValue;
 
 fValue = std::round(fValue);
-fValue /= fExpValue;
+
+if (nExp < 0)
+fValue *= fExpValue;
+else
+fValue /= fExpValue;
 
 // If the original value was near DBL_MAX we got an overflow. Restore and
 // bail out.
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


[Libreoffice-commits] core.git: sal/rtl sc/qa

2020-12-02 Thread Eike Rathke (via logerrit)
 sal/rtl/math.cxx  |   24 
++
 sc/qa/unit/data/functions/addin/fods/convert.fods |6 +-
 sc/qa/unit/data/functions/financial/fods/nper.fods|2 
 sc/qa/unit/data/functions/fods/gammaln.precise.fods   |4 -
 sc/qa/unit/data/functions/mathematical/fods/convert_add.fods  |6 +-
 sc/qa/unit/data/functions/statistical/fods/chisq.inv.fods |   12 ++---
 sc/qa/unit/data/functions/statistical/fods/forecast.ets.mult.fods |   14 ++---
 sc/qa/unit/data/functions/statistical/fods/gammaln.fods   |4 -
 sc/qa/unit/data/functions/statistical/fods/gammaln.precise.fods   |4 -
 sc/qa/unit/data/functions/statistical/fods/geomean.fods   |2 
 sc/qa/unit/data/functions/statistical/fods/harmean.fods   |6 +-
 sc/qa/unit/data/functions/statistical/fods/lognorm.inv.fods   |4 -
 sc/qa/unit/data/functions/statistical/fods/stdev.fods |4 -
 sc/qa/unit/data/functions/statistical/fods/stdev.p.fods   |6 +-
 sc/qa/unit/data/functions/statistical/fods/stdev.s.fods   |4 -
 sc/qa/unit/data/functions/statistical/fods/stdeva.fods|4 -
 sc/qa/unit/data/functions/statistical/fods/stdevp.fods|6 +-
 17 files changed, 49 insertions(+), 63 deletions(-)

New commits:
commit deb119e415213716a76b9b489a700949c031c6fe
Author: Eike Rathke 
AuthorDate: Wed Dec 2 22:21:12 2020 +0100
Commit: Eike Rathke 
CommitDate: Thu Dec 3 01:28:18 2020 +0100

Related: tdf#138360 Use approxFloor() in rtl_math_round()

Ditch mostly but not always working correction value and use
approxFloor() instead which yields better results. With this we
now have one single place approxValue() in case more fine grained
tuning is needed.

Unfortunately all numeric spreadsheet function tests use ROUND()
in a manner such that they mostly blindly do a ROUND(...;12)
regardless of magnitude, sometimes effectively rounding to the
14th significant digit that may fail in cases like for

14.2040730851385
  ^
where the constant (rounded) value is stored as is but the
calculated value is
14.204073085138471
and the old round() yielded
14.204073085139 for both but the new round() more correctly
results in
14.204073085139 and
14.204073085138
so the spreadsheet test case sources had to be changed to
ROUND(...;11) in such places.

Change-Id: I211bb868a4f4dc9e68f4c7dcc2a187b5e175416f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/107135
Reviewed-by: Eike Rathke 
Tested-by: Jenkins

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index e6f09f18030e..a296927635bf 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -1133,6 +1133,9 @@ double SAL_CALL rtl_math_round(double fValue, int 
nDecPlaces,
 {
 OSL_ASSERT(nDecPlaces >= -20 && nDecPlaces <= 20);
 
+if (!std::isfinite(fValue))
+return fValue;
+
 if (fValue == 0.0)
 return fValue;
 
@@ -1190,24 +1193,7 @@ double SAL_CALL rtl_math_round(double fValue, int 
nDecPlaces,
 switch ( eMode )
 {
 case rtl_math_RoundingMode_Corrected :
-{
-int nExp;   // exponent for correction
-if ( fValue > 0.0 )
-nExp = static_cast( floor( log10( fValue ) ) );
-else
-nExp = 0;
-
-int nIndex;
-
-if (nExp < 0)
-nIndex = 15;
-else if (nExp >= 14)
-nIndex = 0;
-else
-nIndex = 15 - nExp;
-
-fValue = floor(fValue + 0.5 + nCorrVal[nIndex]);
-}
+fValue = rtl::math::approxFloor(fValue + 0.5);
 break;
 case rtl_math_RoundingMode_Down:
 fValue = rtl::math::approxFloor(fValue);
@@ -1321,7 +1307,7 @@ double SAL_CALL rtl_math_approxValue( double fValue ) 
SAL_THROW_EXTERN_C()
 if (!std::isfinite(fValue))
 return fOrigValue;
 
-fValue = rtl_math_round(fValue, 0, rtl_math_RoundingMode_Corrected);
+fValue = std::round(fValue);
 fValue /= fExpValue;
 
 // If the original value was near DBL_MAX we got an overflow. Restore and
diff --git a/sc/qa/unit/data/functions/addin/fods/convert.fods 
b/sc/qa/unit/data/functions/addin/fods/convert.fods
index 12ba09dcd326..64eb2db5ff82 100644
--- a/sc/qa/unit/data/functions/addin/fods/convert.fods
+++ b/sc/qa/unit/data/functions/addin/fods/convert.fods
@@ -3409,7 +3409,7 @@
  
   14.2857127610345
  
- 
+ 
   TRUE
  
  
@@ -4425,7 +4425,7 @@
  
   11.1445349270435
  
- 
+ 
   TRUE
  
  
@@ -5867,4 +5867,4 @@

   
  
-
\ No newline at end of file
+
diff --git a/sc/qa/unit/data/functions/financial/fods/nper.fods 
b/sc/qa/unit/data/functions/financial/fods/nper.fods
index 2eac9b8f339f..83dc438afa34 100644
--- 

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

2020-12-02 Thread Eike Rathke (via logerrit)
 sal/rtl/math.cxx |2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

New commits:
commit d8e0b1c81ffa16be8aae2231bcd3c02e8c01cf88
Author: Eike Rathke 
AuthorDate: Tue Dec 1 23:22:33 2020 +0100
Commit: Eike Rathke 
CommitDate: Wed Dec 2 11:37:11 2020 +0100

Typo in rounded digit string, tdf#138360 follow-up

Change-Id: Ic436d3e9f0c93cb36c5e4377519f2aeb6b7fbd5f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/107034
Reviewed-by: Eike Rathke 
Tested-by: Jenkins

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 10417742b3a2..e6f09f18030e 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -290,7 +290,7 @@ void doubleToString(typename T::String ** pResult,
 // Writing pDig up to decimals(-1,-2) then appending one digit from
 // pRou xor one or two digits from pSlot[].
 constexpr char pDig[] = "7976931348623157";
-constexpr char pRou[] = "8087931459623267"; // the only up-carry 
is 80
+constexpr char pRou[] = "8087931359623267"; // the only up-carry 
is 80
 static_assert(SAL_N_ELEMENTS(pDig) == SAL_N_ELEMENTS(pRou), "digit 
count mismatch");
 constexpr sal_Int32 nDig2 = RTL_CONSTASCII_LENGTH(pRou) - 2;
 sal_Int32 nCapacity = RTL_CONSTASCII_LENGTH(pRou) + 8;  // + "-1.E+308"
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2020-12-01 Thread Eike Rathke (via logerrit)
 sal/rtl/math.cxx |5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

New commits:
commit 49aff144c72a5258cf2ca392a0cfb7a31fb86819
Author: Eike Rathke 
AuthorDate: Tue Dec 1 19:08:26 2020 +0100
Commit: Eike Rathke 
CommitDate: Wed Dec 2 00:27:30 2020 +0100

Related: tdf#138360 Rounding integers to decimals is futile

Change-Id: Ica25747a26d6c2637c46808d1b73aeeed6e1df37
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/107001
Reviewed-by: Eike Rathke 
Tested-by: Jenkins

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 1d6cb88327f9..10417742b3a2 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -1146,7 +1146,10 @@ double SAL_CALL rtl_math_round(double fValue, int 
nDecPlaces,
 
 // Rounding to decimals between integer distance precision (gaps) does not
 // make sense, do not even try to multiply/divide and introduce inaccuracy.
-if (nDecPlaces >= 0 && fValue >= (static_cast(1) << 52))
+// For same reasons, do not attempt to round integers to decimals.
+if (nDecPlaces >= 0
+&& (fValue >= (static_cast(1) << 52)
+|| isRepresentableInteger(fValue)))
 return bSign ? -fValue : fValue;
 
 double fFac = 0;
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


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

2020-11-28 Thread Eike Rathke (via logerrit)
 sal/rtl/math.cxx |   39 ---
 1 file changed, 36 insertions(+), 3 deletions(-)

New commits:
commit 5abb1890ffafe5a2212076208a1c6e226f1ffa4e
Author: Eike Rathke 
AuthorDate: Sat Nov 28 21:40:01 2020 +0100
Commit: Eike Rathke 
CommitDate: Sat Nov 28 23:09:02 2020 +0100

Resolves: tdf#138360 better accuracy in rtl_math_round()

Decimal negative exponents (powers) are imprecise
1e-1   0.10001
1e-2   0.01
1e-3   0.001
1e-4   0.0001
1e-5   1.0001e-05
1e-6   9.9995e-07
1e-7   9.9995e-08
1e-8   1e-08
1e-9   1.0001e-09
1e-10  1e-10
1e-11  9.9994e-12
1e-12  9.9998e-13
1e-13  1e-13
1e-14  1e-14
1e-15  1.0001e-15
1e-16  9.9998e-17
1e-17  1.0001e-17
1e-18  1.0001e-18
1e-19  9.9998e-20
1e-20  9.9995e-21

so use the positive exponents instead and swap multiplication and
division when scaling and scaling back the value, which multiplies
the rounded value with a precise integer instead of dividing it by
an imprecise fraction.

For a large (absolute) value check if it is roundable to the
desired decimals at all with the binade's distance precision gap
and if not then diminish the decimals parameter. This prevents
possible inaccuracies due to overly scaling the value.

Change-Id: I41a113078031a552cf98d72f8cb2b10bdc88dea4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/106830
Reviewed-by: Eike Rathke 
Tested-by: Jenkins

diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx
index 6ed4906270e0..1d6cb88327f9 100644
--- a/sal/rtl/math.cxx
+++ b/sal/rtl/math.cxx
@@ -1144,16 +1144,44 @@ double SAL_CALL rtl_math_round(double fValue, int 
nDecPlaces,
 if (bSign)
 fValue = -fValue;
 
+// Rounding to decimals between integer distance precision (gaps) does not
+// make sense, do not even try to multiply/divide and introduce inaccuracy.
+if (nDecPlaces >= 0 && fValue >= (static_cast(1) << 52))
+return bSign ? -fValue : fValue;
+
 double fFac = 0;
 if (nDecPlaces != 0)
 {
+if (nDecPlaces > 1 && fValue > 4294967296.0)
+{
+// 4294967296 is 2^32 with room for at least 20 decimals, checking
+// smaller values is not necessary. Lower the limit if more than 20
+// decimals were to be allowed.
+
+// Determine how many decimals are representable in the precision.
+// Anything greater 2^52 and 0.0 was already ruled out above.
+// Theoretically 0.5, 0.25, 0.125, 0.0625, 0.03125, ...
+const double fDec = 52 - log2(fValue) + 1;
+if (fDec < nDecPlaces)
+nDecPlaces = static_cast(fDec);
+}
+
+/* TODO: this was without the inverse factor and determining max
+ * possible decimals, it could now be adjusted to be more lenient. */
 // max 20 decimals, we don't have unlimited precision
 // #38810# and no overflow on fValue*=fFac
 if (nDecPlaces < -20 || 20 < nDecPlaces || fValue > (DBL_MAX / 1e20))
 return bSign ? -fValue : fValue;
 
-fFac = getN10Exp(nDecPlaces);
-fValue *= fFac;
+// Avoid 1e-5 (1.0001e-05) and such inaccurate fractional
+// factors that later when dividing back spoil things. For negative
+// decimals divide first with the inverse, then multiply the rounded
+// value back.
+fFac = getN10Exp(abs(nDecPlaces));
+if (nDecPlaces < 0)
+fValue /= fFac;
+else
+fValue *= fFac;
 }
 
 switch ( eMode )
@@ -1245,7 +1273,12 @@ double SAL_CALL rtl_math_round(double fValue, int 
nDecPlaces,
 }
 
 if (nDecPlaces != 0)
-fValue /= fFac;
+{
+if (nDecPlaces < 0)
+fValue *= fFac;
+else
+fValue /= fFac;
+}
 
 return bSign ? -fValue : fValue;
 }
___
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits


  1   2   3   >