Skip to content
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

Merged
merged 7 commits into from
Feb 11, 2025
Merged

Static str #1605

merged 7 commits into from
Feb 11, 2025

Conversation

SlaaneshChosen
Copy link
Contributor

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 as string_t. When we try to read static_string in glaze, an optimization static 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.

// static string
template <class T>
concept static_str_t =
str_t<T> && !string_view_t<T> && has_assign<T> && is_static<T>;
Copy link
Collaborator

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; }
Copy link
Collaborator

Choose a reason for hiding this comment

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

data() should be added

@SlaaneshChosen
Copy link
Contributor Author

To be more strict, you are right.

@SlaaneshChosen
Copy link
Contributor Author

I have updated the concept; please check it.

@stephenberry
Copy link
Owner

Thanks for working on this. Glaze will treat a std::array<char, N> as a static string. But, I can see how support for custom static string types could be useful.

One thing that I think would be good to change is to rework the is_static_helper. I tend to think it is best to add fields to the glz::meta specialization, so that a single glz::meta can set multiple options like this and it consolidates template specializations to just glz::meta.

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: static constexpr auto glaze_static_string = true;

You can make this change, or I can make it when I'm able. Thanks so much for this pull request!

@SlaaneshChosen
Copy link
Contributor Author

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.

@stephenberry
Copy link
Owner

Thanks for the updates! I'll merge this in.

@stephenberry stephenberry merged commit 7cf9508 into stephenberry:main Feb 11, 2025
12 checks passed
@SlaaneshChosen SlaaneshChosen deleted the static-str branch February 12, 2025 01:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants