Skip to content

Commit bab2880

Browse files
dirkmuellerearlephilhower
authored andcommitted
Base64::encode : const correctness / String by reference passing (#6581)
This is another instance in the core library where we pass in read-only parameters as pass-by-value, where in the case of String() that is inefficient as it involves copy-constructor/temp string creations.
1 parent 3890e1a commit bab2880

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

cores/esp8266/base64.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ extern "C" {
3131

3232
/**
3333
* convert input data to base64
34-
* @param data uint8_t *
34+
* @param data const uint8_t *
3535
* @param length size_t
3636
* @return String
3737
*/
38-
String base64::encode(uint8_t * data, size_t length, bool doNewLines) {
38+
String base64::encode(const uint8_t * data, size_t length, bool doNewLines) {
3939
// base64 needs more size then the source data, use cencode.h macros
4040
size_t size = ((doNewLines ? base64_encode_expected_len(length)
4141
: base64_encode_expected_len_nonewlines(length)) + 1);
@@ -62,10 +62,10 @@ String base64::encode(uint8_t * data, size_t length, bool doNewLines) {
6262

6363
/**
6464
* convert input data to base64
65-
* @param text String
65+
* @param text const String&
6666
* @return String
6767
*/
68-
String base64::encode(String text, bool doNewLines) {
69-
return base64::encode((uint8_t *) text.c_str(), text.length(), doNewLines);
68+
String base64::encode(const String& text, bool doNewLines) {
69+
return base64::encode((const uint8_t *) text.c_str(), text.length(), doNewLines);
7070
}
7171

cores/esp8266/base64.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class base64 {
3030
// NOTE: The default behaviour of backend (lib64)
3131
// is to add a newline every 72 (encoded) characters output.
3232
// This may 'break' longer uris and json variables
33-
static String encode(uint8_t * data, size_t length, bool doNewLines = true);
34-
static String encode(String text, bool doNewLines = true);
33+
static String encode(const uint8_t * data, size_t length, bool doNewLines = true);
34+
static String encode(const String& text, bool doNewLines = true);
3535
private:
3636
};
3737

0 commit comments

Comments
 (0)