-
Notifications
You must be signed in to change notification settings - Fork 153
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
Static str #1605
Static str #1605
Conversation
include/glaze/core/common.hpp
Outdated
// static string | ||
template <class T> | ||
concept static_str_t = | ||
str_t<T> && !string_view_t<T> && has_assign<T> && is_static<T>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to add resizable<T> && has_data<T>
. It has fixed capacity, but not fixed size.
naive_static_str_t(std::string_view sv) { assign(sv.data(), sv.size()); } | ||
operator std::string_view() const { return std::string_view(data, length); } | ||
|
||
size_t size() const { return N; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data()
should be added
To be more strict, you are right. |
I have updated the concept; please check it. |
Thanks for working on this. Glaze will treat a One thing that I think would be good to change is to rework the So, this: template <size_t N>
struct glz::detail::is_static_helper<naive_static_str_t<N>>: std::true_type{}; Would change to: template <size_t N>
struct glz::meta<naive_static_str_t <N>>
{
static constexpr auto glaze_static_string = true;
}; I try to also allow these definitions locally, so the naive_static_str_t class could define: You can make this change, or I can make it when I'm able. Thanks so much for this pull request! |
yes, I think using 'glz::meta' is a more elegant way, for defining 'is_xxx_helper' classes arbitrarily will cause code's fragmentation. I have switched the concept implementation. |
Thanks for the updates! I'll merge this in. |
There is a special kind of string,
static_string
, which behaves almost same as 'std::string' except that it has a fixed capacity. This kind of string is used for better performance, for it usually uses compile-fixed buffers and is POD. Here is an example, boost.static_string.When applying
static_string
in glaze, however, thery are usually recognized asstring_t
. When we try to readstatic_string
in glaze, an optimizationstatic constexpr auto string_padding_bytes = 8;
tryies to reserve extra memories for alignment, which may exceed the capacity of the 'static_string'. These behavior will cause some undefined behaviors and the program will crash.In this PR, I try to show the usage of 'static_string' and fix this issue.