-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve HttpHeaders using HashMap(enum, String) (#1463)
* Improve HttpHeaders using HashMap(enum, String), introduce CStringArray class * Lookups faster
- Loading branch information
Showing
7 changed files
with
247 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/**** | ||
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development. | ||
* Created 2015 by Skurydin Alexey | ||
* http://github.com/SmingHub/Sming | ||
* All files of the Sming Core are provided under the LGPL v3 license. | ||
* | ||
* @author: 2018 - Mikee47 <mike@sillyhouse.net> | ||
* | ||
****/ | ||
|
||
#include "CStringArray.h" | ||
|
||
bool CStringArray::add(const char* str, unsigned length) | ||
{ | ||
if(length == 0) | ||
length = strlen(str); | ||
if(!reserve(len + length + 1)) | ||
return false; | ||
if(len) | ||
buffer[len++] = '\0'; // Separator between strings | ||
memcpy(buffer + len, str, length + 1); // Copy final nul terminator | ||
len += length; | ||
++count_; | ||
return true; | ||
} | ||
|
||
int CStringArray::indexOf(const char* str) const | ||
{ | ||
if(str == nullptr) | ||
return -1; | ||
|
||
unsigned index = 0; | ||
for(unsigned offset = 0; offset < len; ++index) { | ||
const char* s = buffer + offset; | ||
if(strcasecmp(str, s) == 0) | ||
return index; | ||
offset += strlen(s) + 1; | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
const char* CStringArray::getValue(unsigned index) const | ||
{ | ||
if(index < count_) { | ||
for(unsigned offset = 0; offset < length(); --index) { | ||
const char* s = buffer + offset; | ||
if(index == 0) | ||
return s; | ||
offset += strlen(s) + 1; | ||
} | ||
} | ||
|
||
return nullptr; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/**** | ||
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development. | ||
* Created 2015 by Skurydin Alexey | ||
* http://github.com/SmingHub/Sming | ||
* All files of the Sming Core are provided under the LGPL v3 license. | ||
* | ||
* @author: 2018 - Mikee47 <mike@sillyhouse.net> | ||
* | ||
****/ | ||
|
||
#ifndef _SMING_CORE_DATA_STRING_ARRAY_H_ | ||
#define _SMING_CORE_DATA_STRING_ARRAY_H_ | ||
|
||
#include "WString.h" | ||
|
||
/** @brief Class to manage a double null-terminated list of strings, such as "one\0two\0three" | ||
* @note | ||
* | ||
* Comparison with Vector<String> | ||
* | ||
* Advantages: | ||
* More memory efficient. Uses only a single heap allocation | ||
* Useful for simple lookups, e.g. mapping enumerated values to strings | ||
* | ||
* Disadvantages: | ||
* | ||
* Slower. Items must be iterated using multiple strlen() calls | ||
* Ordering not supported | ||
* Insertions / deletions not supported (yet) | ||
*/ | ||
class CStringArray : protected String | ||
{ | ||
public: | ||
/** @brief append a new string to the end of the array | ||
* @param str | ||
* @retval bool false on memory allocation error | ||
*/ | ||
bool add(const char* str, unsigned length = 0); | ||
|
||
bool add(const String& str) | ||
{ | ||
return add(str.c_str(), str.length()); | ||
} | ||
|
||
/** @brief Find the given string and return its index | ||
* @param str String to find | ||
* @retval int index of given string, -1 if not found | ||
* @note comparison is not case-sensitive | ||
*/ | ||
int indexOf(const char* str) const; | ||
|
||
int indexOf(const String& str) const | ||
{ | ||
return indexOf(str.c_str()); | ||
} | ||
|
||
bool contains(const char* str) const | ||
{ | ||
return indexOf(str) >= 0; | ||
} | ||
|
||
bool contains(const String& str) const | ||
{ | ||
return indexOf(str) >= 0; | ||
} | ||
|
||
/** @brief Get string at the given position | ||
* @param index 0-based index of string to obtain | ||
* @retval const char* nullptr if index is not valid | ||
*/ | ||
const char* getValue(unsigned index) const; | ||
|
||
const char* operator[](unsigned index) const | ||
{ | ||
return getValue(index); | ||
} | ||
|
||
/** @brief Empty the array | ||
*/ | ||
void clear() | ||
{ | ||
invalidate(); | ||
count_ = 0; | ||
} | ||
|
||
unsigned count() const | ||
{ | ||
return count_; | ||
} | ||
|
||
private: | ||
unsigned count_ = 0; | ||
}; | ||
|
||
#endif // _SMING_CORE_DATA_STRING_ARRAY_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.