https://svn.lrde.epita.fr/svn/nolimips/trunk
ChangeLog | 14 +++++++++++++-
src/misc/unique_string.cc | 16 ++++++----------
src/misc/unique_string.hh | 39 +++++++++++++++++++--------------------
src/parse/asm-scan.ll.gen.py | 5 ++---
4 files changed, 40 insertions(+), 34 deletions(-)
Index: ChangeLog
from BenoƮt Perrot <benoit(a)lrde.epita.fr>
* src/misc/unique_string.hh, src/misc/unique_string.cc:
Introduce a constructor from a const char*. Factor string
insertion in pool.
Suggested by Akim Demaille: Use a set of strings instead of a set
of pointers as internal pool. Compose an iterator instead of a
pointer.
* src/parse/asm-scan.ll.gen.py:
Suggested by Akim Demaille: Don't be affraid to side-affect
yytext.
hence make unique_string compose a pointer-to-string instead of a
Index: src/misc/unique_string.cc
--- src/misc/unique_string.cc (revision 189)
+++ src/misc/unique_string.cc (revision 190)
@@ -25,17 +25,13 @@
unique_string::string_to_unique_type unique_string::pool_;
- unique_string::unique_string(const std::string &str)
+ unique_string::string_to_unique_type::const_iterator
+ unique_string::insert_in_pool(const std::string &str)
{
- unique_string::string_to_unique_type::iterator it = pool_.find(&str);
- if (it == pool_.end() || (*it)->compare(str))
- {
- std::string *s = new std::string(str);
- it = pool_.insert(it, s);
- }
-
- str_ = *it;
- assertion(str_);
+ string_to_unique_type::const_iterator it(pool_.find(str));
+ if (it == pool_.end() || it->compare(str))
+ it = pool_.insert(it, str);
+ return it;
}
} // namespace misc
Index: src/misc/unique_string.hh
--- src/misc/unique_string.hh (revision 189)
+++ src/misc/unique_string.hh (revision 190)
@@ -35,34 +35,30 @@
class unique_string
{
private:
- struct string_ptr_less:
- public std::binary_function<const std::string*, const std::string*, bool>
- {
- bool operator()(const std::string* s1, const std::string* s2) const
- {
- return *s1 < *s2;
- }
- };
-
- typedef std::set<const std::string*,
- string_ptr_less> string_to_unique_type;
+ typedef std::set<std::string> string_to_unique_type;
public:
- unique_string(const std::string &str);
+ unique_string(const std::string &str):
+ it_(insert_in_pool(str))
+ {}
+
+ unique_string(const char *str):
+ it_(insert_in_pool(std::string(str)))
+ {}
// Accessors
public:
const std::string &get() const
{
- return *str_;
+ return *it_;
}
operator const std::string &() const
{
- return *str_;
+ return *it_;
}
const std::string & operator* () const
{
- return *str_;
+ return *it_;
}
static unsigned pool_size()
@@ -74,25 +70,28 @@
public:
bool operator==(const unique_string &rhs) const
{
- return str_ == rhs.str_;
+ return it_ == rhs.it_;
}
bool operator!=(const unique_string &rhs) const
{
- return str_ != rhs.str_;
+ return it_ != rhs.it_;
}
bool operator<(const unique_string &rhs) const
{
- if (str_ == rhs.str_)
+ if (it_ == rhs.it_)
return false;
- return *str_ < *(rhs.str_);
+ return *it_ < *rhs.it_;
}
private:
+ static string_to_unique_type::const_iterator insert_in_pool(const std::string
&str);
+
+ private:
static string_to_unique_type pool_;
private:
- const std::string *str_;
+ const string_to_unique_type::const_iterator it_;
};
inline std::ostream &
Index: src/parse/asm-scan.ll.gen.py
--- src/parse/asm-scan.ll.gen.py (revision 189)
+++ src/parse/asm-scan.ll.gen.py (revision 190)
@@ -149,9 +149,8 @@
return LABEL;
}
{id}\":\" {
- std::string label(yytext);
- label.resize(yyleng - 1);
- yylval->id = new misc::unique_string(label);
+ yytext[yyleng - 1] = 0; // Remove last character (side-effects on yytext are allowed)
+ yylval->id = new misc::unique_string(yytext);
return LABEL_DEF;
}