Skip to content

Commit

Permalink
fixup! refactor: optimize PortMidi namematching by delaying QString c…
Browse files Browse the repository at this point in the history
…reation
  • Loading branch information
Swiftb0y committed Sep 10, 2024
1 parent 25f67f0 commit 208692b
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions src/controllers/midi/portmidienumerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

#include <portmidi.h>

#include <QLatin1StringView>
#include <QLatin1String>
#include <QRegularExpression>

#include "controllers/midi/portmidicontroller.h"
#include "moc_portmidienumerator.cpp"
#include "util/cmdlineargs.h"

using namespace Qt::StringLiterals;
using namespace Qt::Literals::StringLiterals;

Check failure on line 12 in src/controllers/midi/portmidienumerator.cpp

View workflow job for this annotation

GitHub Actions / clazy

no member named 'Literals' in namespace 'Qt'; did you mean 'QtLiterals'?

Check failure on line 12 in src/controllers/midi/portmidienumerator.cpp

View workflow job for this annotation

GitHub Actions / clazy

expected namespace name

Check failure on line 12 in src/controllers/midi/portmidienumerator.cpp

View workflow job for this annotation

GitHub Actions / Ubuntu 22.04

‘Qt::Literals’ has not been declared

Check failure on line 12 in src/controllers/midi/portmidienumerator.cpp

View workflow job for this annotation

GitHub Actions / Ubuntu 22.04

‘StringLiterals’ is not a namespace-name

Check failure on line 12 in src/controllers/midi/portmidienumerator.cpp

View workflow job for this annotation

GitHub Actions / coverage

‘Qt::Literals’ has not been declared

Check failure on line 12 in src/controllers/midi/portmidienumerator.cpp

View workflow job for this annotation

GitHub Actions / coverage

‘StringLiterals’ is not a namespace-name

namespace {

Expand Down Expand Up @@ -41,9 +41,9 @@ const QRegularExpression kOutputRegex(u"^(.*) out( \\d+)?( .*)?$"_s,
const QRegularExpression kDeviceNameRegex(u"^(.*) (\\d+)( [^0-9]+)?$"_s);

Check failure on line 41 in src/controllers/midi/portmidienumerator.cpp

View workflow job for this annotation

GitHub Actions / clazy

no matching literal operator for call to 'operator""_s' with arguments of types 'const char16_t *' and 'unsigned long', and no matching literal operator template

Check failure on line 41 in src/controllers/midi/portmidienumerator.cpp

View workflow job for this annotation

GitHub Actions / Ubuntu 22.04

unable to find string literal operator ‘operator""_s’ with ‘const char16_t [24]’, ‘long unsigned int’ arguments

Check failure on line 41 in src/controllers/midi/portmidienumerator.cpp

View workflow job for this annotation

GitHub Actions / coverage

unable to find string literal operator ‘operator""_s’ with ‘const char16_t [24]’, ‘long unsigned int’ arguments

bool namesMatchRegexes(const QRegularExpression& kInputRegex,
QLatin1StringView input_name,
QLatin1String input_name,
const QRegularExpression& kOutputRegex,
QLatin1StringView output_name) {
QLatin1String output_name) {
QRegularExpressionMatch inputMatch = kInputRegex.match(input_name);
if (inputMatch.hasMatch()) {
QString inputDeviceName = inputMatch.captured(1);
Expand All @@ -61,23 +61,23 @@ bool namesMatchRegexes(const QRegularExpression& kInputRegex,
return false;
}

bool namesMatchMidiPattern(QLatin1StringView input_name,
QLatin1StringView output_name) {
bool namesMatchMidiPattern(QLatin1String input_name,
QLatin1String output_name) {
return namesMatchRegexes(kMidiDeviceNameRegex, input_name, kMidiDeviceNameRegex, output_name);
}

bool namesMatchInOutPattern(QLatin1StringView input_name,
QLatin1StringView output_name) {
bool namesMatchInOutPattern(QLatin1String input_name,
QLatin1String output_name) {
return namesMatchRegexes(kInputRegex, input_name, kOutputRegex, output_name);
}

bool namesMatchPattern(QLatin1String input_name,
QLatin1StringView output_name) {
QLatin1String output_name) {
return namesMatchRegexes(kDeviceNameRegex, input_name, kDeviceNameRegex, output_name);
}

bool namesMatchAllowableEdgeCases(QLatin1StringView input_name,
QLatin1StringView output_name) {
bool namesMatchAllowableEdgeCases(QLatin1String input_name,
QLatin1String output_name) {
// Mac OS 10.12 & Korg Kaoss DJ 1.6:
// Korg Kaoss DJ has input 'KAOSS DJ CONTROL' and output 'KAOSS DJ SOUND'.
// This means it doesn't pass the shouldLinkInputToOutput test. Without an
Expand All @@ -94,20 +94,20 @@ bool namesMatchAllowableEdgeCases(QLatin1StringView input_name,
return false;
}

bool namesMatch(QLatin1StringView input_name, QLatin1StringView output_name) {
bool namesMatch(QLatin1String input_name, QLatin1String output_name) {
return namesMatchMidiPattern(input_name, output_name) ||
namesMatchInOutPattern(input_name, output_name) ||
namesMatchPattern(input_name, output_name);
}

QLatin1StringView trimStart(QLatin1StringView str, QLatin1StringView trim) {
QLatin1String trimStart(QLatin1String str, QLatin1String trim) {

Check warning on line 103 in src/controllers/midi/portmidienumerator.cpp

View workflow job for this annotation

GitHub Actions / coverage

‘QLatin1String {anonymous}::trimStart(QLatin1String, QLatin1String)’ defined but not used [-Wunused-function]
if (str.startsWith(trim, Qt::CaseInsensitive)) {
return str.sliced(trim.length());
}
return str;
}

QString trimMiddle(QLatin1StringView str, QLatin1StringView trim) {
QString trimMiddle(QLatin1String str, QLatin1String trim) {

Check warning on line 110 in src/controllers/midi/portmidienumerator.cpp

View workflow job for this annotation

GitHub Actions / coverage

‘QString {anonymous}::trimMiddle(QLatin1String, QLatin1String)’ defined but not used [-Wunused-function]
int offset = str.indexOf(trim, 0, Qt::CaseInsensitive);
if (offset != -1) {
return QString::fromLatin1(str).replace(offset, trim.length(), " ");
Expand Down Expand Up @@ -135,8 +135,8 @@ PortMidiEnumerator::~PortMidiEnumerator() {
}
}

bool shouldLinkInputToOutput(QLatin1StringView input_name,
QLatin1StringView output_name) {
bool shouldLinkInputToOutput(QLatin1String input_name,
QLatin1String output_name) {
// Early exit.
if (input_name == output_name || namesMatchAllowableEdgeCases(input_name, output_name)) {
return true;
Expand All @@ -145,8 +145,8 @@ bool shouldLinkInputToOutput(QLatin1StringView input_name,
// Some device drivers prepend "To" and "From" to the names of their MIDI
// ports. If the output and input device names don't match, let's try
// trimming those words from the start, and seeing if they then match.
QLatin1StringView input_name_trimmed = trimStart(input_name, "from"_L1);
QLatin1StringView output_name_trimmed = trimStart(output_name, "to"_L1);
QLatin1String input_name_trimmed = trimStart(input_name, "from"_L1);
QLatin1String output_name_trimmed = trimStart(output_name, "to"_L1);

if (input_name_trimmed == output_name_trimmed) {
return true;
Expand All @@ -158,8 +158,8 @@ bool shouldLinkInputToOutput(QLatin1StringView input_name,
if (input_name_trimmed == output_name_trimmed ||
namesMatch(input_name, output_name) ||
namesMatch(input_name_trimmed, output_name_trimmed) ||
namesMatch(QLatin1StringView(input_name_trimmed.latin1()),
QLatin1StringView(output_name_trimmed.latin1()))) {
namesMatch(QLatin1String(input_name_trimmed.latin1()),
QLatin1String(output_name_trimmed.latin1()))) {
return true;
}

Expand All @@ -178,7 +178,7 @@ QList<Controller*> PortMidiEnumerator::queryDevices() {

m_devices.clear();

QMap<int, QLatin1StringView> unassignedOutputDevices;
QMap<int, QLatin1String> unassignedOutputDevices;

// Build a complete list of output devices for later pairing
for (int i = 0; i < numDevices; i++) {
Expand All @@ -191,7 +191,7 @@ QList<Controller*> PortMidiEnumerator::queryDevices() {
}
qDebug() << " Found output device"
<< "#" << i << pDeviceInfo->name;
unassignedOutputDevices[i] = QLatin1StringView(pDeviceInfo->name);
unassignedOutputDevices[i] = QLatin1String(pDeviceInfo->name);
}

// Search for input devices and pair them with output devices if applicable
Expand All @@ -217,7 +217,7 @@ QList<Controller*> PortMidiEnumerator::queryDevices() {
int outputDevIndex = -1;

//Search for a corresponding output device
QMapIterator<int, QLatin1StringView> j(unassignedOutputDevices);
QMapIterator<int, QLatin1String> j(unassignedOutputDevices);
while (j.hasNext()) {
j.next();

Expand Down

0 comments on commit 208692b

Please sign in to comment.