1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! Snippet replacement operations on rope structures.
use crate::;
/// Validates that a replacement string meets UTF-8 requirements.
///
/// While Rust's `&str` type guarantees valid UTF-8, this function performs additional
/// validation to ensure the replacement text is suitable for rope operations.
///
/// # Arguments
///
/// * `s` - The replacement string to validate
///
/// # Returns
///
/// Returns `Ok(())` if the string is valid.
///
/// # Errors
///
/// Returns [`SnippetError::InvalidUtf8`] if the string contains null bytes, which are
/// not permitted in text replacements.
///
/// # Examples
///
/// ```rust
/// # use textum::snip::snippet::replacement::validate_replacement_utf8;
/// assert!(validate_replacement_utf8("hello").is_ok());
/// assert!(validate_replacement_utf8("hello\0world").is_err());
/// ```
/// Applies a replacement operation to a rope at the specified character range.
///
/// This function creates a new rope with the specified range replaced by the given text.
/// The operation is performed by removing the range and inserting the replacement.
///
/// # Arguments
///
/// * `rope` - The source rope to modify
/// * `start` - Starting character index (inclusive)
/// * `end` - Ending character index (exclusive)
/// * `replacement` - The text to insert at the position
///
/// # Returns
///
/// Returns a new [`Rope`] with the replacement applied.
///
/// # Behavior
///
/// - If `start == end`: Performs pure insertion (no text removed)
/// - If `replacement` is empty: Performs pure deletion (no text inserted)
/// - Otherwise: Removes `[start..end)` and inserts `replacement`
///
/// # Examples
///
/// ```rust
/// # use textum::snip::snippet::replacement::apply_replacement;
/// # use textum::Rope;
/// let rope = Rope::from_str("hello world");
///
/// // Replace "world" with "rust"
/// let result = apply_replacement(&rope, 6, 11, "rust");
/// assert_eq!(result.to_string(), "hello rust");
///
/// // Insert at position (zero-width range)
/// let result = apply_replacement(&rope, 5, 5, ",");
/// assert_eq!(result.to_string(), "hello, world");
///
/// // Delete range (empty replacement)
/// let result = apply_replacement(&rope, 5, 11, "");
/// assert_eq!(result.to_string(), "hello");
/// ```