include/comphelper/string.hxx              |   62 +++++++++++++++++++++++++++--
 sdext/source/pdfimport/wrapper/wrapper.cxx |   13 +-----
 sw/source/filter/ascii/wrtasc.cxx          |    3 -
 sw/source/uibase/utlui/unotools.cxx        |    3 -
 4 files changed, 66 insertions(+), 15 deletions(-)

New commits:
commit 9b2ad2e29e69fa5b7561f738cc82e49268f2b4ec
Author:     Noel Grandin <noel.gran...@collabora.co.uk>
AuthorDate: Mon Apr 4 14:02:02 2022 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Tue Apr 5 09:22:48 2022 +0200

    add some comphelper::string::toInt* functions for string_views
    
    Change-Id: Icf42e00575edf8fc847bccf9c9505cd9710fc9b2
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132509
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/include/comphelper/string.hxx b/include/comphelper/string.hxx
index 6ada67a6a381..7d1b549bb9e3 100644
--- a/include/comphelper/string.hxx
+++ b/include/comphelper/string.hxx
@@ -16,9 +16,7 @@
  *   except in compliance with the License. You may obtain a copy of
  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
  */
-
-#ifndef INCLUDED_COMPHELPER_STRING_HXX
-#define INCLUDED_COMPHELPER_STRING_HXX
+#pragma once
 
 #include <sal/config.h>
 
@@ -361,8 +359,64 @@ COMPHELPER_DLLPUBLIC bool 
isdigitAsciiString(std::string_view rString);
  */
 COMPHELPER_DLLPUBLIC bool isdigitAsciiString(std::u16string_view rString);
 
+/** Interpret a string as a long integer.
+
+    This function cannot be used for language-specific conversion.
+
+    @param str
+    a string.
+
+    @param radix
+    the radix.  Must be between RTL_USTR_MIN_RADIX (2) and RTL_USTR_MAX_RADIX
+    (36), inclusive.
+
+    @param nStrLength
+    number of chars to process
+
+    @return
+    the long integer value represented by the string, or 0 if the string does
+    not represent a long integer.
+*/
+inline sal_Int64 toInt64(std::u16string_view str, sal_Int16 radix = 10 )
+{
+    return rtl_ustr_toInt64_WithLength(str.data(), radix, str.size());
+}
+inline sal_Int64 toInt64(std::string_view str, sal_Int16 radix = 10 )
+{
+    return rtl_str_toInt64_WithLength(str.data(), radix, str.size());
+}
+
+/** Interpret a string as an integer.
+
+    This function cannot be used for language-specific conversion.
+
+    @param radix
+    the radix.  Must be between RTL_USTR_MIN_RADIX (2) and RTL_USTR_MAX_RADIX
+    (36), inclusive.
+
+    @param nStrLength
+    number of chars to process
+
+    @return
+    the integer value represented by the string, or 0 if the string does not
+    represent an integer.
+ */
+inline sal_Int32 toInt32( std::u16string_view str, sal_Int16 radix = 10 )
+{
+    sal_Int64 n = rtl_ustr_toInt64_WithLength(str.data(), radix, str.size());
+    if (n < SAL_MIN_INT32 || n > SAL_MAX_INT32)
+        n = 0;
+    return n;
+}
+inline sal_Int32 toInt32( std::string_view str, sal_Int16 radix = 10 )
+{
+    sal_Int64 n = rtl_str_toInt64_WithLength(str.data(), radix, str.size());
+    if (n < SAL_MIN_INT32 || n > SAL_MAX_INT32)
+        n = 0;
+    return n;
 }
 
-#endif
+
+}
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sdext/source/pdfimport/wrapper/wrapper.cxx 
b/sdext/source/pdfimport/wrapper/wrapper.cxx
index 9a79e939046d..9f9e9a3c3c87 100644
--- a/sdext/source/pdfimport/wrapper/wrapper.cxx
+++ b/sdext/source/pdfimport/wrapper/wrapper.cxx
@@ -36,6 +36,7 @@
 #include <sal/log.hxx>
 
 #include <comphelper/propertysequence.hxx>
+#include <comphelper/string.hxx>
 #include <com/sun/star/io/XInputStream.hpp>
 #include <com/sun/star/uno/XComponentContext.hpp>
 #include <com/sun/star/rendering/PathCapType.hpp>
@@ -254,25 +255,19 @@ std::string_view LineParser::readNextToken()
 void LineParser::readInt32( sal_Int32& o_Value )
 {
     std::string_view tok = readNextToken();
-    sal_Int64 n = rtl_str_toInt64_WithLength(tok.data(), 10, tok.size());
-    if (n < SAL_MIN_INT32 || n > SAL_MAX_INT32)
-        n = 0;
-    o_Value = n;
+    o_Value = comphelper::string::toInt32(tok);
 }
 
 sal_Int32 LineParser::readInt32()
 {
     std::string_view tok = readNextToken();
-    sal_Int64 n =rtl_str_toInt64_WithLength(tok.data(), 10, tok.size());
-    if (n < SAL_MIN_INT32 || n > SAL_MAX_INT32)
-        n = 0;
-    return n;
+    return comphelper::string::toInt32(tok);
 }
 
 void LineParser::readInt64( sal_Int64& o_Value )
 {
     std::string_view tok = readNextToken();
-    o_Value = rtl_str_toInt64_WithLength(tok.data(), 10, tok.size());
+    o_Value = comphelper::string::toInt64(tok);
 }
 
 void LineParser::readDouble( double& o_Value )
diff --git a/sw/source/filter/ascii/wrtasc.cxx 
b/sw/source/filter/ascii/wrtasc.cxx
index 1f4153293bc3..c37efe6ee713 100644
--- a/sw/source/filter/ascii/wrtasc.cxx
+++ b/sw/source/filter/ascii/wrtasc.cxx
@@ -29,6 +29,7 @@
 #include <frameformats.hxx>
 #include <sfx2/docfile.hxx>
 #include <sfx2/sfxsids.hrc>
+#include <comphelper/string.hxx>
 
 #include <strings.hrc>
 
@@ -44,7 +45,7 @@ SwASCWriter::SwASCWriter( std::u16string_view rFltNm )
                 if( 5 < rFltNm.size() )
                 {
                     std::u16string_view aFilterNum = rFltNm.substr( 5 );
-                    switch( rtl_ustr_toInt64_WithLength(aFilterNum.data(), 10, 
aFilterNum.size()) )
+                    switch( comphelper::string::toInt32(aFilterNum) )
                     {
                     case 437: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_437 ); 
 break;
                     case 850: aNewOpts.SetCharSet( RTL_TEXTENCODING_IBM_850 ); 
 break;
diff --git a/sw/source/uibase/utlui/unotools.cxx 
b/sw/source/uibase/utlui/unotools.cxx
index 1d17a279674f..62c3397bf11c 100644
--- a/sw/source/uibase/utlui/unotools.cxx
+++ b/sw/source/uibase/utlui/unotools.cxx
@@ -50,6 +50,7 @@
 #include <comphelper/processfactory.hxx>
 #include <comphelper/propertysequence.hxx>
 #include <comphelper/servicehelper.hxx>
+#include <comphelper/string.hxx>
 #include <docsh.hxx>
 #include <editsh.hxx>
 #include <swmodule.hxx>
@@ -499,7 +500,7 @@ void SwOneExampleFrame::PopupHdl(std::string_view rId)
     std::string_view sZoomValue;
     if (o3tl::starts_with(rId, "zoom", &sZoomValue))
     {
-        sal_Int16 nZoom = rtl_str_toInt64_WithLength(sZoomValue.data(), 10, 
sZoomValue.length());
+        sal_Int16 nZoom = comphelper::string::toInt32(sZoomValue);
         uno::Reference< view::XViewSettingsSupplier >  
xSettings(m_xController, uno::UNO_QUERY);
         uno::Reference< beans::XPropertySet >  xViewProps = 
xSettings->getViewSettings();
 

Reply via email to