From 808d740477ed8a446d578c6d1ebfb80b19413c7d Mon Sep 17 00:00:00 2001 From: "Ricardo Guzman (Richonguzman)" Date: Tue, 24 Feb 2026 19:29:20 -0300 Subject: [PATCH] update indexOf kill --- src/aprs_is_utils.cpp | 88 +++++++++++++++++++++++-------------------- src/digi_utils.cpp | 12 +++--- src/station_utils.cpp | 4 +- 3 files changed, 57 insertions(+), 47 deletions(-) diff --git a/src/aprs_is_utils.cpp b/src/aprs_is_utils.cpp index 3bd305e..2dc93b9 100644 --- a/src/aprs_is_utils.cpp +++ b/src/aprs_is_utils.cpp @@ -135,30 +135,30 @@ namespace APRS_IS_Utils { } String checkForStartingBytes(const String& packet) { - if (packet.indexOf("\x3c\xff\x01") != -1) { - return packet.substring(0, packet.indexOf("\x3c\xff\x01")); - } else { - return packet; - } + int index = packet.indexOf("\x3c\xff\x01"); + return (index != -1) ? packet.substring(0, index) : packet; } String buildPacketToUpload(const String& packet) { - String packetToUpload = packet.substring(3, packet.indexOf(":")); + int colonIndex = packet.indexOf(":"); + String packetToUpload = packet.substring(3, colonIndex); if (Config.aprs_is.active && passcodeValid && Config.aprs_is.messagesToRF) { packetToUpload += ",qAR,"; } else { packetToUpload += ",qAO,"; } packetToUpload += Config.callsign; - packetToUpload += checkForStartingBytes(packet.substring(packet.indexOf(":"))); + packetToUpload += checkForStartingBytes(packet.substring(colonIndex)); return packetToUpload; } bool processReceivedLoRaMessage(const String& sender, const String& packet, bool thirdParty) { String receivedMessage; - if (packet.indexOf("{") > 0) { // ack? + int leftCurlyBraceIndex = packet.indexOf("{"); + int colonIndex = packet.indexOf(":"); + if (leftCurlyBraceIndex > 0) { // ack? String ackMessage = "ack"; - ackMessage.concat(packet.substring(packet.indexOf("{") + 1)); + ackMessage.concat(packet.substring(leftCurlyBraceIndex + 1)); ackMessage.trim(); //Serial.println(ackMessage); @@ -180,9 +180,9 @@ namespace APRS_IS_Utils { addToBuffer += ":"; addToBuffer += ackMessage; STATION_Utils::addToOutputPacketBuffer(addToBuffer); - receivedMessage = packet.substring(packet.indexOf(":") + 1, packet.indexOf("{")); + receivedMessage = packet.substring(colonIndex + 1, leftCurlyBraceIndex); } else { - receivedMessage = packet.substring(packet.indexOf(":") + 1); + receivedMessage = packet.substring(colonIndex + 1); } if (receivedMessage.indexOf("?") == 0) { if (!Config.display.alwaysOn && Config.display.timeout != 0) { @@ -207,29 +207,30 @@ namespace APRS_IS_Utils { if (Sender != Config.callsign && Utils::callsignIsValid(Sender)) { STATION_Utils::updateLastHeard(Sender); Utils::typeOfPacket(packet.substring(3), 0); // LoRa-APRS - const String& AddresseeAndMessage = packet.substring(packet.indexOf("::") + 2); + int doubleColonIndex = packet.indexOf("::"); + const String& AddresseeAndMessage = packet.substring(doubleColonIndex + 2); String Addressee = AddresseeAndMessage.substring(0, AddresseeAndMessage.indexOf(":")); Addressee.trim(); bool queryMessage = false; - if (packet.indexOf("::") > 10 && Addressee == Config.callsign) { // its a message for me! + if (doubleColonIndex > 10 && Addressee == Config.callsign) { // its a message for me! queryMessage = processReceivedLoRaMessage(Sender, checkForStartingBytes(AddresseeAndMessage), false); } - if (!queryMessage) { - const String& aprsPacket = buildPacketToUpload(packet); - if (!Config.display.alwaysOn && Config.display.timeout != 0) { - displayToggle(true); - } - lastScreenOn = millis(); - #ifdef HAS_A7670 - stationBeacon = true; - A7670_Utils::uploadToAPRSIS(aprsPacket); - stationBeacon = false; - #else - upload(aprsPacket); - #endif - Utils::println("---> Uploaded to APRS-IS"); - displayShow(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine, 0); + if (queryMessage) return; + + const String& aprsPacket = buildPacketToUpload(packet); + if (!Config.display.alwaysOn && Config.display.timeout != 0) { + displayToggle(true); } + lastScreenOn = millis(); + #ifdef HAS_A7670 + stationBeacon = true; + A7670_Utils::uploadToAPRSIS(aprsPacket); + stationBeacon = false; + #else + upload(aprsPacket); + #endif + Utils::println("---> Uploaded to APRS-IS"); + displayShow(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine, 0); } } } @@ -245,26 +246,30 @@ namespace APRS_IS_Utils { outputPacket.concat(",TCPIP,"); outputPacket.concat(Config.callsign); outputPacket.concat("*"); + int colonEqualIndex = packet.indexOf(":="); + int doubleColonIndex = packet.indexOf("::"); + int colonInvAccentIndex = packet.indexOf(":`"); + switch (packetType) { case 0: // gps - if (packet.indexOf(":=") > 0) { - outputPacket += packet.substring(packet.indexOf(":=")); + if (colonEqualIndex > 0) { + outputPacket += packet.substring(colonEqualIndex); } else { outputPacket += packet.substring(packet.indexOf(":!")); } break; case 1: // messages - outputPacket += packet.substring(packet.indexOf("::")); + outputPacket += packet.substring(doubleColonIndex); break; case 2: // status outputPacket += packet.substring(packet.indexOf(":>")); break; case 3: // telemetry - outputPacket += packet.substring(packet.indexOf("::")); + outputPacket += packet.substring(doubleColonIndex); break; case 4: // mic-e - if (packet.indexOf(":`") > 0) { - outputPacket += packet.substring(packet.indexOf(":`")); + if (colonInvAccentIndex > 0) { + outputPacket += packet.substring(colonInvAccentIndex); } else { outputPacket += packet.substring(packet.indexOf(":'")); } @@ -316,18 +321,21 @@ namespace APRS_IS_Utils { if (packet.startsWith("#")) { if (Config.digi.backupDigiMode) lastServerCheck = currentTime; } else { - if (Config.aprs_is.messagesToRF && packet.indexOf("::") > 0) { + int doubleColonIndex = packet.indexOf("::"); + if (Config.aprs_is.messagesToRF && doubleColonIndex > 0) { String Sender = packet.substring(0, packet.indexOf(">")); - const String& AddresseeAndMessage = packet.substring(packet.indexOf("::") + 2); - String Addressee = AddresseeAndMessage.substring(0, AddresseeAndMessage.indexOf(":")); + const String& AddresseeAndMessage = packet.substring(doubleColonIndex + 2); + int colonIndex = AddresseeAndMessage.indexOf(":"); + String Addressee = AddresseeAndMessage.substring(0, colonIndex); Addressee.trim(); if (Addressee == Config.callsign) { // its for me! String receivedMessage; - if (AddresseeAndMessage.indexOf("{") > 0) { // ack? + int curlyBraceIndex = AddresseeAndMessage.indexOf("{"); + if (curlyBraceIndex > 0) { // ack? processAckMessage(Sender, AddresseeAndMessage); - receivedMessage = AddresseeAndMessage.substring(AddresseeAndMessage.indexOf(":") + 1, AddresseeAndMessage.indexOf("{")); + receivedMessage = AddresseeAndMessage.substring(colonIndex + 1, curlyBraceIndex); } else { - receivedMessage = AddresseeAndMessage.substring(AddresseeAndMessage.indexOf(":") + 1); + receivedMessage = AddresseeAndMessage.substring(colonIndex + 1); } if (receivedMessage.indexOf("?") == 0) { Utils::println("Rx Query (APRS-IS) : " + packet); diff --git a/src/digi_utils.cpp b/src/digi_utils.cpp index ac73f07..c7bd7d8 100644 --- a/src/digi_utils.cpp +++ b/src/digi_utils.cpp @@ -61,14 +61,14 @@ namespace DIGI_Utils { int digiMode = Config.digi.mode; String tempPath = path; - if (tempPath.indexOf("WIDE1-1") != -1 && (digiMode == 2 || digiMode == 3)) { // WIDE1-1 Digipeater - if (tempPath.indexOf("*") != -1 ) return ""; // "*" shouldn't be in WIDE1-1 (only) type of packet + if (tempPath.indexOf("WIDE1-1") != -1 && (digiMode == 2 || digiMode == 3)) { // WIDE1-1 + if (tempPath.indexOf("*") != -1 ) return ""; // "*" shouldn't be in WIDE1-1 (only) type of packet tempPath.replace("WIDE1-1", stationCallsign + "*"); } else if (tempPath.indexOf("WIDE2-") != -1 && digiMode == 3) { // WIDE2-n Digipeater tempPath = cleanPathAsterisks(path); - if (path.indexOf("WIDE2-1") != -1) { + if (tempPath.indexOf("WIDE2-1") != -1) { tempPath.replace("WIDE2-1", stationCallsign + "*"); - } else if (path.indexOf("WIDE2-2") != -1) { + } else if (tempPath.indexOf("WIDE2-2") != -1) { tempPath.replace("WIDE2-2", stationCallsign + "*,WIDE2-1"); } else { return ""; @@ -153,8 +153,8 @@ namespace DIGI_Utils { STATION_Utils::updateLastHeard(Sender); Utils::typeOfPacket(temp, 2); // Digi - bool queryMessage = false; - int doubleColonIndex = temp.indexOf("::"); + bool queryMessage = false; + int doubleColonIndex = temp.indexOf("::"); if (doubleColonIndex > 10) { // it's a message String AddresseeAndMessage = temp.substring(doubleColonIndex + 2); String Addressee = AddresseeAndMessage.substring(0, AddresseeAndMessage.indexOf(":")); diff --git a/src/station_utils.cpp b/src/station_utils.cpp index 6831d76..be21b67 100644 --- a/src/station_utils.cpp +++ b/src/station_utils.cpp @@ -25,6 +25,8 @@ #include "utils.h" #include +#define SECS_TO_WAIT 3 // soon to be deleted... + extern Configuration Config; extern uint32_t lastRxTime; @@ -212,7 +214,7 @@ namespace STATION_Utils { } void processOutputPacketBuffer() { - int timeToWait = 3 * 1000; // 3 segs between packet Tx and also Rx ??? + int timeToWait = SECS_TO_WAIT * 1000; // 3 segs between packet Tx and also Rx ??? uint32_t lastRx = millis() - lastRxTime; uint32_t lastTx = millis() - lastTxTime; if (outputPacketBuffer.size() > 0 && lastTx > timeToWait && lastRx > timeToWait) {