@@ -2604,23 +2604,77 @@ class basic_string {
2604
2604
#pragma region Modifiers
2605
2605
#pragma region Non-STL API
2606
2606
2607
+ /* *
2608
+ * @brief Resizes the string to a specified number of characters, padding with the specified character if needed.
2609
+ * @param count The new size of the string.
2610
+ * @param character The character to fill new elements with, if expanding. Defaults to null character.
2611
+ * @return `true` if the resizing was successful, `false` otherwise.
2612
+ */
2607
2613
bool try_resize (size_type count, value_type character = ' \0 ' ) noexcept ;
2608
2614
2615
+ /* *
2616
+ * @brief Attempts to reduce memory usage by freeing unused memory.
2617
+ * @return `true` if the operation was successful and potentially reduced the memory footprint, `false` otherwise.
2618
+ */
2619
+ bool try_shrink_to_fit () noexcept {
2620
+ return _with_alloc ([&](sz_alloc_type &alloc) { return sz_string_shrink_to_fit (&string_, &alloc); });
2621
+ }
2622
+
2623
+ /* *
2624
+ * @brief Attempts to reserve enough space for a specified number of characters.
2625
+ * @param capacity The new capacity to reserve.
2626
+ * @return `true` if the reservation was successful, `false` otherwise.
2627
+ */
2609
2628
bool try_reserve (size_type capacity) noexcept {
2610
2629
return _with_alloc ([&](sz_alloc_type &alloc) { return sz_string_reserve (&string_, capacity, &alloc); });
2611
2630
}
2612
2631
2632
+ /* *
2633
+ * @brief Assigns a new value to the string, replacing its current contents.
2634
+ * @param other The string view whose contents to assign.
2635
+ * @return `true` if the assignment was successful, `false` otherwise.
2636
+ */
2613
2637
bool try_assign (string_view other) noexcept ;
2614
2638
2639
+ /* *
2640
+ * @brief Assigns a concatenated sequence to the string, replacing its current contents.
2641
+ * @param other The concatenation object representing the sequence to assign.
2642
+ * @return `true` if the assignment was successful, `false` otherwise.
2643
+ */
2615
2644
template <typename first_type, typename second_type>
2616
2645
bool try_assign (concatenation<first_type, second_type> const &other) noexcept ;
2617
2646
2647
+ /* *
2648
+ * @brief Attempts to add a single character to the end of the string.
2649
+ * @param c The character to add.
2650
+ * @return `true` if the character was successfully added, `false` otherwise.
2651
+ */
2618
2652
bool try_push_back (char_type c) noexcept ;
2619
2653
2654
+ /* *
2655
+ * @brief Attempts to append a given character array to the string.
2656
+ * @param str The pointer to the array of characters to append.
2657
+ * @param length The number of characters to append.
2658
+ * @return `true` if the append operation was successful, `false` otherwise.
2659
+ */
2620
2660
bool try_append (const_pointer str, size_type length) noexcept ;
2621
2661
2662
+ /* *
2663
+ * @brief Attempts to append a string view to the string.
2664
+ * @param str The string view to append.
2665
+ * @return `true` if the append operation was successful, `false` otherwise.
2666
+ */
2622
2667
bool try_append (string_view str) noexcept { return try_append (str.data (), str.size ()); }
2623
2668
2669
+ /* *
2670
+ * @brief Clears the contents of the string and resets its length to 0.
2671
+ * @return Always returns `true` as this operation cannot fail under normal conditions.
2672
+ */
2673
+ bool try_clear () noexcept {
2674
+ clear ();
2675
+ return true ;
2676
+ }
2677
+
2624
2678
/* *
2625
2679
* @brief Erases ( @b in-place ) a range of characters defined with signed offsets.
2626
2680
* @return Number of characters removed.
@@ -2683,6 +2737,14 @@ class basic_string {
2683
2737
if (!try_resize (count, character)) throw std::bad_alloc ();
2684
2738
}
2685
2739
2740
+ /* *
2741
+ * @brief Reclaims the unused memory, if any.
2742
+ * @throw `std::bad_alloc` if the allocation fails.
2743
+ */
2744
+ void shrink_to_fit () noexcept (false ) {
2745
+ if (!try_shrink_to_fit ()) throw std::bad_alloc ();
2746
+ }
2747
+
2686
2748
/* *
2687
2749
* @brief Informs the string object of a planned change in size, so that it pre-allocate once.
2688
2750
* @throw `std::length_error` if the string is too long.
0 commit comments