Skip to content

introduce to_dstring #3110

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

Merged
merged 1 commit into from
Feb 19, 2019
Merged
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
12 changes: 12 additions & 0 deletions src/util/dstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@ std::ostream &dstringt::operator<<(std::ostream &out) const
{
return out << as_string();
}

dstringt get_dstring_number(std::size_t value)
{
static const dstringt *const dstring_numbers = [] {
dstringt *array = new dstringt[DSTRING_NUMBERS_MAX + 1];
for(std::size_t i = 0; i <= DSTRING_NUMBERS_MAX; i++)
array[i] = dstringt(std::to_string(i));
return array;
}();

return dstring_numbers[value];
}
16 changes: 16 additions & 0 deletions src/util/dstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Author: Daniel Kroening, [email protected]
#include <string>

#include "invariant.h"
#include "magic.h"
#include "string_container.h"

/// \ref dstringt has one field, an unsigned integer \ref no which is an index
Expand Down Expand Up @@ -220,4 +221,19 @@ struct diagnostics_helpert<dstringt>
}
};

dstringt get_dstring_number(std::size_t);

/// equivalent to dstringt(std::to_string(value)), i.e., produces a string
/// from a number
template <typename T>
static inline
typename std::enable_if<std::is_integral<T>::value, dstringt>::type
to_dstring(T value)
{
if(value >= 0 && value <= static_cast<T>(DSTRING_NUMBERS_MAX))
return get_dstring_number(value);
else
return std::to_string(value);
}

#endif // CPROVER_UTIL_DSTRING_H
4 changes: 4 additions & 0 deletions src/util/irep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,11 @@ long long irept::get_long_long(const irep_namet &name) const

void irept::set(const irep_namet &name, const long long value)
{
#ifdef USE_DSTRING
add(name).id(to_dstring(value));
#else
add(name).id(std::to_string(value));
#endif
}

void irept::remove(const irep_namet &name)
Expand Down
3 changes: 3 additions & 0 deletions src/util/magic.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ const std::size_t STRING_REFINEMENT_MAX_CHAR_WIDTH = 16;
// Limit the size of strings in traces to 64M chars to avoid memout
const std::size_t MAX_CONCRETE_STRING_SIZE = 1 << 26;

// The top end of the range of integers for which dstrings are precomputed
constexpr std::size_t DSTRING_NUMBERS_MAX = 64;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there any value in using constexpr over const?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Only that you can then could produce constexpr dstrings for numbers up to that value from a constexpr integer.


#endif