Skip to content

[Proposal] Clarify (the existence of) the constant empty String #5546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cores/esp8266/AddrList.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ struct netifWrapper

// common to all addresses of this interface
String ifname () const { return String(_netif->name[0]) + _netif->name[1]; }
const char* ifhostname () const { return _netif->hostname?: emptyString.c_str(); }
const char* ifhostname () const { return _netif->hostname?: String::empty.c_str(); }
const char* ifmac () const { return (const char*)_netif->hwaddr; }
int ifnumber () const { return _netif->num; }

Expand Down
1 change: 1 addition & 0 deletions cores/esp8266/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,3 +800,4 @@ float String::toFloat(void) const {
// global empty string to allow returning const String& with nothing

const String emptyString;
const String __emptyString __attribute__((alias("emptyString")));
8 changes: 7 additions & 1 deletion cores/esp8266/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class __FlashStringHelper;
#define F(string_literal) (FPSTR(PSTR(string_literal)))

// The string class
class String;
extern const String __emptyString;
class String {
// use a function pointer to allow for "if (s)" without the
// complications of an operator bool(). for more information, see:
Expand Down Expand Up @@ -70,6 +72,9 @@ class String {
explicit String(double, unsigned char decimalPlaces = 2);
~String(void);

// empty string singleton
static constexpr const String& empty = ::__emptyString;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't call the static member "empty". It should be ok to also call it "emptyString", because it is scoped, or call it something else (emptyStr, nullString, defString, etc)

Copy link
Collaborator

@devyte devyte Jan 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the deprecation, can this be implemented the other way around? make this member the "real" object, and make the global a reference to this one.


// memory management
// return true on success, false on failure (in which case, the string
// is left unchanged). reserve(0), if successful, will validate an
Expand Down Expand Up @@ -294,7 +299,8 @@ class StringSumHelper: public String {
}
};

extern const String emptyString;
// only for backward compatibility
extern const String emptyString __attribute__((deprecated("Use String::empty instead.")));

#endif // __cplusplus
#endif // String_class_h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ ESP8266HTTPUpdateServer::ESP8266HTTPUpdateServer(bool serial_debug)
{
_serial_output = serial_debug;
_server = NULL;
_username = emptyString;
_password = emptyString;
_username = String::empty;
_password = String::empty;
_authenticated = false;
}

Expand All @@ -33,7 +33,7 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const String& path

// handler for the /update form page
_server->on(path.c_str(), HTTP_GET, [&](){
if(_username != emptyString && _password != emptyString && !_server->authenticate(_username.c_str(), _password.c_str()))
if(_username != String::empty && _password != String::empty && !_server->authenticate(_username.c_str(), _password.c_str()))
return _server->requestAuthentication();
_server->send_P(200, PSTR("text/html"), serverIndex);
});
Expand Down Expand Up @@ -61,7 +61,7 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const String& path
if (_serial_output)
Serial.setDebugOutput(true);

_authenticated = (_username == emptyString || _password == emptyString || _server->authenticate(_username.c_str(), _password.c_str()));
_authenticated = (_username == String::empty || _password == String::empty || _server->authenticate(_username.c_str(), _password.c_str()));
if(!_authenticated){
if (_serial_output)
Serial.printf("Unauthenticated Update\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class ESP8266HTTPUpdateServer

void setup(ESP8266WebServer *server)
{
setup(server, emptyString, emptyString);
setup(server, String::empty, String::empty);
}

void setup(ESP8266WebServer *server, const String& path)
{
setup(server, path, emptyString, emptyString);
setup(server, path, String::empty, String::empty);
}

void setup(ESP8266WebServer *server, const String& username, const String& password)
Expand Down
18 changes: 9 additions & 9 deletions libraries/ESP8266WebServer/src/ESP8266WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void ESP8266WebServer::begin(uint16_t port) {
String ESP8266WebServer::_extractParam(String& authReq,const String& param,const char delimit) const {
int _begin = authReq.indexOf(param);
if (_begin == -1)
return emptyString;
return String::empty;
return authReq.substring(_begin+param.length(),authReq.indexOf(delimit,_begin+param.length()));
}

Expand Down Expand Up @@ -486,7 +486,7 @@ void ESP8266WebServer::_streamFileCore(const size_t fileSize, const String & fil
contentType != String(FPSTR(mimeTable[none].mimeType))) {
sendHeader(F("Content-Encoding"), F("gzip"));
}
send(200, contentType, emptyString);
send(200, contentType, String::empty);
}


Expand All @@ -499,19 +499,19 @@ const String& ESP8266WebServer::arg(String name) const {
if ( _currentArgs[i].key == name )
return _currentArgs[i].value;
}
return emptyString;
return String::empty;
}

const String& ESP8266WebServer::arg(int i) const {
if (i >= 0 && i < _currentArgCount)
return _currentArgs[i].value;
return emptyString;
return String::empty;
}

const String& ESP8266WebServer::argName(int i) const {
if (i >= 0 && i < _currentArgCount)
return _currentArgs[i].key;
return emptyString;
return String::empty;
}

int ESP8266WebServer::args() const {
Expand All @@ -536,7 +536,7 @@ const String& ESP8266WebServer::header(String name) const {
if (_currentHeaders[i].key.equalsIgnoreCase(name))
return _currentHeaders[i].value;
}
return emptyString;
return String::empty;
}

void ESP8266WebServer::collectHeaders(const char* headerKeys[], const size_t headerKeysCount) {
Expand All @@ -553,13 +553,13 @@ void ESP8266WebServer::collectHeaders(const char* headerKeys[], const size_t hea
const String& ESP8266WebServer::header(int i) const {
if (i < _headerKeysCount)
return _currentHeaders[i].value;
return emptyString;
return String::empty;
}

const String& ESP8266WebServer::headerName(int i) const {
if (i < _headerKeysCount)
return _currentHeaders[i].key;
return emptyString;
return String::empty;
}

int ESP8266WebServer::headers() const {
Expand Down Expand Up @@ -619,7 +619,7 @@ void ESP8266WebServer::_handleRequest() {

void ESP8266WebServer::_finalizeResponse() {
if (_chunked) {
sendContent(emptyString);
sendContent(String::empty);
}
}

Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266WiFi/src/ESP8266WiFiAP.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ESP8266WiFiAPClass {
public:

bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4);
bool softAP(const String& ssid,const String& passphrase = emptyString,int channel = 1,int ssid_hidden = 0,int max_connection = 4);
bool softAP(const String& ssid,const String& passphrase = String::empty,int channel = 1,int ssid_hidden = 0,int max_connection = 4);
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
bool softAPdisconnect(bool wifioff = false);

Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266WiFi/src/ESP8266WiFiSTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ESP8266WiFiSTAClass {

wl_status_t begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
wl_status_t begin(const String& ssid, const String& passphrase = emptyString, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
wl_status_t begin(const String& ssid, const String& passphrase = String::empty, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
wl_status_t begin();

//The argument order for ESP is not the same as for Arduino. However, there is compatibility code under the hood
Expand Down