diff --git a/src/StringIndexer.h b/src/StringIndexer.h index 020dd3b..2be35a9 100644 --- a/src/StringIndexer.h +++ b/src/StringIndexer.h @@ -26,32 +26,9 @@ class StringIndexer #endif }; public: - using index_t=uint8_t; + using index_t = uint8_t; - static index_t strToIndex(const char* str, uint8_t len) - { - for(auto it=strings.begin(); it!=strings.end(); it++) - { - if (strncmp(it->second.str.c_str(), str, len)==0) - { - it->second.used++; - return it->first; - } - } - for(index_t index=1; index; index++) - { - if (strings.find(index)==strings.end()) - { - strings[index].str = std::string(str, len); - strings[index].used++; - // Serial << "Creating index " << index << " for (" << strings[index].str.c_str() << ") len=" << len << endl; - return index; - } - } - return 0; // TODO out of indexes - } - - static const std::string& str(const index_t& index) + static const std::string& str(const index_t& index) { static std::string dummy; const auto& it=strings.find(index); @@ -82,6 +59,32 @@ class StringIndexer static uint16_t count() { return strings.size(); } private: + friend class IndexedString; + + // increment use of str or create a new index + static index_t strToIndex(const char* str, uint8_t len) + { + for(auto it=strings.begin(); it!=strings.end(); it++) + { + if (it->second.str.length() == len && strcmp(it->second.str.c_str(), str)==0) + { + it->second.used++; + return it->first; + } + } + for(index_t index=1; index; index++) + { + if (strings.find(index)==strings.end()) + { + strings[index].str = std::string(str, len); + strings[index].used++; + // Serial << "Creating index " << index << " for (" << strings[index].str.c_str() << ") len=" << len << endl; + return index; + } + } + return 0; // TODO out of indexes + } + static std::map strings; }; diff --git a/tests/string-indexer-tests/string-indexer-tests.ino b/tests/string-indexer-tests/string-indexer-tests.ino index 0cd3811..32506ef 100644 --- a/tests/string-indexer-tests/string-indexer-tests.ino +++ b/tests/string-indexer-tests/string-indexer-tests.ino @@ -62,6 +62,14 @@ test(indexer_same_strings_should_equal) assertTrue(one == two); } +test(indexer_compare_strings_with_same_beginning) +{ + IndexedString two("one_two"); + IndexedString one("one"); + + assertNotEqual(one.getIndex(), two.getIndex()); +} + test(indexer_indexed_operator_eq) { IndexedString one("one");