Skip to content

Commit 822c2da

Browse files
committed
Issue #5: Tip 3: Passing all strings as const String &. It does show an effect
1 parent cecf952 commit 822c2da

File tree

2 files changed

+42
-42
lines changed

2 files changed

+42
-42
lines changed

src/UniversalTelegramBot.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@
3838
#define ZERO_COPY(STR) ((char*)STR.c_str())
3939
#define BOT_CMD(STR) buildCommand(F(STR))
4040

41-
UniversalTelegramBot::UniversalTelegramBot(String token, Client &client) {
42-
_token = token;
41+
UniversalTelegramBot::UniversalTelegramBot(const String& token, Client &client) {
42+
updateToken(token);
4343
#ifdef ARDUINO_ESP8266_RELEASE_2_5_0
4444
//client->setInsecure();
4545
#endif
4646
this->client = &client;
4747
}
4848

49-
void UniversalTelegramBot::updateToken(String token) {
49+
void UniversalTelegramBot::updateToken(const String& token) {
5050
_token = token;
5151
}
5252

53-
String UniversalTelegramBot::buildCommand(String cmd) {
53+
String UniversalTelegramBot::buildCommand(const String& cmd) {
5454
String command;
5555

5656
command += F("bot");
@@ -61,7 +61,7 @@ String UniversalTelegramBot::buildCommand(String cmd) {
6161
return command;
6262
}
6363

64-
String UniversalTelegramBot::sendGetToTelegram(String command) {
64+
String UniversalTelegramBot::sendGetToTelegram(const String& command) {
6565
String mess = "";
6666
long now;
6767
bool avail;
@@ -113,7 +113,7 @@ String UniversalTelegramBot::sendGetToTelegram(String command) {
113113
return mess;
114114
}
115115

116-
String UniversalTelegramBot::sendPostToTelegram(String command, JsonObject payload) {
116+
String UniversalTelegramBot::sendPostToTelegram(const String& command, JsonObject payload) {
117117

118118
String body = "";
119119
String headers = "";
@@ -195,8 +195,8 @@ String UniversalTelegramBot::sendPostToTelegram(String command, JsonObject paylo
195195
}
196196

197197
String UniversalTelegramBot::sendMultipartFormDataToTelegram(
198-
String command, String binaryProperyName, String fileName,
199-
String contentType, String chat_id, int fileSize,
198+
const String& command, const String& binaryProperyName, const String& fileName,
199+
const String& contentType, const String& chat_id, int fileSize,
200200
MoreDataAvailable moreDataAvailableCallback,
201201
GetNextByte getNextByteCallback,
202202
GetNextBuffer getNextBufferCallback,
@@ -577,8 +577,8 @@ bool UniversalTelegramBot::processResult(JsonObject result, int messageIndex) {
577577
* SendMessage - function to send message to telegram *
578578
* (Arguments to pass: chat_id, text to transmit and markup(optional)) *
579579
***********************************************************************/
580-
bool UniversalTelegramBot::sendSimpleMessage(String chat_id, String text,
581-
String parse_mode) {
580+
bool UniversalTelegramBot::sendSimpleMessage(const String& chat_id, const String& text,
581+
const String& parse_mode) {
582582

583583
bool sent = false;
584584
#ifdef _debug
@@ -606,8 +606,8 @@ bool UniversalTelegramBot::sendSimpleMessage(String chat_id, String text,
606606
return sent;
607607
}
608608

609-
bool UniversalTelegramBot::sendMessage(String chat_id, String text,
610-
String parse_mode) {
609+
bool UniversalTelegramBot::sendMessage(const String& chat_id, const String& text,
610+
const String& parse_mode) {
611611

612612
DynamicJsonDocument payload(maxMessageLength);
613613
payload["chat_id"] = chat_id;
@@ -620,7 +620,7 @@ bool UniversalTelegramBot::sendMessage(String chat_id, String text,
620620
}
621621

622622
bool UniversalTelegramBot::sendMessageWithReplyKeyboard(
623-
String chat_id, String text, String parse_mode, String keyboard,
623+
const String& chat_id, const String& text, const String& parse_mode, const String& keyboard,
624624
bool resize, bool oneTime, bool selective) {
625625

626626
DynamicJsonDocument payload(maxMessageLength);
@@ -648,10 +648,10 @@ bool UniversalTelegramBot::sendMessageWithReplyKeyboard(
648648
return sendPostMessage(payload.as<JsonObject>());
649649
}
650650

651-
bool UniversalTelegramBot::sendMessageWithInlineKeyboard(String chat_id,
652-
String text,
653-
String parse_mode,
654-
String keyboard) {
651+
bool UniversalTelegramBot::sendMessageWithInlineKeyboard(const String& chat_id,
652+
const String& text,
653+
const String& parse_mode,
654+
const String& keyboard) {
655655

656656
DynamicJsonDocument payload(maxMessageLength);
657657
payload["chat_id"] = chat_id;
@@ -720,7 +720,7 @@ String UniversalTelegramBot::sendPostPhoto(JsonObject payload) {
720720
}
721721

722722
String UniversalTelegramBot::sendPhotoByBinary(
723-
String chat_id, String contentType, int fileSize,
723+
const String& chat_id, const String& contentType, int fileSize,
724724
MoreDataAvailable moreDataAvailableCallback,
725725
GetNextByte getNextByteCallback, GetNextBuffer getNextBufferCallback, GetNextBufferLen getNextBufferLenCallback) {
726726

@@ -739,11 +739,11 @@ String UniversalTelegramBot::sendPhotoByBinary(
739739
return response;
740740
}
741741

742-
String UniversalTelegramBot::sendPhoto(String chat_id, String photo,
743-
String caption,
742+
String UniversalTelegramBot::sendPhoto(const String& chat_id, const String& photo,
743+
const String& caption,
744744
bool disable_notification,
745745
int reply_to_message_id,
746-
String keyboard) {
746+
const String& keyboard) {
747747

748748
DynamicJsonDocument payload(maxMessageLength);
749749
payload["chat_id"] = chat_id;
@@ -766,7 +766,7 @@ String UniversalTelegramBot::sendPhoto(String chat_id, String photo,
766766
return sendPostPhoto(payload.as<JsonObject>());
767767
}
768768

769-
bool UniversalTelegramBot::checkForOkResponse(String &response) {
769+
bool UniversalTelegramBot::checkForOkResponse(const String& response) {
770770
int last_id;
771771
DynamicJsonDocument doc(response.length());
772772
deserializeJson(doc, response);
@@ -778,7 +778,7 @@ bool UniversalTelegramBot::checkForOkResponse(String &response) {
778778
return doc["ok"] | false; // default is false, but this is more explicit and clear
779779
}
780780

781-
bool UniversalTelegramBot::sendChatAction(String chat_id, String text) {
781+
bool UniversalTelegramBot::sendChatAction(const String& chat_id, const String& text) {
782782

783783
bool sent = false;
784784
#ifdef _debug

src/UniversalTelegramBot.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,49 +64,49 @@ struct telegramMessage {
6464

6565
class UniversalTelegramBot {
6666
public:
67-
UniversalTelegramBot(String token, Client &client);
68-
void updateToken(String token);
69-
String sendGetToTelegram(String command);
70-
String sendPostToTelegram(String command, JsonObject payload);
67+
UniversalTelegramBot(const String& token, Client &client);
68+
void updateToken(const String& token);
69+
String sendGetToTelegram(const String& command);
70+
String sendPostToTelegram(const String& command, JsonObject payload);
7171
String
72-
sendMultipartFormDataToTelegram(String command, String binaryProperyName,
73-
String fileName, String contentType,
74-
String chat_id, int fileSize,
72+
sendMultipartFormDataToTelegram(const String& command, const String& binaryProperyName,
73+
const String& fileName, const String& contentType,
74+
const String& chat_id, int fileSize,
7575
MoreDataAvailable moreDataAvailableCallback,
7676
GetNextByte getNextByteCallback,
7777
GetNextBuffer getNextBufferCallback,
7878
GetNextBufferLen getNextBufferLenCallback);
7979

8080
bool getMe();
8181

82-
bool sendSimpleMessage(String chat_id, String text, String parse_mode);
83-
bool sendMessage(String chat_id, String text, String parse_mode = "");
84-
bool sendMessageWithReplyKeyboard(String chat_id, String text,
85-
String parse_mode, String keyboard,
82+
bool sendSimpleMessage(const String& chat_id, const String& text, const String& parse_mode);
83+
bool sendMessage(const String& chat_id, const String& text, const String& parse_mode = "");
84+
bool sendMessageWithReplyKeyboard(const String& chat_id, const String& text,
85+
const String& parse_mode, const String& keyboard,
8686
bool resize = false, bool oneTime = false,
8787
bool selective = false);
88-
bool sendMessageWithInlineKeyboard(String chat_id, String text,
89-
String parse_mode, String keyboard);
88+
bool sendMessageWithInlineKeyboard(const String& chat_id, const String& text,
89+
const String& parse_mode, const String& keyboard);
9090

91-
bool sendChatAction(String chat_id, String text);
91+
bool sendChatAction(const String& chat_id, const String& text);
9292

9393
bool sendPostMessage(JsonObject payload);
9494
String sendPostPhoto(JsonObject payload);
95-
String sendPhotoByBinary(String chat_id, String contentType, int fileSize,
95+
String sendPhotoByBinary(const String& chat_id, const String& contentType, int fileSize,
9696
MoreDataAvailable moreDataAvailableCallback,
9797
GetNextByte getNextByteCallback,
9898
GetNextBuffer getNextBufferCallback,
9999
GetNextBufferLen getNextBufferLenCallback);
100-
String sendPhoto(String chat_id, String photo, String caption = "",
100+
String sendPhoto(const String& chat_id, const String& photo, const String& caption = "",
101101
bool disable_notification = false,
102-
int reply_to_message_id = 0, String keyboard = "");
102+
int reply_to_message_id = 0, const String& keyboard = "");
103103

104104
bool setMyCommands(const String& commandArray);
105105

106-
String buildCommand(String cmd);
106+
String buildCommand(const String& cmd);
107107

108108
int getUpdates(long offset);
109-
bool checkForOkResponse(String &response);
109+
bool checkForOkResponse(const String& response);
110110
telegramMessage messages[HANDLE_MESSAGES];
111111
long last_message_received;
112112
String name;

0 commit comments

Comments
 (0)