dbaccess/qa/extras/hsql_schema_import.cxx      |   16 +++---
 dbaccess/source/filter/hsqldb/createparser.cxx |   60 ++++++++++++-------------
 dbaccess/source/filter/hsqldb/createparser.hxx |    4 -
 dbaccess/source/filter/hsqldb/utils.cxx        |   12 ++---
 dbaccess/source/filter/hsqldb/utils.hxx        |    2 
 5 files changed, 48 insertions(+), 46 deletions(-)

New commits:
commit 590323f4235e5ec3de2dc6dee28a4f03288ac6d7
Author:     Noel Grandin <noel.gran...@collabora.co.uk>
AuthorDate: Thu Apr 14 12:19:57 2022 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Thu Apr 14 16:00:47 2022 +0200

    use more string_view in dbaccess
    
    Change-Id: I256ffe22fa060be6a6fc32e73d845879d71a187d
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/133007
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/dbaccess/qa/extras/hsql_schema_import.cxx 
b/dbaccess/qa/extras/hsql_schema_import.cxx
index 6d714ae9a8ae..f5f34d38b69a 100644
--- a/dbaccess/qa/extras/hsql_schema_import.cxx
+++ b/dbaccess/qa/extras/hsql_schema_import.cxx
@@ -62,7 +62,7 @@ public:
 void HsqlSchemaImportTest::testIntegerPrimaryKeyNotNull()
 {
     FbCreateStmtParser aCreateParser;
-    aCreateParser.parse("CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT 
NULL PRIMARY KEY)");
+    aCreateParser.parse(u"CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT 
NULL PRIMARY KEY)");
 
     CPPUNIT_ASSERT_EQUAL(OUString{ "\"myTable\"" }, 
aCreateParser.getTableName());
     const auto& columns = aCreateParser.getColumnDef();
@@ -79,7 +79,7 @@ void HsqlSchemaImportTest::testVarcharWithParam()
 {
     FbCreateStmtParser aCreateParser;
     aCreateParser.parse(
-        "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, 
\"myText\" "
+        u"CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, 
\"myText\" "
         "VARCHAR(50))");
 
     const auto& columns = aCreateParser.getColumnDef();
@@ -101,7 +101,7 @@ void HsqlSchemaImportTest::testVarcharWithoutParam()
 {
     FbCreateStmtParser aCreateParser;
     aCreateParser.parse(
-        "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, 
\"myText\" "
+        u"CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, 
\"myText\" "
         "VARCHAR)");
 
     const auto& columns = aCreateParser.getColumnDef();
@@ -116,7 +116,7 @@ void HsqlSchemaImportTest::testNumericWithTwoParam()
 {
     FbCreateStmtParser aCreateParser;
     aCreateParser.parse(
-        "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, 
\"Betrag\" "
+        u"CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, 
\"Betrag\" "
         "NUMERIC(8,2))");
 
     const auto& columns = aCreateParser.getColumnDef();
@@ -137,7 +137,7 @@ void HsqlSchemaImportTest::testIntegerAutoincremental()
 {
     FbCreateStmtParser aCreateParser;
     aCreateParser.parse(
-        "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY 
GENERATED "
+        u"CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY 
GENERATED "
         "BY DEFAULT AS IDENTITY(START WITH 0), \"myText\" VARCHAR(50))");
 
     const auto& columns = aCreateParser.getColumnDef();
@@ -157,7 +157,7 @@ void HsqlSchemaImportTest::testTimestampWithParam()
 {
     FbCreateStmtParser aCreateParser;
     aCreateParser.parse(
-        "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, 
\"myText\" "
+        u"CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, 
\"myText\" "
         "TIMESTAMP(0))");
 
     const auto& columns = aCreateParser.getColumnDef();
@@ -179,7 +179,7 @@ void HsqlSchemaImportTest::testDefaultValueNow()
 {
     FbCreateStmtParser aCreateParser;
     aCreateParser.parse(
-        "CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, 
\"myDate\" "
+        u"CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT NULL PRIMARY KEY, 
\"myDate\" "
         "TIMESTAMP DEFAULT NOW)");
 
     const auto& columns = aCreateParser.getColumnDef();
@@ -194,7 +194,7 @@ void HsqlSchemaImportTest::testDefaultValueNow()
 void HsqlSchemaImportTest::testEvilNullColumnName()
 {
     FbCreateStmtParser aCreateParser;
-    aCreateParser.parse("CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT 
NULL PRIMARY KEY, "
+    aCreateParser.parse(u"CREATE CACHED TABLE \"myTable\"(\"id\" INTEGER NOT 
NULL PRIMARY KEY, "
                         "\"myEvilNOT NULLName\" "
                         "VARCHAR(20))");
 
diff --git a/dbaccess/source/filter/hsqldb/createparser.cxx 
b/dbaccess/source/filter/hsqldb/createparser.cxx
index c62640e8bc3f..03952655dfd8 100644
--- a/dbaccess/source/filter/hsqldb/createparser.cxx
+++ b/dbaccess/source/filter/hsqldb/createparser.cxx
@@ -23,6 +23,7 @@
 #include "createparser.hxx"
 #include "utils.hxx"
 #include <com/sun/star/sdbc/DataType.hpp>
+#include <o3tl/string_view.hxx>
 
 using namespace ::comphelper;
 using namespace css::sdbc;
@@ -31,16 +32,17 @@ namespace
 {
 /// Returns substring of sSql from the first occurrence of '(' until the
 /// last occurrence of ')' (excluding the parenthesis)
-OUString lcl_getColumnPart(const OUString& sSql)
+std::u16string_view lcl_getColumnPart(std::u16string_view sSql)
 {
-    sal_Int32 nBeginIndex = sSql.indexOf("(") + 1;
-    if (nBeginIndex < 0)
+    size_t nBeginIndex = sSql.find('(');
+    if (nBeginIndex == std::u16string_view::npos)
     {
         SAL_WARN("dbaccess", "No column definitions found");
-        return OUString();
+        return std::u16string_view();
     }
-    sal_Int32 nCount = sSql.lastIndexOf(")") - nBeginIndex;
-    return sSql.copy(nBeginIndex, nCount);
+    sal_Int32 nCount = sSql.rfind(')') - nBeginIndex - 1;
+    auto sPart = sSql.substr(nBeginIndex + 1, nCount);
+    return sPart;
 }
 
 /// Constructs a vector of strings that represents the definitions of each
@@ -80,10 +82,10 @@ sal_Int32 lcl_getAutoIncrementDefault(std::u16string_view 
sColumnDef)
     return -1;
 }
 
-OUString lcl_getDefaultValue(std::u16string_view sColumnDef)
+std::u16string_view lcl_getDefaultValue(std::u16string_view sColumnDef)
 {
     constexpr std::u16string_view DEFAULT_KW = u"DEFAULT";
-    auto nDefPos = sColumnDef.find(DEFAULT_KW);
+    size_t nDefPos = sColumnDef.find(DEFAULT_KW);
     if (nDefPos > 0 && nDefPos != std::u16string_view::npos
         && lcl_getAutoIncrementDefault(sColumnDef) < 0)
     {
@@ -91,12 +93,12 @@ OUString lcl_getDefaultValue(std::u16string_view sColumnDef)
             = o3tl::trim(sColumnDef.substr(nDefPos + DEFAULT_KW.size()));
 
         // next word is the value
-        auto nNextSpace = fromDefault.find(' ');
-        return OUString((nNextSpace > 0 && nNextSpace != 
std::u16string_view::npos)
-                            ? fromDefault.substr(0, nNextSpace)
-                            : fromDefault);
+        size_t nNextSpace = fromDefault.find(' ');
+        return (nNextSpace > 0 && nNextSpace != std::u16string_view::npos)
+                   ? fromDefault.substr(0, nNextSpace)
+                   : fromDefault;
     }
-    return OUString{};
+    return std::u16string_view();
 }
 
 bool lcl_isNullable(std::u16string_view sColumnDef)
@@ -175,15 +177,15 @@ struct ColumnTypeParts
  * Separates full type descriptions (e.g. NUMERIC(5,4)) to type name (NUMERIC) 
and
  * parameters (5,4)
  */
-ColumnTypeParts lcl_getColumnTypeParts(const OUString& sFullTypeName)
+ColumnTypeParts lcl_getColumnTypeParts(std::u16string_view sFullTypeName)
 {
     ColumnTypeParts parts;
-    auto nParenPos = sFullTypeName.indexOf("(");
-    if (nParenPos > 0)
+    auto nParenPos = sFullTypeName.find('(');
+    if (nParenPos > 0 && nParenPos != std::u16string_view::npos)
     {
-        parts.typeName = o3tl::trim(sFullTypeName.subView(0, nParenPos));
-        OUString sParamStr
-            = sFullTypeName.copy(nParenPos + 1, sFullTypeName.indexOf(")") - 
nParenPos - 1);
+        parts.typeName = o3tl::trim(sFullTypeName.substr(0, nParenPos));
+        std::u16string_view sParamStr
+            = sFullTypeName.substr(nParenPos + 1, sFullTypeName.find(')') - 
nParenPos - 1);
         auto sParams = string::split(sParamStr, sal_Unicode(u','));
         for (const auto& sParam : sParams)
         {
@@ -192,7 +194,7 @@ ColumnTypeParts lcl_getColumnTypeParts(const OUString& 
sFullTypeName)
     }
     else
     {
-        parts.typeName = sFullTypeName.trim();
+        parts.typeName = o3tl::trim(sFullTypeName);
         lcl_addDefaultParameters(parts.params, 
lcl_getDataTypeFromHsql(parts.typeName));
     }
     return parts;
@@ -204,13 +206,13 @@ namespace dbahsql
 {
 CreateStmtParser::CreateStmtParser() {}
 
-void CreateStmtParser::parsePrimaryKeys(const OUString& sPrimaryPart)
+void CreateStmtParser::parsePrimaryKeys(std::u16string_view sPrimaryPart)
 {
-    sal_Int32 nParenPos = sPrimaryPart.indexOf("(");
-    if (nParenPos > 0)
+    size_t nParenPos = sPrimaryPart.find('(');
+    if (nParenPos > 0 && nParenPos != std::u16string_view::npos)
     {
-        OUString sParamStr
-            = sPrimaryPart.copy(nParenPos + 1, sPrimaryPart.lastIndexOf(")") - 
nParenPos - 1);
+        std::u16string_view sParamStr
+            = sPrimaryPart.substr(nParenPos + 1, sPrimaryPart.rfind(')') - 
nParenPos - 1);
         auto sParams = string::split(sParamStr, sal_Unicode(u','));
         for (const auto& sParam : sParams)
         {
@@ -271,23 +273,23 @@ void 
CreateStmtParser::parseColumnPart(std::u16string_view sColumnPart)
                                  std::move(typeParts.params), isPrimaryKey,
                                  
lcl_getAutoIncrementDefault(sColumnWithoutName),
                                  lcl_isNullable(sColumnWithoutName), 
bCaseInsensitive,
-                                 lcl_getDefaultValue(sColumnWithoutName));
+                                 
OUString(lcl_getDefaultValue(sColumnWithoutName)));
 
         m_aColumns.push_back(aColDef);
     }
 }
 
-void CreateStmtParser::parse(const OUString& sSql)
+void CreateStmtParser::parse(std::u16string_view sSql)
 {
     // TODO Foreign keys
-    if (!sSql.startsWith("CREATE"))
+    if (!o3tl::starts_with(sSql, u"CREATE"))
     {
         SAL_WARN("dbaccess", "Not a create statement");
         return;
     }
 
     m_sTableName = utils::getTableNameFromStmt(sSql);
-    OUString sColumnPart = lcl_getColumnPart(sSql);
+    std::u16string_view sColumnPart = lcl_getColumnPart(sSql);
     parseColumnPart(sColumnPart);
 }
 
diff --git a/dbaccess/source/filter/hsqldb/createparser.hxx 
b/dbaccess/source/filter/hsqldb/createparser.hxx
index 85610ebfd3f3..a92f745860d5 100644
--- a/dbaccess/source/filter/hsqldb/createparser.hxx
+++ b/dbaccess/source/filter/hsqldb/createparser.hxx
@@ -24,7 +24,7 @@ private:
 
 protected:
     void parseColumnPart(std::u16string_view sColumnPart);
-    void parsePrimaryKeys(const OUString& sPrimaryPart);
+    void parsePrimaryKeys(std::u16string_view sPrimaryPart);
 
 public:
     CreateStmtParser();
@@ -56,7 +56,7 @@ public:
      *
      * @param SQL "CREATE" statement
      */
-    void parse(const OUString& sSql);
+    void parse(std::u16string_view sSql);
 
     /**
      * Recreate the sql statement.
diff --git a/dbaccess/source/filter/hsqldb/utils.cxx 
b/dbaccess/source/filter/hsqldb/utils.cxx
index 10e07cf3ba30..724ffccfb37f 100644
--- a/dbaccess/source/filter/hsqldb/utils.cxx
+++ b/dbaccess/source/filter/hsqldb/utils.cxx
@@ -87,7 +87,7 @@ OUString utils::convertToUTF8(std::string_view original)
     return res;
 }
 
-OUString utils::getTableNameFromStmt(const OUString& sSql)
+OUString utils::getTableNameFromStmt(std::u16string_view sSql)
 {
     auto stmtComponents = comphelper::string::split(sSql, sal_Unicode(u' '));
     assert(stmtComponents.size() > 2);
@@ -103,17 +103,17 @@ OUString utils::getTableNameFromStmt(const OUString& sSql)
     // it may contain spaces if it's put into apostrophes.
     if (wordIter->indexOf("\"") >= 0)
     {
-        sal_Int32 nAposBegin = sSql.indexOf("\"");
-        sal_Int32 nAposEnd = nAposBegin;
+        size_t nAposBegin = sSql.find('"');
+        size_t nAposEnd = nAposBegin;
         bool bProperEndAposFound = false;
         while (!bProperEndAposFound)
         {
-            nAposEnd = sSql.indexOf("\"", nAposEnd + 1);
+            nAposEnd = sSql.find('"', nAposEnd + 1);
             if (sSql[nAposEnd - 1] != u'\\')
                 bProperEndAposFound = true;
         }
-        OUString result = sSql.copy(nAposBegin, nAposEnd - nAposBegin + 1);
-        return result;
+        std::u16string_view result = sSql.substr(nAposBegin, nAposEnd - 
nAposBegin + 1);
+        return OUString(result);
     }
 
     // next word is the table's name
diff --git a/dbaccess/source/filter/hsqldb/utils.hxx 
b/dbaccess/source/filter/hsqldb/utils.hxx
index b8ed1a222c4c..cc3e2f0df16f 100644
--- a/dbaccess/source/filter/hsqldb/utils.hxx
+++ b/dbaccess/source/filter/hsqldb/utils.hxx
@@ -19,7 +19,7 @@ namespace dbahsql::utils
 {
 OUString convertToUTF8(std::string_view original);
 
-OUString getTableNameFromStmt(const OUString& sSql);
+OUString getTableNameFromStmt(std::u16string_view sSql);
 
 void ensureFirebirdTableLength(const OUString& sName);
 }

Reply via email to