@@ -2,12 +2,12 @@ use std::mem;
22
33/// A string builder for constructing source code.
44///
5+ /// `CodeBuffer` provides safe abstractions over a byte array.
6+ /// Essentially same as `String` but with additional methods.
57///
6- /// `CodeBuffer` provides safe abstractions over a byte array, allowing for
7- /// a compact byte-array representation without soundness holes.
8- ///
9- /// Use one of the various `print_*` methods to add text into a buffer. When you
10- /// are done, call [`take_source_text`] to extract the final [`String`].
8+ /// Use one of the various `print_*` methods to add text into the buffer.
9+ /// When you are done, call [`take_source_text`] or `String::from(code_buffer)`
10+ /// to extract the final [`String`].
1111///
1212/// # Example
1313/// ```
@@ -53,31 +53,31 @@ impl CodeBuffer {
5353
5454 /// Create a new, empty `CodeBuffer` with the specified capacity.
5555 ///
56- /// The buffer will be able to hold at least `capacity` bytes without
57- /// reallocating. This method is allowed to allocate for more bytes than
58- /// `capacity`. If `capacity` is 0, the buffer will not allocate.
56+ /// The buffer will be able to hold at least `capacity` bytes without reallocating.
57+ /// This method is allowed to allocate for more bytes than `capacity`.
58+ /// If `capacity` is 0, the buffer will not allocate.
5959 ///
6060 /// It is important to note that although the returned buffer has the
6161 /// minimum *capacity* specified, the buffer will have a zero *length*.
6262 ///
6363 /// # Panics
6464 ///
65- /// Panics if the new capacity exceeds `isize::MAX` _bytes_ .
65+ /// Panics if the new capacity exceeds `isize::MAX` bytes .
6666 #[ inline]
6767 pub fn with_capacity ( capacity : usize ) -> Self {
6868 Self { buf : Vec :: with_capacity ( capacity) }
6969 }
7070
71- /// Returns the number of bytes in this buffer.
71+ /// Returns the number of bytes in the buffer.
7272 ///
73- /// This is _not_ the same as the number of characters in the buffer, since
74- /// non-ASCII characters require multiple bytes.
73+ /// This is *not* the same as the number of characters in the buffer,
74+ /// since non-ASCII characters require multiple bytes.
7575 #[ inline]
7676 pub fn len ( & self ) -> usize {
7777 self . buf . len ( )
7878 }
7979
80- /// Returns `true` if this buffer contains no characters.
80+ /// Returns `true` if the buffer contains no characters.
8181 ///
8282 /// # Example
8383 /// ```
@@ -93,15 +93,15 @@ impl CodeBuffer {
9393 self . buf . is_empty ( )
9494 }
9595
96- /// Reserves capacity for at least `additional` more characters in the given
97- /// `CodeBuffer`. The buffer may reserve more space to speculatively avoid
98- /// frequent reallocations. After calling ` reserve`, capacity will be
99- /// greater than or equal to `self.len() + additional`. Does nothing if
100- /// capacity is already sufficient.
96+ /// Reserves capacity for at least `additional` more bytes in the buffer.
97+ ///
98+ /// The buffer may reserve more space to speculatively avoid frequent reallocations.
99+ /// After calling `reserve`, capacity will be greater than or equal to `self.len() + additional`.
100+ /// Does nothing if capacity is already sufficient.
101101 ///
102102 /// # Panics
103103 ///
104- /// Panics if the new capacity exceeds `isize::MAX` _bytes_ .
104+ /// Panics if the new capacity exceeds `isize::MAX` bytes .
105105 ///
106106 /// # Example
107107 /// ```
@@ -115,8 +115,9 @@ impl CodeBuffer {
115115 }
116116
117117 /// Peek the `n`th character from the end of the buffer.
118- /// When `n` is zero, the last character is returned. Returns [`None`] if
119- /// `n` exceeds the length of the buffer.
118+ ///
119+ /// When `n` is zero, the last character is returned.
120+ /// Returns [`None`] if `n` exceeds the length of the buffer.
120121 ///
121122 /// # Example
122123 /// ```
@@ -131,14 +132,14 @@ impl CodeBuffer {
131132 #[ inline]
132133 #[ must_use = "Peeking is pointless if the peeked char isn't used" ]
133134 pub fn peek_nth_back ( & self , n : usize ) -> Option < char > {
134- // SAFETY: ` buf` is a valid UTF-8 string because of invariants upheld by CodeBuffer
135+ // SAFETY: All methods of `CodeBuffer` ensure ` buf` is valid UTF-8
135136 unsafe { std:: str:: from_utf8_unchecked ( & self . buf ) } . chars ( ) . nth_back ( n)
136137 }
137138
138- /// Push a single ASCII character into the buffer
139+ /// Push a single ASCII byte into the buffer.
139140 ///
140141 /// # Panics
141- /// If `ch ` is not a valid UTF-8 code point in the ASCII range (`0 - 0x7F`).
142+ /// If `b ` is not an ASCII byte (`0 - 0x7F`).
142143 ///
143144 /// # Example
144145 /// ```
@@ -160,24 +161,21 @@ impl CodeBuffer {
160161 self . buf . push ( b) ;
161162 }
162163
163- /// Print a byte without checking that this buffer still represents a valid
164+ /// Push a byte to the buffer, without checking that the buffer still represents a valid
164165 /// UTF-8 string.
165166 ///
166- /// If you are looking to print a byte you know is valid ASCII, prefer
167- /// [`print_ascii_byte`]. If you are not certain, you may use [`print_char`]
168- /// as a safe alternative.
167+ /// If you are looking to print a byte you know is valid ASCII, prefer [`print_ascii_byte`].
168+ /// If you are not certain, you may use [`print_char`] as a safe alternative.
169169 ///
170- /// # Safety
171- /// The caller must ensure that, after 1 or more sequential calls, this
172- /// buffer represents a valid UTF-8 string.
170+ /// # SAFETY
171+ /// The caller must ensure that, after 1 or more sequential calls, the buffer represents
172+ /// a valid UTF-8 string.
173173 ///
174- /// It is safe for a single call to temporarily result in invalid UTF-8, as
175- /// long as UTF-8 integrity is restored before calls to any other `print`
176- /// method or [`take_source_text`]. This lets you, for example, print an
177- /// 8-byte code point using 4 separate calls to this method.
178- ///
179- /// If you find yourself in such a scenario, consider using
180- /// [`print_unchecked`] instead.
174+ /// It is safe for a single call to temporarily result in invalid UTF-8, as long as
175+ /// UTF-8 integrity is restored before calls to any other `print_*` method or
176+ /// [`take_source_text`]. This lets you, for example, print an 4-byte Unicode character
177+ /// using 4 separate calls to this method. However, consider using [`print_unchecked`]
178+ /// instead for that use case.
181179 ///
182180 /// # Example
183181 /// ```
@@ -207,12 +205,11 @@ impl CodeBuffer {
207205 self . buf . push ( ch) ;
208206 }
209207
210- /// Print a single Unicode character into the buffer.
208+ /// Push a single Unicode character into the buffer.
211209 ///
212- /// When pushing multiple characters, consider choosing [`print_str`] over
213- /// this method since it's much more efficient. If you really want to insert
214- /// only a single character and you're certain it's ASCII, consider using
215- /// [`print_ascii_byte`].
210+ /// When pushing multiple characters, consider choosing [`print_str`] over this method
211+ /// since it's much more efficient. If you really want to insert only a single character
212+ /// and you're certain it's ASCII, consider using [`print_ascii_byte`].
216213 ///
217214 /// # Example
218215 /// ```
@@ -234,7 +231,7 @@ impl CodeBuffer {
234231 self . buf . extend ( ch. encode_utf8 ( & mut b) . as_bytes ( ) ) ;
235232 }
236233
237- /// Push a string into this the buffer.
234+ /// Push a string into the buffer.
238235 ///
239236 /// # Example
240237 /// ```
@@ -250,7 +247,7 @@ impl CodeBuffer {
250247 /// Push a sequence of ASCII characters into the buffer.
251248 ///
252249 /// # Panics
253- /// If any byte in the iterator is not valid ASCII.
250+ /// Panics if any byte in the iterator is not ASCII.
254251 ///
255252 /// # Example
256253 /// ```
@@ -272,26 +269,25 @@ impl CodeBuffer {
272269 }
273270 }
274271
275- /// Print a sequence of bytes without checking that this buffer still
272+ /// Print a sequence of bytes without checking that the buffer still
276273 /// represents a valid UTF-8 string.
277274 ///
278275 /// # Safety
279276 ///
280- /// The caller must ensure that, after being called, this buffer represents
277+ /// The caller must ensure that, after this method call, the buffer represents
281278 /// a valid UTF-8 string. In practice, this means only two cases are valid:
282279 ///
283280 /// 1. Both the buffer and the byte sequence are valid UTF-8,
284- /// 2. The buffer became invalid after a call to [`print_byte_unchecked`] and `bytes`
285- /// completes any incomplete code points, returning the buffer to a valid
286- /// state.
281+ /// 2. The buffer became invalid after a call to [`print_byte_unchecked`] and `bytes` completes
282+ /// any incomplete Unicode characters, returning the buffer to a valid state.
287283 ///
288284 /// # Example
289285 /// ```
290286 /// use oxc_codegen::CodeBuffer;
291287 /// let mut code = CodeBuffer::new();
292288 ///
293- /// // Indent to a dynamic level. Sound because all elements in this
294- /// // iterator are valid 1-byte UTF-8 code points ( ASCII) .
289+ /// // Indent to a dynamic level.
290+ /// // Sound because all elements in this iterator are ASCII characters .
295291 /// unsafe {
296292 /// code.print_unchecked(std::iter::repeat(b' ').take(4));
297293 /// }
@@ -306,7 +302,7 @@ impl CodeBuffer {
306302 self . buf . extend ( bytes) ;
307303 }
308304
309- /// Get contents of `CodeBuffer` as a byte slice.
305+ /// Get contents of buffer as a byte slice.
310306 ///
311307 /// # Example
312308 /// ```
@@ -320,14 +316,12 @@ impl CodeBuffer {
320316 & self . buf
321317 }
322318
323- /// Convert a `CodeBuffer` into a string of source code, leaving its
324- /// internal buffer empty and finalizing the codegen process.
325- ///
326- /// It is safe to re-use a buffer after calling this method. Its contents
327- /// will be emptied out, but all memory resources are retained and in a
328- /// valid state. You may use [`String::from`] if you don't intend on
329- /// re-using the buffer. It simply calls this method and drops the
330- /// `CodeBuffer` afterwards.
319+ /// Convert a buffer into a string of source code, leaving its internal buffer empty.
320+ ///
321+ /// It is safe to re-use a `CodeBuffer` after calling this method, but there is little benefit
322+ /// to doing so, as the `CodeBuffer` will be left in an empty state with no backing allocation.
323+ ///
324+ /// You may alternatively use `String::from(code_buffer)`, which may be slightly more efficient.
331325 ///
332326 /// # Example
333327 /// ```
0 commit comments