Skip to content
Open
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
8 changes: 7 additions & 1 deletion src/util/dstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ class dstringt final
{
}

// this one is not safe for static objects
// NOLINTNEXTLINE(runtime/explicit)
dstringt(const std::string_view &s) : no(get_string_container()[s])
{
}

// this one is not safe for static objects
// NOLINTNEXTLINE(runtime/explicit)
dstringt(const std::string &s):no(get_string_container()[s])
Expand Down Expand Up @@ -102,7 +108,7 @@ class dstringt final
}

/// equivalent of as_string().starts_with(s)
bool starts_with(const std::string &prefix) const
bool starts_with(const std::string_view &prefix) const
{
return as_string().compare(0, prefix.size(), prefix) == 0;
}
Expand Down
43 changes: 30 additions & 13 deletions src/util/string_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,41 @@ Author: Daniel Kroening, kroening@kroening.com
#include "memory_units.h"
#include "string_hash.h"

struct string_ptrt
class string_ptr_hasht;

class string_ptrt
{
const char *s;
size_t len;
public:
explicit string_ptrt(const char *_s);

const char *c_str() const
explicit string_ptrt(const std::string_view &_s)
: s(_s.data()), len(_s.size())
{
return s;
}

explicit string_ptrt(const char *_s);

explicit string_ptrt(const std::string &_s):s(_s.c_str()), len(_s.size())
explicit string_ptrt(const std::string &_s) : s(_s.data()), len(_s.size())
{
}

// this compares the contents of the string, not the address
bool operator==(const string_ptrt &other) const;

protected:
// The string need not be zero terminated.
const char *s;
size_t len;

friend string_ptr_hasht;
};

// NOLINTNEXTLINE(readability/identifiers)
class string_ptr_hash
class string_ptr_hasht
{
public:
size_t operator()(const string_ptrt s) const { return hash_string(s.s); }
size_t operator()(const string_ptrt s) const
{
return hash_string(s.s, s.len);
}
};

/// Has estimated statistics about string container
Expand All @@ -68,6 +79,11 @@ class string_containert
return get(s);
}

unsigned operator[](const std::string_view &s)
{
return get(s);
}

unsigned operator[](const std::string &s)
{
return get(s);
Expand All @@ -93,12 +109,13 @@ class string_containert

protected:
// the 'unsigned' ought to be size_t
typedef std::unordered_map<string_ptrt, unsigned, string_ptr_hash>
typedef std::unordered_map<string_ptrt, unsigned, string_ptr_hasht>
hash_tablet;
hash_tablet hash_table;

unsigned get(const char *s);
unsigned get(const std::string &s);
unsigned get(const char *);
unsigned get(const std::string_view &);
unsigned get(const std::string &);

typedef std::list<std::string> string_listt;
string_listt string_list;
Expand Down
16 changes: 5 additions & 11 deletions src/util/string_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,16 @@ Author: Daniel Kroening, kroening@kroening.com

#include "string_hash.h"

size_t hash_string(const std::string &s)
size_t hash_string(const std::string_view &s)
{
size_t h=0;
size_t size=s.size();

for(unsigned i=0; i<size; i++)
h=(h<<5)-h+s[i];

return h;
return hash_string(s.data(), s.size());
}

size_t hash_string(const char *s)
size_t hash_string(const char *s, std::size_t len)
{
size_t h=0;
std::size_t h = 0;

for(; *s!=0; s++)
for(; len != 0; --len)
h=(h<<5)-h+*s;

return h;
Expand Down
5 changes: 3 additions & 2 deletions src/util/string_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ Author: Daniel Kroening, kroening@kroening.com
#define CPROVER_UTIL_STRING_HASH_H

#include <string>
#include <string_view>

size_t hash_string(const std::string &s);
size_t hash_string(const char *s);
size_t hash_string(const std::string_view &);
size_t hash_string(const char *s, std::size_t len);

// NOLINTNEXTLINE(readability/identifiers)
struct string_hash
Expand Down
Loading