1#[cfg(all(feature = "alloc", not(feature = "std")))]
7use alloc::string::String;
8
9#[cfg(feature = "alloc")]
10use crate::parser::trusted as trusted_parser;
11
12#[cfg(feature = "alloc")]
16pub(crate) fn set_fragment(s: &mut String, fragment: Option<&str>) {
17 remove_fragment(s);
18 if let Some(fragment) = fragment {
19 s.reserve(fragment.len() + 1);
20 s.push('#');
21 s.push_str(fragment);
22 }
23}
24
25#[cfg(feature = "alloc")]
27#[inline]
28pub(crate) fn remove_fragment(s: &mut String) {
29 if let Some(colon_pos) = s.find('#') {
30 s.truncate(colon_pos);
31 }
32}
33
34#[cfg(feature = "alloc")]
38pub(crate) fn split_fragment_owned(mut s: String) -> (String, Option<String>) {
39 let prefix_len = match trusted_parser::split_fragment(&s) {
40 (_, None) => return (s, None),
41 (prefix, Some(_fragment)) => prefix.len(),
42 };
43
44 let fragment = s.split_off(prefix_len + 1);
46 {
48 let hash = s.pop();
50 assert_eq!(hash, Some('#'));
51 }
52 assert_eq!(s.len(), prefix_len);
53
54 (s, Some(fragment))
55}