feedparser_rs/util/
text.rs

1//! Text processing utilities
2//!
3//! This module provides functions for text manipulation,
4//! such as trimming, normalizing whitespace, and encoding conversion.
5
6/// Efficient bytes to string conversion - zero-copy for valid UTF-8
7///
8/// Uses `std::str::from_utf8()` for zero-copy conversion when the input
9/// is valid UTF-8, falling back to lossy conversion otherwise.
10///
11/// # Examples
12///
13/// ```
14/// use feedparser_rs::util::text::bytes_to_string;
15///
16/// let valid_utf8 = b"Hello, world!";
17/// assert_eq!(bytes_to_string(valid_utf8), "Hello, world!");
18///
19/// let invalid_utf8 = &[0xFF, 0xFE, 0xFD];
20/// let result = bytes_to_string(invalid_utf8);
21/// assert!(!result.is_empty()); // Lossy conversion succeeded
22/// ```
23#[inline]
24pub fn bytes_to_string(value: &[u8]) -> String {
25    std::str::from_utf8(value).map_or_else(
26        |_| String::from_utf8_lossy(value).into_owned(),
27        std::string::ToString::to_string,
28    )
29}