From 94bdab8eeaa42ed85700265eccba40370da61a81 Mon Sep 17 00:00:00 2001 From: Laurent Montel Date: Fri, 21 Jun 2024 09:21:37 +0200 Subject: [PATCH] fix: put everything from utils/strings.h into the namespace Utils Closed: #44 --- src/core/codedocument.cpp | 2 +- src/core/textdocument.cpp | 12 ++--- src/core/utils.cpp | 2 +- src/core/utils.h | 14 +++--- src/utils/strings.cpp | 2 +- src/utils/strings.h | 2 +- tests/tst_stringutils.cpp | 98 +++++++++++++++++++-------------------- 7 files changed, 66 insertions(+), 66 deletions(-) diff --git a/src/core/codedocument.cpp b/src/core/codedocument.cpp index 1b43ec2e..42dcc164 100644 --- a/src/core/codedocument.cpp +++ b/src/core/codedocument.cpp @@ -428,7 +428,7 @@ Symbol *CodeDocument::findSymbol(const QString &name, int options) const LOG("CodeDocument::findSymbol", LOG_ARG("text", name), options); auto symbols = this->symbols(); - const auto regexp = (options & FindRegexp) ? createRegularExpression(name, options) : QRegularExpression {}; + const auto regexp = (options & FindRegexp) ? Utils::createRegularExpression(name, options) : QRegularExpression {}; auto byName = [name, options, regexp](Symbol *symbol) { if (options & FindWholeWords) return symbol->name().compare(name, diff --git a/src/core/textdocument.cpp b/src/core/textdocument.cpp index ee2187de..93a3d1d6 100644 --- a/src/core/textdocument.cpp +++ b/src/core/textdocument.cpp @@ -1358,7 +1358,7 @@ bool TextDocument::replaceOne(const QString &before, const QString &after, int o const bool usesRegExp = options & FindRegexp; const bool preserveCase = options & PreserveCase; - auto regexp = createRegularExpression(before, options, usesRegExp); + auto regexp = Utils::createRegularExpression(before, options, usesRegExp); if (find(before, options)) { cursor.beginEditBlock(); auto found = m_document->textCursor(); @@ -1367,9 +1367,9 @@ bool TextDocument::replaceOne(const QString &before, const QString &after, int o QString afterText = after; if (usesRegExp) { QRegularExpressionMatch match = regexp.match(selectedText()); - afterText = expandRegExpReplacement(after, match.capturedTexts()); + afterText = Utils::expandRegExpReplacement(after, match.capturedTexts()); } else if (preserveCase) { - afterText = matchCaseReplacement(cursor.selectedText(), after); + afterText = Utils::matchCaseReplacement(cursor.selectedText(), after); } cursor.insertText(afterText); cursor.endEditBlock(); @@ -1449,7 +1449,7 @@ int TextDocument::replaceAll(const QString &before, const QString &after, int op m_document->setTextCursor(cursor); cursor.beginEditBlock(); - auto regexp = createRegularExpression(before, options, usesRegExp); + auto regexp = Utils::createRegularExpression(before, options, usesRegExp); while (find(before, options)) { const auto found = m_document->textCursor(); cursor.setPosition(found.selectionStart()); @@ -1461,9 +1461,9 @@ int TextDocument::replaceAll(const QString &before, const QString &after, int op QString afterText = after; if (usesRegExp) { QRegularExpressionMatch match = regexp.match(selectedText()); - afterText = expandRegExpReplacement(after, match.capturedTexts()); + afterText = Utils::expandRegExpReplacement(after, match.capturedTexts()); } else if (preserveCase) { - afterText = matchCaseReplacement(cursor.selectedText(), after); + afterText = Utils::matchCaseReplacement(cursor.selectedText(), after); } cursor.insertText(afterText); ++count; diff --git a/src/core/utils.cpp b/src/core/utils.cpp index 7cf53897..66fe98c0 100644 --- a/src/core/utils.cpp +++ b/src/core/utils.cpp @@ -177,7 +177,7 @@ QString Utils::mktemp(const QString &pattern) QString Utils::convertCase(const QString &str, Case from, Case to) { LOG("Utils::convertCase", str, from, to); - return Core::convertCase(str, static_cast<::Core::Case>(from), static_cast<::Core::Case>(to)); + return ::Utils::convertCase(str, static_cast<::Utils::Case>(from), static_cast<::Utils::Case>(to)); } /*! diff --git a/src/core/utils.h b/src/core/utils.h index 31fa42e2..2c9541a1 100644 --- a/src/core/utils.h +++ b/src/core/utils.h @@ -23,12 +23,12 @@ class Utils : public QObject public: enum Case { - CamelCase = static_cast(::Core::Case::CamelCase), - PascalCase = static_cast(::Core::Case::PascalCase), - SnakeCase = static_cast(::Core::Case::SnakeCase), - UpperCase = static_cast(::Core::Case::UpperCase), - KebabCase = static_cast(::Core::Case::KebabCase), - TitleCase = static_cast(::Core::Case::TitleCase), + CamelCase = static_cast(::Utils::Case::CamelCase), + PascalCase = static_cast(::Utils::Case::PascalCase), + SnakeCase = static_cast(::Utils::Case::SnakeCase), + UpperCase = static_cast(::Utils::Case::UpperCase), + KebabCase = static_cast(::Utils::Case::KebabCase), + TitleCase = static_cast(::Utils::Case::TitleCase), }; Q_ENUM(Case) @@ -48,7 +48,7 @@ public slots: static QString mktemp(const QString &pattern); - static QString convertCase(const QString &str, Core::Utils::Case from, Core::Utils::Case to); + static QString convertCase(const QString &str, Case from, Case to); static void copyToClipboard(const QString &text); diff --git a/src/utils/strings.cpp b/src/utils/strings.cpp index d95f7dbe..6584ba84 100644 --- a/src/utils/strings.cpp +++ b/src/utils/strings.cpp @@ -13,7 +13,7 @@ #include #include -namespace Core { +namespace Utils { static QString nextWordInString(const QString &str, QString::const_iterator &i, Case c) { diff --git a/src/utils/strings.h b/src/utils/strings.h index 20cb4b18..5b0a8a4d 100644 --- a/src/utils/strings.h +++ b/src/utils/strings.h @@ -13,7 +13,7 @@ #include #include -namespace Core { +namespace Utils { enum class Case { CamelCase, diff --git a/tests/tst_stringutils.cpp b/tests/tst_stringutils.cpp index 63314e9b..3a92cfe7 100644 --- a/tests/tst_stringutils.cpp +++ b/tests/tst_stringutils.cpp @@ -12,7 +12,7 @@ #include -using namespace Core; +using namespace Utils; class TestStringUtils : public QObject { @@ -21,59 +21,59 @@ class TestStringUtils : public QObject private slots: void test_formatCase() { - QCOMPARE(convertCase("toCamelCase", Core::Case::CamelCase, Core::Case::CamelCase), "toCamelCase"); - QCOMPARE(convertCase("ToCamelCase", Core::Case::PascalCase, Core::Case::CamelCase), "toCamelCase"); - QCOMPARE(convertCase("to_camel_case", Core::Case::SnakeCase, Core::Case::CamelCase), "toCamelCase"); - QCOMPARE(convertCase("to-camel-case", Core::Case::KebabCase, Core::Case::CamelCase), "toCamelCase"); - QCOMPARE(convertCase("TO_CAMEL_CASE", Core::Case::UpperCase, Core::Case::CamelCase), "toCamelCase"); - QCOMPARE(convertCase("To Camel Case", Core::Case::TitleCase, Core::Case::CamelCase), "toCamelCase"); - - QCOMPARE(convertCase("toPascalCase", Core::Case::CamelCase, Core::Case::PascalCase), "ToPascalCase"); - QCOMPARE(convertCase("ToPascalCase", Core::Case::PascalCase, Core::Case::PascalCase), "ToPascalCase"); - QCOMPARE(convertCase("to_pascal_case", Core::Case::SnakeCase, Core::Case::PascalCase), "ToPascalCase"); - QCOMPARE(convertCase("to-pascal-case", Core::Case::KebabCase, Core::Case::PascalCase), "ToPascalCase"); - QCOMPARE(convertCase("TO_PASCAL_CASE", Core::Case::UpperCase, Core::Case::PascalCase), "ToPascalCase"); - QCOMPARE(convertCase("To Pascal Case", Core::Case::TitleCase, Core::Case::PascalCase), "ToPascalCase"); - - QCOMPARE(convertCase("toSnakeCase", Core::Case::CamelCase, Core::Case::SnakeCase), "to_snake_case"); - QCOMPARE(convertCase("ToSnakeCase", Core::Case::PascalCase, Core::Case::SnakeCase), "to_snake_case"); - QCOMPARE(convertCase("to_snake_case", Core::Case::SnakeCase, Core::Case::SnakeCase), "to_snake_case"); - QCOMPARE(convertCase("to-snake-case", Core::Case::KebabCase, Core::Case::SnakeCase), "to_snake_case"); - QCOMPARE(convertCase("TO_SNAKE_CASE", Core::Case::UpperCase, Core::Case::SnakeCase), "to_snake_case"); - QCOMPARE(convertCase("To Snake Case", Core::Case::TitleCase, Core::Case::SnakeCase), "to_snake_case"); - - QCOMPARE(convertCase("toUpperCase", Core::Case::CamelCase, Core::Case::UpperCase), "TO_UPPER_CASE"); - QCOMPARE(convertCase("ToUpperCase", Core::Case::PascalCase, Core::Case::UpperCase), "TO_UPPER_CASE"); - QCOMPARE(convertCase("to_upper_case", Core::Case::SnakeCase, Core::Case::UpperCase), "TO_UPPER_CASE"); - QCOMPARE(convertCase("to-upper-case", Core::Case::KebabCase, Core::Case::UpperCase), "TO_UPPER_CASE"); - QCOMPARE(convertCase("TO_UPPER_CASE", Core::Case::UpperCase, Core::Case::UpperCase), "TO_UPPER_CASE"); - QCOMPARE(convertCase("To Upper Case", Core::Case::TitleCase, Core::Case::UpperCase), "TO_UPPER_CASE"); - - QCOMPARE(convertCase("toKebabCase", Core::Case::CamelCase, Core::Case::KebabCase), "to-kebab-case"); - QCOMPARE(convertCase("ToKebabCase", Core::Case::PascalCase, Core::Case::KebabCase), "to-kebab-case"); - QCOMPARE(convertCase("to_kebab_case", Core::Case::SnakeCase, Core::Case::KebabCase), "to-kebab-case"); - QCOMPARE(convertCase("to-kebab-case", Core::Case::KebabCase, Core::Case::KebabCase), "to-kebab-case"); - QCOMPARE(convertCase("TO_KEBAB_CASE", Core::Case::UpperCase, Core::Case::KebabCase), "to-kebab-case"); - QCOMPARE(convertCase("To Kebab Case", Core::Case::TitleCase, Core::Case::KebabCase), "to-kebab-case"); - - QCOMPARE(convertCase("toTitleCase", Core::Case::CamelCase, Core::Case::TitleCase), "To Title Case"); - QCOMPARE(convertCase("ToTitleCase", Core::Case::PascalCase, Core::Case::TitleCase), "To Title Case"); - QCOMPARE(convertCase("to_title_case", Core::Case::SnakeCase, Core::Case::TitleCase), "To Title Case"); - QCOMPARE(convertCase("to-title-case", Core::Case::KebabCase, Core::Case::TitleCase), "To Title Case"); - QCOMPARE(convertCase("TO_TITLE_CASE", Core::Case::UpperCase, Core::Case::TitleCase), "To Title Case"); - QCOMPARE(convertCase("To Title Case", Core::Case::TitleCase, Core::Case::TitleCase), "To Title Case"); - - QCOMPARE(convertCase("toTitleCaseWithAnException", Core::Case::CamelCase, Core::Case::TitleCase), + QCOMPARE(convertCase("toCamelCase", Utils::Case::CamelCase, Utils::Case::CamelCase), "toCamelCase"); + QCOMPARE(convertCase("ToCamelCase", Utils::Case::PascalCase, Utils::Case::CamelCase), "toCamelCase"); + QCOMPARE(convertCase("to_camel_case", Utils::Case::SnakeCase, Utils::Case::CamelCase), "toCamelCase"); + QCOMPARE(convertCase("to-camel-case", Utils::Case::KebabCase, Utils::Case::CamelCase), "toCamelCase"); + QCOMPARE(convertCase("TO_CAMEL_CASE", Utils::Case::UpperCase, Utils::Case::CamelCase), "toCamelCase"); + QCOMPARE(convertCase("To Camel Case", Utils::Case::TitleCase, Utils::Case::CamelCase), "toCamelCase"); + + QCOMPARE(convertCase("toPascalCase", Utils::Case::CamelCase, Utils::Case::PascalCase), "ToPascalCase"); + QCOMPARE(convertCase("ToPascalCase", Utils::Case::PascalCase, Utils::Case::PascalCase), "ToPascalCase"); + QCOMPARE(convertCase("to_pascal_case", Utils::Case::SnakeCase, Utils::Case::PascalCase), "ToPascalCase"); + QCOMPARE(convertCase("to-pascal-case", Utils::Case::KebabCase, Utils::Case::PascalCase), "ToPascalCase"); + QCOMPARE(convertCase("TO_PASCAL_CASE", Utils::Case::UpperCase, Utils::Case::PascalCase), "ToPascalCase"); + QCOMPARE(convertCase("To Pascal Case", Utils::Case::TitleCase, Utils::Case::PascalCase), "ToPascalCase"); + + QCOMPARE(convertCase("toSnakeCase", Utils::Case::CamelCase, Utils::Case::SnakeCase), "to_snake_case"); + QCOMPARE(convertCase("ToSnakeCase", Utils::Case::PascalCase, Utils::Case::SnakeCase), "to_snake_case"); + QCOMPARE(convertCase("to_snake_case", Utils::Case::SnakeCase, Utils::Case::SnakeCase), "to_snake_case"); + QCOMPARE(convertCase("to-snake-case", Utils::Case::KebabCase, Utils::Case::SnakeCase), "to_snake_case"); + QCOMPARE(convertCase("TO_SNAKE_CASE", Utils::Case::UpperCase, Utils::Case::SnakeCase), "to_snake_case"); + QCOMPARE(convertCase("To Snake Case", Utils::Case::TitleCase, Utils::Case::SnakeCase), "to_snake_case"); + + QCOMPARE(convertCase("toUpperCase", Utils::Case::CamelCase, Utils::Case::UpperCase), "TO_UPPER_CASE"); + QCOMPARE(convertCase("ToUpperCase", Utils::Case::PascalCase, Utils::Case::UpperCase), "TO_UPPER_CASE"); + QCOMPARE(convertCase("to_upper_case", Utils::Case::SnakeCase, Utils::Case::UpperCase), "TO_UPPER_CASE"); + QCOMPARE(convertCase("to-upper-case", Utils::Case::KebabCase, Utils::Case::UpperCase), "TO_UPPER_CASE"); + QCOMPARE(convertCase("TO_UPPER_CASE", Utils::Case::UpperCase, Utils::Case::UpperCase), "TO_UPPER_CASE"); + QCOMPARE(convertCase("To Upper Case", Utils::Case::TitleCase, Utils::Case::UpperCase), "TO_UPPER_CASE"); + + QCOMPARE(convertCase("toKebabCase", Utils::Case::CamelCase, Utils::Case::KebabCase), "to-kebab-case"); + QCOMPARE(convertCase("ToKebabCase", Utils::Case::PascalCase, Utils::Case::KebabCase), "to-kebab-case"); + QCOMPARE(convertCase("to_kebab_case", Utils::Case::SnakeCase, Utils::Case::KebabCase), "to-kebab-case"); + QCOMPARE(convertCase("to-kebab-case", Utils::Case::KebabCase, Utils::Case::KebabCase), "to-kebab-case"); + QCOMPARE(convertCase("TO_KEBAB_CASE", Utils::Case::UpperCase, Utils::Case::KebabCase), "to-kebab-case"); + QCOMPARE(convertCase("To Kebab Case", Utils::Case::TitleCase, Utils::Case::KebabCase), "to-kebab-case"); + + QCOMPARE(convertCase("toTitleCase", Utils::Case::CamelCase, Utils::Case::TitleCase), "To Title Case"); + QCOMPARE(convertCase("ToTitleCase", Utils::Case::PascalCase, Utils::Case::TitleCase), "To Title Case"); + QCOMPARE(convertCase("to_title_case", Utils::Case::SnakeCase, Utils::Case::TitleCase), "To Title Case"); + QCOMPARE(convertCase("to-title-case", Utils::Case::KebabCase, Utils::Case::TitleCase), "To Title Case"); + QCOMPARE(convertCase("TO_TITLE_CASE", Utils::Case::UpperCase, Utils::Case::TitleCase), "To Title Case"); + QCOMPARE(convertCase("To Title Case", Utils::Case::TitleCase, Utils::Case::TitleCase), "To Title Case"); + + QCOMPARE(convertCase("toTitleCaseWithAnException", Utils::Case::CamelCase, Utils::Case::TitleCase), "To Title Case With an Exception"); - QCOMPARE(convertCase("ToTitleCaseWithAnException", Core::Case::PascalCase, Core::Case::TitleCase), + QCOMPARE(convertCase("ToTitleCaseWithAnException", Utils::Case::PascalCase, Utils::Case::TitleCase), "To Title Case With an Exception"); - QCOMPARE(convertCase("to_title_case_with_an_exception", Core::Case::SnakeCase, Core::Case::TitleCase), + QCOMPARE(convertCase("to_title_case_with_an_exception", Utils::Case::SnakeCase, Utils::Case::TitleCase), "To Title Case With an Exception"); - QCOMPARE(convertCase("to-title-case-with-an-exception", Core::Case::KebabCase, Core::Case::TitleCase), + QCOMPARE(convertCase("to-title-case-with-an-exception", Utils::Case::KebabCase, Utils::Case::TitleCase), "To Title Case With an Exception"); - QCOMPARE(convertCase("TO_TITLE_CASE_WITH_AN_EXCEPTION", Core::Case::UpperCase, Core::Case::TitleCase), + QCOMPARE(convertCase("TO_TITLE_CASE_WITH_AN_EXCEPTION", Utils::Case::UpperCase, Utils::Case::TitleCase), "To Title Case With an Exception"); - QCOMPARE(convertCase("To Title Case With an Exception", Core::Case::TitleCase, Core::Case::TitleCase), + QCOMPARE(convertCase("To Title Case With an Exception", Utils::Case::TitleCase, Utils::Case::TitleCase), "To Title Case With an Exception"); }